Skip to content

Commit

Permalink
url: use resolved path to convert UNC paths to URL
Browse files Browse the repository at this point in the history
PR-URL: #56302
Fixes: #56262
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Yagiz Nizipli <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Xuguang Mei <[email protected]>
  • Loading branch information
aduh95 authored Dec 25, 2024
1 parent b420c8f commit 1d1d8f2
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/internal/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -1512,32 +1512,35 @@ function fileURLToPath(path, options = kEmptyObject) {

function pathToFileURL(filepath, options = kEmptyObject) {
const windows = options?.windows ?? isWindows;
if (windows && StringPrototypeStartsWith(filepath, '\\\\')) {
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.
// 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);
Expand Down

0 comments on commit 1d1d8f2

Please sign in to comment.