Files
reverse-engineering-anything/tests/boundary/filesystem/cliJsonInput.test.ts
T
morluto cbc0dd1e7c fix(contracts)!: require absolute local paths for remaining caller-supplied file inputs (#1004)
* fix(contracts)!: require absolute local paths for remaining caller-supplied file inputs

Follow-up to the session snapshot/export tightening: the same invisible server-cwd resolution applied to open_binary target paths (parseBinaryTarget(path, process.cwd())), import_evidence_bundle bundle reads, firmware input files (resolve(raw)), browser executable launch paths (resolve-then-spawn), Electron executable/application/root paths (realpath on raw input), and the expected server path check.

Each field gains the shared isAbsoluteLocalPath refinement plus a catalog-visible description, since JSON Schema cannot express the rule. Examples, scenario fixtures, and contract tests updated to absolute paths; new schema cases cover rejection/acceptance/omission per field.

Explicitly unchanged (verified rooted or out of scope): inspect_plist paths (resolved against the target bundle dir), firmware output_directory (already provider-enforced), filesystem observation paths (resolved against the caller-supplied working_directory), keyed-archive member paths (archive-relative), JS evidence member paths (evidence-relative under root_alias), provider-internal execution schemas, CLI-only doctor flows, and CLI options (own resolve against the operator cwd).

* fix(cli): resolve operator-relative paths before shared absolute-path validation

The contract tightening rejects relative paths, but three CLI commands forward operator-relative input into the shared schemas: firmware commands pass raw args to FirmwareAnalysisService, and the Electron/browser scenario commands validate parsed scenario JSON directly. CLI paths are relative to the operator shell, so resolve them against the working directory first: resolve() in firmwareCommands, and a shared resolveCliJsonPaths JSON key-path helper (cliJsonInput) applied in electronCommands and cliBrowserScenarioCommands. MCP behavior is unchanged — MCP callers must still send absolute paths.

Covers the regression with a relative-path firmware CLI acceptance test (asserts provider-stage capability_unavailable, not a validation error) and unit tests for the helper.
2026-10-08 00:52:37 +08:00

95 lines
3.2 KiB
TypeScript

import { writeFile } from "node:fs/promises";
import { isAbsolute, join, resolve } from "node:path";
import { describe, expect, it } from "vitest";
import {
parseCliJsonInput,
resolveCliJsonPaths,
} from "../../../src/cliJsonInput.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
describe("CLI JSON input", () => {
it("preserves JSON scalars and distinguishes malformed inline text from files", async () => {
for (const value of [null, false, 0, "text", [], {}])
expect(
await parseCliJsonInput(JSON.stringify(value), "test-input"),
).toEqual({ ok: true, value });
expect(await parseCliJsonInput("{", "test-input")).toMatchObject({
ok: false,
error: {
details: { issues: [{ reason: "invalid_format", expected: "JSON" }] },
},
});
expect(await parseCliJsonInput("[", "test-input")).toMatchObject({
ok: false,
error: {
details: { issues: [{ reason: "invalid_format", expected: "JSON" }] },
},
});
const root = await createTestTempDirectory("rea-json-input-");
const path = join(root, "input.json");
await writeFile(path, '{"value":1}');
expect(await parseCliJsonInput(path, "test-input")).toEqual({
ok: true,
value: { value: 1 },
});
const bracketPath = join(root, "[input].json");
await writeFile(bracketPath, '["preserved"]');
expect(await parseCliJsonInput(bracketPath, "test-input")).toEqual({
ok: true,
value: ["preserved"],
});
for (const missingPath of [
join(root, "{capture}.json"),
join(root, "[missing].json"),
])
expect(await parseCliJsonInput(missingPath, "test-input")).toMatchObject({
ok: false,
error: { input_path: missingPath, input_reason: "read-failed" },
});
expect(await parseCliJsonInput(root, "test-input")).toMatchObject({
ok: false,
error: { input_reason: "read-failed" },
});
});
});
describe("resolveCliJsonPaths", () => {
it("resolves named relative fields against the operator working directory", () => {
expect(
resolveCliJsonPaths({ executable_path: "chrome" }, [["executable_path"]]),
).toEqual({ executable_path: resolve("chrome") });
expect(isAbsolute(resolve("chrome"))).toBe(true);
});
it("leaves absolute fields unchanged", () => {
const value = { path: "/tmp/evidence.json" };
expect(resolveCliJsonPaths(value, [["path"]])).toEqual(value);
});
it("resolves nested key paths and preserves sibling fields", () => {
expect(
resolveCliJsonPaths(
{
browser: { mode: "launch", executable_path: "chrome" },
actions: [],
},
[["browser", "executable_path"]],
),
).toEqual({
browser: { mode: "launch", executable_path: resolve("chrome") },
actions: [],
});
});
it("passes through non-objects, missing keys, and non-string fields", () => {
expect(resolveCliJsonPaths(null, [["path"]])).toBe(null);
expect(resolveCliJsonPaths("text", [["path"]])).toBe("text");
expect(resolveCliJsonPaths({ other: 1 }, [["path"]])).toEqual({
other: 1,
});
expect(resolveCliJsonPaths({ path: 42 }, [["path"]])).toEqual({ path: 42 });
});
});