From 3b978c62a56ad154aabc92f3bf8864c58a1b54b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Wed, 21 Mar 2018 19:20:53 +0100 Subject: [PATCH 1/2] path: fix regression in posix.normalize Fixes a regression introduced in https://github.com/nodejs/node/commit/4ae320f2b3c745402955019d6a57a22ee2b8d3bd The posix version of normalize should not treat backslash as a path separator. Fixes: https://github.com/nodejs/node/issues/19519 --- lib/path.js | 17 ++++++++++++----- test/parallel/test-path-normalize.js | 2 ++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/path.js b/lib/path.js index 7a4868852698db..f9e7fd693811b4 100644 --- a/lib/path.js +++ b/lib/path.js @@ -44,13 +44,17 @@ function isPathSeparator(code) { return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH; } +function isPosixPathSeparator(code) { + return code === CHAR_FORWARD_SLASH; +} + function isWindowsDeviceRoot(code) { return code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z || code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z; } // Resolves . and .. elements in a path with directory names -function normalizeString(path, allowAboveRoot, separator) { +function normalizeString(path, allowAboveRoot, separator, isPathSeparator) { var res = ''; var lastSegmentLength = 0; var lastSlash = -1; @@ -272,7 +276,8 @@ const win32 = { // fails) // Normalize the tail path - resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\'); + resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\', + isPathSeparator); return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) || '.'; @@ -364,7 +369,8 @@ const win32 = { var tail; if (rootEnd < len) - tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\'); + tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\', + isPathSeparator); else tail = ''; if (tail.length === 0 && !isAbsolute) @@ -1095,7 +1101,8 @@ const posix = { // handle relative paths to be safe (might happen when process.cwd() fails) // Normalize the path - resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/'); + resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/', + isPosixPathSeparator); if (resolvedAbsolute) { if (resolvedPath.length > 0) @@ -1121,7 +1128,7 @@ const posix = { path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH; // Normalize the path - path = normalizeString(path, !isAbsolute, '/'); + path = normalizeString(path, !isAbsolute, '/', isPosixPathSeparator); if (path.length === 0 && !isAbsolute) path = '.'; diff --git a/test/parallel/test-path-normalize.js b/test/parallel/test-path-normalize.js index 0dd1b8339f4c64..e1d3b9ce1e6c02 100644 --- a/test/parallel/test-path-normalize.js +++ b/test/parallel/test-path-normalize.js @@ -39,6 +39,7 @@ assert.strictEqual( path.win32.normalize('../.../../foobar/../../../bar/../../baz'), '..\\..\\..\\..\\baz' ); +assert.strictEqual(path.win32.normalize('foo/bar\\baz'), 'foo\\bar\\baz'); assert.strictEqual(path.posix.normalize('./fixtures///b/../b/c.js'), 'fixtures/b/c.js'); @@ -68,3 +69,4 @@ assert.strictEqual( path.posix.normalize('../.../../foobar/../../../bar/../../baz'), '../../../../baz' ); +assert.strictEqual(path.posix.normalize('foo/bar\\baz'), 'foo/bar\\baz'); From 579847d3c74ed0a675063c6d2d86d1532b7645c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Zasso?= Date: Thu, 22 Mar 2018 08:15:01 +0100 Subject: [PATCH 2/2] braces --- lib/path.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/path.js b/lib/path.js index f9e7fd693811b4..427d158e7a7834 100644 --- a/lib/path.js +++ b/lib/path.js @@ -368,11 +368,12 @@ const win32 = { } var tail; - if (rootEnd < len) + if (rootEnd < len) { tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\', isPathSeparator); - else + } else { tail = ''; + } if (tail.length === 0 && !isAbsolute) tail = '.'; if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1)))