fix(files): reject nonregular capture and JSON inputs
Verify selected files through a nonblocking opened handle before reading. Preserve regular-file symlinks and return actionable CLI/MCP diagnostics without publishing output. Co-authored-by: YouWish <29498139+Y0UW1SH@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
import { access } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { expect } from "vitest";
|
||||
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import { cliTest } from "../../support/cli/cliFixture.js";
|
||||
|
||||
cliTest.skipIf(process.platform === "win32")(
|
||||
"exits with a capture_path invalid_request for a named pipe before publishing output",
|
||||
async ({ cli, processes }) => {
|
||||
const root = await createTestTempDirectory("rea-script-selection-cli-");
|
||||
const capturePath = join(root, "capture.pipe");
|
||||
const outputDirectory = join(root, "export");
|
||||
const created = await processes.run("mkfifo", [capturePath], {
|
||||
timeoutMs: 5_000,
|
||||
});
|
||||
expect(created.exitCode).toBe(0);
|
||||
|
||||
const response = await cli.run({
|
||||
arguments: ["export-web-scripts", capturePath, outputDirectory, "--json"],
|
||||
environment: { REA_LOG_LEVEL: "silent" },
|
||||
timeoutMs: 5_000,
|
||||
});
|
||||
expect(response.exitCode).toBe(1);
|
||||
expect(response.signal).toBeNull();
|
||||
expect(response.json).toMatchObject({
|
||||
error: "Script export failed",
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
operation: "export_web_scripts",
|
||||
issues: [
|
||||
{
|
||||
path: ["capture_path"],
|
||||
reason: "invalid_value",
|
||||
message: expect.stringContaining("regular file"),
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
expect(response.stdout).toContain(capturePath);
|
||||
await expect(access(outputDirectory)).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
},
|
||||
);
|
||||
@@ -1,11 +1,89 @@
|
||||
import { chmod, writeFile } from "node:fs/promises";
|
||||
import { execFile } from "node:child_process";
|
||||
import { chmod, symlink, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import { describe, expect, it, onTestFinished } from "vitest";
|
||||
|
||||
import { parseCliJsonInput } from "../../../src/cliJsonInput.js";
|
||||
import { readWithoutFifoWriter } from "../../fixtures/fifoInput.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
describe("CLI JSON file selection", () => {
|
||||
it("accepts a symlink to a regular JSON file", async () => {
|
||||
const root = await createTestTempDirectory("rea-json-input-symlink-");
|
||||
const path = join(root, "input.json");
|
||||
const selected = join(root, "selected.json");
|
||||
await writeFile(path, '{"value":1}');
|
||||
await symlink(path, selected, "file");
|
||||
expect(await parseCliJsonInput(selected, "test-input")).toEqual({
|
||||
ok: true,
|
||||
value: { value: 1 },
|
||||
});
|
||||
});
|
||||
|
||||
it
|
||||
.skipIf(process.platform === "win32")
|
||||
.each(["a named pipe", "a symlink to a named pipe"])(
|
||||
"rejects %s without waiting for a writer",
|
||||
async (kind) => {
|
||||
const root = await createTestTempDirectory("rea-json-input-fifo-");
|
||||
const fifoPath = join(root, "input.pipe");
|
||||
await promisify(execFile)("mkfifo", [fifoPath]);
|
||||
const selected =
|
||||
kind === "a named pipe" ? fifoPath : join(root, "selected.json");
|
||||
if (selected !== fifoPath) await symlink(fifoPath, selected, "file");
|
||||
|
||||
const outcome = await readWithoutFifoWriter(fifoPath, () =>
|
||||
parseCliJsonInput(selected, "test-input"),
|
||||
);
|
||||
expect(outcome.state).toBe("completed");
|
||||
if (outcome.state !== "completed")
|
||||
throw new Error("JSON read waited for a FIFO writer");
|
||||
expect(outcome.result).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
code: "invalid_request",
|
||||
input_path: selected,
|
||||
input_reason: "read-failed",
|
||||
details: {
|
||||
issues: [
|
||||
{
|
||||
path: [],
|
||||
reason: "invalid_value",
|
||||
message: expect.stringContaining("(ENOTFILE)"),
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"rejects a character device before parsing JSON bytes",
|
||||
async () => {
|
||||
expect(await parseCliJsonInput("/dev/null", "test-input")).toMatchObject({
|
||||
ok: false,
|
||||
error: {
|
||||
code: "invalid_request",
|
||||
input_path: "/dev/null",
|
||||
input_reason: "read-failed",
|
||||
details: {
|
||||
issues: [
|
||||
{
|
||||
path: [],
|
||||
reason: "invalid_value",
|
||||
message: expect.stringContaining("(ENOTFILE)"),
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("CLI JSON input", () => {
|
||||
it("distinguishes malformed inline text from files and preserves bracket-prefixed paths", async () => {
|
||||
expect(await parseCliJsonInput("[", "test-input")).toMatchObject({
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import {
|
||||
access,
|
||||
@@ -9,6 +10,7 @@ import {
|
||||
writeFile,
|
||||
} from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import { describe, expect, it, onTestFinished } from "vitest";
|
||||
|
||||
@@ -19,6 +21,7 @@ import { selectScriptCapture } from "../../../src/browser/assets/ScriptCaptureAd
|
||||
import { javascriptApplicationAnalysisResultSchema } from "../../../src/domain/javascript/javascriptApplicationAnalysis.js";
|
||||
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
|
||||
import { webScriptExportResultSchema } from "../../../src/domain/webScriptExport.js";
|
||||
import { readWithoutFifoWriter } from "../../fixtures/fifoInput.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import {
|
||||
scriptCaptureEvidenceFixture,
|
||||
@@ -272,6 +275,128 @@ describe("captured script publication failures and cleanup", () => {
|
||||
});
|
||||
|
||||
describe("captured script selection failures", () => {
|
||||
it("accepts a capture symlink to a regular file and preserves its selected path", async () => {
|
||||
const { root, input } = await setup();
|
||||
const selected = join(root, "selected-capture.json");
|
||||
await symlink(input.capture_path, selected, "file");
|
||||
|
||||
const result = await exportWebScripts({ ...input, capture_path: selected });
|
||||
if (!result.ok) throw result.error;
|
||||
expect(result.value.subject?.local_path).toBe(selected);
|
||||
expect(
|
||||
webScriptExportResultSchema.parse(result.value.normalized_result)
|
||||
.capture_sha256,
|
||||
).toBe(
|
||||
createHash("sha256")
|
||||
.update(await readFile(input.capture_path))
|
||||
.digest("hex"),
|
||||
);
|
||||
await expect(
|
||||
access(join(input.output_directory, "manifest.json")),
|
||||
).resolves.toBeUndefined();
|
||||
});
|
||||
|
||||
it
|
||||
.skipIf(process.platform === "win32")
|
||||
.each(["a named pipe", "a symlink to a named pipe"])(
|
||||
"rejects %s without waiting for a writer or creating output",
|
||||
async (kind) => {
|
||||
const { root, input } = await setup();
|
||||
const fifoPath = join(root, "capture.pipe");
|
||||
await promisify(execFile)("mkfifo", [fifoPath]);
|
||||
const selected =
|
||||
kind === "a named pipe"
|
||||
? fifoPath
|
||||
: join(root, "selected-capture.json");
|
||||
if (selected !== fifoPath) await symlink(fifoPath, selected, "file");
|
||||
|
||||
const outcome = await readWithoutFifoWriter(fifoPath, () =>
|
||||
exportWebScripts({ ...input, capture_path: selected }),
|
||||
);
|
||||
expect(outcome.state).toBe("completed");
|
||||
if (outcome.state !== "completed")
|
||||
throw new Error("Capture read waited for a FIFO writer");
|
||||
if (outcome.result.ok)
|
||||
throw new Error("Expected invalid capture selection");
|
||||
expect(projectAnalysisError(outcome.result.error)).toMatchObject({
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
issues: [
|
||||
{
|
||||
path: ["capture_path"],
|
||||
reason: "invalid_value",
|
||||
message: expect.stringContaining("regular file"),
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
expect(
|
||||
JSON.stringify(projectAnalysisError(outcome.result.error)),
|
||||
).toContain(selected);
|
||||
await expect(access(input.output_directory)).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"rejects a character device as a capture selection before parsing bytes",
|
||||
async () => {
|
||||
const { input } = await setup();
|
||||
const result = await exportWebScripts({
|
||||
...input,
|
||||
capture_path: "/dev/null",
|
||||
});
|
||||
if (result.ok) throw new Error("Expected invalid capture selection");
|
||||
expect(projectAnalysisError(result.error)).toMatchObject({
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
issues: [
|
||||
{
|
||||
path: ["capture_path"],
|
||||
reason: "invalid_value",
|
||||
message: expect.stringContaining("regular file"),
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
expect(JSON.stringify(projectAnalysisError(result.error))).toContain(
|
||||
"/dev/null",
|
||||
);
|
||||
await expect(access(input.output_directory)).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"honors a pre-aborted capture export before opening a named pipe",
|
||||
async () => {
|
||||
const { root, input } = await setup();
|
||||
const fifoPath = join(root, "capture.pipe");
|
||||
await promisify(execFile)("mkfifo", [fifoPath]);
|
||||
const controller = new AbortController();
|
||||
controller.abort();
|
||||
|
||||
const outcome = await readWithoutFifoWriter(fifoPath, () =>
|
||||
exportWebScripts(
|
||||
{ ...input, capture_path: fifoPath },
|
||||
{ signal: controller.signal },
|
||||
),
|
||||
);
|
||||
expect(outcome.state).toBe("completed");
|
||||
if (outcome.state !== "completed")
|
||||
throw new Error("Aborted capture read waited for a FIFO writer");
|
||||
if (outcome.result.ok) throw new Error("Expected cancellation");
|
||||
expect(outcome.result.error._tag).toBe("AnalysisCancelledError");
|
||||
await expect(access(input.output_directory)).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("captured script path and permission failures", () => {
|
||||
it("reports a directory selected as the capture as invalid input", async () => {
|
||||
const { root, input } = await setup();
|
||||
const directory = join(root, "captures");
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { access } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import { Client, InMemoryTransport } from "@modelcontextprotocol/client";
|
||||
import { STDIO_DEFAULT_MAX_BUFFER_SIZE } from "@modelcontextprotocol/server";
|
||||
import { expect, it, onTestFinished } from "vitest";
|
||||
|
||||
import { silentLogger } from "../../../src/logger.js";
|
||||
import { EvidenceMcpServer } from "../../../src/server/EvidenceMcpServer.js";
|
||||
import { registerWebScriptTool } from "../../../src/server/registerWebScriptTool.js";
|
||||
import { ToolResultDelivery } from "../../../src/server/toolResult.js";
|
||||
import { readWithoutFifoWriter } from "../../fixtures/fifoInput.js";
|
||||
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"returns a capture_path invalid_request for a named pipe without waiting or publishing output",
|
||||
async () => {
|
||||
const root = await createTestTempDirectory("rea-script-selection-mcp-");
|
||||
const capturePath = join(root, "capture.pipe");
|
||||
const outputDirectory = join(root, "export");
|
||||
await promisify(execFile)("mkfifo", [capturePath]);
|
||||
|
||||
const server = new EvidenceMcpServer(
|
||||
{ name: "script-selection", version: "1" },
|
||||
{},
|
||||
undefined,
|
||||
new ToolResultDelivery(STDIO_DEFAULT_MAX_BUFFER_SIZE),
|
||||
);
|
||||
registerWebScriptTool(server, {
|
||||
logger: silentLogger,
|
||||
recordEvidence: undefined,
|
||||
});
|
||||
const client = new Client({ name: "script-selection-test", version: "1" });
|
||||
onTestFinished(async () => {
|
||||
await client.close();
|
||||
await server.close();
|
||||
});
|
||||
const [clientTransport, serverTransport] =
|
||||
InMemoryTransport.createLinkedPair();
|
||||
await server.connect(serverTransport);
|
||||
await client.connect(clientTransport);
|
||||
|
||||
const outcome = await readWithoutFifoWriter(capturePath, () =>
|
||||
client.callTool({
|
||||
name: "export_web_scripts",
|
||||
arguments: {
|
||||
capture_path: capturePath,
|
||||
output_directory: outputDirectory,
|
||||
},
|
||||
}),
|
||||
);
|
||||
expect(outcome.state).toBe("completed");
|
||||
if (outcome.state !== "completed")
|
||||
throw new Error("MCP capture selection waited for a FIFO writer");
|
||||
expect(parseMcpToolError(outcome.result)).toMatchObject({
|
||||
error: {
|
||||
code: "invalid_request",
|
||||
details: {
|
||||
operation: "export_web_scripts",
|
||||
issues: [
|
||||
{
|
||||
path: ["capture_path"],
|
||||
reason: "invalid_value",
|
||||
message: expect.stringContaining("regular file"),
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
});
|
||||
expect(JSON.stringify(outcome.result)).toContain(capturePath);
|
||||
await expect(access(outputDirectory)).rejects.toMatchObject({
|
||||
code: "ENOENT",
|
||||
});
|
||||
},
|
||||
);
|
||||
Reference in New Issue
Block a user