From dc8a3e60786acfd801cc7a412ab9366a566e0db7 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Wed, 14 Oct 2026 00:46:08 +0800 Subject: [PATCH] fix(artifacts): bind extraction to opened target --- src/artifacts/ArtifactProvider.ts | 1 + .../extraction/ArtifactExtraction.ts | 10 ++++ .../artifactProvider.extractionFormat.test.ts | 59 +++++++------------ 3 files changed, 33 insertions(+), 37 deletions(-) diff --git a/src/artifacts/ArtifactProvider.ts b/src/artifacts/ArtifactProvider.ts index ee5d32ae8..a3fe16a11 100644 --- a/src/artifacts/ArtifactProvider.ts +++ b/src/artifacts/ArtifactProvider.ts @@ -188,6 +188,7 @@ class ArtifactClient implements AnalysisClient { const result = await extractArtifact( { inputPath: this.target.sourcePath ?? this.target.path, + expectedRootSha256: this.target.sha256, outputRoot: parsed.output_root, environment: this.environment, integrityPolicy: parsed.integrity_policy, diff --git a/src/artifacts/extraction/ArtifactExtraction.ts b/src/artifacts/extraction/ArtifactExtraction.ts index ec9cf584c..e6ce2118c 100644 --- a/src/artifacts/extraction/ArtifactExtraction.ts +++ b/src/artifacts/extraction/ArtifactExtraction.ts @@ -45,6 +45,8 @@ import type { ArtifactIntegrityPolicyName } from "../../domain/artifactIntegrity /** Local extraction input with the output root chosen by the adapter. */ export interface ArtifactExtractionInput { readonly inputPath: string; + /** Digest of the active session target, when extraction is session-bound. */ + readonly expectedRootSha256?: string; readonly outputRoot: string; readonly environment: Readonly; readonly integrityPolicy: ArtifactIntegrityPolicyName; @@ -73,6 +75,14 @@ const extractArtifactInScope = async ( admitRootFormat: (format) => requireExtractableFormat(format, input.inputPath), }); + if ( + input.expectedRootSha256 !== undefined && + snapshot.manifest.root_sha256 !== input.expectedRootSha256 + ) + throw new ArtifactReaderFailure( + "integrity", + `Active artifact digest changed: expected ${input.expectedRootSha256}, observed ${snapshot.manifest.root_sha256}`, + ); return materializeArtifactInventoryInScope( input, sourcePath, diff --git a/tests/boundary/providers/native/artifactProvider.extractionFormat.test.ts b/tests/boundary/providers/native/artifactProvider.extractionFormat.test.ts index 0e3e8ddd5..0b52565e4 100644 --- a/tests/boundary/providers/native/artifactProvider.extractionFormat.test.ts +++ b/tests/boundary/providers/native/artifactProvider.extractionFormat.test.ts @@ -17,7 +17,7 @@ import { artifactExtractionResultSchema } from "../../../../src/domain/artifactG import type { BinaryTarget } from "../../../../src/domain/binaryTargetTypes.js"; it.each(["plist", "pe"] as const)( - "extracts current iNES bytes after a non-extractable %s target is replaced", + "refuses iNES bytes after a non-extractable %s target is replaced", async (format) => { const root = await createTestTempDirectory("rea-extract-current-"); const path = join(root, "source.bin"); @@ -29,40 +29,28 @@ it.each(["plist", "pe"] as const)( if (!target.ok) throw target.error; expect(target.value.format).toBe(format); const client = new ArtifactProvider(process.env).createClient(target.value); - const bytes = inesFixture({ - trainer: true, - trailing: Buffer.from("tail"), - }); - await writeFile(path, bytes); + await writeFile(path, inesFixture()); const output = join(root, "output"); const result = await client.execute( "extract_artifact", artifactExtractionExecutionSchema.parse({ output_root: output }), ); - if (!result.ok) throw result.error; - const extracted = artifactExtractionResultSchema.parse(result.value.result); - expect(extracted.manifest.root_format).toBe("ines"); - const paths = [ - "header.bin", - "trainer.bin", - "prg/000.bin", - "prg/001.bin", - "chr/000.bin", - "trailing.bin", - ]; - expect(extracted.artifacts).toHaveLength(paths.length); - expect( - Buffer.concat( - await Promise.all(paths.map((part) => readFile(join(output, part)))), - ), - ).toEqual(bytes); + expect(result).toMatchObject({ + ok: false, + error: { + _tag: "ArtifactOperationError", + operation: "extract_artifact", + reason: "integrity", + }, + }); + await expect(access(output)).rejects.toThrow(); await client.close(); }, ); describe("artifact extraction format support", () => { it.each(["zip", "asar", "hap", "hsp", "app"] as const)( - "extracts the scanned %s after an active iNES path is replaced", + "refuses a replaced iNES target that scans as %s", async (format) => { const root = await createTestTempDirectory("rea-extract-replaced-"); const path = join(root, `source.${format}`); @@ -86,19 +74,16 @@ describe("artifact extraction format support", () => { "extract_artifact", artifactExtractionExecutionSchema.parse({ output_root: output }), ); - if (!result.ok) throw result.error; - const extracted = artifactExtractionResultSchema.parse( - result.value.result, - ); - expect(extracted.manifest.root_format).toBe( - format === "app" ? "app-pack" : format, - ); - expect( - extracted.artifacts.map(({ relative_path }) => relative_path), - ).toEqual(["payload.txt"]); - expect(await readFile(join(output, "payload.txt"), "utf8")).toBe( - "evidence\n", - ); + expect(result).toMatchObject({ + ok: false, + error: { + _tag: "ArtifactOperationError", + operation: "extract_artifact", + reason: "integrity", + }, + }); + await expect(access(output)).rejects.toThrow(); + await client.close(); }, );