fix(artifacts): bind extraction to opened target

This commit is contained in:
morluto
2026-10-14 00:46:08 +08:00
parent 0434be924e
commit dc8a3e6078
3 changed files with 33 additions and 37 deletions
+1
View File
@@ -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,
@@ -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<NodeJS.ProcessEnv>;
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,
@@ -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();
},
);