Skip to content

Commit ce98a72

Browse files
Copilotjazelly
andcommitted
fs: handle extended Windows path prefix in realpathSync and realpath
Strip the \\?\ extended-length path prefix from Windows paths in fs.realpathSync and fs.realpath before the path walking logic. The \\?\ prefix is a Win32 API mechanism to bypass MAX_PATH limits. When a user passes a path like \\?\C:\path\to\file.js, Node.js should handle it transparently by converting to the standard C:\path\to\file.js form for internal processing. The \\?\ prefix is re-added as needed via path.toNamespacedPath() before actual system calls. Handles two forms of extended paths: - \\?\C:\path -> C:\path (extended drive path) - \\?\UNC\server\share -> \\server\share (extended UNC path) Fixes: nodejs#62446 Agent-Logs-Url: https://github.com/jazelly/node/sessions/77c338e9-ef46-46f1-8800-b6ad9a74ab36 Co-authored-by: jazelly <[email protected]>
1 parent 53bcd11 commit ce98a72

2 files changed

Lines changed: 115 additions & 0 deletions

File tree

‎lib/fs.js‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2628,6 +2628,43 @@ function unwatchFile(filename, listener) {
26282628
}
26292629

26302630

2631+
// Strips the Windows extended-length path prefix (\\?\) from a resolved path.
2632+
// Extended-length paths (\\?\C:\... or \\?\UNC\...) are a Win32 API mechanism
2633+
// to bypass MAX_PATH limits. Node.js should handle them transparently by
2634+
// converting to standard paths for internal processing. The \\?\ prefix is
2635+
// re-added when needed via path.toNamespacedPath() before system calls.
2636+
// See: https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file
2637+
function stripExtendedPathPrefix(p) {
2638+
// Check for \\?\ prefix (charCode 92=\, 63=?)
2639+
if (p.length >= 4 &&
2640+
StringPrototypeCharCodeAt(p, 0) === CHAR_BACKWARD_SLASH &&
2641+
StringPrototypeCharCodeAt(p, 1) === CHAR_BACKWARD_SLASH &&
2642+
StringPrototypeCharCodeAt(p, 2) === 63 /* ? */ &&
2643+
StringPrototypeCharCodeAt(p, 3) === CHAR_BACKWARD_SLASH) {
2644+
// \\?\C:\ -> C:\ (extended drive path)
2645+
if (p.length >= 6) {
2646+
const drive = StringPrototypeCharCodeAt(p, 4);
2647+
if (((drive >= 65 /* A */ && drive <= 90 /* Z */) ||
2648+
(drive >= 97 /* a */ && drive <= 122 /* z */)) &&
2649+
StringPrototypeCharCodeAt(p, 5) === 58 /* : */) {
2650+
return StringPrototypeSlice(p, 4);
2651+
}
2652+
}
2653+
// \\?\UNC\server\share -> \\server\share (extended UNC path)
2654+
if (p.length >= 8 &&
2655+
(StringPrototypeCharCodeAt(p, 4) === 85 /* U */ ||
2656+
StringPrototypeCharCodeAt(p, 4) === 117 /* u */) &&
2657+
(StringPrototypeCharCodeAt(p, 5) === 78 /* N */ ||
2658+
StringPrototypeCharCodeAt(p, 5) === 110 /* n */) &&
2659+
(StringPrototypeCharCodeAt(p, 6) === 67 /* C */ ||
2660+
StringPrototypeCharCodeAt(p, 6) === 99 /* c */) &&
2661+
StringPrototypeCharCodeAt(p, 7) === CHAR_BACKWARD_SLASH) {
2662+
return '\\\\' + StringPrototypeSlice(p, 8);
2663+
}
2664+
}
2665+
return p;
2666+
}
2667+
26312668
let splitRoot;
26322669
if (isWindows) {
26332670
// Regex to find the device root on Windows (e.g. 'c:\\'), including trailing
@@ -2690,6 +2727,12 @@ function realpathSync(p, options) {
26902727
validatePath(p);
26912728
p = pathModule.resolve(p);
26922729

2730+
// On Windows, strip the extended-length path prefix (\\?\) so that the
2731+
// path walking logic below works with standard drive-letter or UNC roots.
2732+
if (isWindows) {
2733+
p = stripExtendedPathPrefix(p);
2734+
}
2735+
26932736
const cache = options[realpathCacheKey];
26942737
const maybeCachedResult = cache?.get(p);
26952738
if (maybeCachedResult) {
@@ -2793,6 +2836,11 @@ function realpathSync(p, options) {
27932836
// Resolve the link, then start over
27942837
p = pathModule.resolve(resolvedLink, StringPrototypeSlice(p, pos));
27952838

2839+
// Strip extended path prefix again in case pathModule.resolve re-added it
2840+
if (isWindows) {
2841+
p = stripExtendedPathPrefix(p);
2842+
}
2843+
27962844
// Skip over roots
27972845
current = base = splitRoot(p);
27982846
pos = current.length;
@@ -2851,6 +2899,12 @@ function realpath(p, options, callback) {
28512899
validatePath(p);
28522900
p = pathModule.resolve(p);
28532901

2902+
// On Windows, strip the extended-length path prefix (\\?\) so that the
2903+
// path walking logic below works with standard drive-letter or UNC roots.
2904+
if (isWindows) {
2905+
p = stripExtendedPathPrefix(p);
2906+
}
2907+
28542908
const seenLinks = new SafeMap();
28552909
const knownHard = new SafeSet();
28562910

@@ -2951,6 +3005,12 @@ function realpath(p, options, callback) {
29513005
function gotResolvedLink(resolvedLink) {
29523006
// Resolve the link, then start over
29533007
p = pathModule.resolve(resolvedLink, StringPrototypeSlice(p, pos));
3008+
3009+
// Strip extended path prefix again in case pathModule.resolve re-added it
3010+
if (isWindows) {
3011+
p = stripExtendedPathPrefix(p);
3012+
}
3013+
29543014
current = base = splitRoot(p);
29553015
pos = current.length;
29563016

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// This test verifies that fs.realpathSync and fs.realpath correctly handle
5+
// Windows extended-length path prefixes (\\?\C:\... and \\?\UNC\...).
6+
// See: https://github.com/nodejs/node/issues/62446
7+
8+
if (!common.isWindows)
9+
common.skip('Windows-specific test.');
10+
11+
const assert = require('assert');
12+
const fs = require('fs');
13+
const path = require('path');
14+
const tmpdir = require('../common/tmpdir');
15+
16+
tmpdir.refresh();
17+
18+
const testFile = tmpdir.resolve('extended-path-test.js');
19+
fs.writeFileSync(testFile, 'module.exports = 42;');
20+
21+
// Construct the extended-length path for the test file.
22+
// The \\?\ prefix is a Win32 API mechanism to bypass MAX_PATH limits.
23+
const extendedPath = `\\\\?\\${testFile}`;
24+
25+
// fs.realpathSync should handle the \\?\ prefix and return a standard path.
26+
{
27+
const result = fs.realpathSync(extendedPath);
28+
// The result should be the resolved path without the \\?\ prefix.
29+
assert.strictEqual(result.toLowerCase(), testFile.toLowerCase());
30+
}
31+
32+
// fs.realpath (async) should also handle the \\?\ prefix.
33+
fs.realpath(extendedPath, common.mustSucceed((result) => {
34+
assert.strictEqual(result.toLowerCase(), testFile.toLowerCase());
35+
}));
36+
37+
// Also test that the extended path for the drive root works.
38+
{
39+
const driveRoot = path.parse(testFile).root; // e.g., 'C:\'
40+
const extendedRoot = `\\\\?\\${driveRoot}`;
41+
const result = fs.realpathSync(extendedRoot);
42+
assert.strictEqual(result.toLowerCase(), driveRoot.toLowerCase());
43+
}
44+
45+
// Test extended-length path with subdirectory.
46+
const subDir = tmpdir.resolve('sub', 'dir');
47+
fs.mkdirSync(subDir, { recursive: true });
48+
const subFile = path.join(subDir, 'file.txt');
49+
fs.writeFileSync(subFile, 'hello');
50+
51+
{
52+
const extendedSubFile = `\\\\?\\${subFile}`;
53+
const result = fs.realpathSync(extendedSubFile);
54+
assert.strictEqual(result.toLowerCase(), subFile.toLowerCase());
55+
}

0 commit comments

Comments
 (0)