refactor(artifacts): remove selectors and traversal quotas

This commit is contained in:
morluto
2026-09-29 12:15:49 +08:00
parent fe342faa83
commit 8c24272ee7
31 changed files with 268 additions and 666 deletions
+2 -11
View File
@@ -1,18 +1,11 @@
import { Buffer } from "node:buffer";
import { posix } from "node:path";
import {
ArtifactReaderFailure,
type ArtifactLimits,
} from "./ArtifactReader.js";
import { ArtifactReaderFailure } from "./ArtifactReader.js";
const DRIVE_OR_UNC = /^(?:[A-Za-z]:|\\|\/\/)/u;
/** Normalize an untrusted logical archive path without touching filesystem. */
export const normalizeArtifactPath = (
input: string,
limits: Pick<ArtifactLimits, "maxDepth" | "maxPathBytes">,
): string => {
export const normalizeArtifactPath = (input: string): string => {
if (
input.includes("\0") ||
input.includes("\\") ||
@@ -28,8 +21,6 @@ export const normalizeArtifactPath = (
if (
normalized.length === 0 ||
parts.some((part) => part === "" || part === "." || part === "..") ||
parts.length > limits.maxDepth ||
Buffer.byteLength(normalized, "utf8") > limits.maxPathBytes ||
posix.normalize(normalized) !== normalized
)
throw new ArtifactReaderFailure("path", "Artifact path is not normalized");
+16 -37
View File
@@ -23,10 +23,7 @@ import {
} from "../domain/errors.js";
import type { JsonValue } from "../domain/jsonValue.js";
import { err, ok } from "../domain/result.js";
import {
ArtifactReaderFailure,
type ArtifactLimits,
} from "./ArtifactReader.js";
import { ArtifactReaderFailure } from "./ArtifactReader.js";
import { ARTIFACT_GRAPH_PROVIDER } from "../application/InvestigationProviders.js";
import { createEvidence } from "../domain/evidence.js";
import { createArtifactInspection } from "../domain/artifactInspection.js";
@@ -119,11 +116,6 @@ class ArtifactClient implements AnalysisClient {
inputPath: this.target.sourcePath ?? this.target.path,
inputFormat: this.target.format,
outputRoot: parsed.output_root,
selection:
"paths" in parsed
? { paths: parsed.paths }
: { occurrenceIds: parsed.occurrence_ids },
limits: DEFAULT_ARTIFACT_LIMITS,
},
options?.signal,
);
@@ -225,37 +217,24 @@ class ArtifactClient implements AnalysisClient {
},
options?: ExecutionOptions,
) {
return inventoryArtifactFully(
this.target.sourcePath ?? this.target.path,
DEFAULT_ARTIFACT_LIMITS,
{
...(options?.signal === undefined ? {} : { signal: options.signal }),
nativeMount: resolveNativeMountPolicy(
parsed.native_mount_approved === true,
this.nativeMountEnabled,
),
integrity: resolveArtifactIntegrityPolicy(
parsed.integrity_policy === "fail"
? { mode: "fail" }
: {
mode: parsed.integrity_policy,
},
this.integrityContinueEnabled,
),
},
);
return inventoryArtifactFully(this.target.sourcePath ?? this.target.path, {
...(options?.signal === undefined ? {} : { signal: options.signal }),
nativeMount: resolveNativeMountPolicy(
parsed.native_mount_approved === true,
this.nativeMountEnabled,
),
integrity: resolveArtifactIntegrityPolicy(
parsed.integrity_policy === "fail"
? { mode: "fail" }
: {
mode: parsed.integrity_policy,
},
this.integrityContinueEnabled,
),
});
}
}
const DEFAULT_ARTIFACT_LIMITS: ArtifactLimits = {
maxEntries: 10_000,
maxTotalBytes: 1_073_741_824,
maxEntryBytes: 268_435_456,
maxCompressionRatio: 1_000,
maxDepth: 20,
maxPathBytes: 4_096,
};
const isArtifactOperation = (
operation: AnalysisOperation,
): operation is ArtifactAnalysisOperation =>
-11
View File
@@ -31,16 +31,6 @@ export interface ArtifactReader {
close(): Promise<void>;
}
/** Hard traversal limits shared by every reader and extraction operation. */
export interface ArtifactLimits {
readonly maxEntries: number;
readonly maxTotalBytes: number;
readonly maxEntryBytes: number;
readonly maxCompressionRatio: number;
readonly maxDepth: number;
readonly maxPathBytes: number;
}
/** Typed adapter failure translated at provider boundary. */
export class ArtifactReaderFailure extends Error {
constructor(
@@ -48,7 +38,6 @@ export class ArtifactReaderFailure extends Error {
| "cancelled"
| "format"
| "integrity"
| "limit"
| "path"
| "policy"
| "unavailable",
+6 -26
View File
@@ -16,10 +16,7 @@ import {
ArtifactPathRegistry,
normalizeArtifactPath,
} from "./ArtifactPaths.js";
import {
ArtifactReaderFailure,
type ArtifactLimits,
} from "./ArtifactReader.js";
import { ArtifactReaderFailure } from "./ArtifactReader.js";
/** One file durably written to an operation-owned output tree. */
export interface SafeOutputFile {
@@ -40,24 +37,18 @@ export type SafeOutputCleanup =
/** Symlink-resistant materialization in an exclusively owned, initially absent tree. */
export class SafeOutputTree {
readonly #registry = new ArtifactPathRegistry();
readonly #limits: ArtifactLimits;
readonly #outputRoot: string;
#totalBytes = 0;
#published = false;
#cleanup: SafeOutputCleanup = {
status: "not-required",
};
private constructor(outputRoot: string, limits: ArtifactLimits) {
private constructor(outputRoot: string) {
this.#outputRoot = outputRoot;
this.#limits = limits;
}
/** Exclusively create the absent destination as this operation's owned tree. */
static async create(
outputRoot: string,
limits: ArtifactLimits,
): Promise<SafeOutputTree> {
static async create(outputRoot: string): Promise<SafeOutputTree> {
if (!isAbsolute(outputRoot))
throw new ArtifactReaderFailure(
"path",
@@ -96,7 +87,7 @@ export class SafeOutputTree {
} finally {
await stagingHandle.close();
}
return new SafeOutputTree(canonicalOutput, limits);
return new SafeOutputTree(canonicalOutput);
} catch (cause: unknown) {
await rm(canonicalOutput, { recursive: true, force: true });
throw cause;
@@ -111,7 +102,7 @@ export class SafeOutputTree {
return structuredClone(this.#cleanup);
}
/** Stream one selected regular file with exact byte and digest verification. */
/** Stream one regular file with exact byte and digest verification. */
async write(
relativePath: string,
source: Readable,
@@ -119,7 +110,7 @@ export class SafeOutputTree {
signal?: AbortSignal,
): Promise<SafeOutputFile> {
this.#assertWritable();
const path = normalizeArtifactPath(relativePath, this.#limits);
const path = normalizeArtifactPath(relativePath);
this.#registry.add(path, "file");
const destination = await this.#prepareParent(path);
const handle = await open(
@@ -143,16 +134,6 @@ export class SafeOutputTree {
abortIfNeeded(signal);
const chunk = streamChunkToBuffer(raw);
bytes += chunk.length;
if (bytes > this.#limits.maxEntryBytes)
throw new ArtifactReaderFailure(
"limit",
`Observed entry bytes exceed limit: ${path}`,
);
if (this.#totalBytes + bytes > this.#limits.maxTotalBytes)
throw new ArtifactReaderFailure(
"limit",
"Observed extraction bytes exceed cumulative limit",
);
hash.update(chunk);
await writeAll(handle, chunk);
}
@@ -170,7 +151,6 @@ export class SafeOutputTree {
"integrity",
`Durable readback verification failed: ${path}`,
);
this.#totalBytes += bytes;
return { relativePath: path, sha256, bytesWritten: bytes };
} catch (cause: unknown) {
await handle.close().catch(() => undefined);