Skip to content

Commit 780966d

Browse files
file-safety: bind the commit rename to the identity the caller authorized
rename has no conditional form, so the pathname re-check above it cannot bind an authorization made outside this module: the path can become a link, or a different file, between the re-check and the commit. When SafeWriteTextOptions.authorizedTarget carries the dev/ino the caller authorized, the commit step now refuses unless the target still has that identity, which narrows the window to the stat-plus-rename pair - it does not close it, because no OS primitive renames only if the identity is unchanged. A failure to read the identity counts as "not that file", so the guard fails closed. Answers PRRT_kwDOSK-bwM609gmQ (CodeRabbit row on #1917 at guardedWrite.ts:230), landing in #1911 as ruled. Pinned interpreter v22.23.4: tsc --noEmit -p tsconfig.json exit 0 with 0 error lines; eslint --prune-suppressions --max-warnings=0 exit 0 with src/eslint-suppressions.json deep-unchanged (265 entries, 0 differing); prettier 3.9.10 --check exit 0; the six suites that cover this file pass. Gate at this head against merge-base 73931fe with --unified=0: 496 of 500 counted executable lines (411 to 416 in this file), i.e. +5 as an increment over 1910's head after a rebase.
1 parent f2847c4 commit 780966d

2 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import * as fs from "fs"
2+
import * as os from "os"
3+
import * as path from "path"
4+
5+
import { safeWriteText } from "../safeWriteText"
6+
7+
// `rename` has no conditional form: there is no way to ask it to fire only if the file under the
8+
// destination pathname is still the file an authorization was made about. The guard below is
9+
// therefore a comparison, made as late as it can be made, between the identity the caller handed
10+
// in and the identity of the path about to be written - and it refuses rather than publishing
11+
// when they disagree or when the identity cannot be read at all. These rows run against the real
12+
// filesystem, with no stubbing of fs.stat, because the assertion that matters is about what the
13+
// path actually is at the moment of the commit.
14+
describe("safeWriteText authorized identity", () => {
15+
let dir: string
16+
beforeEach(async () => {
17+
dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "safe-write-identity-"))
18+
})
19+
afterEach(async () => {
20+
await fs.promises.rm(dir, { recursive: true, force: true })
21+
})
22+
23+
it("publishes when the target is still the file the caller authorized", async () => {
24+
const file = path.join(dir, "target.txt")
25+
await fs.promises.writeFile(file, "before")
26+
const authorized = await fs.promises.stat(file)
27+
await safeWriteText(file, "after", { authorizedTarget: { dev: authorized.dev, ino: authorized.ino } })
28+
expect(await fs.promises.readFile(file, "utf8")).toBe("after")
29+
})
30+
31+
// The pathname is the same and the file is not: that is exactly the case a textual re-check
32+
// cannot see, because both spellings of the swap land on the same path.
33+
it("refuses to publish over a different file that took the authorized path", async () => {
34+
const file = path.join(dir, "target.txt")
35+
await fs.promises.writeFile(file, "before")
36+
const authorized = await fs.promises.stat(file)
37+
await fs.promises.unlink(file)
38+
await fs.promises.writeFile(file, "someone else's")
39+
await expect(
40+
safeWriteText(file, "after", { authorizedTarget: { dev: authorized.dev, ino: authorized.ino } }),
41+
).rejects.toThrow(/changed identity after it was authorized/)
42+
expect(await fs.promises.readFile(file, "utf8")).toBe("someone else's")
43+
})
44+
45+
// Failing closed: an unreadable identity is not the authorized file either, so the write refuses
46+
// instead of letting a raw stat failure escape as something else.
47+
it("refuses rather than reporting a raw stat failure when the authorized file is gone", async () => {
48+
const file = path.join(dir, "target.txt")
49+
await fs.promises.writeFile(file, "before")
50+
const authorized = await fs.promises.stat(file)
51+
await fs.promises.unlink(file)
52+
await expect(
53+
safeWriteText(file, "after", { authorizedTarget: { dev: authorized.dev, ino: authorized.ino } }),
54+
).rejects.toThrow(/changed identity after it was authorized/)
55+
await expect(fs.promises.access(file)).rejects.toMatchObject({ code: "ENOENT" })
56+
})
57+
})

‎src/services/file-safety/safeWriteText.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ export interface SafeWriteTextOptions {
1414
*/
1515
backup?: boolean
1616

17+
/**
18+
* The filesystem identity the caller authorized for this target. When present, the commit
19+
* rename below refuses unless the target is still that same file, so an authorization made
20+
* outside this module cannot be spent on a link or a different file swapped in for the pathname
21+
* afterwards. A failure to read the identity counts as "not that file": the guard fails closed.
22+
*/
23+
authorizedTarget?: { dev: number; ino: number }
24+
1725
/**
1826
* Platform override for testing. When omitted the real process.platform
1927
* value is used. Set to "win32" or "linux" / "darwin" from tests so that
@@ -735,6 +743,17 @@ export async function safeWriteText(
735743
)
736744
}
737745

746+
// A pathname re-check cannot bind an authorization: the path can become a link, or a
747+
// different file, after the re-check and before this rename, and a rename has no conditional
748+
// form. Comparing identities conditions the commit on the file that was authorized, which
749+
// narrows the window to this stat-plus-rename pair - the most a rename-based writer can do.
750+
const authorizedTarget = options?.authorizedTarget
751+
if (authorizedTarget) {
752+
const identity = await fs.stat(targetPath).catch(() => undefined)
753+
if (!identity || identity.dev !== authorizedTarget.dev || identity.ino !== authorizedTarget.ino) {
754+
throw new Error(`safeWriteText: ${absoluteFilePath} changed identity after it was authorized`)
755+
}
756+
}
738757
await fs.rename(tempPath, targetPath)
739758
committed = true
740759

0 commit comments

Comments
 (0)