Skip to content

Commit 94240dc

Browse files
fix(checkpoints): refuse a read-only destination before the atomic rename
writeFileAtomically stages into a sibling file and renames it into place. POSIX rename(2) only needs write permission on the containing directory, so the rename replaced a destination the user marked read-only where the direct write this helper replaced failed with EACCES. Check the resolved destination for writability before a staging file exists, so a refused write leaves the previous bytes and no leftovers. The spec case states its own premise: it runs only where access(2) actually refuses a 0444 file, asserts EACCES on POSIX and EPERM on Windows, and shows the directory holds nothing but the original file afterwards. Addresses review comment 4347051548 on pull 1404.
1 parent d25a24b commit 94240dc

2 files changed

Lines changed: 45 additions & 0 deletions

File tree

‎src/integrations/editor/__tests__/atomicWrite.spec.ts‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// npx vitest run integrations/editor/__tests__/atomicWrite.spec.ts
22

33
import fs from "fs/promises"
4+
import { constants as fsConstants } from "fs"
45
import os from "os"
56
import path from "path"
67

@@ -44,6 +45,39 @@ describe("writeFileAtomically", () => {
4445
expect(stat.mode & 0o777).toBe(0o755)
4546
})
4647

48+
// The compatibility case rename(2) skips: it needs write permission on the directory,
49+
// not on the destination, so a read-only destination must still fail the way the
50+
// direct write this helper replaced failed. POSIX reports EACCES for that, Windows
51+
// maps the read-only attribute onto EPERM, and a root-run container can ignore
52+
// mode bits altogether, which is why the case checks its own premise.
53+
it("refuses a read-only destination and leaves the previous bytes and no leftovers", async () => {
54+
const target = path.join(dir, "locked.txt")
55+
await fs.writeFile(target, "old", "utf8")
56+
await fs.chmod(target, 0o444)
57+
58+
let destinationWritable = true
59+
try {
60+
await fs.access(target, fsConstants.W_OK)
61+
} catch {
62+
// access(2) refused the 0444 destination: that is the premise this case needs.
63+
destinationWritable = false
64+
}
65+
if (destinationWritable) {
66+
// Where access(2) ignores mode bits the premise is absent; the case then
67+
// reports nothing rather than asserting the wrong thing.
68+
await fs.chmod(target, 0o644)
69+
return
70+
}
71+
72+
await expect(writeFileAtomically(target, "new")).rejects.toMatchObject({
73+
code: process.platform === "win32" ? "EPERM" : "EACCES",
74+
})
75+
76+
expect(await fs.readFile(target, "utf8")).toBe("old")
77+
expect(await fs.readdir(dir)).toEqual(["locked.txt"])
78+
await fs.chmod(target, 0o644)
79+
})
80+
4781
posixOnly("updates a symlink through its target without replacing the symlink", async () => {
4882
const target = path.join(dir, "real.txt")
4983
const link = path.join(dir, "link.txt")

‎src/integrations/editor/atomicWrite.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as fs from "fs/promises"
2+
import { constants as fsConstants } from "fs"
23

34
/**
45
* Write a file so readers only ever observe the previous bytes or the complete
@@ -15,16 +16,26 @@ export async function writeFileAtomically(absolutePath: string, content: string,
1516
let targetPath = absolutePath
1617
let targetMode = mode
1718
let targetOwnership: { uid: number; gid: number } | undefined
19+
let targetExists = false
1820
try {
1921
targetPath = await fs.realpath(absolutePath)
2022
const stat = await fs.stat(targetPath)
23+
targetExists = true
2124
targetMode = targetMode ?? stat.mode & 0o7777
2225
targetOwnership = { uid: stat.uid, gid: stat.gid }
2326
} catch (error) {
2427
if ((error as NodeJS.ErrnoException)?.code !== "ENOENT") {
2528
throw error
2629
}
2730
}
31+
// POSIX rename(2) only asks for write permission on the containing directory, so a
32+
// rename would replace a destination the user marked read-only where the direct
33+
// write this helper replaced failed with EACCES. Check the destination itself,
34+
// before any staging file exists; a destination that is not there yet stays
35+
// creatable. Windows reports the same condition as EPERM via the read-only attribute.
36+
if (targetExists) {
37+
await fs.access(targetPath, fsConstants.W_OK)
38+
}
2839
// Derive the sibling staging path with plain string operations so the helper stays
2940
// usable in unit tests that mock the "path" module down to a few functions.
3041
const separatorIndex = Math.max(targetPath.lastIndexOf("/"), targetPath.lastIndexOf("\\"))

0 commit comments

Comments
 (0)