Skip to content

Commit 2a5ac93

Browse files
committed
url: use resolved path to convert UNC paths to URL
PR-URL: #56302 Fixes: #56262 Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Xuguang Mei <meixuguang@gmail.com>
1 parent c2837f0 commit 2a5ac93

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

lib/internal/url.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -1512,32 +1512,35 @@ function fileURLToPath(path, options = kEmptyObject) {
15121512

15131513
function pathToFileURL(filepath, options = kEmptyObject) {
15141514
const windows = options?.windows ?? isWindows;
1515-
if (windows && StringPrototypeStartsWith(filepath, '\\\\')) {
1515+
const isUNC = windows && StringPrototypeStartsWith(filepath, '\\\\');
1516+
let resolved = isUNC ?
1517+
filepath :
1518+
(windows ? path.win32.resolve(filepath) : path.posix.resolve(filepath));
1519+
if (isUNC || (windows && StringPrototypeStartsWith(resolved, '\\\\'))) {
15161520
// UNC path format: \\server\share\resource
15171521
// Handle extended UNC path and standard UNC path
15181522
// "\\?\UNC\" path prefix should be ignored.
15191523
// Ref: https://learn.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation
1520-
const isExtendedUNC = StringPrototypeStartsWith(filepath, '\\\\?\\UNC\\');
1524+
const isExtendedUNC = StringPrototypeStartsWith(resolved, '\\\\?\\UNC\\');
15211525
const prefixLength = isExtendedUNC ? 8 : 2;
1522-
const hostnameEndIndex = StringPrototypeIndexOf(filepath, '\\', prefixLength);
1526+
const hostnameEndIndex = StringPrototypeIndexOf(resolved, '\\', prefixLength);
15231527
if (hostnameEndIndex === -1) {
15241528
throw new ERR_INVALID_ARG_VALUE(
15251529
'path',
1526-
filepath,
1530+
resolved,
15271531
'Missing UNC resource path',
15281532
);
15291533
}
15301534
if (hostnameEndIndex === 2) {
15311535
throw new ERR_INVALID_ARG_VALUE(
15321536
'path',
1533-
filepath,
1537+
resolved,
15341538
'Empty UNC servername',
15351539
);
15361540
}
1537-
const hostname = StringPrototypeSlice(filepath, prefixLength, hostnameEndIndex);
1538-
return new URL(StringPrototypeSlice(filepath, hostnameEndIndex), hostname, kCreateURLFromWindowsPathSymbol);
1541+
const hostname = StringPrototypeSlice(resolved, prefixLength, hostnameEndIndex);
1542+
return new URL(StringPrototypeSlice(resolved, hostnameEndIndex), hostname, kCreateURLFromWindowsPathSymbol);
15391543
}
1540-
let resolved = windows ? path.win32.resolve(filepath) : path.posix.resolve(filepath);
15411544
// path.resolve strips trailing slashes so we must add them back
15421545
const filePathLast = StringPrototypeCharCodeAt(filepath,
15431546
filepath.length - 1);

0 commit comments

Comments
 (0)