1
0
Fork 0

Fix normalization of relative paths going up a few dirs, fixes #1759

pull/1485/merge
Jordi Boggiano 2013-04-03 19:28:06 +02:00
parent 57c34033ff
commit f744ec16f5
2 changed files with 26 additions and 1 deletions

View File

@ -318,10 +318,12 @@ class Filesystem
$path = substr($path, strlen($prefix)); $path = substr($path, strlen($prefix));
} }
$appended = false;
foreach (explode('/', $path) as $chunk) { foreach (explode('/', $path) as $chunk) {
if ('..' === $chunk) { if ('..' === $chunk && $appended) {
array_pop($parts); array_pop($parts);
} elseif ('.' !== $chunk && '' !== $chunk) { } elseif ('.' !== $chunk && '' !== $chunk) {
$appended = true;
$parts[] = $chunk; $parts[] = $chunk;
} }
} }

View File

@ -139,4 +139,27 @@ class FilesystemTest extends TestCase
$fs = new Filesystem; $fs = new Filesystem;
$this->assertGreaterThanOrEqual(10, $fs->size("$tmp/composer_testdir")); $this->assertGreaterThanOrEqual(10, $fs->size("$tmp/composer_testdir"));
} }
/**
* @dataProvider provideNormalizedPaths
*/
public function testNormalizePath($expected, $actual)
{
$fs = new Filesystem;
$this->assertEquals($expected, $fs->normalizePath($actual));
}
public function provideNormalizedPaths()
{
return array(
array('../foo', '../foo'),
array('c:/foo/bar', 'c:/foo//bar'),
array('C:/foo/bar', 'C:/foo/./bar'),
array('C:/bar', 'C:/foo/../bar'),
array('/bar', '/foo/../bar/'),
array('phar://c:/Foo', 'phar://c:/Foo/Bar/..'),
array('phar://c:/', 'phar://c:/Foo/Bar/../../../..'),
array('/', '/Foo/Bar/../../../..'),
);
}
} }