fix(artifacts): validate admitted roots before classification

This commit is contained in:
64579525
2026-10-12 16:37:04 +08:00
parent 5ba8b2f6bf
commit e6d4d7fb84
6 changed files with 121 additions and 37 deletions
+3 -2
View File
@@ -32,7 +32,7 @@ import {
} from "../domain/analysisErrorCore.js";
import { ArtifactOperationError } from "../domain/artifactOperationError.js";
import type { AnalysisError } from "../domain/analysisErrorBase.js";
import { jsonValueSchema, type JsonValue } from "../domain/jsonValue.js";
import type { JsonValue } from "../domain/jsonValue.js";
import { interfaceBuilderLimitsSchema } from "../domain/apple/interfaceBuilderGraph.js";
import { err, ok, type Result } from "../domain/result.js";
import { ArtifactReaderFailure } from "./ArtifactReader.js";
@@ -346,10 +346,11 @@ class ArtifactClient implements AnalysisClient {
this.target.sourcePath ?? this.target.path,
inventory.manifest,
);
const { ines, ...inventoryGraph } = inventory;
const inventoryEvidence = createEvidence(subject, ARTIFACT_GRAPH_PROVIDER, {
operation: "inventory_artifact",
parameters: inventoryParameters,
result: jsonValueSchema.parse(inventory),
result: { ...inventoryGraph, ...(ines === undefined ? {} : { ines }) },
rawResult: null,
limitations: inventory.limitations,
// The nested inventory already names every occurrence's logical path.
+13 -26
View File
@@ -1,5 +1,4 @@
import { createHash } from "node:crypto";
import { lstat } from "node:fs/promises";
import { Readable } from "node:stream";
import {
parseInesLayout,
@@ -8,7 +7,8 @@ import {
} from "../domain/inesContainer.js";
import type { ArtifactCommand } from "../domain/artifactGraph.js";
import {
sameRegularFileState,
RegularFileChangedError,
verifyRegularFileState,
type StableRegularFileDescriptor,
} from "../filesystem/RegularFile.js";
import { readFileHandleChunks } from "../filesystem/readFileHandleChunks.js";
@@ -177,29 +177,16 @@ export class InesArtifactReader implements ArtifactReader {
async #verifySource(): Promise<void> {
if (this.#closed)
throw new ArtifactReaderFailure("unavailable", "iNES reader is closed");
const [opened, selected] = await Promise.all([
this.source.handle.stat(),
lstat(this.path).catch((cause: unknown) => {
if (
cause instanceof Error &&
"code" in cause &&
["ENOENT", "ENOTDIR", "ELOOP"].includes(String(cause.code))
)
throw new ArtifactReaderFailure(
"integrity",
"iNES source changed during reading",
{ cause },
);
throw cause;
}),
]);
if (
!sameRegularFileState(this.source.initial, opened) ||
!sameRegularFileState(this.source.initial, selected)
)
throw new ArtifactReaderFailure(
"integrity",
"iNES source changed during reading",
);
try {
await verifyRegularFileState(this.path, this.source);
} catch (cause: unknown) {
if (cause instanceof RegularFileChangedError)
throw new ArtifactReaderFailure(
"integrity",
"iNES source changed during reading",
{ cause: cause.cause ?? cause },
);
throw cause;
}
}
}
+20 -4
View File
@@ -1,4 +1,8 @@
import { sameRegularFileState } from "../../filesystem/RegularFile.js";
import {
sameRegularFileState,
verifyRegularFileState,
RegularFileChangedError,
} from "../../filesystem/RegularFile.js";
import type { FileHandle } from "node:fs/promises";
import type { Stats } from "node:fs";
import type { StableRegularFileDescriptor } from "../../filesystem/RegularFile.js";
@@ -108,7 +112,7 @@ export const classifyAndHashRootForInventory = async (
"integrity",
`Root artifact changed before inventory: ${path}`,
);
const format = await classifyRootFormat(path, handle);
const format = await classifyRootFormat(path, handle, initial);
const digest = await hashStableRootArtifactHandle(
path,
handle,
@@ -130,7 +134,17 @@ export const classifyAndHashRootForInventory = async (
},
};
} catch (cause: unknown) {
outcome = { kind: "failed", cause };
outcome = {
kind: "failed",
cause:
cause instanceof RegularFileChangedError
? new ArtifactReaderFailure(
"integrity",
`Root artifact changed during inventory: ${path}`,
{ cause: cause.cause ?? cause },
)
: cause,
};
}
if (outcome.kind === "failed" || outcome.value.rootSource === undefined) {
const cleanupAttempt = await options.resourceScope.release(owner);
@@ -150,12 +164,14 @@ export const classifyAndHashRootForInventory = async (
const classifyRootFormat = async (
path: string,
handle: FileHandle,
initial: Stats,
): Promise<ArtifactOccurrence["artifact_format"]> => {
const magic = Buffer.alloc(ARTIFACT_CLASSIFICATION_PREFIX_BYTES);
const observed = await handle.read(magic, 0, magic.length, 0);
await verifyRegularFileState(path, { handle, initial });
const prefix = magic.subarray(0, observed.bytesRead);
if (nesContainerFormat(prefix) !== undefined) {
const parsed = parseInesLayout(prefix, (await handle.stat()).size);
const parsed = parseInesLayout(prefix, initial.size);
if (!parsed.ok) throw new ArtifactReaderFailure("format", parsed.error);
return "ines";
}