Files
reverse-engineering-anything/tests/boundary/filesystem/safeOutputTree.test.ts
T
Adolanium 36c6c61003 fix(resources): close unadmitted output handles and preserve caller directories (#13201)
* fix(resources): close output handles when pending-file admission fails

Rollback admitted pending files before closing their handles.
An admission failure skipped the close and left the descriptor open.
Close the handles after that failure, keep the unadmitted tree, and report the admission error.
A close failure stays on the admission error as cleanup.

* fix(resources): rethrow a refusal of an unowned output directory

create wrapped a non-empty accepted directory in SafeOutputTreeCreationFailure.
Callers then rolled that tree back, and the cleanup treated the caller's files as an unowned remainder.
Rethrow SafeOutputTreeDestinationRefusal when this call did not create the directory.

* fix(resources): retry pending-file cleanup without a closed handle

Store identity from the exclusive handle before the path check. Rollback closes that handle only after the identity is stored. A later rollback checks the path. A handle with no identity yet stays open.

* fix(resources): retain cleanup for a refused existing output directory

A withheld copy can find a caller file after its directory was planned empty. The refusal keeps the constructed tree so the writer can retry verification. Rollback preserves the root and leaves that file in place.

* fix(resources): report a refused existing directory as input

A file that appears in an accepted directory after planning belongs to the caller. create rethrows that refusal without attaching a tree. writeOutputTree reports an input error for the selected field. A tree this call created still rolls back through SafeOutputTreeCreationFailure.
2026-11-26 13:31:15 -05:00

600 lines
23 KiB
TypeScript

import { createHash } from "node:crypto";
import {
access,
chmod,
mkdir,
open,
readFile,
readdir,
readlink,
rename,
rm,
symlink,
writeFile,
} from "node:fs/promises";
import { join } from "node:path";
import { Readable } from "node:stream";
import { describe, expect, it, vi } from "vitest";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { SafeOutputTree } from "../../../src/resources/SafeOutputTree.js";
import { SafeOutputTreeDestinationRefusal } from "../../../src/resources/SafeOutputTreeAdmission.js";
import { SafeOutputTreeCreationFailure } from "../../../src/resources/SafeOutputTreeCreationFailure.js";
import { ArtifactReaderFailure } from "../../../src/resources/ArtifactReader.js";
describe("safe artifact output cleanup", () => {
it("adopts an empty caller directory without taking authority to remove it", async () => {
const parent = await createTestTempDirectory("rea-admitted-output-");
const output = join(parent, "output");
await mkdir(output);
const tree = await SafeOutputTree.create(output, process.platform, {
acceptExistingEmpty: true,
});
await tree.write("file", Readable.from([Buffer.from("owned")]), {
bytes: 5,
sha256: createHash("sha256").update("owned").digest("hex"),
});
await tree.rollback();
expect(await readdir(output)).toEqual([]);
await tree.closeHandles();
});
it("keeps cleanup incomplete when the owned root disappears before commit", async () => {
const parent = await createTestTempDirectory("rea-safe-output-");
const output = join(parent, "published");
const tree = await SafeOutputTree.create(output);
await rm(output, { recursive: true });
await expect(tree.commit()).rejects.toMatchObject({ code: "ENOENT" });
await expect(tree.rollback()).rejects.toMatchObject({
reason: "integrity",
integrityCheck: "output-lifecycle",
cause: { code: "ENOENT" },
cleanup: { resources: [output] },
});
expect(tree.cleanup).toEqual({
status: "incomplete",
residualPaths: ["published"],
});
});
it("retries pending-file cleanup after the exclusive handle can be identified", async () => {
const parent = await createTestTempDirectory("rea-safe-output-pending-");
const output = join(parent, "published");
const destination = join(output, "payload.txt");
const prototypeHandle = await open(join(parent, "handle"), "w");
const handlePrototype = Object.getPrototypeOf(prototypeHandle) as Pick<
typeof prototypeHandle,
"stat"
>;
await prototypeHandle.close();
await rm(join(parent, "handle"));
const tree = await SafeOutputTree.create(output);
let pendingHandle: { fd: number } | undefined;
const stat = vi.spyOn(handlePrototype, "stat");
stat.mockImplementation(function (this: { fd: number }) {
pendingHandle = this;
return Promise.reject(new Error("injected file identity failure"));
});
try {
await expect(
tree.write("payload.txt", Readable.from(Buffer.from("owned")), {
sha256: createHash("sha256").update("owned").digest("hex"),
bytes: Buffer.byteLength("owned"),
}),
).rejects.toThrow("injected file identity failure");
expect(pendingHandle?.fd).toBeGreaterThanOrEqual(0);
const failure = await tree.rollback().catch((cause: unknown) => cause);
expect(failure).toMatchObject({
message: "injected file identity failure",
cleanup: { resources: [output] },
});
expect(pendingHandle?.fd).toBeGreaterThanOrEqual(0);
expect(await readFile(destination, "utf8")).toBe("");
expect(tree.cleanup).toEqual({
status: "incomplete",
residualPaths: ["published"],
});
stat.mockRestore();
expect(await tree.rollback()).toEqual({
status: "complete",
residualPaths: [],
});
expect(pendingHandle?.fd).toBe(-1);
await expect(readFile(destination, "utf8")).rejects.toMatchObject({
code: "ENOENT",
});
} finally {
stat.mockRestore();
}
});
it("returns a non-empty accepted directory without wrapping or removing it", async () => {
const parent = await createTestTempDirectory("rea-safe-output-nonempty-");
const output = join(parent, "output");
await mkdir(output);
await writeFile(join(output, "caller.txt"), "keep");
const failure = await SafeOutputTree.create(output, process.platform, {
acceptExistingEmpty: true,
}).catch((cause: unknown) => cause);
expect(failure).toBeInstanceOf(SafeOutputTreeDestinationRefusal);
expect(failure).not.toBeInstanceOf(SafeOutputTreeCreationFailure);
expect(failure).toMatchObject({
message: "Output directory is not empty",
});
expect(await readFile(join(output, "caller.txt"), "utf8")).toBe("keep");
});
});
describe("safe artifact output file identity admission", () => {
it.skipIf(process.platform === "win32")(
"closes a captured pending identity and retries the path without that handle",
async () => {
const parent = await createTestTempDirectory(
"rea-safe-output-pending-path-",
);
const output = join(parent, "published");
const destination = join(output, "payload.txt");
const displaced = join(parent, "owned-payload.txt");
const prototypeHandle = await open(join(parent, "handle"), "w");
const handlePrototype = Object.getPrototypeOf(prototypeHandle) as Pick<
typeof prototypeHandle,
"stat"
>;
await prototypeHandle.close();
await rm(join(parent, "handle"));
const tree = await SafeOutputTree.create(output);
const originalStat = handlePrototype.stat;
let pendingHandle: { fd: number } | undefined;
const remember = (handle: { fd: number }): void => {
pendingHandle = handle;
};
const stat = vi.spyOn(handlePrototype, "stat");
stat.mockImplementationOnce(function (this: { fd: number }) {
remember(this);
return Promise.reject(new Error("injected file identity failure"));
});
await expect(
tree.write("payload.txt", Readable.from(Buffer.from("owned")), {
sha256: createHash("sha256").update("owned").digest("hex"),
bytes: Buffer.byteLength("owned"),
}),
).rejects.toThrow("injected file identity failure");
stat.mockImplementationOnce(async function (
this: typeof prototypeHandle,
...args
) {
remember(this);
const metadata = await originalStat.apply(this, args);
await rename(destination, displaced);
await writeFile(destination, "replacement");
return metadata;
});
try {
const failure = await tree.rollback().catch((cause: unknown) => cause);
expect(failure).toBeInstanceOf(ArtifactReaderFailure);
expect(failure).toMatchObject({
message: `Extraction file identity changed: ${destination}`,
cleanup: { resources: [output] },
});
expect(pendingHandle?.fd).toBe(-1);
expect(await readFile(destination, "utf8")).toBe("replacement");
expect(await readFile(displaced, "utf8")).toBe("");
await rm(destination);
await rename(displaced, destination);
expect(await tree.rollback()).toEqual({
status: "complete",
residualPaths: [],
});
expect(await readdir(parent)).toEqual([]);
} finally {
stat.mockRestore();
}
},
);
it.skipIf(process.platform === "win32")(
"retains an O_EXCL file handle until its identity can be retried",
async () => {
const parent = await createTestTempDirectory("rea-safe-output-admit-");
const output = join(parent, "published");
const destination = join(output, "payload.txt");
const displaced = join(parent, "owned-payload.txt");
const outside = join(parent, "outside.txt");
await writeFile(outside, "outside bytes");
const prototypeHandle = await open(join(parent, "handle"), "w");
const handlePrototype = Object.getPrototypeOf(prototypeHandle) as Pick<
typeof prototypeHandle,
"stat"
>;
await prototypeHandle.close();
await rm(join(parent, "handle"));
const tree = await SafeOutputTree.create(output);
const originalStat = handlePrototype.stat;
const stat = vi.spyOn(handlePrototype, "stat");
stat
.mockImplementationOnce(function (
this: typeof prototypeHandle,
...args
) {
return originalStat.apply(this, args);
})
.mockRejectedValueOnce(new Error("injected file identity failure"));
await expect(
tree.write("payload.txt", Readable.from(Buffer.from("owned")), {
sha256: createHash("sha256").update("owned").digest("hex"),
bytes: Buffer.byteLength("owned"),
}),
).rejects.toThrow("injected file identity failure");
stat.mockRestore();
await rename(destination, displaced);
await symlink(outside, destination, "file");
const failure = await tree.rollback().catch((cause: unknown) => cause);
expect(failure).toBeInstanceOf(ArtifactReaderFailure);
expect(await readlink(destination)).toBe(outside);
expect(await readFile(outside, "utf8")).toBe("outside bytes");
await rm(destination);
await rename(displaced, destination);
expect(await tree.rollback()).toEqual({
status: "complete",
residualPaths: [],
});
expect(await readdir(parent)).toEqual(["outside.txt"]);
},
);
});
describe("safe artifact output setup ownership", () => {
it.skipIf(process.platform === "win32")(
"returns its exact tree owner when setup fails after root identity capture",
async () => {
const parent = await createTestTempDirectory("rea-safe-output-setup-");
const output = join(parent, "published");
const blocker = join(output, "unowned.txt");
const prototypeHandle = await open(join(parent, "handle"), "w");
const handlePrototype = Object.getPrototypeOf(prototypeHandle) as Pick<
typeof prototypeHandle,
"chmod"
>;
await prototypeHandle.close();
await rm(join(parent, "handle"));
const chmodSpy = vi
.spyOn(handlePrototype, "chmod")
.mockImplementationOnce(async function (this: typeof prototypeHandle) {
await writeFile(blocker, "leave until cleanup retry");
throw new Error("injected setup failure");
});
const failure = await SafeOutputTree.create(output).catch(
(cause: unknown) => cause,
);
chmodSpy.mockRestore();
expect(failure).toBeInstanceOf(SafeOutputTreeCreationFailure);
if (!(failure instanceof SafeOutputTreeCreationFailure))
throw new Error("expected setup failure to retain its tree owner");
expect(failure.cause).toMatchObject({
message: "injected setup failure",
});
expect(failure.tree.outputRoot).toBe(output);
const cleanupFailure = await failure.tree
.rollback()
.catch((cause: unknown) => cause);
expect(cleanupFailure).toBeInstanceOf(ArtifactReaderFailure);
expect(await readFile(blocker, "utf8")).toBe("leave until cleanup retry");
await rm(blocker);
expect(await failure.tree.rollback()).toEqual({
status: "complete",
residualPaths: [],
});
expect(await readdir(parent)).toEqual([]);
},
);
});
describe("safe artifact output tree identity", () => {
it.skipIf(process.platform === "win32")(
"refuses a replaced root and retries cleanup when the owned root is restored",
async () => {
const parent = await createTestTempDirectory("rea-safe-output-root-");
const output = join(parent, "published");
const displaced = join(parent, "owned-original");
const outside = join(parent, "outside");
await mkdir(outside);
const tree = await SafeOutputTree.create(output);
await rename(output, displaced);
await symlink(outside, output, "dir");
const writeFailure = await tree
.write("escaped.txt", Readable.from(Buffer.from("outside")), {
sha256: createHash("sha256").update("outside").digest("hex"),
bytes: Buffer.byteLength("outside"),
})
.catch((cause: unknown) => cause);
expect(writeFailure).toBeInstanceOf(ArtifactReaderFailure);
await expect(access(join(outside, "escaped.txt"))).rejects.toThrow();
await expect(tree.commit()).rejects.toBeInstanceOf(ArtifactReaderFailure);
const cleanupFailure = await tree
.rollback()
.catch((cause: unknown) => cause);
expect(cleanupFailure).toBeInstanceOf(ArtifactReaderFailure);
expect(cleanupFailure).toMatchObject({
cleanup: { resources: [output] },
});
expect(await readlink(output)).toBe(outside);
await rm(output);
await rename(displaced, output);
expect(await tree.rollback()).toEqual({
status: "complete",
residualPaths: [],
});
expect(await readdir(parent)).toEqual(["outside"]);
},
);
it.skipIf(process.platform === "win32")(
"refuses a replaced nested parent without touching it and retries after restoration",
async () => {
const parent = await createTestTempDirectory("rea-safe-output-parent-");
const output = join(parent, "published");
const nested = join(output, "nested");
const displaced = join(parent, "owned-nested");
const outside = join(parent, "outside");
await mkdir(outside);
await writeFile(join(outside, "sentinel.txt"), "keep");
const tree = await SafeOutputTree.create(output);
const bytes = Buffer.from("owned");
await tree.write("nested/owned.txt", Readable.from(bytes), {
sha256: createHash("sha256").update(bytes).digest("hex"),
bytes: bytes.byteLength,
});
await rename(nested, displaced);
await symlink(outside, nested, "dir");
const writeFailure = await tree
.write("nested/escaped.txt", Readable.from(Buffer.from("outside")), {
sha256: createHash("sha256").update("outside").digest("hex"),
bytes: Buffer.byteLength("outside"),
})
.catch((cause: unknown) => cause);
expect(writeFailure).toBeInstanceOf(ArtifactReaderFailure);
await expect(access(join(outside, "escaped.txt"))).rejects.toThrow();
expect(await readFile(join(outside, "sentinel.txt"), "utf8")).toBe(
"keep",
);
const cleanupFailure = await tree
.rollback()
.catch((cause: unknown) => cause);
expect(cleanupFailure).toBeInstanceOf(ArtifactReaderFailure);
expect(await readlink(nested)).toBe(outside);
expect(await readFile(join(outside, "sentinel.txt"), "utf8")).toBe(
"keep",
);
await rm(nested);
await rename(displaced, nested);
expect(await tree.rollback()).toEqual({
status: "complete",
residualPaths: [],
});
expect(await readdir(parent)).toEqual(["outside"]);
},
);
it.skipIf(process.platform === "win32")(
"preserves an outside file when a tracked output file is replaced by a symlink",
async () => {
const parent = await createTestTempDirectory("rea-safe-output-file-");
const output = join(parent, "published");
const tracked = join(output, "nested", "file.txt");
const displaced = join(parent, "owned-file.txt");
const outside = join(parent, "outside.txt");
await writeFile(outside, "outside bytes");
const tree = await SafeOutputTree.create(output);
const bytes = Buffer.from("owned bytes");
await tree.write("nested/file.txt", Readable.from(bytes), {
sha256: createHash("sha256").update(bytes).digest("hex"),
bytes: bytes.byteLength,
});
await rename(tracked, displaced);
await symlink(outside, tracked, "file");
const failure = await tree.rollback().catch((cause: unknown) => cause);
expect(failure).toBeInstanceOf(ArtifactReaderFailure);
expect(tree.cleanup).toMatchObject({ status: "incomplete" });
expect(await readlink(tracked)).toBe(outside);
expect(await readFile(outside, "utf8")).toBe("outside bytes");
await rm(tracked);
await rename(displaced, tracked);
expect(await tree.rollback()).toEqual({
status: "complete",
residualPaths: [],
});
expect(await readFile(outside, "utf8")).toBe("outside bytes");
},
);
});
it("retains an untracked regular entry and retries after it is removed", async () => {
const parent = await createTestTempDirectory("rea-safe-output-untracked-");
const output = join(parent, "published");
const untracked = join(output, "untracked.txt");
const tree = await SafeOutputTree.create(output);
const bytes = Buffer.from("owned bytes");
await tree.write("owned.txt", Readable.from(bytes), {
sha256: createHash("sha256").update(bytes).digest("hex"),
bytes: bytes.byteLength,
});
await writeFile(untracked, "leave untouched");
const failure = await tree.rollback().catch((cause: unknown) => cause);
expect(failure).toBeInstanceOf(ArtifactReaderFailure);
expect(tree.cleanup).toMatchObject({ status: "incomplete" });
expect(await readdir(output)).toEqual(["untracked.txt"]);
expect(await readFile(untracked, "utf8")).toBe("leave untouched");
await rm(untracked);
expect(await tree.rollback()).toEqual({
status: "complete",
residualPaths: [],
});
expect(await readdir(parent)).toEqual([]);
});
describe("safe artifact output tree", () => {
it("removes only its owned tree after digest failure and proves absence", async () => {
const parent = await createTestTempDirectory("rea-safe-output-");
const output = join(parent, "published");
const tree = await SafeOutputTree.create(output);
await expect(
tree.write("nested/file.txt", Readable.from(Buffer.from("unexpected")), {
sha256: "0".repeat(64),
bytes: Buffer.byteLength("unexpected"),
}),
).rejects.toThrow(/disagrees/u);
expect(await tree.rollback()).toMatchObject({
status: "complete",
residualPaths: [],
});
await expect(access(output)).rejects.toThrow();
expect(await readdir(parent)).toEqual([]);
});
it.skipIf(process.platform === "win32" || process.getuid?.() === 0)(
"reports the owned residual root when rollback lacks directory permissions",
async () => {
const parent = await createTestTempDirectory("rea-safe-output-rollback-");
const output = join(parent, "published");
const tree = await SafeOutputTree.create(output);
const bytes = Buffer.from("retained until rollback can retry");
await tree.write("nested/file.txt", Readable.from(bytes), {
sha256: createHash("sha256").update(bytes).digest("hex"),
bytes: bytes.byteLength,
});
try {
await chmod(output, 0o500);
const failure = await tree.rollback().catch((cause: unknown) => cause);
expect(failure).toBeInstanceOf(ArtifactReaderFailure);
expect(failure).toMatchObject({
cause: { code: "EACCES" },
cleanup: { resources: [output] },
});
} finally {
await chmod(output, 0o700).catch(() => undefined);
await tree.rollback();
}
},
);
it.skipIf(process.platform === "win32" || process.getuid?.() === 0)(
"retains cleanup progress when one owned sibling is not writable",
async () => {
const parent = await createTestTempDirectory("rea-safe-output-siblings-");
const output = join(parent, "published");
const completed = join(output, "completed");
const blocked = join(output, "blocked");
const tree = await SafeOutputTree.create(output);
for (const directory of ["completed", "blocked"]) {
const bytes = Buffer.from(join(output, directory));
await tree.write(`${directory}/file.txt`, Readable.from(bytes), {
sha256: createHash("sha256").update(bytes).digest("hex"),
bytes: bytes.byteLength,
});
}
try {
await chmod(blocked, 0o500);
const failure = await tree.rollback().catch((cause: unknown) => cause);
expect(failure).toBeInstanceOf(ArtifactReaderFailure);
expect(failure).toMatchObject({
cause: { code: "EACCES" },
cleanup: { resources: [output] },
});
expect(await readdir(output)).toEqual(["blocked"]);
await expect(access(completed)).rejects.toThrow();
expect(await readFile(join(blocked, "file.txt"), "utf8")).toBe(blocked);
await expect(
tree.write("new/file.txt", Readable.from(Buffer.from("late")), {
sha256: createHash("sha256").update("late").digest("hex"),
bytes: Buffer.byteLength("late"),
}),
).rejects.toBeInstanceOf(ArtifactReaderFailure);
await expect(tree.commit()).rejects.toBeInstanceOf(
ArtifactReaderFailure,
);
} finally {
await chmod(blocked, 0o700);
}
expect(await tree.rollback()).toEqual({
status: "complete",
residualPaths: [],
});
expect(await readdir(parent)).toEqual([]);
},
);
it("returns detached cleanup reports", async () => {
const parent = await createTestTempDirectory("rea-safe-output-");
const tree = await SafeOutputTree.create(join(parent, "published"));
const cleanup = await tree.rollback();
if (cleanup.status !== "complete") throw new Error("expected cleanup");
(cleanup.residualPaths as unknown as string[]).push("/forged/path");
expect(tree.cleanup).toEqual({ status: "complete", residualPaths: [] });
expect(await tree.rollback()).toEqual({
status: "complete",
residualPaths: [],
});
await expect(
tree.write("late.txt", Readable.from(Buffer.from("late")), {
sha256: createHash("sha256").update("late").digest("hex"),
bytes: Buffer.byteLength("late"),
}),
).rejects.toBeInstanceOf(ArtifactReaderFailure);
await expect(tree.commit()).rejects.toBeInstanceOf(ArtifactReaderFailure);
});
});
it.each([
{ path: "hello.txt:hidden", kind: "stream syntax" },
{ path: "nested/hello.txt::$DATA", kind: "stream syntax" },
{ path: "folder:stream/file.txt", kind: "stream syntax" },
{ path: "Folder/Icon\r", kind: "control character" },
{ path: "Fold\ter/file.txt", kind: "control character" },
])(
"refuses Windows $kind before materializing $path",
async ({ path, kind }) => {
const parent = await createTestTempDirectory("rea-safe-output-name-");
const output = join(parent, "published");
const tree = await SafeOutputTree.create(output, "win32");
const bytes = Buffer.from("regular file content");
const source = Readable.from(bytes);
const message =
kind === "stream syntax"
? `Windows destination cannot materialize artifact path as a regular file: ${path}; ':' denotes alternate data stream syntax.`
: `Windows destination cannot materialize artifact path: ${JSON.stringify(path)}; Windows names cannot contain U+0001-U+001F.`;
try {
await expect(
tree.write(path, source, {
sha256: createHash("sha256").update(bytes).digest("hex"),
bytes: bytes.byteLength,
}),
).rejects.toMatchObject({ reason: "path", message });
expect(source.destroyed).toBe(true);
expect(await readdir(output)).toEqual([]);
} finally {
expect(await tree.rollback()).toEqual({
status: "complete",
residualPaths: [],
});
}
expect(await readdir(parent)).toEqual([]);
},
);