Skip to content

fix(windows): gate shares before case-folded containment checks - #784

Merged
steipete merged 4 commits into
mainfrom
fix/windows-share-casefold-gates
Oct 1, 2026
Merged

steipete merged 4 commits into
mainfrom
fix/windows-share-casefold-gates

Conversation

@steipete

@steipete steipete commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem

Follow-up to #781, which landed the Windows share/device gate (reject UNC and device-namespace paths before any filesystem call). ClawSweeper's review of #781 found three paths where a caller-supplied network spelling could still be probed before that gate decided:

  1. Root direct admission: prepareRootTraversal ran the gate only after direct root-prefix admission, whose Windows case-fold branch inspects the caller's prefix identity (lstat).
  2. pathScope.existing/files: a lexically accepted path that is missing under the Root falls back to resolveNearestExistingPath on the caller's spelling.
  3. Secret-file writers: the path.relative containment check passes, then queue-key canonicalization lstats the caller's path.

Root cause

The share key in #781 deliberately folds ASCII case only, because Unicode folding is not how Windows resolves host names (U+212A KELVIN SIGN lowercases to k, while an attacker can answer LLMNR/NetBIOS lookups for the distinct name). But the library's other lexical containment checks fold Unicode case: Windows path.relative, the Root prefix identity match, and isPathInside. So \\fs-safe-Kost\share\root\x written with a Kelvin sign compared as inside \\fs-safe-kost\share\root, and the code then touched the caller's spelling before the ASCII gate ran.

Fix

The share/device decision now runs first at the shared lexical choke points rather than at individual call sites:

  • prepareRootTraversal: before direct admission (so the case-fold identity lookup never sees a foreign spelling)
  • pathScope's lexical core pathStaysWithinRoot (covers resolve, resolveAll, existing, files, writable, ensureDir)
  • the public isPathInside (covers root-context, install-path, symlink-parents, isPathInsideWithRealpath and other users)
  • secret-file assertPathWithinRoot (both the queue key and write preparation)

A path spelled exactly under a trusted boundary (byte prefix, no .. climbing above it) is never foreign, so Roots on \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopyN\... keep working now that the gate runs first.

The namespace rule is also more precise. For \\?\ and \\.\ spellings, the authority (head; plus host and share for UNC) must not be empty or end in a dot or space, which Win32 would rewrite. Later all-dot/space components may not climb above it (\\.\C:\..\UNC\host resolves to UNC in both Node and Win32), and GLOBALROOT/Global stay unidentifiable. Local spellings such as \\?\C:\root\a\..\b and \\?\C:\x. stay local instead of being rejected.

Owner decision: cross-share aliases

The maintainer's brief for #781 requires absolute inputs that are not lexically inside the boundary, in particular UNC and device-namespace paths, to be rejected lexically before any filesystem access. Accordingly, an absolute path on a different share or device is rejected even if a filesystem alias would resolve it back inside the boundary. Docs (security-model.md, path.md) and the changelog now state this and the upgrade path: pass boundary-relative paths, or spell them on the boundary's own share. The boundary's own share (ASCII case-insensitive), local alias walks, and namespaced drives are unchanged.

Proof

  • New regressions in test/windows-share-root-admission.test.ts:

    • POSIX-runnable: isPathInside rejects the Kelvin host while ASCII-case variants and exact GLOBALROOT children stay inside. Root direct admission with a Kelvin-host candidate is rejected with no node:fs call receiving the Kelvin spelling.
    • Windows-only: pathScope lexical methods and both secret writers reject the Kelvin host with no fs call. Also a real network Root on the runner's \\localhost\<drive>$ admin share: the same-share \\LOCALHOST\... spelling resolves to the in-root file, while \\127.0.0.1\... (another share key, same machine) is rejected with no fs call receiving that spelling. This case skips itself only when the admin share is unreachable.
  • Negative control: moving the Root gate back after direct admission and removing the isPathInside gate makes both POSIX regressions fail, and the Root case records lstat on the Kelvin-host prefix.

  • Local (macOS): full pnpm test 13,229 passed (only the known locally unbuildable allocator artifact suite fails); typecheck, lint:file-size, lint:fs-boundary, docs:check, git diff --check pass.

  • Autoreview (Codex, P1): the four remaining findings were INDEX-only, describing the pre-fix committed state that this commit replaces; no working-tree findings.

  • Real Windows A/B on fs-safe-windows-16core: run 36803970186 (throwaway diag branch: this change plus a setup file recording every node:fs call whose argument starts with two separators; JavaScript fallback; Node 22 and 24; before restores src/ from main 900a770, tests unchanged; verbose reporter).

    test (Windows) before (main) after (this PR)
    isPathInside rejects a Kelvin-sign host under an ASCII share × fails (main admits it) ✓
    pathScope lexical methods and both secret writers reject a Kelvin-sign host, no fs call × fails (main admits it) ✓
    real network Root on \\localhost\C$\…: same-share \\LOCALHOST\c$\… resolves to the in-root file; \\127.0.0.1\C$\… (other share key, same machine) rejected with no fs call ✓ ran ✓ ran
    security suites + share-admission file n/a 262 passed

    In the after legs, every recorded UNC-shaped fs call (22 per Node version) targets the trusted \\localhost\C$ Root or its same-share \\LOCALHOST\c$ spelling; none target the Kelvin host, 127.0.0.1, or a canary host. The Root direct-admission Kelvin regression is POSIX-runnable (platform spoof) and is covered by the local negative control above, where the old ordering records lstat on the Kelvin-host prefix.

Review 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 then probed before the ASCII-only share gate ran.

Run the share/device gate first in prepareRootTraversal (before the case-fold
identity lookup), in the pathScope lexical core, in isPathInside, and in the
secret-file containment check. 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: their 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; otherwise the target is
unidentifiable. Local namespaced-drive spellings such as \\?\C:\a\..\b stay
local.

Document the intentional cross-share compatibility change and the upgrade
path: pass boundary-relative paths or spell them on the boundary's own share.
@steipete
steipete requested a review from a team as a code owner October 1, 2026 02:03
@clawsweeper

clawsweeper Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@clawsweeper clawsweeper Bot added P0 Emergency: data loss, security bypass, crash loop, or unusable core runtime. merge-risk: 🚨 compatibility 🚨 Merging this PR could break existing users, config, migrations, defaults, or upgrades. merge-risk: 🚨 security-boundary 🚨 Merging this PR could weaken sandboxing, authorization, credentials, or sensitive data. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. labels Oct 1, 2026
@clawsweeper

clawsweeper Bot commented Oct 1, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed September 30, 2026, 10:42 PM ET / October 1, 2026, 02:42 UTC (Revision 3).

ClawSweeper review

What this changes

The branch checks Windows share and device boundaries before case-folded path containment, preserves explicitly trusted namespace children, and updates regression tests and compatibility documentation.

Merge readiness

✅ Ready for maintainer review

This PR remains necessary: current main still permits the early Unicode-folded admission paths identified in the report. No introduced correctness or security defect was found, and the previous review’s cleared findings remain resolved.

Priority: P0
Reviewed head: c73ce3d85d997030640b7b916269054855d9c963

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused security repair with strong Windows before/after evidence and no actionable introduced defect.
Proof confidence 🦞 diamond lobster (5/6) ✨ media proof bonus Sufficient (linked_artifact): The captured real-Windows Node 22/24 trace exercises production containment and secret-write admission, observes foreign-host rejection before filesystem dispatch, and demonstrates successful same-share Root resolution. Relevant code is unchanged since the prior proof-positive review; direct artifact-log retrieval was proxy-blocked. No stored-data contract changes.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Verified Sufficient (linked_artifact): The captured real-Windows Node 22/24 trace exercises production containment and secret-write admission, observes foreign-host rejection before filesystem dispatch, and demonstrates successful same-share Root resolution. Relevant code is unchanged since the prior proof-positive review; direct artifact-log retrieval was proxy-blocked. No stored-data contract changes.
Evidence reviewed 8 items Pinned introduced change: Inspected the complete introduced diff between the pinned main base and original PR head. It changes six production files, two test files, and three documentation files; no dependency, publishing, or workflow change is introduced.
Root gate precedes candidate identity inspection: The foreign-share check now precedes direct Root admission. Tracing root-boundary.ts confirms that case-folded admission can inspect the supplied prefix identity, making this ordering necessary.
Other early admission paths covered: The path-scope lexical core rejects a foreign authority before returning lexical success to existing-path fallbacks. Secret-file containment rejects it before queue-key canonicalization and write preparation. Public isPathInside also checks authority before Unicode-folded comparison.
Findings None None.
Security None None.

How this fits together

fs-safe checks caller-supplied paths against trusted filesystem roots before Node performs reads, writes, or metadata lookups. Windows share admission prevents rejected paths from first contacting an attacker-selected network host.

flowchart LR
A[Caller supplied path] --> B[Share and device admission]
C[Trusted filesystem root] --> B
B --> D[Containment and identity checks]
B --> E[Reject foreign authority]
D --> F[Guarded filesystem operation]
Loading

Before merge

None.

Agent review details

Security

None.

Review metrics

Metric Value Why it matters
Production and test LOC production +68/-31; tests +94/-6 Production growth implements shared authority admission and bounded namespace parsing for the demonstrated security gap.

Technical review

Best possible solution:

Keep Windows authority admission ahead of identity and fallback lookups, with trusted-share support and explicit cross-share upgrade guidance.

Do we have a high-confidence way to reproduce the issue?

Yes, source establishes the path: current main performs Unicode-folded Root admission before authority rejection, and the supplied Windows A/B trace demonstrates related containment failures. No target code was executed during this read-only review.

Is this the best way to solve the issue?

Yes, moving authority checks into the shared admission points addresses the demonstrated probes without duplicating filesystem operations or changing stored state.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against b0429464a423.

Labels

Label changes:

No label changes.

Label justifications:

  • P0: The follow-up closes early filesystem probes that can contact an attacker-selected SMB host and expose Windows credentials.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (linked_artifact): The captured real-Windows Node 22/24 trace exercises production containment and secret-write admission, observes foreign-host rejection before filesystem dispatch, and demonstrates successful same-share Root resolution. Relevant code is unchanged since the prior proof-positive review; direct artifact-log retrieval was proxy-blocked. No stored-data contract changes.
  • proof: sufficient: Contributor real behavior proof is sufficient. The captured real-Windows Node 22/24 trace exercises production containment and secret-write admission, observes foreign-host rejection before filesystem dispatch, and demonstrates successful same-share Root resolution. Relevant code is unchanged since the prior proof-positive review; direct artifact-log retrieval was proxy-blocked. No stored-data contract changes.

Evidence

What I checked:

  • Pinned introduced change: Inspected the complete introduced diff between the pinned main base and original PR head. It changes six production files, two test files, and three documentation files; no dependency, publishing, or workflow change is introduced. (c73ce3d85d99)
  • Root gate precedes candidate identity inspection: The foreign-share check now precedes direct Root admission. Tracing root-boundary.ts confirms that case-folded admission can inspect the supplied prefix identity, making this ordering necessary. (src/root-path.ts:179, c73ce3d85d99)
  • Other early admission paths covered: The path-scope lexical core rejects a foreign authority before returning lexical success to existing-path fallbacks. Secret-file containment rejects it before queue-key canonicalization and write preparation. Public isPathInside also checks authority before Unicode-folded comparison. (src/path-scope-lexical.ts:21, c73ce3d85d99)
  • Main and release still need the follow-up: Pinned main still invokes direct Root admission before its foreign-share guard. The latest release v0.21.3 lacks the share-key implementation in windows-path-syntax.ts. The merged predecessor fix(windows): reject UNC paths before filesystem access #781 therefore does not supersede this follow-up. (src/root-path.ts:179, b0429464a423)
  • Windows final-effect proof: The captured PR body reports real Windows Node 22/24 A/B observations through production path owners: trusted localhost share access succeeds, while distinct-share and Kelvin-host inputs produce no filesystem calls using those spellings; after legs report 262 passed. GitHub confirms all four jobs succeeded in https://github.com/openclaw/fs-safe/actions/runs/36803970186. Direct log retrieval was blocked by the proxy’s artifact-host allowlist, so raw logs were not independently read; this reviewer limitation does not negate the supplied trace. (test/windows-share-root-admission.test.ts:311, c73ce3d85d99)
  • Compatibility direction and upgrade guidance: The repository-member author explicitly records the owner direction in the captured body’s cross-share-alias section: reject different-share absolute spellings before filesystem access. Documentation supplies the alternative of boundary-relative paths or the boundary’s own share. Same-share access is exercised by the real Windows proof; no stored-data format changes. (docs/security-model.md:66, c73ce3d85d99)

Likely related people:

  • Peter Steinberger: Raw commit 900a770 adds src/windows-path-syntax.ts:29 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 900a77074fcd; files: src/windows-path-syntax.ts)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

Workflow

  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

History

Review history (2 earlier review cycles)
  • reviewed 2026-10-01T02:12:24.483Z sha 8a83830 :: needs maintainer review before merge. :: none
  • reviewed 2026-10-01T02:26:48.226Z sha da77c36 :: needs maintainer review before merge. :: none

@clawsweeper clawsweeper Bot added proof: sufficient Contributor real behavior proof is sufficient. and removed merge-risk: 🚨 security-boundary 🚨 Merging this PR could weaken sandboxing, authorization, credentials, or sensitive data. merge-risk: 🚨 compatibility 🚨 Merging this PR could break existing users, config, migrations, defaults, or upgrades. labels Oct 1, 2026
@steipete
steipete merged commit fce1668 into main Oct 1, 2026
48 checks passed
@steipete
steipete deleted the fix/windows-share-casefold-gates branch October 1, 2026 02:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

P0 Emergency: data loss, security bypass, crash loop, or unusable core runtime. proof: sufficient Contributor real behavior proof is sufficient. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant