Repository navigation
fix(windows): reject UNC paths before filesystem access - #781
Conversation
On Windows, even lstat or realpath on an attacker-chosen UNC path makes the OS contact that SMB host (NTLM exposure, SSRF, multi-second stall). A hosted runner diagnostic caught Root.read/open/stat/mkdir probing \\server\share\secret.txt and \\server\share\ from the absolute-alias walk in rawPathRelativeToCanonicalRoot before rejecting them as outside the Root. Derive a syntactic share/device root key for spellings with two leading separators (\\?\UNC\ and \\.\UNC\ folded to \\, ASCII-only case folding, namespaced drives excluded as local). Namespace spellings whose authority is not syntactic are always foreign: Node hands \\?\ and \\.\ strings to Win32 unchanged, where dot segments climb out of \\.\C:\ or \\?\UNC\host\share and trailing dots, spaces and empty components are rewritten, and GLOBALROOT/Global expose whole object namespaces. Reject foreign share/device paths lexically, with each site's existing error, before the Root canonical alias walk (plus defense in depth inside the walk), the pathScope existing/files realpath fallback, secret-file queue-key canonicalization (existing lexical containment now runs first), writeViaSiblingTempPath, movePathToTrash, and archive output preparation. Local alias walks, namespaced drives and the boundary's own share keep working; POSIX behavior is unchanged.
…tonebraker-0027b2
|
Codex review: needs real behavior proof before merge. Reviewed September 30, 2026, 9:34 PM ET / October 1, 2026, 01:34 UTC (Revision 5). ClawSweeper reviewWhat this changesThe branch adds Windows share and device-path checks to filesystem roots, path scopes, secret writes, temporary output, trash, and archive preparation, with tests and security documentation. Merge readiness⛔ Blocked before merge - 12 items remain Current main still has the reported UNC lookup problem. The PR is useful, but three entry paths can still reach a different Unicode-named host before the new rejection runs, so it is not ready to merge. Priority: P0 Review scores
Verification
How this fits togetherfs-safe accepts caller-supplied paths and checks them against trusted filesystem roots before Node performs filesystem operations. These checks protect reads, writes, secret files, trash moves, and archive output. flowchart LR
A[Caller-supplied path] --> B[Windows path parsing]
B --> C[Share and device check]
C --> D[Root containment check]
D --> E[Filesystem identity checks]
E --> F[Read, write, or rejection]
Decision needed
Why: The PR intentionally changes previously accepted alias behavior, and the captured discussion contains no explicit maintainer acceptance of that compatibility tradeoff. Before merge
Findings
Agent review detailsSecurityNeeds attention: Three early-admission gaps can still permit a different Unicode-named UNC host to receive a filesystem lookup before rejection. Review metrics
Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Check the raw share or device authority before every prefix or relative-path comparison, then prove both same-share access and different-host rejection on Windows and document the approved cross-share upgrade behavior. Do we have a high-confidence way to reproduce the issue? Yes for the reported UNC lookup: the captured PR body gives a Windows before-and-after trace. The Unicode-host bypass has a high-confidence source path and Node path-comparison diagnostic, but no final-I/O Windows trace. Is this the best way to solve the issue? No. Lexical rejection is the right boundary, but these three guards must run before comparisons that can equate different host spellings. Full review comments:
Overall correctness: patch is incorrect AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 1ad57695bf87. LabelsLabel changes: No label changes. Label justifications:
EvidenceSecurity concerns:
What I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
HistoryReview history (4 earlier review cycles)
|
…tonebraker-0027b2
…tonebraker-0027b2 # Conflicts: # CHANGELOG.md
Follow-up to #781. ClawSweeper's review of #781 found that the library's other lexical containment checks fold Unicode case (Windows path.relative, the Root prefix identity match, isPathInside), so a host spelled with U+212A KELVIN SIGN compared as the trusted ASCII host and was probed (Root identity lstat, pathScope missing-file fallback, secret-file queue key) before the ASCII-only share gate ran. Run the share/device gate first at the shared lexical choke points: prepareRootTraversal (before direct admission), the pathScope lexical core, the public isPathInside, and secret-file containment. A spelling exactly under a trusted boundary without climbing above it is never foreign, which keeps Roots on GLOBALROOT shadow-copy paths working. Refine namespace spellings: the authority (head, plus host and share for UNC) must not be empty or end in a dot or space, and later all-dot or all-space components may not climb above it; GLOBALROOT/Global stay unidentifiable. Local spellings such as \\?\C:\a\..\b stay local. Document the intentional cross-share compatibility change and the upgrade path (boundary-relative paths, or paths spelled on the boundary's own share), and account for the gate in the Windows isPathInside fast-path oracle. Proof: real Windows A/B on fs-safe-windows-16core (run 36803970186): main admits the Kelvin host in isPathInside, pathScope and secret writers; this change rejects it with no fs call, and a real \\localhost\C$ network Root keeps resolving its own share while \\127.0.0.1\C$ is rejected without an fs call.
Problem
A hosted-runner timing diagnostic on 2026-09-30 (run 36742077471,
fs-safe-windows-16core, Node 22 and 24, JavaScript fallback) wrapped everynode:fscall and found library code callingfs.lstatSyncon attacker-style UNC payloads before rejecting them:lstatSync("\\server\share\secret.txt")fromRoot.read/open/stat(payload)(2770–2811 ms per call)lstatSync("\\server\share\")fromRoot.mkdir("\\server\share")On Windows even a metadata lookup on
\\host\share\...makes the OS resolve and connect tohostover SMB. An attacker who controls a pathname can therefore make the process reach out to a host of their choosing (NTLM credential exposure / SSRF) and stall it for seconds, even though the path is eventually rejected as outside the Root.Root cause
prepareRootTraversalsupports absolute spellings that reach the Root through a filesystem alias (symlinked ancestors, junctions, macOS/tmpvs/private/tmp). When an absolute candidate is not lexically inside the Root,rawPathRelativeToCanonicalRootlstat/realpaths each prefix of the candidate to look for such an alias. Namespace-alias admission (hasWindowsPathAlias) only rejects colon aliases, so UNC (\\host\share,//host/share), extended UNC (\\?\UNC\host\share,\\.\UNC\...) and device-namespace roots (\\?\GLOBALROOT\...,\\.\pipe\...,\\?\Volume{...}) went straight into that walk. For\\server\sharethe single empty segment produced exactly the\\server\share\lookup seen in the log.A read-only audit of every public path-taking API found four more sites that probe the filesystem before their own containment check, independent of the Root resolver: secret-file writers (queue-key canonicalization),
writeViaSiblingTempPath(target parent realpath),movePathToTrash(target lstat), and archive output preparation (prepareArchiveOutputPathparent realpath).Fix
windowsShareOrDeviceRoot(pure syntax): comparable share/device root for any spelling with two leading separators. It folds\\?\UNC\/\\.\UNC\to\\, normalizes separators, and folds ASCII case only, so Unicode case folding (for example U+212A KELVIN SIGN) cannot make a different host compare equal. Namespaced drive roots\\?\C:\and\\.\C:\stay local and are excluded. Namespace spellings whose target the spelling cannot establish are always foreign: Node hands\\?\/\\.\strings to Win32 unchanged, where..climbs out of a drive or share (path.win32.resolve("\\\\.\\C:\\..\\UNC\\evil\\s")is\\.\UNC\evil\s) and trailing dots, spaces and empty components are rewritten, andGLOBALROOT/Globalexpose whole object namespaces. Ordinary\\host\share\..stays clamped at the share by Node.isForeignWindowsShareOrDevicePath(win32 only): true when that key matches none of the trusted boundary spellings.prepareRootTraversal(Root read/open/stat/exists/list/entries/walk/mkdir/remove/resolve,openRootFile*, root JSON readers, local-roots, external output) plus defense in depth insiderawPathRelativeToCanonicalRootpathScope.existing/filesabsolute realpath fallbackwriteViaSiblingTempPath,movePathToTrash, archive output preparationCompatibility: local alias walks, namespaced drives, and a Root/allowed root/destination that itself lives on
\\server\sharekeep working (same-share candidates are still resolved). The one intentional behavior change: an absolute input on a different share or device namespace is no longer resolved through filesystem aliases back into the boundary. No test or documented contract relied on that; error codes are unchanged. POSIX behavior is unchanged.Out of scope, noted for follow-up: when symlink following is allowed, an admitted local path whose symlink/reparse target is a UNC share is still resolved by the OS; this change only gates caller-supplied spellings. Standalone APIs where the caller supplies the location by contract (
resolveAbsolutePathFor*,readSecretFile, stores, lock targets,copyTree, ...) keep accepting UNC paths by design.Proof
New
test/windows-share-root-admission.test.ts: syntax tables (UNC, extended/device UNC, mixed separators, doubled separator, GLOBALROOT/Volume/pipe/PhysicalDrive, namespaced drives, same-share, Kelvin sign, POSIX no-op); POSIX-runnable regressions that spoofprocess.platformand spy onnode:fsto prove no call receives the canary host; Windows-only integration over Root,pathScope, secret writers, sibling-temp, trash and archive preparation in default and native-off modes, asserting nonode:fs/fs/promisescall argument contains the canary host.Negative control: removing the gates makes the spy tests fail with the canary lookups recorded (
/fs-safe-unc-canary,.../share,.../share/secret.txt, and one per audit site).Autoreview (Codex, P1): two rounds of findings fixed (GLOBALROOT key conflation; Win32 trimming/dot-segment aliases of namespace spellings, which led to the fail-closed namespace rule above), third round scoped-clean.
Local (macOS): full
pnpm test13,142 passed (only the known locally-unbuildable allocator artifact suite fails); typecheck,lint:file-size,lint:fs-boundary,docs:check,git diff --checkpass.Windows A/B proof on
fs-safe-windows-16core: run 36794090469 (throwawaydiag/unc-admission-proofbranch: this branch plus a setup file that records everynode:fscall whose argument starts with two separators, excluding namespaced drives; JavaScript fallback; Node 22 and 24;beforerestoressrc/from main 851bd85, tests unchanged). Suites:test:securityfiles plus the new test file.src/)The before leg reproduces the original observation:
write-boundary-bypass→lstatSync("\\server\share\")took 2782 ms, andread-boundary-bypass→lstatSync("\\server\share\secret.txt"); the canary payloads additionally reachlstatSync/realpathSync.nativeacross\\?\UNC\…,\\.\UNC\…and\\?\GLOBALROOT\Device\Mup\…prefixes. The after leg includes the real-Windows integration tests for Root,pathScope, secret writers, sibling-temp, trash and archive preparation in default and native-off modes.