feat(client): symlink-refusing filesystem primitives - #30
Merged
Merged
Conversation
This was referenced Aug 25, 2026
XieX
force-pushed
the
xie/skills-02-safe-fs
branch
from
August 28, 2026 18:02
f0205fa to
4c34138
Compare
XieX
force-pushed
the
xie/skills-02-safe-fs
branch
from
August 31, 2026 18:10
4c34138 to
afca284
Compare
XieX
marked this pull request as ready for review
September 14, 2026 20:34
The primitives the skills materialization layer will be built on, landed on their own because they know nothing about skills and can be read without any of it. Nothing is exported from the package root — this is internal machinery. `atomicWrite` creates its temp file exclusively (`O_EXCL`) in the target's own directory, so the rename is same-filesystem and cannot be redirected through a staging area an attacker controls, fsyncs the file, renames at mode `0644`, and fsyncs the directory. `openDirectoryNoFollow` opens with `O_NOFOLLOW | O_DIRECTORY`, so a symlinked directory is refused by the kernel rather than by a check that could be raced. `unlinkNoFollow` is the single managed-file delete. Both destructive calls go through the `fsOps` record so exactly those two operations, and nothing else, are interceptable from a test. On the TOCTOU exposure, stated plainly rather than papered over: Node exposes no `*at()` family — no `renameat`, `unlinkat`, or `openat`, and no `rename` on `FileHandle` — so a destructive operation cannot be addressed relative to a pinned directory descriptor. `SUPPORTS_DIR_FD` is a real feature probe of that family, and it is `false` here. What narrows the window instead is a `(dev, ino)` re-check against the pinned handle immediately before each destructive step, on top of the `O_NOFOLLOW` opens and the exclusive same-directory temp. That does not close the window. An attacker who already has write permission on the managed root can still win the race, and the two swap-race cases that would prove otherwise are skipped off the same probe the implementation gates on — written out in full so they become live the moment the probe flips. The `(dev, ino)` re-check is, on this runtime, the whole defense against a directory swapped between validation and a path-based rename or unlink, and it is invisible from the layer above: it could be deleted outright with every higher-level test still green. So it is staged here at the primitive layer instead — pin the handle, swap the directory underneath it, invoke the primitive. The checks here are also deliberately redundant with the ones the materialization layer will apply. A symlinked skill directory will be refused twice. Testing each layer on its own is what keeps that redundancy from rotting into a single point of failure. Client package: 360 -> 373 tests. typecheck and biome clean. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Review feedback on the safe-fs primitives. TEMP_NAME_ATTEMPTS was 128 with no justification behind it. Temp names carry 64 bits of randomness from randomBytes(8), so an EEXIST collision is not something that occurs in practice — the loop is a bound, not a probabilistic strategy. Lowered to 5 and the comment now says why. Comment sweep over both files: - Drop the reference to the Python SDK's use of the *at() family; the behavior of the family is what matters here. - Drop forward references to skills-fs.ts and skills-fs.test.ts, neither of which exists at this commit. - Drop the development narrative: why the module was split out, "its own file for two reasons", the mutation-testing result, and the defenses of test strategy and of probing instead of hardcoding. - Tighten the remaining prose, keeping the non-obvious technical content (O_NOFOLLOW vs lstat, recursive mkdir treating a symlink as present, same-directory temp for an atomic rename, why fsOps exists, and the residual TOCTOU exposure at SUPPORTS_DIR_FD). Comment lines 126 -> 106 and 54 -> 40. 355 client tests, typecheck, and biome all pass. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
XieX
force-pushed
the
xie/skills-02-safe-fs
branch
from
September 16, 2026 18:26
c9d20a1 to
3db4993
Compare
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 3db4993. Configure here.
andrewklatzke
approved these changes
Sep 16, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.

Second of seven. The filesystem primitives the skills materialization layer is built on, landed separately because they know nothing about skills and can be reviewed without any of it. Nothing is exported from the package root — this is internal machinery.
What's here
atomicWritecreates its temp file exclusively (O_EXCL) in the target's own directory, so the rename is same-filesystem and can't be redirected through a staging area an attacker controls. It fsyncs the file, renames at mode0644, then fsyncs the directory.openDirectoryNoFollowopens withO_NOFOLLOW | O_DIRECTORY, so a symlinked directory is refused by the kernel rather than by a check that could be raced.unlinkNoFollowis the single managed-file delete.Both destructive calls route through the
fsOpsrecord so exactly those two operations, and nothing else, are interceptable from a test.The write primitives take
data: Uint8Arrayrather thanBuffer(aBufferis aUint8Array, so this is a pure widening): skill content is opaque bytes end to end, and the layer that writes it should not demand a Node-specific subtype.The TOCTOU limitation, stated plainly
Node exposes no
*at()family — norenameat,unlinkat, oropenat, and norenameonFileHandle— so a destructive operation cannot be addressed relative to a pinned directory descriptor.SUPPORTS_DIR_FDis a real feature probe of that family, and it isfalsehere.What narrows the window instead is a
(dev, ino)re-check against the pinned handle immediately before each destructive step, on top of theO_NOFOLLOWopens and the exclusive same-directory temp file. That does not close the window. An attacker who already has write permission on the managed root can still win the race. The two swap-race cases that would prove otherwise are skipped off the same probe the implementation gates on, and written out in full so they become live the moment the probe flips.This is recorded at
SUPPORTS_DIR_FDrather than papered over.Why the
(dev, ino)re-check is tested hereOn this runtime it is the whole defense against a directory swapped between validation and a path-based rename or unlink — and it is invisible from the layer above. It could be deleted outright with every higher-level test still green. So it's staged here at the primitive layer: pin the handle, swap the directory underneath it, invoke the primitive.
The checks here are also deliberately redundant with the ones the materialization layer will apply — a symlinked skill directory gets refused twice. Exercising each layer on its own is what keeps that redundancy from rotting into a single point of failure.
Verification
Client package 342 → 355 tests.
typecheckandbiomeclean.🤖 Generated with Claude Code
Note
Overview
Adds internal
safe-fshelpers the skills materialization stack will use:atomicWrite/atomicWriteIn(exclusive same-directory temps, fsync, rename at0644),openDirectoryNoFollow/openOrCreateDirectory(O_NOFOLLOW, symlink-aware mkdir), andunlinkNoFollow. Destructiverename/unlinkgo through anfsOpsseam for tests; writes acceptUint8Array.SUPPORTS_DIR_FDprobes Node for descriptor-relative*at()APIs (currently false) and documents the remaining TOCTOU gap.assertUnswappedre-checks(dev, ino)on the pinned directory handle immediately before each destructive step.safe-fs.test.tscovers open flags,O_EXCLretry on temp collisions (mockedopen), cleanup, and staged directory-swap races for write and unlink.Reviewed by Cursor Bugbot for commit 3db4993. Bugbot is set up for automated code reviews on this repo. Configure here.