fix(filesystem): reject unstable artifact targets

This commit is contained in:
morluto
2026-10-10 04:14:54 +08:00
parent 66a20a2597
commit 7829b1aeef
11 changed files with 359 additions and 58 deletions
@@ -0,0 +1,42 @@
import { execFile } from "node:child_process";
import { lstat, rename, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";
import { expect, it } from "vitest";
import { inventoryArtifact } from "../../../src/artifacts/inventory/ArtifactInventory.js";
import { classifyAndHashRoot } from "../../../src/artifacts/inventory/classify.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
it.skipIf(process.platform === "win32")(
"rejects a FIFO artifact root without waiting for a writer",
async () => {
const root = await createTestTempDirectory("rea-artifact-root-fifo-");
const fifoPath = join(root, "input.asar");
await promisify(execFile)("mkfifo", [fifoPath]);
await expect(inventoryArtifact(fifoPath)).rejects.toMatchObject({
reason: "format",
message: `Artifact root is not a regular file: ${fifoPath}`,
});
},
);
it("rejects a root path replaced between its initial stat and open", async () => {
const root = await createTestTempDirectory("rea-artifact-root-replaced-");
const path = join(root, "input.bin");
const replacement = join(root, "replacement.bin");
await writeFile(path, "first bytes\n");
const expectedMetadata = await lstat(path);
await writeFile(replacement, "different bytes\n");
await rm(path);
await rename(replacement, path);
await expect(
classifyAndHashRoot(path, false, expectedMetadata),
).rejects.toMatchObject({
reason: "integrity",
message: `Root artifact changed before inventory: ${path}`,
});
});
@@ -24,6 +24,29 @@ import {
} from "../../../src/domain/binaryTarget.fixture.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { readWithoutFifoWriter } from "../../fixtures/fifoInput.js";
it.skipIf(process.platform === "win32")(
"rejects a selected FIFO without waiting for a writer",
async () => {
const root = await createTestTempDirectory("rea-binary-target-fifo-");
const fifoPath = join(root, "input.bin");
await promisify(execFile)("mkfifo", [fifoPath]);
const outcome = await readWithoutFifoWriter(fifoPath, () =>
parseBinaryTarget(fifoPath),
);
expect(outcome.state).toBe("completed");
if (outcome.state !== "completed")
throw new Error("Binary target resolution waited for a FIFO writer");
expect(outcome.result).toMatchObject({
ok: false,
error: {
_tag: "BinaryTargetError",
reason: "target is not a regular file",
},
});
},
);
describe("binary target I/O: app plist permissions and decoding", () => {
it.each([
+48
View File
@@ -0,0 +1,48 @@
import { execFile } from "node:child_process";
import { join } from "node:path";
import { promisify } from "node:util";
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
import { expect, it } from "vitest";
import { ArtifactProvider } from "../../../src/artifacts/ArtifactProvider.js";
import { createServer } from "../../../src/server/createServer.js";
import { createTestBinarySession } from "../../fixtures/binarySession.js";
import { readWithoutFifoWriter } from "../../fixtures/fifoInput.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
it.skipIf(process.platform === "win32")(
"rejects an open_binary FIFO without waiting for a writer",
async () => {
const root = await createTestTempDirectory("rea-open-binary-fifo-");
const fifoPath = join(root, "input.bin");
await promisify(execFile)("mkfifo", [fifoPath]);
const session = createTestBinarySession(new ArtifactProvider(process.env));
const server = createServer({ kind: "session", session });
const client = new Client({ name: "fifo-target-test", version: "1" });
const [clientTransport, serverTransport] =
InMemoryTransport.createLinkedPair();
try {
await server.connect(serverTransport);
await client.connect(clientTransport);
const outcome = await readWithoutFifoWriter(fifoPath, () =>
client.callTool({ name: "open_binary", arguments: { path: fifoPath } }),
);
expect(outcome.state).toBe("completed");
if (outcome.state !== "completed")
throw new Error("MCP open_binary waited for a FIFO writer");
expect(outcome.result.isError).toBe(true);
expect(parseMcpToolError(outcome.result)).toMatchObject({
error: { message: expect.stringContaining("not a regular file") },
});
} finally {
await Promise.allSettled([
client.close(),
server.close(),
session.close(),
]);
}
},
);