From 6708d1c2f0ec93d29692b820765c34a11fbdae25 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 18 Dec 2024 11:19:14 +0100 Subject: [PATCH 1/3] url: use resolved path to convert UNC paths to URL --- lib/internal/url.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index 14b0ef61d2f91c..6b565e1bfad2c9 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -1512,32 +1512,32 @@ function fileURLToPath(path, options = kEmptyObject) { function pathToFileURL(filepath, options = kEmptyObject) { const windows = options?.windows ?? isWindows; - if (windows && StringPrototypeStartsWith(filepath, '\\\\')) { + let resolved = windows ? path.win32.resolve(filepath) : path.posix.resolve(filepath); + if (windows && StringPrototypeStartsWith(resolved, '\\\\')) { // UNC path format: \\server\share\resource // Handle extended UNC path and standard UNC path // "\\?\UNC\" path prefix should be ignored. // Ref: https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation - const isExtendedUNC = StringPrototypeStartsWith(filepath, '\\\\?\\UNC\\'); + const isExtendedUNC = StringPrototypeStartsWith(resolved, '\\\\?\\UNC\\'); const prefixLength = isExtendedUNC ? 8 : 2; - const hostnameEndIndex = StringPrototypeIndexOf(filepath, '\\', prefixLength); + const hostnameEndIndex = StringPrototypeIndexOf(resolved, '\\', prefixLength); if (hostnameEndIndex === -1) { throw new ERR_INVALID_ARG_VALUE( 'path', - filepath, + resolved, 'Missing UNC resource path', ); } if (hostnameEndIndex === 2) { throw new ERR_INVALID_ARG_VALUE( 'path', - filepath, + resolved, 'Empty UNC servername', ); } - const hostname = StringPrototypeSlice(filepath, prefixLength, hostnameEndIndex); - return new URL(StringPrototypeSlice(filepath, hostnameEndIndex), hostname, kCreateURLFromWindowsPathSymbol); + const hostname = StringPrototypeSlice(resolved, prefixLength, hostnameEndIndex); + return new URL(StringPrototypeSlice(resolved, hostnameEndIndex), hostname, kCreateURLFromWindowsPathSymbol); } - let resolved = windows ? path.win32.resolve(filepath) : path.posix.resolve(filepath); // path.resolve strips trailing slashes so we must add them back const filePathLast = StringPrototypeCharCodeAt(filepath, filepath.length - 1); From 85afa775554e60bd1ffc82493da5ba6d2aee0f15 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Mon, 23 Dec 2024 22:44:29 +0100 Subject: [PATCH 2/3] fixup! url: use resolved path to convert UNC paths to URL --- lib/internal/url.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index 6b565e1bfad2c9..c043b0f4534e17 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -1512,8 +1512,11 @@ function fileURLToPath(path, options = kEmptyObject) { function pathToFileURL(filepath, options = kEmptyObject) { const windows = options?.windows ?? isWindows; - let resolved = windows ? path.win32.resolve(filepath) : path.posix.resolve(filepath); - if (windows && StringPrototypeStartsWith(resolved, '\\\\')) { + const isUNC = windows && StringPrototypeStartsWith(filepath, '\\\\'); + let resolved = isUNC ? + filepath : + (windows ? path.win32.resolve(filepath) : path.posix.resolve(filepath)); + if (isUNC || windows && StringPrototypeStartsWith(resolved, '\\\\')) { // UNC path format: \\server\share\resource // Handle extended UNC path and standard UNC path // "\\?\UNC\" path prefix should be ignored. From de54f8dbe7e7853cd4b8f04813a0be3418a54420 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Tue, 24 Dec 2024 00:26:56 +0100 Subject: [PATCH 3/3] Update lib/internal/url.js --- lib/internal/url.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/url.js b/lib/internal/url.js index c043b0f4534e17..f6e58e196860fc 100644 --- a/lib/internal/url.js +++ b/lib/internal/url.js @@ -1516,7 +1516,7 @@ function pathToFileURL(filepath, options = kEmptyObject) { let resolved = isUNC ? filepath : (windows ? path.win32.resolve(filepath) : path.posix.resolve(filepath)); - if (isUNC || windows && StringPrototypeStartsWith(resolved, '\\\\')) { + if (isUNC || (windows && StringPrototypeStartsWith(resolved, '\\\\'))) { // UNC path format: \\server\share\resource // Handle extended UNC path and standard UNC path // "\\?\UNC\" path prefix should be ignored.