diff --git a/src/artifacts/extraction/ArtifactExtraction.ts b/src/artifacts/extraction/ArtifactExtraction.ts index ec47b2ac291..76ff46a85b1 100644 --- a/src/artifacts/extraction/ArtifactExtraction.ts +++ b/src/artifacts/extraction/ArtifactExtraction.ts @@ -310,6 +310,8 @@ interface ExtractedOccurrence { readonly sha256: string; readonly bytes_written: number; readonly created: true; + /** The selected occurrence's own limitations; not part of the file digest. */ + readonly limitations: readonly string[]; } const materializeSelection = async ({ @@ -517,6 +519,7 @@ const writeSelectedEntries = async ({ sha256: written.sha256, bytes_written: written.bytesWritten, created: true, + limitations: selectedItem.occurrence.limitations, }); selected.delete(occurrence.occurrence_id); } @@ -578,12 +581,16 @@ const createExtractionResult = ( ); const extractedContradictions = integrityContradictions.length - (rootContradicted ? 1 : 0); + const files = extracted.map(({ limitations: _limitations, ...file }) => file); + const limitedFiles = extracted.filter( + ({ limitations }) => limitations.length > 0, + ).length; const extractionSemantic = { source_manifest_id: inventory.manifest.manifest_id, selected_occurrence_ids: selected .map(({ occurrence }) => occurrence.occurrence_id) .sort(compareUnicodeCodePoints), - files_sha256: digestCanonicalValue(extracted, "Artifact"), + files_sha256: digestCanonicalValue(files, "Artifact"), output_root_alias: "$OUTPUT_ROOT" as const, }; return artifactExtractionResultSchema.parse({ @@ -593,7 +600,13 @@ const createExtractionResult = ( extraction_id: `aex_${digestCanonicalValue(extractionSemantic, "Artifact")}`, }, output_root: outputRoot, - artifacts: extracted, + // Each file keeps its occurrence's limitations, so the extraction's own + // list does not grow with the archive. + artifacts: extracted.map(({ limitations, ...file }) => + limitations.length === 0 + ? file + : { ...file, limitations: [...limitations] }, + ), containment_verified: true, cleanup: { attempted: false, verified: true, residual_paths: [] }, provenance: [...inventory.provenance, ...materializationProvenance], @@ -604,11 +617,11 @@ const createExtractionResult = ( integrity_contradictions: integrityContradictions, limitations: [ ...traversal.limitations, - ...selected.flatMap(({ occurrence }) => - occurrence.limitations.map( - (limitation) => `${occurrence.logical_path}: ${limitation}`, - ), - ), + ...(limitedFiles === 0 + ? [] + : [ + `${String(limitedFiles)} extracted file(s) carry their inventory occurrence's limitations in artifacts[].limitations.`, + ]), input.occurrenceIds !== undefined ? "Only caller-selected regular occurrences exposed by the active artifact reader were materialized; unselected entries and nested archive members were not extracted. Inventory integrity checks still cover the full input." : traversal.status === "complete" diff --git a/src/domain/artifactGraph.ts b/src/domain/artifactGraph.ts index 8d758bba374..35866050851 100644 --- a/src/domain/artifactGraph.ts +++ b/src/domain/artifactGraph.ts @@ -337,6 +337,13 @@ const extractedArtifactSchema = z.object({ sha256: sha256Schema, bytes_written: z.int().min(0).max(Number.MAX_SAFE_INTEGER), created: z.boolean(), + limitations: z + .array(z.string()) + .min(1) + .exactOptional() + .describe( + "The inventory occurrence's own limitations, such as a case-folding collision or a normalized member name; absent when it has none", + ), }); /** Path-independent commitment to one selected extraction operation. */ diff --git a/tests/acceptance/artifactExtractionLimitations.test.ts b/tests/acceptance/artifactExtractionLimitations.test.ts new file mode 100644 index 00000000000..30b34dae094 --- /dev/null +++ b/tests/acceptance/artifactExtractionLimitations.test.ts @@ -0,0 +1,56 @@ +import { writeFile } from "node:fs/promises"; +import { join } from "node:path"; + +import { TextReader, Uint8ArrayWriter, ZipWriter } from "@zip.js/zip.js"; +import { expect } from "vitest"; + +import { artifactExtractionResultSchema } from "../../src/domain/artifactGraph.js"; +import { parseEvidence } from "../../src/domain/evidence.js"; +import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js"; +import { cliTest } from "../support/cli/cliFixture.js"; + +cliTest( + "keeps each extracted file's occurrence limitations on that file", + async ({ cli }) => { + const root = await createTestTempDirectory("rea-zip-case-extraction-"); + const archivePath = join(root, "case.zip"); + const writer = new ZipWriter(new Uint8ArrayWriter()); + // 50 pairs that collide under case folding, as R8-shrunk resources do. + for (let index = 0; index < 50; index += 1) + for (const name of [ + `res/-A${String(index)}.xml`, + `res/-a${String(index)}.xml`, + ]) + await writer.add(name, new TextReader(name)); + await writeFile(archivePath, await writer.close()); + const output = join(root, "out"); + const extracted = await cli.run({ + arguments: [ + "extract-artifact", + archivePath, + "--output-root", + output, + "--json", + ], + }); + expect(extracted.exitCode, extracted.stdout + extracted.stderr).toBe(0); + const extraction = artifactExtractionResultSchema.parse( + parseEvidence(extracted.json).normalized_result, + ); + expect(extraction.artifacts).toHaveLength(100); + expect( + extraction.artifacts.find( + ({ relative_path }) => relative_path === "res/-A0.xml", + )?.limitations, + ).toEqual([ + expect.stringContaining( + "Logical path res/-A0.xml collides under English (en-US) Unicode case folding with res/-a0.xml", + ), + ]); + // The extraction's own list counts them instead of repeating each one. + expect(extraction.limitations).toContain( + "100 extracted file(s) carry their inventory occurrence's limitations in artifacts[].limitations.", + ); + expect(extraction.limitations.length).toBeLessThan(10); + }, +); diff --git a/tests/acceptance/pyinstallerArchive.test.ts b/tests/acceptance/pyinstallerArchive.test.ts index 54f724ca70e..afca23f7fa0 100644 --- a/tests/acceptance/pyinstallerArchive.test.ts +++ b/tests/acceptance/pyinstallerArchive.test.ts @@ -250,9 +250,14 @@ cliTest( scope: "container-entries", status: "complete", }); - expect(extracted.limitations).toContainEqual( + // The reconstructed header belongs to the file it describes. + expect( + extracted.artifacts.find( + ({ relative_path }) => relative_path === "PYZ.pyz/app.pyc", + )?.limitations, + ).toContainEqual( expect.stringMatching( - /^PYZ\.pyz\/app\.pyc:.*header.*reconstructed.*archive.*magic.*digest.*derived file/iu, + /header.*reconstructed.*archive.*magic.*digest.*derived file/iu, ), ); const expected = new Map([