From cc81f5bac395dbf49ecf6dfd780e01fed9e4b2c3 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Mon, 12 Jul 2021 13:36:57 +0200 Subject: [PATCH] Fix support for UNC paths in normalizePath, refs #9993 --- src/Composer/Util/Filesystem.php | 14 ++++++++++---- tests/Composer/Test/Util/FilesystemTest.php | 1 + 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/Composer/Util/Filesystem.php b/src/Composer/Util/Filesystem.php index 2f51920966d4..69f097958c3f 100644 --- a/src/Composer/Util/Filesystem.php +++ b/src/Composer/Util/Filesystem.php @@ -544,7 +544,13 @@ public function normalizePath($path) $parts = array(); $path = strtr($path, '\\', '/'); $prefix = ''; - $absolute = false; + $absolute = ''; + + // extract windows UNC paths e.g. \\foo\bar + if (strpos($path, '//') === 0 && \strlen($path) > 2) { + $absolute = '//'; + $path = substr($path, 2); + } // extract a prefix being a protocol://, protocol:, protocol://drive: or simply drive: if (preg_match('{^( [0-9a-z]{2,}+: (?: // (?: [a-z]: )? )? | [a-z]: )}ix', $path, $match)) { @@ -553,13 +559,13 @@ public function normalizePath($path) } if (strpos($path, '/') === 0) { - $absolute = true; + $absolute = '/'; $path = substr($path, 1); } $up = false; foreach (explode('/', $path) as $chunk) { - if ('..' === $chunk && ($absolute || $up)) { + if ('..' === $chunk && ($absolute !== '' || $up)) { array_pop($parts); $up = !(empty($parts) || '..' === end($parts)); } elseif ('.' !== $chunk && '' !== $chunk) { @@ -568,7 +574,7 @@ public function normalizePath($path) } } - return $prefix.($absolute ? '/' : '').implode('/', $parts); + return $prefix.((string) $absolute).implode('/', $parts); } /** diff --git a/tests/Composer/Test/Util/FilesystemTest.php b/tests/Composer/Test/Util/FilesystemTest.php index 0fb4babd828e..7ac013a78d9d 100644 --- a/tests/Composer/Test/Util/FilesystemTest.php +++ b/tests/Composer/Test/Util/FilesystemTest.php @@ -220,6 +220,7 @@ public function provideNormalizedPaths() array('../src', 'Foo/Bar/../../../src'), array('c:../b', 'c:.\\..\\a\\..\\b'), array('phar://c:../Foo', 'phar://c:../Foo'), + array('//foo/bar', '\\\\foo\\bar'), ); }