Skip to content

Commit ae7f2e6

Browse files
test(utils): carry the confinement-escape teardown over byte-equal staged bytes
The teardown row ran with staged bytes different from the bytes already at the target, so the byte comparison inside undoEscapedPublish was what left the restored original alone. Parameterize it over both payload shapes: the distinct-bytes case keeps the assertion that already held, and the byte-equal case is the one content cannot distinguish, which now rests on the rollback-restored flag. Both cases reach the restored file by rename - the ancestor swap is a rename and so is the rollback - so neither outcome turns on ext4 reusing dev, ino and size across a remove and create; a remove-and-recreate fixture would have been reading that accident on a Linux runner instead of the teardown. Each case takes its own directories because the first leaves the ancestor as a junction or symlink the second could not reuse.
1 parent 4e44e13 commit ae7f2e6

1 file changed

Lines changed: 64 additions & 47 deletions

File tree

‎src/utils/__tests__/safeWriteJson.test.ts‎

Lines changed: 64 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,61 +1363,78 @@ describe("safeWriteJson", () => {
13631363
await undoEscapedPublish(escaped, ours)
13641364
expect(fsSyncActual.existsSync(escaped)).toBe(false)
13651365
})
1366-
test("tears down rollback and staged-file cleanup before reporting a confinement escape", async () => {
1367-
// The catch-path re-check reported the escape ahead of the backup rollback and the
1368-
// staged-file cleanup, so a recheck failure skipped both. This fixture takes the Windows
1369-
// EPERM fallback so a backup and a staged file both exist when the failure lands, and
1370-
// swaps the ancestor between the fallback two renames via the trace seam, moving the
1371-
// ancestor own contents under the pointing link so every path stays reachable and the
1372-
// teardown is asserted rather than merely attempted.
1373-
const projectDir = path.join(tempDir, "teardown-project")
1374-
const ancestor = path.join(projectDir, "settings")
1375-
await fs.mkdir(ancestor, { recursive: true })
1376-
const target = path.join(ancestor, "mcp.json")
1377-
await fs.writeFile(target, JSON.stringify({ inside: true }))
1378-
const swapRoot = path.join(tempDir, "teardown-swap-root")
1379-
await fs.mkdir(swapRoot, { recursive: true })
1380-
const renames: string[][] = []
1381-
let commitAttempts = 0
1382-
vi.mocked(fs.rename).mockImplementation(async (from, to) => {
1383-
const source = String(from)
1384-
renames.push([source, String(to)])
1385-
if (source.includes(".new_")) {
1386-
commitAttempts++
1387-
if (commitAttempts === 1) {
1388-
throw Object.assign(new Error("EPERM: operation not permitted"), { code: "EPERM" })
1389-
}
1390-
if (commitAttempts === 2) {
1391-
throw Object.assign(new Error("EIO on the fallback commit"), { code: "EIO" })
1366+
// Two payloads against the same fixture. The distinct-bytes case is the one that already
1367+
// passed: there the byte comparison inside undoEscapedPublish is what leaves the restored
1368+
// original alone. The byte-equal case is the one content cannot tell apart, so it rests on
1369+
// the rollback-restored check in safeWriteJson.ts instead. The fixture reaches the restored
1370+
// file by rename in both cases - the ancestor swap is a rename and so is the rollback -
1371+
// which is what keeps this pair measuring the teardown rather than ext4 reusing dev, ino
1372+
// and size across a remove and create, the accident a remove-and-recreate fixture would
1373+
// have been reading on a Linux runner. Each case takes its own directories because the
1374+
// first leaves the ancestor as a junction or symlink the second could not reuse.
1375+
it.each([
1376+
{
1377+
label: "distinct staged bytes",
1378+
dir: "distinct",
1379+
payload: { written: "payload" },
1380+
restored: { inside: true },
1381+
},
1382+
{ label: "byte-equal staged bytes", dir: "equal", payload: { inside: true }, restored: { inside: true } },
1383+
])(
1384+
"tears down rollback and staged-file cleanup before reporting a confinement escape ($label)",
1385+
async ({ dir, payload, restored }: { dir: string; payload: unknown; restored: unknown }) => {
1386+
// The catch-path re-check reported the escape ahead of the backup rollback and the
1387+
// staged-file cleanup, so a recheck failure skipped both. This fixture takes the Windows
1388+
// EPERM fallback so a backup and a staged file both exist when the failure lands, and
1389+
// swaps the ancestor between the fallback two renames via the trace seam, moving the
1390+
// ancestor own contents under the pointing link so every path stays reachable and the
1391+
// teardown is asserted rather than merely attempted.
1392+
const projectDir = path.join(tempDir, "teardown-project-" + dir)
1393+
const ancestor = path.join(projectDir, "settings")
1394+
await fs.mkdir(ancestor, { recursive: true })
1395+
const target = path.join(ancestor, "mcp.json")
1396+
await fs.writeFile(target, JSON.stringify({ inside: true }))
1397+
const swapRoot = path.join(tempDir, "teardown-swap-root-" + dir)
1398+
await fs.mkdir(swapRoot, { recursive: true })
1399+
const renames: string[][] = []
1400+
let commitAttempts = 0
1401+
vi.mocked(fs.rename).mockImplementation(async (from, to) => {
1402+
const source = String(from)
1403+
renames.push([source, String(to)])
1404+
if (source.includes(".new_")) {
1405+
commitAttempts++
1406+
if (commitAttempts === 1) {
1407+
throw Object.assign(new Error("EPERM: operation not permitted"), { code: "EPERM" })
1408+
}
1409+
if (commitAttempts === 2) {
1410+
throw Object.assign(new Error("EIO on the fallback commit"), { code: "EIO" })
1411+
}
13921412
}
1393-
}
1394-
return fsPromisesActuals.rename!(from, to)
1395-
})
1396-
const reason = await safeWriteJson(
1397-
target,
1398-
{ written: "payload" },
1399-
{
1413+
return fsPromisesActuals.rename!(from, to)
1414+
})
1415+
const reason = await safeWriteJson(target, payload, {
14001416
confineTo: projectDir,
14011417
trace: ({ stage, detail }) => {
14021418
if (stage === "replacement-rename" && detail === "eperm-fallback") {
14031419
fsSyncActual.renameSync(ancestor, path.join(swapRoot, "settings"))
14041420
fsSyncActual.symlinkSync(path.join(swapRoot, "settings"), ancestor, ancestorLinkKind)
14051421
}
14061422
},
1407-
},
1408-
).then(
1409-
() => undefined,
1410-
(rejection: unknown) => rejection,
1411-
)
1412-
expect(reason).toBeInstanceOf(ConfinedPathEscapeError)
1413-
// A rename FROM the backup path is the rollback; none happened while the escape threw
1414-
// ahead of the teardown.
1415-
expect(renames.some((call) => call[0].includes(".bak_"))).toBe(true)
1416-
// The original content is back where the target lives, not stranded in the backup.
1417-
expect(await readFileContent(target)).toEqual({ inside: true })
1418-
// The staged .new file is gone rather than left beside it.
1419-
expect((await fs.readdir(ancestor)).filter((entry) => entry.includes(".new_"))).toEqual([])
1420-
})
1423+
}).then(
1424+
() => undefined,
1425+
(rejection: unknown) => rejection,
1426+
)
1427+
expect(reason).toBeInstanceOf(ConfinedPathEscapeError)
1428+
// A rename FROM the backup path is the rollback; none happened while the escape threw
1429+
// ahead of the teardown.
1430+
expect(renames.some((call) => call[0].includes(".bak_"))).toBe(true)
1431+
// The original content is back where the target lives, not stranded in the backup.
1432+
expect(await readFileContent(target)).toEqual(restored)
1433+
// The staged .new file is gone rather than left beside it.
1434+
expect((await fs.readdir(ancestor)).filter((entry) => entry.includes(".new_"))).toEqual([])
1435+
},
1436+
)
1437+
14211438
test("keeps the restored original when the escape re-check fires over a successful fallback commit", async () => {
14221439
// The teardown fixture above fails the fallback commit, so it never reaches the shape the
14231440
// finding names. Here the EPERM fallback commits, the post-commit re-check sees the escape,

0 commit comments

Comments
 (0)