Skip to content

Commit 8fa7d85

Browse files
Stage the confined write's bytes after staging, and copy the target mode onto it
CI's ubuntu leg was red on three rows of safeWriteJson.test.ts that windows skips, and all three were this merge's doing: - "confines a write whose symlink referent stays inside the confined directory" and "confines a scope path that itself runs through a symlink and does not exist yet" both died with `ENOENT: no such file or directory, open '<dir>/.<name> new_<ts>_<rand>.tmp'`. The escape-undo capture read the staged temp before _streamDataToFile had written it, so every confined write that reached the publish tried to read a file that did not exist yet. Windows never showed this because those rows are test.skipIf(win32), and this box cannot make a file symlink at all (measured on pinned node v22.23.4: fs.symlinkSync of a file -> EPERM). - "preserves a restrictive 0o600 target mode through the atomic publish" failed with `expected 420 to be 384`: the publish widened a 0o600 target to 0o644. The blob main brought in stages with the umask default and never copies the target's mode; the Zoo-Code-Org#1912 head 085fbf8 carries _copyTargetMode for this same function, and that head is not an ancestor of 73931fe (git merge-base --is-ancestor, exit 1), so this does not claim main accepted and dropped it - only that the two resolutions differ on a permission property, which is the leftover recorded on the PR rather than a thing to resolve silently inside one unit. Both fixes hold the seam rule: staging, the mode copy, the flush, the commit rename and the rollback rename all name the same resolved string. The helper is grafted verbatim from 085fbf8 (stat the target, chmod the staged file, log and continue on a stat or chmod failure, nothing to copy for a target that is not there yet) so the two resolutions of this file stay one shape. The staged-bytes read moved after the staging write and before the mode copy, so a restrictive target mode cannot turn the capture into a failure. Verification, pinned node v22.23.4 (box node is 25.9.0): misc shard exit 0 with 2116 passed / 17 skipped, identical tallies to before the change, and it cannot prove the three rows because windows skips them; tsc -p src/tsconfig.json exit 0; prettier --check clean; eslint --max-warnings=0 exit 0; tree clean at this commit. The ubuntu leg that found the rows is the arbiter for them. Refs: Zoo-Code-Org#1375, Zoo-Code-Org#1991, Zoo-Code-Org#2019.
1 parent e3d6327 commit 8fa7d85

1 file changed

Lines changed: 43 additions & 7 deletions

File tree

‎src/utils/safeWriteJson.ts‎

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,16 +382,26 @@ async function safeWriteJson(filePath: string, data: unknown, options?: SafeWrit
382382
// filesystems, and mixing the two spellings is how a publish ends up beside the link
383383
// it meant to replace.
384384

385-
// The bytes this write is about to publish identify the file a post-commit escape
386-
// undo may remove. They are only needed when confinement can trigger that undo, and
387-
// reading them here keeps the comparison independent of serialization details.
385+
await _streamDataToFile(actualTempNewFilePath, data, options?.prettyPrint)
386+
387+
// The bytes this write publishes identify the file a post-commit escape undo may
388+
// remove. They are read after the staged file is written - before it there is nothing
389+
// to read, and reading early failed with ENOENT on every confined write that reached
390+
// the publish - and before the mode copy below, so a restrictive target mode cannot
391+
// turn this read into a failure. A read failure aborts the write: publishing without
392+
// the bytes would leave the undo unable to verify (and remove) an escaped file.
388393
if (confinementRoot !== undefined) {
389-
// A read failure must abort the write: publishing without the bytes would make
390-
// the post-commit escape undo unable to verify (and remove) an escaped file.
391394
stagedBytes = await fs.readFile(actualTempNewFilePath)
392395
}
393396

394-
await _streamDataToFile(actualTempNewFilePath, data, options?.prettyPrint)
397+
// CWE-732: the commit rename below lands the staged file onto the target, so the
398+
// file that survives carries the staged file's mode, not the target's. A 0o600
399+
// target would otherwise be published 0o644 from the umask-derived default. The
400+
// #1912 head (085fbf85e) carries this step for the same function; the blob main
401+
// brought in does not, and without it the two resolutions differ on a permission
402+
// property and the row that pins it can only pass on one platform.
403+
await _copyTargetMode(resolvedTargetPath, actualTempNewFilePath)
404+
395405
await _flushFileToDisk(actualTempNewFilePath)
396406

397407
// Step 2: Commit the replacement with a single atomic rename. The
@@ -569,7 +579,33 @@ async function _streamDataToFile(targetPath: string, data: unknown, prettyPrint
569579
}
570580

571581
/**
572-
* Flushes a fully written temp file to storage before it is committed.
582+
* Copies the existing target's permission mode onto the staged file so the publish
583+
* does not silently widen it. A missing target has no mode to copy; a stat or chmod
584+
* that fails for another reason is logged and the write proceeds with the staged
585+
* file's own mode, because the replacement protocol - not the mode - is what makes the
586+
* write atomic.
587+
*/
588+
async function _copyTargetMode(targetPath: string, stagedPath: string): Promise<void> {
589+
let mode: number
590+
try {
591+
const stats = await fs.stat(targetPath)
592+
mode = stats.mode & 0o777
593+
} catch (statError: unknown) {
594+
const code = getErrorCode(statError)
595+
if (code && code !== "ENOENT" && code !== "ENOTDIR") {
596+
console.error(`Could not read the mode of ${targetPath} before publishing onto it:`, statError)
597+
}
598+
return
599+
}
600+
try {
601+
await fs.chmod(stagedPath, mode)
602+
} catch (chmodError: unknown) {
603+
console.error(`Failed to copy mode ${mode.toString(8)} from ${targetPath} onto ${stagedPath}:`, chmodError)
604+
}
605+
}
606+
607+
/**
608+
* Syncs the directory that holds a freshly committed file.
573609
* Best effort: durability against power loss is a bonus here, process-crash
574610
* safety comes from the rename protocol itself, and a filesystem that
575611
* refuses fsync must not block the write.

0 commit comments

Comments
 (0)