fix(artifacts): bind extraction to opened target
This commit is contained in:
@@ -188,6 +188,7 @@ class ArtifactClient implements AnalysisClient {
|
|||||||
const result = await extractArtifact(
|
const result = await extractArtifact(
|
||||||
{
|
{
|
||||||
inputPath: this.target.sourcePath ?? this.target.path,
|
inputPath: this.target.sourcePath ?? this.target.path,
|
||||||
|
expectedRootSha256: this.target.sha256,
|
||||||
outputRoot: parsed.output_root,
|
outputRoot: parsed.output_root,
|
||||||
environment: this.environment,
|
environment: this.environment,
|
||||||
integrityPolicy: parsed.integrity_policy,
|
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. */
|
/** Local extraction input with the output root chosen by the adapter. */
|
||||||
export interface ArtifactExtractionInput {
|
export interface ArtifactExtractionInput {
|
||||||
readonly inputPath: string;
|
readonly inputPath: string;
|
||||||
|
/** Digest of the active session target, when extraction is session-bound. */
|
||||||
|
readonly expectedRootSha256?: string;
|
||||||
readonly outputRoot: string;
|
readonly outputRoot: string;
|
||||||
readonly environment: Readonly<NodeJS.ProcessEnv>;
|
readonly environment: Readonly<NodeJS.ProcessEnv>;
|
||||||
readonly integrityPolicy: ArtifactIntegrityPolicyName;
|
readonly integrityPolicy: ArtifactIntegrityPolicyName;
|
||||||
@@ -73,6 +75,14 @@ const extractArtifactInScope = async (
|
|||||||
admitRootFormat: (format) =>
|
admitRootFormat: (format) =>
|
||||||
requireExtractableFormat(format, input.inputPath),
|
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(
|
return materializeArtifactInventoryInScope(
|
||||||
input,
|
input,
|
||||||
sourcePath,
|
sourcePath,
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ import { artifactExtractionResultSchema } from "../../../../src/domain/artifactG
|
|||||||
import type { BinaryTarget } from "../../../../src/domain/binaryTargetTypes.js";
|
import type { BinaryTarget } from "../../../../src/domain/binaryTargetTypes.js";
|
||||||
|
|
||||||
it.each(["plist", "pe"] as const)(
|
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) => {
|
async (format) => {
|
||||||
const root = await createTestTempDirectory("rea-extract-current-");
|
const root = await createTestTempDirectory("rea-extract-current-");
|
||||||
const path = join(root, "source.bin");
|
const path = join(root, "source.bin");
|
||||||
@@ -29,40 +29,28 @@ it.each(["plist", "pe"] as const)(
|
|||||||
if (!target.ok) throw target.error;
|
if (!target.ok) throw target.error;
|
||||||
expect(target.value.format).toBe(format);
|
expect(target.value.format).toBe(format);
|
||||||
const client = new ArtifactProvider(process.env).createClient(target.value);
|
const client = new ArtifactProvider(process.env).createClient(target.value);
|
||||||
const bytes = inesFixture({
|
await writeFile(path, inesFixture());
|
||||||
trainer: true,
|
|
||||||
trailing: Buffer.from("tail"),
|
|
||||||
});
|
|
||||||
await writeFile(path, bytes);
|
|
||||||
const output = join(root, "output");
|
const output = join(root, "output");
|
||||||
const result = await client.execute(
|
const result = await client.execute(
|
||||||
"extract_artifact",
|
"extract_artifact",
|
||||||
artifactExtractionExecutionSchema.parse({ output_root: output }),
|
artifactExtractionExecutionSchema.parse({ output_root: output }),
|
||||||
);
|
);
|
||||||
if (!result.ok) throw result.error;
|
expect(result).toMatchObject({
|
||||||
const extracted = artifactExtractionResultSchema.parse(result.value.result);
|
ok: false,
|
||||||
expect(extracted.manifest.root_format).toBe("ines");
|
error: {
|
||||||
const paths = [
|
_tag: "ArtifactOperationError",
|
||||||
"header.bin",
|
operation: "extract_artifact",
|
||||||
"trainer.bin",
|
reason: "integrity",
|
||||||
"prg/000.bin",
|
},
|
||||||
"prg/001.bin",
|
});
|
||||||
"chr/000.bin",
|
await expect(access(output)).rejects.toThrow();
|
||||||
"trailing.bin",
|
|
||||||
];
|
|
||||||
expect(extracted.artifacts).toHaveLength(paths.length);
|
|
||||||
expect(
|
|
||||||
Buffer.concat(
|
|
||||||
await Promise.all(paths.map((part) => readFile(join(output, part)))),
|
|
||||||
),
|
|
||||||
).toEqual(bytes);
|
|
||||||
await client.close();
|
await client.close();
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
describe("artifact extraction format support", () => {
|
describe("artifact extraction format support", () => {
|
||||||
it.each(["zip", "asar", "hap", "hsp", "app"] as const)(
|
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) => {
|
async (format) => {
|
||||||
const root = await createTestTempDirectory("rea-extract-replaced-");
|
const root = await createTestTempDirectory("rea-extract-replaced-");
|
||||||
const path = join(root, `source.${format}`);
|
const path = join(root, `source.${format}`);
|
||||||
@@ -86,19 +74,16 @@ describe("artifact extraction format support", () => {
|
|||||||
"extract_artifact",
|
"extract_artifact",
|
||||||
artifactExtractionExecutionSchema.parse({ output_root: output }),
|
artifactExtractionExecutionSchema.parse({ output_root: output }),
|
||||||
);
|
);
|
||||||
if (!result.ok) throw result.error;
|
expect(result).toMatchObject({
|
||||||
const extracted = artifactExtractionResultSchema.parse(
|
ok: false,
|
||||||
result.value.result,
|
error: {
|
||||||
);
|
_tag: "ArtifactOperationError",
|
||||||
expect(extracted.manifest.root_format).toBe(
|
operation: "extract_artifact",
|
||||||
format === "app" ? "app-pack" : format,
|
reason: "integrity",
|
||||||
);
|
},
|
||||||
expect(
|
});
|
||||||
extracted.artifacts.map(({ relative_path }) => relative_path),
|
await expect(access(output)).rejects.toThrow();
|
||||||
).toEqual(["payload.txt"]);
|
await client.close();
|
||||||
expect(await readFile(join(output, "payload.txt"), "utf8")).toBe(
|
|
||||||
"evidence\n",
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user