From a0adf56855f59a301d9a1f69b4267ced4fd4cf03 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] path: fix regression in posix.normalize MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes a regression introduced in [1]. The posix version of normalize should not treat backslash as a path separator. [1] https://github.com/nodejs/node/commit/4ae320f2 PR-URL: https://github.com/nodejs/node/pull/19520 Fixes: https://github.com/nodejs/node/issues/19519 Reviewed-By: Rich Trott Reviewed-By: Tobias Nießen Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- lib/path.js | 22 +++++++++++++++------- test/parallel/test-path-normalize.js | 2 ++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/lib/path.js b/lib/path.js index 7a4868852698db..427d158e7a7834 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) || '.'; @@ -363,10 +368,12 @@ const win32 = { } var tail; - if (rootEnd < len) - tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\'); - else + if (rootEnd < len) { + tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\', + isPathSeparator); + } else { tail = ''; + } if (tail.length === 0 && !isAbsolute) tail = '.'; if (tail.length > 0 && isPathSeparator(path.charCodeAt(len - 1))) @@ -1095,7 +1102,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 +1129,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');