Files
reverse-engineering-anything/tests/boundary/cli/webScriptSelectionCli.test.ts
T
morluto 080635e7eb fix(artifacts)!: refuse an unusable extract_artifact output_root first, as caller input (#15387)
* fix(artifacts): refuse an unusable extract_artifact output_root before scanning

extract_artifact checked output_root only when it created the output tree,
after scanning the whole archive and preparing its reader. A destination
that already existed was refused there, and the refusal carried the
archive's complete inventory as its partial observation: 1.7 MB of error
text for a 2,000-entry ZIP.

The read-only SafeOutputTree.checkDestination admission, which firmware
and environment-bundle extraction already use, now runs before the archive
is read. SafeOutputTree.create still rechecks and acquires the destination
exclusively, so a destination that changes after the early check is still
refused there.

Refs #15326

* fix(artifacts)!: report a refused extract_artifact output_root as caller input

A destination refusal on extract_artifact is now an input error on
output_root, as firmware, UEFI, script and bundle export report theirs.
Every destination refusal conforms to the input contract and names the
fix instead of pointing at the schema.

BREAKING CHANGE: extract_artifact reports a refused output_root as
invalid_request with an issue at output_root (CLI: outputRoot) instead of
artifact_operation_failed with reason path. Destination refusals of
extract_firmware, UEFI extraction, export_web_scripts,
export_environment_bundle, environment-export/build and desktop build
exports no longer carry recovery.kind inspect_input_contract; read
remediation.action and details.issues instead.

Refs #15326

* test(artifacts): expect the caller-input refusal for a refused extract_artifact output_root

The provider extraction boundary tests still asserted ArtifactOperationError reason path, the shape this change replaces; they now expect AnalysisInputError with an issue at output_root.

* test(artifacts): prove extract_artifact refuses the destination before reading the source
2026-12-01 02:23:38 +00:00

214 lines
7.0 KiB
TypeScript

import { createCommandCli } from "../../support/cli/commandCli.js";
import { cliCommand } from "../../../src/cli/commandRegistry.js";
import { access, mkdir, readdir, symlink, writeFile } from "node:fs/promises";
import { join } from "node:path";
import pino from "pino";
import { expect, it, vi } from "vitest";
import { registerWebScriptCommands } from "../../../src/cli/webScriptCommands.js";
import * as webScriptExport from "../../../src/application/WebScriptExportService.js";
import { cliCommandFor } from "../../../src/contracts/cliCommandNames.js";
import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js";
import { err, ok } from "../../../src/domain/result.js";
import { ArtifactResourceScope } from "../../../src/resources/ArtifactResourceScope.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import {
scriptCaptureEvidenceFixture,
scriptScenarioFixture,
} from "../../fixtures/webScriptCapture.js";
import { cliTest } from "../../support/cli/cliFixture.js";
const registeredExportCommand = () => {
const cli = createCommandCli();
registerWebScriptCommands(cli, pino({ enabled: false }));
return cliCommand(cli, cliCommandFor("export_web_scripts"))!.definition;
};
it("forwards an interrupt into the export workflow and reports cancellation", async () => {
const command = registeredExportCommand();
const exported = vi
.spyOn(webScriptExport, "exportWebScripts")
.mockImplementation(async (_input, _resources, options) => {
process.emit("SIGINT");
return err(
new AnalysisCancelledError("export_web_scripts", {
cause: options?.signal?.reason,
}),
);
});
const previousExitCode = process.exitCode;
try {
const output = await command.run({
structuredOutput: false,
options: {},
args: {
capturePath: "/unused/capture.json",
outputDirectory: "/unused/out",
},
});
expect(exported.mock.calls[0]?.[2]?.signal?.aborted).toBe(true);
expect(output).toMatchObject({ code: "cancelled" });
expect(process.exitCode).toBe(130);
} finally {
exported.mockRestore();
process.exitCode = previousExitCode;
}
});
it.each(["cleanup failure", "cancellation during cleanup"] as const)(
"retains exported Evidence on %s",
async (outcome) => {
const command = registeredExportCommand();
const evidence = scriptCaptureEvidenceFixture();
const exported = vi
.spyOn(webScriptExport, "exportWebScripts")
.mockResolvedValue(ok(evidence));
const close = vi
.spyOn(ArtifactResourceScope.prototype, "close")
.mockImplementation(async () => {
if (outcome === "cleanup failure") throw new Error("close denied");
process.emit("SIGINT");
});
const previousExitCode = process.exitCode;
try {
const output = await command.run({
structuredOutput: false,
options: {},
args: {
capturePath: "/unused/capture.json",
outputDirectory: "/unused/out",
},
});
expect(close).toHaveBeenCalledOnce();
expect(output).toMatchObject({
error: "Script export failed",
code:
outcome === "cleanup failure" ? "cleanup_incomplete" : "cancelled",
details: { partial_observation: evidence },
});
} finally {
exported.mockRestore();
close.mockRestore();
process.exitCode = previousExitCode;
}
},
);
it("closes export resources when the operation throws", async () => {
const command = registeredExportCommand();
const exported = vi
.spyOn(webScriptExport, "exportWebScripts")
.mockRejectedValue(new Error("unexpected export failure"));
const close = vi
.spyOn(ArtifactResourceScope.prototype, "close")
.mockResolvedValue();
try {
const output = await command.run({
structuredOutput: false,
options: {},
args: {
capturePath: "/unused/capture.json",
outputDirectory: "/unused/out",
},
});
expect(close).toHaveBeenCalledOnce();
expect(output).toMatchObject({
error: "Script export failed",
details: { operation: "export_web_scripts" },
});
} finally {
exported.mockRestore();
close.mockRestore();
}
});
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]);
expect(created.exitCode).toBe(0);
const response = await cli.run({
arguments: ["export-web-scripts", capturePath, outputDirectory, "--json"],
environment: { REA_LOG_LEVEL: "silent" },
});
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: ["capturePath"],
reason: "invalid_value",
message: expect.stringContaining("regular file"),
},
],
},
});
expect(response.stdout).toContain(capturePath);
await expect(access(outputDirectory)).rejects.toMatchObject({
code: "ENOENT",
});
},
);
cliTest(
"refuses an existing output directory or link as invalid output_directory input",
async ({ cli }) => {
const root = await createTestTempDirectory("rea-script-export-existing-");
const capturePath = join(root, "capture.json");
await writeFile(capturePath, JSON.stringify(scriptScenarioFixture()));
const retained = join(root, "retained");
await mkdir(retained);
await writeFile(join(retained, "marker.txt"), "keep");
const link = join(root, "output-link");
await symlink(
retained,
link,
process.platform === "win32" ? "junction" : "dir",
);
for (const outputDirectory of [retained, link]) {
const response = await cli.run({
arguments: [
"export-web-scripts",
capturePath,
outputDirectory,
"--json",
],
environment: { REA_LOG_LEVEL: "silent" },
});
expect(response.exitCode).toBe(1);
expect(response.json).toMatchObject({
code: "invalid_request",
category: "invalid_input",
// A schema-valid path the filesystem refused: the fix is named,
// not the input schema.
remediation: {
action: expect.stringContaining(
"Choose a different destination path",
),
},
details: {
operation: "export_web_scripts",
issues: [
{
path: ["outputDirectory"],
reason: "invalid_value",
message: "Output root already exists",
},
],
},
});
}
expect(await readdir(retained)).toEqual(["marker.txt"]);
},
);