diff --git a/src/artifacts/extraction/ArtifactExtraction.ts b/src/artifacts/extraction/ArtifactExtraction.ts index 9bfdd13b4..8e40d6cc5 100644 --- a/src/artifacts/extraction/ArtifactExtraction.ts +++ b/src/artifacts/extraction/ArtifactExtraction.ts @@ -54,12 +54,26 @@ export const extractArtifact = async ( (occurrence.entry_kind === "file" || occurrence.entry_kind === "slice") && occurrence.logical_path !== ".", ); - const selectedIds = new Set( - selectedOccurrences.map(({ occurrence_id: id }) => id), - ); const regularPaths = new Set( selectedOccurrences.map(({ logical_path: path }) => path), ); + const activeOccurrences = selectedOccurrences.filter(({ logical_path }) => { + // Nested members remain represented by their containing regular file; + // only entries exposed by the active reader are materialized. + let path = logical_path; + for ( + let slash = path.lastIndexOf("/"); + slash >= 0; + slash = path.lastIndexOf("/") + ) { + path = path.slice(0, slash); + if (regularPaths.has(path)) return false; + } + return true; + }); + const selectedIds = new Set( + activeOccurrences.map(({ occurrence_id: id }) => id), + ); const occurrences = new Map(); const neededNodes = new Set(); collectOccurrences( @@ -75,7 +89,7 @@ export const extractArtifact = async ( occurrences, nodes, }; - const selected = selectedOccurrences.map((occurrence) => { + const selected = activeOccurrences.map((occurrence) => { // REA never decrypts archive entries, so an encrypted entry makes the // complete extraction unsupported rather than the archive invalid. if (occurrence.encrypted) @@ -101,25 +115,11 @@ export const extractArtifact = async ( ); return { occurrence, node }; }); - const activeSelected = selected.filter(({ occurrence }) => { - // Nested members remain represented by their containing regular file; - // only entries exposed by the active reader are materialized. - let path = occurrence.logical_path; - for ( - let slash = path.lastIndexOf("/"); - slash >= 0; - slash = path.lastIndexOf("/") - ) { - path = path.slice(0, slash); - if (regularPaths.has(path)) return false; - } - return true; - }); return materializeSelection({ input, sourcePath, inventory, - selected: activeSelected, + selected, signal, }); }; diff --git a/tests/boundary/providers/native/artifactProvider.extractionCompleteness.test.ts b/tests/boundary/providers/native/artifactProvider.extractionCompleteness.test.ts index 0a160a1da..e5994c79a 100644 --- a/tests/boundary/providers/native/artifactProvider.extractionCompleteness.test.ts +++ b/tests/boundary/providers/native/artifactProvider.extractionCompleteness.test.ts @@ -1,7 +1,7 @@ import { access, mkdir, readFile, rm, writeFile } from "node:fs/promises"; import { join } from "node:path"; -import { createPackage } from "@electron/asar"; +import { createPackage, createPackageWithOptions } from "@electron/asar"; import { expect, it, onTestFinished, vi } from "vitest"; import { ArtifactProvider } from "../../../../src/artifacts/ArtifactProvider.js"; @@ -92,6 +92,50 @@ it("preserves every equal-content occurrence and copies a nested ASAR as its con ); }); +it("ignores unavailable members of a nested ASAR but refuses them as active entries", async () => { + const root = await createTestTempDirectory("rea-extract-nested-unpacked-"); + const contents = join(root, "contents"); + await mkdir(join(contents, "native"), { recursive: true }); + await writeFile(join(contents, "main.js"), "export default 1;\n"); + await writeFile(join(contents, "native", "addon.node"), "native bytes\n"); + + const source = join(root, "bundle"); + await mkdir(source); + const archive = join(source, "app.asar"); + await createPackageWithOptions(contents, archive, { unpack: "**/*.node" }); + await rm(join(`${archive}.unpacked`, "native", "addon.node")); + + const nestedOutput = join(root, "nested-output"); + const nestedResult = await extract(source, nestedOutput); + if (!nestedResult.ok) throw nestedResult.error; + const nestedExtraction = artifactExtractionResultSchema.parse( + nestedResult.value.result, + ); + expect( + nestedExtraction.extraction_manifest.selected_occurrence_ids, + ).toHaveLength(1); + expect( + nestedExtraction.artifacts.map(({ relative_path }) => relative_path), + ).toEqual(["app.asar"]); + expect(await readFile(join(nestedOutput, "app.asar"))).toEqual( + await readFile(archive), + ); + + const activeOutput = join(root, "active-output"); + const activeResult = await extract(archive, activeOutput); + if (activeResult.ok) + throw new Error("Unavailable active ASAR member must fail extraction"); + expect(projectAnalysisError(activeResult.error)).toMatchObject({ + code: "artifact_operation_failed", + details: { + operation: "extract_artifact", + reason: "format", + detail: expect.stringContaining("native/addon.node"), + }, + }); + await expect(access(activeOutput)).rejects.toThrow(); +}); + // Synchronize a real filesystem change after the actual inventory scan. The // production reader, writes, failure translation and rollback all remain real. const afterInventory = (change: () => Promise): void => {