diff --git a/src/application/ArtifactExtraction.ts b/src/application/ArtifactExtraction.ts index e1d6a823e..d33bbe90d 100644 --- a/src/application/ArtifactExtraction.ts +++ b/src/application/ArtifactExtraction.ts @@ -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.`, ]), ], }); diff --git a/src/cli/coreBinaryCommands.ts b/src/cli/coreBinaryCommands.ts index 6c70a2626..4ff3d8649 100644 --- a/src/cli/coreBinaryCommands.ts +++ b/src/cli/coreBinaryCommands.ts @@ -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, diff --git a/tests/boundary/filesystem/artifactExtractionNestedIntegrity.test.ts b/tests/boundary/filesystem/artifactExtractionNestedIntegrity.test.ts new file mode 100644 index 000000000..f4df985f6 --- /dev/null +++ b/tests/boundary/filesystem/artifactExtractionNestedIntegrity.test.ts @@ -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", + ); +});