fix(artifacts): bind iNES extraction admission to inventory

This commit is contained in:
64579525
2026-10-12 17:15:12 +08:00
parent 9b8916152b
commit 2f0e4d79ea
3 changed files with 203 additions and 15 deletions
+37 -8
View File
@@ -4,7 +4,10 @@ import { compareUnicodeCodePoints } from "../../domain/unicodeCodePointOrder.js"
import { digestCanonicalValue } from "../../domain/canonicalDigest.js";
import { AsarArtifactReader } from "../AsarArtifactReader.js";
import { InesArtifactReader } from "../InesArtifactReader.js";
import { openRootArtifact } from "../inventory/hashStableRootArtifact.js";
import {
openRootArtifact,
hashStableRootArtifactHandle,
} from "../inventory/hashStableRootArtifact.js";
import {
ArtifactPathRegistry,
normalizeArtifactPath,
@@ -218,7 +221,12 @@ const materializeSelection = async ({
const byPath = new Map(
selected.map((item) => [item.occurrence.logical_path, item]),
);
const reader = await createReader(sourcePath, input);
const reader = await createReader(
sourcePath,
input,
inventory.manifest,
signal,
);
const readerOwner: ArtifactResourceOwner = {
kind: "reader" as const,
reader,
@@ -441,19 +449,37 @@ const createReader = async (
ArtifactExtractionInput,
"inputPath" | "inputFormat" | "environment" | "resourceScope"
>,
manifest: ArtifactGraphManifest,
signal?: AbortSignal,
): Promise<ArtifactReader> => {
const format = input.inputFormat;
if (await requireExtractableFormat(path, input))
return new DirectoryArtifactReader(path);
if (format === "asar") return new AsarArtifactReader(path);
if (format === "ines") {
const owner = await openRootArtifact(path, input.resourceScope);
// The scanned container determines admission; a replaced path cannot select
// a different reader. Bind the reopened descriptor to its observed bytes.
if (manifest.root_format === "ines") {
const owner = await openRootArtifact(
path,
input.resourceScope,
signal,
"inventoried",
);
try {
const initial = await owner.handle.stat();
const digest = await hashStableRootArtifactHandle(
path,
owner.handle,
initial,
signal,
);
if (digest.sha256 !== manifest.root_sha256)
throw new ArtifactReaderFailure(
"integrity",
`Inventoried iNES source bytes changed: ${path}`,
);
return new InesArtifactReader(
path,
{
handle: owner.handle,
initial: await owner.handle.stat(),
initial,
},
owner,
);
@@ -471,6 +497,9 @@ const createReader = async (
throw cause;
}
}
if (await requireExtractableFormat(path, input))
return new DirectoryArtifactReader(path);
if (format === "asar") return new AsarArtifactReader(path);
if (isZipFormat(format)) return new ZipArtifactReader(path, format);
return new MachOSliceArtifactReader(path, input.environment);
};
@@ -23,6 +23,7 @@ export const openRootArtifact = async (
path: string,
resourceScope: ArtifactResourceScope,
signal?: AbortSignal,
admission: "initial" | "inventoried" = "initial",
): Promise<OwnedFileHandle> => {
try {
return new OwnedFileHandle(
@@ -31,14 +32,35 @@ export const openRootArtifact = async (
} catch (cause: unknown) {
const primary =
cause instanceof RegularFileAdmissionFailure ? cause.cause : cause;
const changed =
admission === "inventoried" &&
(primary instanceof NonRegularFileReadError ||
(primary instanceof Error &&
"code" in primary &&
["ENOENT", "ENOTDIR", "ELOOP", "EISDIR", "ENOTFILE"].includes(
String(primary.code),
)));
// Normalize the primary failure before cleanup so failed closes keep both facts.
const failure =
primary instanceof NonRegularFileReadError
signal?.aborted === true
? new ArtifactReaderFailure(
"format",
`Artifact root is not a regular file: ${path}`,
"cancelled",
"Artifact admission cancelled",
{ cause: primary },
)
: primary;
: changed
? new ArtifactReaderFailure(
"integrity",
`Inventoried artifact source changed: ${path}`,
{ cause: primary },
)
: primary instanceof NonRegularFileReadError
? new ArtifactReaderFailure(
"format",
`Artifact root is not a regular file: ${path}`,
{ cause: primary },
)
: primary;
if (cause instanceof RegularFileAdmissionFailure) {
const resource = `root artifact descriptor for ${path}`;
const cleanup = await resourceScope.release({