8000 [Filesystem] Fixed makePathRelative by ausi · Pull Request #22321 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Filesystem] Fixed makePathRelative #22321

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 20 additions & 22 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,34 +358,37 @@ public function makePathRelative($endPath, $startPath)
$startPath = str_replace('\\', '/', $startPath);
}

$stripDriveLetter = function ($path) {
if (strlen($path) > 2 && ':' === $path[1] && '/' === $path[2] && ctype_alpha($path[0])) {
return substr($path, 2);
}

return $path;
};

$endPath = $stripDriveLetter($endPath);
$startPath = $stripDriveLetter($startPath);

// Split the paths into arrays
$startPathArr = explode('/', trim($startPath, '/'));
$endPathArr = explode('/', trim($endPath, '/'));

if ('/' !== $startPath[0]) {
array_shift($startPathArr);
}

if ('/' !== $endPath[0]) {
array_shift($endPathArr);
}

$normalizePathArray = function ($pathSegments) {
$normalizePathArray = function ($pathSegments, $absolute) {
$result = array();

foreach ($pathSegments as $segment) {
if ('..' === $segment) {
if ('..' === $segment && ($absolute || count($result))) {
array_pop($result);
} else {
} elseif ('.' !== $segment) {
$result[] = $segment;
}
}

return $result;
};

$startPathArr = $normalizePathArray($startPathArr);
$endPathArr = $normalizePathArray($endPathArr);
$startPathArr = $normalizePathArray($startPathArr, static::isAbsolutePath($startPath));
$endPathArr = $normalizePathArray($endPathArr, static::isAbsolutePath($endPath));

// Find for which directory the common path stops
$index = 0;
Expand All @@ -394,19 +397,14 @@ public function makePathRelative($endPath, $startPath)
}

// Determine how deep the start path is relative to the common path (ie, "web/bundles" = 2 levels)
if (count($startPathArr) === 1 && $startPathArr[0] === '') {
if (1 === count($startPathArr) && '' === $startPathArr[0]) {
$depth = 0;
} else {
$depth = count($startPathArr) - $index;
}

// When we need to traverse from the start, and we are starting from a root path, don't add '../'
if ('/' === $startPath[0] && 0 === $index && 0 === $depth) {
$traverser = '';
} else {
// Repeated "../" for each level need to reach the common path
$traverser = str_repeat('../', $depth);
}
// Repeated "../" for each level need to reach the common path
$traverser = str_repeat('../', $depth);

$endPathRemainder = implode('/', array_slice($endPathArr, $index));

Expand Down Expand Up @@ -500,7 +498,7 @@ public function isAbsolutePath($file)
{
return strspn($file, '/\\', 0, 1)
|| (strlen($file) > 3 && ctype_alpha($file[0])
&& substr($file, 1, 1) === ':'
&& ':' === substr($file, 1, 1)
&& strspn($file, '/\\', 2, 1)
)
|| null !== parse_url($file, PHP_URL_SCHEME)
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/Filesystem/Tests/FilesystemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -847,6 +847,7 @@ public function providePathsForMakePathRelative()
array('/var/lib/symfony/src/Symfony', '/var/lib/symfony/src/Symfony/Component/', '../'),
array('var/lib/symfony/', 'var/lib/symfony/src/Symfony/Component', '../../../'),
array('/usr/lib/symfony/', '/var/lib/symfony/src/Symfony/Component', '../../../../../../usr/lib/symfony/'),
array(' 8000 usr/lib/symfony/', 'var/lib/symfony/src/Symfony/Component', '../../../../../../usr/lib/symfony/'),
array('/var/lib/symfony/src/Symfony/', '/var/lib/symfony/', 'src/Symfony/'),
array('/aa/bb', '/aa/bb', './'),
array('/aa/bb', '/aa/bb/', './'),
Expand Down Expand Up @@ -878,6 +879,17 @@ public function providePathsForMakePathRelative()
array('C:/aa/bb/../../cc', 'C:/aa/../dd/..', 'cc/'),
array('C:/../aa/bb/cc', 'C:/aa/dd/..', 'bb/cc/'),
array('C:/../../aa/../bb/cc', 'C:/aa/dd/..', '../bb/cc/'),
array('aa/bb', 'aa/cc', '../bb/'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not something that is supported. The docblock states that the paths have to be absolute.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was already an existing unit test (FilesystemTest.php:1097) that tested relative paths.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not add new ones. Tests should respect the contract which is to pass an absolute path.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chalasr the function always supported relative paths (before 3.2.7). There are even pre-existing test cases for that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tests should respect the contract which is to pass an absolute path.

@chalasr Should I remove the pre-existing relative path tests then too? Or should I just remove the relative path tests that I added?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please provide a new link to this test? Previous one is outdated and I can't find it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it’s line 1102 in the current master branch:

array('var/lib/symfony/', 'var/lib/symfony/src/Symfony/Component', '../../../'),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, it should be removed (we did revert a similar test for the very same reason #22133 (comment)). It's safe to do it here I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in a2a1d0d

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chalasr Actually, the example that you gave was a test I mistakenly added in that very same PR. So it was never part of a merge. :)

array('aa/cc', 'bb/cc', '../../aa/cc/'),
array('aa/bb', 'aa/./cc', '../bb/'),
array('aa/./bb', 'aa/cc', '../bb/'),
array('aa/./bb', 'aa/./cc', '../bb/'),
array('../../', '../../', './'),
array('../aa/bb/', 'aa/bb/', '../../../aa/bb/'),
array('../../../', '../../', '../'),
array('', '', './'),
array('', 'aa/', '../'),
array('aa/', '', 'aa/'),
);

if ('\\' === DIRECTORY_SEPARATOR) {
Expand Down
0