Skip to content

Commit ad18ac4

Browse files
file-safety: give the identity guard a row where the target changes mid-write
The review on safeWriteText.identity.spec.ts asked for a row where the target is replaced between staging and the commit rename rather than before the call. The seams it needs are already in the writer: options.platform picks which step runs, and options.execFileRunner is the icacls call that step makes. platform: "win32" plus an injected runner now swaps the target for a second live file when icacls is asked to dump its DACL, which is after the temp is staged and before the commit-time identity check, so the guard fires from a change inside the call and the stranger keeps the pathname it was swapped in for. The swap renames the target aside instead of unlinking it so the authorized inode stays allocated - the unlink-and-rewrite shape is the one that agreed with itself on Windows and disagreed on the ubuntu runner, and a comment in this file records that trap. The second row reads the same gate from the other side: with the override saying linux the injected runner is never called. On a Windows host that path ends in PostCommitDurabilityError - fsync on a read-only handle is EPERM on win32, the same reason the backup fsync above opens its handle r+ - so the row asserts the seam and the committed content and admits no other error class. The third row asserts the clean publish and is skipped on win32 for that measured reason: the ubuntu CI arm is the instrument for it, and that arm's negative control is this same file with the override omitted, where process.platform is the platform under test rather than a value handed to the writer. Verification, pinned node v22.23.4 with stdio on file descriptors, run from wt-u2-1911/src through its src/node_modules (vitest 4.1.12, realpath Zoo-Code/node_modules/.pnpm/[email protected]_@opentelemetr_ 041b5b095ecf5a50bc5f59b473dbb794): services/file-safety identity, main and alias specs give 88 passed and 1 skipped in 3 files; tsc --noEmit -p tsconfig.json in src reports 0 errors; eslint --prune-suppressions --max-warnings=0 exits 0 on the touched file and its suppressions rewrite was reverted, so src/eslint-suppressions.json is byte-identical with HEAD; prettier --check is clean and formatting ran before measuring. Gate: 496 of 500 counted executable lines against 73931fe both before and after this commit - the delta is 103 lines in one spec file and specs charge nothing, and no production line changed.
1 parent 2e79e37 commit ad18ac4

1 file changed

Lines changed: 103 additions & 1 deletion

File tree

‎src/services/file-safety/__tests__/safeWriteText.identity.spec.ts‎

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,42 @@ import * as fs from "fs"
22
import * as os from "os"
33
import * as path from "path"
44

5-
import { safeWriteText } from "../safeWriteText"
5+
import { PostCommitDurabilityError, safeWriteText } from "../safeWriteText"
6+
import type { SafeWriteTextOptions } from "../safeWriteText"
7+
8+
const IS_WINDOWS = process.platform === "win32"
9+
10+
// Plays icacls for a test: records the flag it was asked for and, on its first call, moves *file*
11+
// aside and leaves a different live file at that pathname. Renamed rather than unlinked, so the
12+
// authorized inode stays allocated and cannot be handed back to the swap (the reuse that made the
13+
// unlink-and-rewrite fixture above agree with itself on one runner and disagree on the other). The
14+
// tail callback is read off the argument list because execFileRunner is typed as child_process
15+
// .execFile, whose signatures take 2, 3 or 4 arguments.
16+
// The return type needs an assertion, not a plain annotation: execFileRunner is typed as
17+
// child_process.execFile, whose type carries a __promisify__ property that no plain function
18+
// literal has, so the seam is built as a function and asserted into that shape.
19+
function icaclsSeam(onDump?: [string, string], calls?: string[]): SafeWriteTextOptions["execFileRunner"] {
20+
let callsMade = 0
21+
const seam = (...args: unknown[]): void => {
22+
const tail = args[args.length - 1]
23+
const finish = (error?: Error | null): void => {
24+
if (typeof tail === "function") (tail as (e?: Error | null) => void)(error)
25+
}
26+
const argv = args[1]
27+
calls?.push(Array.isArray(argv) ? String(argv[1] ?? "") : "")
28+
if (onDump && callsMade++ === 0) {
29+
const [swapFrom, swapAside] = onDump
30+
void (async (): Promise<void> => {
31+
await fs.promises.rename(swapFrom, swapAside)
32+
await fs.promises.writeFile(swapFrom, "someone else's")
33+
finish()
34+
})()
35+
return
36+
}
37+
finish()
38+
}
39+
return seam as unknown as SafeWriteTextOptions["execFileRunner"]
40+
}
641

742
// `rename` has no conditional form: there is no way to ask it to fire only if the file under the
843
// destination pathname is still the file an authorization was made about. The guard below is
@@ -52,6 +87,73 @@ describe("safeWriteText authorized identity", () => {
5287
expect(await fs.promises.readFile(other, "utf8")).toBe("someone else's")
5388
})
5489

90+
// The row the review asked for, in the shape its own suggestion named: the file under the pathname
91+
// changes *during* the call rather than before it, through the seam the win32 step uses. platform
92+
// is overridden to "win32" so the DACL step runs on either runner, and the runner swaps the target
93+
// when icacls is asked to dump its DACL - after the temp is staged, before the commit-time identity
94+
// check. A check made on entry had already chosen this pathname and would publish over the stranger.
95+
it("refuses when a second live file takes the pathname while the write is in progress", async () => {
96+
const file = path.join(dir, "target.txt")
97+
const aside = path.join(dir, "authorized.txt")
98+
const calls: string[] = []
99+
await fs.promises.writeFile(file, "before")
100+
const authorized = await fs.promises.stat(file, { bigint: true })
101+
await expect(
102+
safeWriteText(file, "after", {
103+
authorizedTarget: { dev: authorized.dev, ino: authorized.ino },
104+
platform: "win32",
105+
execFileRunner: icaclsSeam([file, aside], calls),
106+
}),
107+
).rejects.toThrow(/changed identity after it was authorized/)
108+
expect(calls).toEqual(["/save"]) // the win32 step ran, from the override alone
109+
expect(await fs.promises.readFile(file, "utf8")).toBe("someone else's")
110+
expect(await fs.promises.readFile(aside, "utf8")).toBe("before")
111+
})
112+
113+
// The other side of the same gate: with the override reading linux, no runner reaches icacls. The
114+
// commit rename is asserted through the durability error the linux arm can raise on a Windows host,
115+
// because fsync on a read-only handle is EPERM on win32 - the same reason the backup fsync below
116+
// opens its handle "r+" - so on win32 hosts this row sees PostCommitDurabilityError with the
117+
// content already at the target, and on a POSIX host it sees no error at all, which the next row
118+
// asserts where the runner can.
119+
it(`leaves the icacls seam untouched when the platform override says linux`, async () => {
120+
const file = path.join(dir, "target.txt")
121+
const calls: string[] = []
122+
await fs.promises.writeFile(file, "before")
123+
const authorized = await fs.promises.stat(file, { bigint: true })
124+
let failure: unknown = null
125+
try {
126+
await safeWriteText(file, "after", {
127+
authorizedTarget: { dev: authorized.dev, ino: authorized.ino },
128+
platform: "linux",
129+
execFileRunner: icaclsSeam(undefined, calls),
130+
})
131+
} catch (error: unknown) {
132+
failure = error
133+
}
134+
expect(calls).toEqual([])
135+
expect(await fs.promises.readFile(file, "utf8")).toBe("after")
136+
if (failure !== null) expect(failure).toBeInstanceOf(PostCommitDurabilityError)
137+
})
138+
139+
// The linux arm's own row, and the one this box cannot prove: a runner whose real platform is
140+
// linux publishes through the same seam-free path with no error at all. It is skipped, not deleted,
141+
// on Windows - the measured reason is the EPERM above - so the ubuntu CI arm is the instrument that
142+
// proves the clean publish, and its negative control is this same file with the override omitted,
143+
// where process.platform is the platform being tested rather than a value handed to the writer.
144+
it.runIf(!IS_WINDOWS)(`publishes with no durability complaint when the platform override says linux`, async () => {
145+
const file = path.join(dir, "target.txt")
146+
const calls: string[] = []
147+
await fs.promises.writeFile(file, "before")
148+
const authorized = await fs.promises.stat(file, { bigint: true })
149+
await safeWriteText(file, "after", {
150+
authorizedTarget: { dev: authorized.dev, ino: authorized.ino },
151+
platform: "linux",
152+
execFileRunner: icaclsSeam(undefined, calls),
153+
})
154+
expect(calls).toEqual([])
155+
expect(await fs.promises.readFile(file, "utf8")).toBe("after")
156+
})
55157
// Failing closed: an unreadable identity is not the authorized file either, so the write refuses
56158
// instead of letting a raw stat failure escape as something else.
57159
it("refuses rather than reporting a raw stat failure when the authorized file is gone", async () => {

0 commit comments

Comments
 (0)