fix(security): restrict investigation artifact inputs

This commit is contained in:
morluto
2026-07-14 04:48:46 +08:00
parent ee785ca9a9
commit a2076b47a6
17 changed files with 286 additions and 66 deletions
+10
View File
@@ -102,6 +102,16 @@ export const scanArtifactInventory = async (
): Promise<ArtifactInventorySnapshot> => {
abortIfNeeded(signal);
const path = await realpath(inputPath);
return scanCanonicalArtifactInventory(path, limits, signal, nativeMount);
};
/** Scan one already-canonical artifact path without resolving it again. */
export const scanCanonicalArtifactInventory = async (
path: string,
limits: ArtifactLimits,
signal?: AbortSignal,
nativeMount: ArtifactNativeMountPolicy = NATIVE_MOUNT_DISABLED,
): Promise<ArtifactInventorySnapshot> => {
const metadata = await lstat(path);
const rootFormat = await classifyRoot(path, metadata.isDirectory());
const rootDigest = metadata.isDirectory()
@@ -0,0 +1,33 @@
import { realpath } from "node:fs/promises";
import type { ArtifactLimits } from "../artifacts/ArtifactReader.js";
import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js";
import { withinRoot } from "../reference/ReferenceSourceReaderPaths.js";
import {
scanCanonicalArtifactInventory,
type ArtifactInventorySnapshot,
} from "./ArtifactInventory.js";
/** Resolve, authorize, and scan one artifact without a second path resolution. */
export const scanAuthorizedArtifactInventory = async (
inputPath: string,
roots: readonly string[],
limits: ArtifactLimits,
signal?: AbortSignal,
): Promise<ArtifactInventorySnapshot> => {
if (roots.length === 0)
throw new ArtifactReaderFailure(
"path",
"Artifact input roots are disabled",
);
const [path, approved] = await Promise.all([
realpath(inputPath),
Promise.all(roots.map(async (root) => realpath(root))),
]);
if (!approved.some((root) => withinRoot(root, path)))
throw new ArtifactReaderFailure(
"path",
"Artifact path is outside approved input roots",
);
return scanCanonicalArtifactInventory(path, limits, signal);
};
+15 -4
View File
@@ -15,9 +15,9 @@ import { jsonValueSchema } from "../domain/jsonValue.js";
import { err, ok, type Result } from "../domain/result.js";
import {
paginateArtifactInventory,
scanArtifactInventory,
type ArtifactInventorySnapshot,
} from "./ArtifactInventory.js";
import { scanAuthorizedArtifactInventory } from "./AuthorizedArtifactInventory.js";
import { ARTIFACT_GRAPH_PROVIDER } from "./InvestigationProviders.js";
export const AUTOMATIC_RUN_LIMITATION =
@@ -33,17 +33,28 @@ export interface InventoryEvidencePages {
readonly right: readonly Evidence[];
}
/** Scan both inputs once under the same bounded traversal policy. */
/** Authorize and scan both inputs once under the same bounded traversal policy. */
export const scanVersions = async (
input: CrossVersionInvestigationInput,
inputRoots: readonly string[],
signal?: AbortSignal,
): Promise<Result<VersionSnapshots, AnalysisError>> => {
try {
if (signal?.aborted === true)
return err(new AnalysisCancelledError("find_changed_behavior"));
const limits = artifactLimits(input.options);
const left = await scanArtifactInventory(input.left_path, limits, signal);
const right = await scanArtifactInventory(input.right_path, limits, signal);
const left = await scanAuthorizedArtifactInventory(
input.left_path,
inputRoots,
limits,
signal,
);
const right = await scanAuthorizedArtifactInventory(
input.right_path,
inputRoots,
limits,
signal,
);
return ok({ left, right });
} catch (cause: unknown) {
if (cause instanceof ArtifactReaderFailure)
+14 -4
View File
@@ -49,12 +49,18 @@ export interface CrossVersionInvestigationOutcome {
readonly reused: boolean;
}
/** Runtime authority and cancellation capabilities for one investigation. */
export interface CrossVersionInvestigationExecution {
readonly inputRoots: readonly string[];
readonly session?: BinarySessionPort;
readonly signal?: AbortSignal;
}
/** Run or resume a deterministic cross-version artifact investigation. */
export const runCrossVersionInvestigation = async (
input: CrossVersionInvestigationInput,
policy: EvidenceFilePolicy,
session?: BinarySessionPort,
signal?: AbortSignal,
execution: CrossVersionInvestigationExecution,
): Promise<Result<CrossVersionInvestigationOutcome, AnalysisError>> => {
const parsed = crossVersionInvestigationInputSchema.safeParse(input);
if (!parsed.success)
@@ -71,14 +77,18 @@ export const runCrossVersionInvestigation = async (
const preflight = validateWorkspaceRequest(initial.value, parsed.data);
if (!preflight.ok) return preflight;
const snapshots = await scanVersions(parsed.data, signal);
const snapshots = await scanVersions(
parsed.data,
execution.inputRoots,
execution.signal,
);
if (!snapshots.ok) return snapshots;
return continueInvestigation({
input: parsed.data,
initial: initial.value,
snapshots: snapshots.value,
policy,
session,
session: execution.session,
});
};