Repository navigation
fix(atomic): bind replacement modes to descriptors - #91
Conversation
|
Codex review: needs maintainer review before merge. Reviewed August 2, 2026, 5:18 PM ET / 21:18 UTC. ClawSweeper reviewWhat this changesThe branch removes post-rename destination-path mode changes from atomic replacement, applies modes through open descriptors, and documents the conditional Merge readinessKeep this PR open for normal merge review. It is a focused security repair: current Priority: P1 Review scores
Verification
How this fits togetherThe atomic-write subsystem creates a sibling temporary file, optionally synchronizes it, then publishes it by rename or a guarded copy fallback. Its permission handling must remain bound to the intended file rather than resolving the caller-controlled destination path after publication. flowchart LR
A[Caller content and mode] --> B[Sibling temporary file]
B --> C[Write and descriptor-bound mode]
C --> D{Publish method}
D --> E[Atomic rename]
D --> F[Guarded copy fallback]
E --> G[Published replacement]
F --> G
G --> H[Optional parent sync]
Before merge
Agent review detailsSecurityNone. Review metrics
Merge-risk optionsMaintainer options:
Technical reviewBest possible solution: Merge the descriptor-bound mode implementation with its documented conditional sync-adapter contract, then release it as the fix for the atomic replacement permission race. Do we have a high-confidence way to reproduce the issue? Yes. Current main’s source shows a destination-path Is this the best way to solve the issue? Yes. Binding the requested mode to the already-open temp or pinned fallback descriptor is the narrowest maintainable fix and preserves exact modes under umask without dereferencing the published destination path. AGENTS.md: found and applied where relevant. Codex review notes: model internal, reasoning high; reviewed against 2477f5681f68. LabelsLabel changes:
Label justifications:
EvidenceWhat I checked:
Likely related people:
Rating scale
Overall follows the weaker of proof and patch quality. Workflow
|
PR #91 bound replacement file modes to descriptors but left the directory mode applied by pathname after the recursive mkdir. mkdir({recursive: true}) may return a directory that already existed, and a freshly created one can be renamed by whoever controls its parent, so an attacker with mutation rights on the destination's parent could swap in a symlink and redirect the mode change. On POSIX the directory mode now goes through a verified O_DIRECTORY|O_NOFOLLOW descriptor and fchmod; no pathname chmod remains. Windows keeps mkdir(mode) only, since POSIX directory modes are not enforceable there. Plain node:fs injection keeps working on both the async and sync paths. An explicit sync dirMode without the existing optional fchmodSync fails before mutating anything.
Fixes #86.
What was wrong
replaceFileAtomic()andreplaceFileAtomicSync()reached the caller-requestedmodeby callingchmodon the destination path, after the rename had already completed. A local user able to write the parent directory could unlink the destination in that window and drop a symlink in its place;chmodfollowed it and the mode change landed on an unrelated file.The temp file could not carry the mode on its own, because it was created with
writeFile(tempPath, content, { mode, flag: "wx" })and so had its mode reduced by the process umask. The post-renamechmodwas the only thing that reached the exact requested mode, and no option disabled it.Reported with a complete reproduction by @yetval.
What changed
The exclusive temp descriptor is now held open across write, mode application, optional
fsync, and close, before the rename. The copy fallback applies modes through its pinned destination descriptor. No post-publication destinationchmodremains anywhere in the replacement path.The async interface needs no new member: it applies the mode through the
FileHandlereturned by the already-requiredopen. The synchronous path does add an optionalfchmodSync, becauseopenSyncreturns a bare descriptor with no equivalent method;typeof fssatisfies it, and adapters that pass neithermodenorpreserveExistingModeare unaffected. A sync adapter that requests a mode without providingfchmodSyncfails before any mutation rather than silently falling back.Proof
Real syscalls, real on-disk directories, umask
077, with the destination swap staged at the rename boundary. Before, on both entry points, the recordedchmodpath list contained the destination and an unrelated644file silently became600. After:The unrelated file keeps
644, only the parent directory is chmodded, and the requested mode is still reached exactly despite the umask. The final line covers the compatibility shape that matters most in practice — a plainnode:fsinjected asfileSystemtogether with an explicitmode— which is now pinned by a regression test.pnpm check651 passed / 25 skipped,pnpm test:security62 passed,git diff --checkclean, Codex autoreview clean. Regressions fail onmainand pass here.Follow-ups, deliberately not in this PR
Two adjacent by-path
chmodcalls deserve their own review and are untouched here.dirModeis applied viachmod(dir, dirMode)after a recursivemkdirthat may return a pre-existing directory, so an attacker controlling the destination's parent could redirect it — a stronger prerequisite than this issue. AndmovePathWithCopyFallbackchmods randomized staging paths before publication; the names are unpredictable but visible to anyone who can list the writable target directory.