fix(artifacts): keep integrity policy and extraction claims aligned (#1083)
* fix(artifacts): keep integrity policy and extraction claims aligned rea analyze routed a JavaScript application without the integrity policy, so the remediation flag it recommends was rejected on that command. The routed command now accepts --integrity-policy and forwards it. Extraction copied every inventory contradiction into the result and said those files were written. Only materialized occurrences stay in that list; nested contradictions are named as unwritten. * test(artifacts): type the nested extraction fixture as a directory path --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com>
This commit is contained in:
@@ -193,6 +193,15 @@ const createExtractionResult = (
|
||||
selected: readonly SelectedOccurrence[],
|
||||
extracted: readonly ExtractedOccurrence[],
|
||||
): ArtifactExtractionResult => {
|
||||
const materializedIds = new Set(
|
||||
selected.map(({ occurrence }) => occurrence.occurrence_id),
|
||||
);
|
||||
const writtenContradictions = inventory.integrityContradictions.filter(
|
||||
({ occurrence_id }) => materializedIds.has(occurrence_id),
|
||||
);
|
||||
const nestedContradictions = inventory.integrityContradictions.filter(
|
||||
({ occurrence_id }) => !materializedIds.has(occurrence_id),
|
||||
);
|
||||
const extractionSemantic = {
|
||||
source_manifest_id: inventory.manifest.manifest_id,
|
||||
selected_occurrence_ids: selected
|
||||
@@ -212,13 +221,18 @@ const createExtractionResult = (
|
||||
containment_verified: true,
|
||||
cleanup: { attempted: false, verified: true, residual_paths: [] },
|
||||
provenance: [],
|
||||
integrity_contradictions: inventory.integrityContradictions,
|
||||
integrity_contradictions: writtenContradictions,
|
||||
limitations: [
|
||||
"All regular files in the active artifact were materialized; nested archive contents remain represented by their containing file.",
|
||||
...(inventory.integrityContradictions.length === 0
|
||||
...(writtenContradictions.length === 0
|
||||
? []
|
||||
: [
|
||||
`${String(inventory.integrityContradictions.length)} extracted file(s) contradict declared integrity; their observed bytes were written and are untrusted.`,
|
||||
`${String(writtenContradictions.length)} extracted file(s) contradict declared integrity; their observed bytes were written and are untrusted.`,
|
||||
]),
|
||||
...(nestedContradictions.length === 0
|
||||
? []
|
||||
: [
|
||||
`${String(nestedContradictions.length)} nested integrity contradiction(s) were not written as their own files (${nestedContradictions.map(({ logical_path }) => logical_path).join(", ")}); the containing archive was materialized instead.`,
|
||||
]),
|
||||
],
|
||||
});
|
||||
|
||||
@@ -42,7 +42,17 @@ const registerOverviewCommands = (cli: CliInstance, logger: Logger): void => {
|
||||
args: z.object({
|
||||
path: z.string().describe("App, program, or analysis database path"),
|
||||
}),
|
||||
options: overviewOptions,
|
||||
options: overviewOptions.extend({
|
||||
integrityPolicy: z
|
||||
.enum(["fail", "record-and-continue"])
|
||||
.default("fail")
|
||||
.describe(
|
||||
"Behavior when a routed JavaScript application declares integrity that does not match",
|
||||
),
|
||||
}),
|
||||
alias: {
|
||||
integrityPolicy: "integrity-policy",
|
||||
},
|
||||
run: ({ args, options }) =>
|
||||
logCliCommand(logger, "analyze", () =>
|
||||
runRoutedOverview(args.path, options, logger),
|
||||
@@ -107,6 +117,7 @@ const runRoutedOverview = async (
|
||||
readonly snapshot?: string | undefined;
|
||||
readonly "target-format"?: "dos-com" | undefined;
|
||||
readonly provider?: string | undefined;
|
||||
readonly integrityPolicy?: "fail" | "record-and-continue" | undefined;
|
||||
},
|
||||
logger: Logger,
|
||||
) => {
|
||||
@@ -118,6 +129,7 @@ const runRoutedOverview = async (
|
||||
)
|
||||
return runCliJavaScriptApplicationAnalysis({
|
||||
input_path: resolve(path),
|
||||
integrity_policy: options.integrityPolicy ?? "fail",
|
||||
});
|
||||
return runDirectAnalysis(
|
||||
path,
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { createPackage } from "@electron/asar";
|
||||
import { expect, it } from "vitest";
|
||||
|
||||
import { extractArtifact } from "../../../src/application/ArtifactExtraction.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
it("does not claim unmaterialized nested integrity contradictions were extracted", async () => {
|
||||
const root = await createTestTempDirectory("rea-nested-integrity-extract-");
|
||||
const source = join(root, "source");
|
||||
const original = "console.log('ok');\n";
|
||||
const changed = "console.log('no');\n";
|
||||
await mkdir(source);
|
||||
await writeFile(join(source, "main.js"), original);
|
||||
const app = join(root, "app");
|
||||
await mkdir(app);
|
||||
const archive = join(app, "app.asar");
|
||||
await createPackage(source, archive);
|
||||
const bytes = await readFile(archive);
|
||||
const contentOffset = bytes.indexOf(original);
|
||||
expect(contentOffset).toBeGreaterThanOrEqual(0);
|
||||
bytes.write(changed, contentOffset, "utf8");
|
||||
await writeFile(archive, bytes);
|
||||
|
||||
const result = await extractArtifact({
|
||||
inputPath: app,
|
||||
// A directory path is read as a directory before this format is consulted.
|
||||
inputFormat: "zip",
|
||||
outputRoot: join(root, "out"),
|
||||
integrity: { mode: "record-and-continue" },
|
||||
});
|
||||
|
||||
expect(result.artifacts.map(({ relative_path }) => relative_path)).toEqual([
|
||||
"app.asar",
|
||||
]);
|
||||
expect(result.integrity_contradictions).toEqual([]);
|
||||
expect(result.limitations.join("\n")).toContain(
|
||||
"were not written as their own files",
|
||||
);
|
||||
expect(result.limitations.join("\n")).toContain("main.js");
|
||||
expect(result.limitations.join("\n")).not.toContain(
|
||||
"extracted file(s) contradict",
|
||||
);
|
||||
});
|
||||
Reference in New Issue
Block a user