Skip to content

Commit a8a2021

Browse files
author
lane-u5a-2359
committed
test(file-safety): pin the refusal the mode-copy identity guard owes
Give the staging check one file identity and the opened descriptor another, so the write has to refuse before the mode copy instead of publishing after having changed the mode of a file it never staged. With the guard in place the row passes; with the guard taken away the same row fails because the promise resolves rather than rejecting. It sits in the caller-supplied staging path group, beside the rows that stub the same flow. Measured with the committed instrument over the four selector ranges the guard derived, blocking on src/services/file-safety/safeWriteText.ts falls from 10 to 5 on that reduced selection. The row is a spec file, so the instrument counts 0 executable lines for it and it spends none of the headroom that file has under the per-file cap. Carries the test obligation for commit 5efde13 and answers review comment 4533489470 on pull 1912.
1 parent 98246bf commit a8a2021

1 file changed

Lines changed: 37 additions & 0 deletions

File tree

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,6 +1995,43 @@ describe("caller-supplied staging path", () => {
19951995
expect(fs.unlink).toHaveBeenCalledWith("/tmp/test-dir/.new_x_1.tmp")
19961996
})
19971997

1998+
it("refuses the mode copy when the opened descriptor is not the file the staging check validated", async () => {
1999+
const targetPath = "/tmp/test-dir/target.txt"
2000+
const customTempPath = "/tmp/test-dir/.new_control_swap.tmp"
2001+
const stagingName = path.resolve(customTempPath)
2002+
vi.mocked(fs.realpath).mockResolvedValue(targetPath)
2003+
// Three different identities: the staging file the location and file-type check
2004+
// validated, the target, and the file the staging name carries when the open happens.
2005+
// The third is a plain single-name regular file, exactly what a symlink to such a
2006+
// file hands the open, so the nlink guard on the opened descriptor passes it by.
2007+
const validated = _fileStatsWithIdentity(11n, 41n)
2008+
// The stand-in carries nlink as a number because fstatSync without bigint reports one, which
2009+
// is what the guard on the opened descriptor compares. The name check further down lstats
2010+
// with bigint, where nlink is a bigint, so this double gets the bigint spelling too.
2011+
const swappedOnDisk = _fileStatsWithIdentity(77n, 41n)
2012+
;(swappedOnDisk as unknown as { nlink: bigint }).nlink = 1n
2013+
const openedDescriptor = _fileStatsWithIdentity(77n, 41n)
2014+
let stagingLookups = 0
2015+
vi.mocked(fs.lstat).mockImplementation(async (p) => {
2016+
if (path.resolve(String(p)) !== stagingName) {
2017+
return swappedOnDisk
2018+
}
2019+
stagingLookups += 1
2020+
return stagingLookups === 1 ? validated : swappedOnDisk
2021+
})
2022+
vi.mocked(fsSync.statSync).mockReturnValue(_stats(0o600))
2023+
vi.mocked(fsSync.openSync).mockReturnValue(9)
2024+
vi.mocked(fsSync.fstatSync).mockReturnValue(openedDescriptor)
2025+
2026+
await expect(safeWriteText(targetPath, "", { tempPath: customTempPath, platform: "linux" })).rejects.toThrow(
2027+
"Staging file changed between the check and the mode copy",
2028+
)
2029+
// The mode change must not reach a file this write never staged.
2030+
expect(fsSync.fchmodSync).not.toHaveBeenCalled()
2031+
expect(fs.rename).not.toHaveBeenCalled()
2032+
// The refusal lands after the open, so the descriptor is still closed.
2033+
expect(fsSync.closeSync).toHaveBeenCalledWith(9)
2034+
})
19982035
it("rejects when the target identity cannot be compared for a reason other than a missing target", async () => {
19992036
const targetPath = "/tmp/test-dir/target.txt"
20002037
vi.mocked(fs.realpath).mockResolvedValue(targetPath)

0 commit comments

Comments
 (0)