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
+11 -2
View File
@@ -17,6 +17,7 @@ import { BinaryTargetError } from "../domain/configurationErrors.js";
import {
openRegularFile,
sameRegularFileState,
verifyRegularFileState,
} from "../filesystem/RegularFile.js";
import { AnalysisCancelledError } from "../domain/analysisErrorCore.js";
import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
@@ -174,7 +175,12 @@ export const parseBinaryTarget = async (
kind: "database",
format: "analysis-database",
});
const artifactFormat = await detectArtifactFormat(path, handle, signal);
const artifactFormat = await detectArtifactFormat(
path,
handle,
metadata,
signal,
);
if (artifactFormat !== undefined) {
const identity = {
path,
@@ -263,6 +269,7 @@ const throwIfTargetResolutionCancelled = (signal?: AbortSignal): void => {
const detectArtifactFormat = async (
path: string,
handle: FileHandle,
initial: Stats,
signal?: AbortSignal,
): Promise<
| Exclude<
@@ -283,6 +290,8 @@ const detectArtifactFormat = async (
const magic = Buffer.alloc(16);
const observed = await handle.read(magic, 0, magic.length, 0);
throwIfTargetResolutionCancelled(signal);
await verifyRegularFileState(path, { handle, initial });
throwIfTargetResolutionCancelled(signal);
if (
observed.bytesRead >= 8 &&
magic.subarray(0, 8).equals(Buffer.from([0, 0x61, 0x73, 0x6d, 1, 0, 0, 0]))
@@ -295,7 +304,7 @@ const detectArtifactFormat = async (
const prefix = magic.subarray(0, observed.bytesRead);
const nesFormat = nesContainerFormat(prefix);
if (nesFormat !== undefined) {
const parsed = parseInesLayout(prefix, (await handle.stat()).size);
const parsed = parseInesLayout(prefix, initial.size);
throwIfTargetResolutionCancelled(signal);
if (!parsed.ok)
throw new BinaryTargetError(path, parsed.error, {
+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";
}
+30 -3
View File
@@ -1,6 +1,6 @@
import { constants } from "node:fs";
import type { BigIntStats, Stats } from "node:fs";
import { open, type FileHandle } from "node:fs/promises";
import { lstat, open, type FileHandle } from "node:fs/promises";
import { OwnedFileHandle } from "./OwnedFileHandle.js";
@@ -39,8 +39,11 @@ export class RegularFileAdmissionFailure extends Error {
/** A regular input no longer matches the extent or identity admitted for reading. */
export class RegularFileChangedError extends Error {
readonly code = "ESTALE";
constructor(readonly path: string) {
super(`Selected input changed while being read: ${path}`);
constructor(
readonly path: string,
options?: ErrorOptions,
) {
super(`Selected input changed while being read: ${path}`, options);
this.name = "RegularFileChangedError";
}
}
@@ -51,6 +54,30 @@ export interface StableRegularFileDescriptor {
readonly initial: Stats;
}
/** Revalidate admitted bytes before interpreting them, including lost path identity. */
export const verifyRegularFileState = async (
path: string,
source: StableRegularFileDescriptor,
): Promise<void> => {
const [opened, selected] = await Promise.all([
source.handle.stat(),
lstat(path).catch((cause: unknown) => {
if (
cause instanceof Error &&
"code" in cause &&
["ENOENT", "ENOTDIR", "ELOOP"].includes(String(cause.code))
)
throw new RegularFileChangedError(path, { cause });
throw cause;
}),
]);
if (
!sameRegularFileState(source.initial, opened) ||
!sameRegularFileState(source.initial, selected)
)
throw new RegularFileChangedError(path);
};
/** Open and verify the selected file descriptor without blocking on pipes. */
export const openRegularFile = async (
path: string,
@@ -270,6 +270,50 @@ it.each(["truncated", "overwritten"])(
},
);
it.each(["inventory", "target"])(
"rejects changed iNES prefixes at the %s entry point",
async (route) => {
const root = await createTestTempDirectory("rea-ines-classification-race-");
const path = join(root, "source.nes");
for (const change of ["truncated", "counts", "signature"] as const) {
const bytes = inesFixture();
await writeFile(path, bytes);
const sample = await open(path, "r");
const prototype = Object.getPrototypeOf(sample) as Pick<
typeof sample,
"read"
>;
await sample.close();
const originalRead = prototype.read;
const read = vi
.spyOn(prototype, "read")
.mockImplementationOnce(async function (this: typeof sample, ...args) {
read.mockRestore();
const changed =
change === "truncated" ? bytes.subarray(0, 16) : Buffer.from(bytes);
if (change === "counts") changed[4] = 255;
if (change === "signature") changed.fill(0, 0, 4);
await writeFile(path, changed);
return originalRead.call(this, ...args);
});
try {
if (route === "inventory") {
await expect(inventoryArtifact(path)).rejects.toMatchObject({
reason: "integrity",
});
} else {
expect(await parseBinaryTarget(path)).toMatchObject({
ok: false,
error: { reason: expect.stringContaining("changed") },
});
}
} finally {
read.mockRestore();
}
}
},
);
it("releases an extraction-owned source descriptor on repeated close", async () => {
const root = await createTestTempDirectory("rea-ines-owned-source-");
const path = join(root, "source.nes");