refactor(tools): return complete analysis results inline
This commit is contained in:
@@ -21,6 +21,9 @@ const browserScenarioParameters = (
|
||||
step_id,
|
||||
action,
|
||||
})),
|
||||
action_timeouts_ms: scenario.actions.map((item) =>
|
||||
"timeout_ms" in item ? (item.timeout_ms ?? null) : null,
|
||||
),
|
||||
secret_declarations: scenario.secrets.map(
|
||||
({ secret_id, environment_variable, purpose, redaction }) => ({
|
||||
secret_id,
|
||||
@@ -30,7 +33,6 @@ const browserScenarioParameters = (
|
||||
}),
|
||||
),
|
||||
capture: scenario.capture,
|
||||
limits: { ...scenario.limits },
|
||||
});
|
||||
|
||||
/** Create Evidence without retaining resolved scenario secret values. */
|
||||
|
||||
@@ -9,7 +9,6 @@ describe("capability inventory contract", () => {
|
||||
{ open: false, capabilities: [] },
|
||||
{
|
||||
processCaptureEnabled: false,
|
||||
evidenceFileRoots: 0,
|
||||
},
|
||||
);
|
||||
const unavailable = inventory.find(({ available }) => !available);
|
||||
|
||||
@@ -4,7 +4,6 @@ import { buildCapabilityInventory } from "./CapabilityInventory.js";
|
||||
|
||||
const enabledPolicy: Parameters<typeof buildCapabilityInventory>[1] = {
|
||||
processCaptureEnabled: true,
|
||||
evidenceFileRoots: 1,
|
||||
browserObservationEnabled: true,
|
||||
browserScenarioEnabled: true,
|
||||
electronObservationEnabled: true,
|
||||
@@ -169,7 +168,6 @@ describe("capability inventory: caller guidance", () => {
|
||||
const inventory = buildCapabilityInventory(status(), {
|
||||
...enabledPolicy,
|
||||
processCaptureEnabled: false,
|
||||
evidenceFileRoots: 0,
|
||||
browserObservationEnabled: false,
|
||||
electronObservationEnabled: false,
|
||||
v8InspectorObservationEnabled: false,
|
||||
|
||||
@@ -28,7 +28,6 @@ type ToolAvailabilityReason = "available" | ToolUnavailabilityReason;
|
||||
type ProviderDescriptor = ProviderCapability;
|
||||
export type AvailabilityPolicy = {
|
||||
readonly processCaptureEnabled: boolean;
|
||||
readonly evidenceFileRoots: number;
|
||||
readonly browserObservationEnabled?: boolean;
|
||||
readonly browserScenarioEnabled?: boolean;
|
||||
readonly electronObservationEnabled?: boolean;
|
||||
@@ -229,11 +228,6 @@ const policyAvailability = ({
|
||||
if (name === "plan_managed_runtime_correlation")
|
||||
return { reason: "available", remediation: null };
|
||||
if (kind === "application") return { reason: "available", remediation: null };
|
||||
if (name === "import_evidence_bundle" && policy.evidenceFileRoots === 0)
|
||||
return {
|
||||
reason: "policy_disabled",
|
||||
remediation: "Configure or grant an evidence_read root.",
|
||||
};
|
||||
const electron = electronPolicyAvailability(name, kind, policy);
|
||||
if (electron !== null) return electron;
|
||||
if (kind === "runtime-provider")
|
||||
|
||||
@@ -54,7 +54,7 @@ export const electronObservationIdentity = (
|
||||
observation_sha256: sha256Text(
|
||||
`${context.snapshot.manifest.root_sha256}\0${scope}\0${key}`,
|
||||
),
|
||||
scope: scope.slice(0, 4_096),
|
||||
scope,
|
||||
});
|
||||
|
||||
/** Add a direct AST syntax relationship. */
|
||||
|
||||
@@ -26,7 +26,6 @@ import type {
|
||||
JavaScriptSourceMapObservation,
|
||||
JavaScriptSourceMapOriginal,
|
||||
} from "./JavaScriptArtifactAnalysisTypes.js";
|
||||
import { JAVASCRIPT_APPLICATION_RESOURCE_LIMITS } from "./JavaScriptArtifactReconstructionInput.js";
|
||||
import { analyzeJavaScriptJsonModule } from "./JavaScriptJsonModules.js";
|
||||
|
||||
interface MutableArtifactAnalysis {
|
||||
@@ -44,22 +43,14 @@ interface MutableArtifactAnalysis {
|
||||
|
||||
interface ArtifactAnalysisContext {
|
||||
readonly state: MutableArtifactAnalysis;
|
||||
readonly deadline: number;
|
||||
readonly now: () => number;
|
||||
}
|
||||
|
||||
/** Analyze every selected source under shared AST and parser safety bounds. */
|
||||
/** Analyze every text source from the artifact without a result quota. */
|
||||
export const analyzeJavaScriptArtifactFiles = (
|
||||
fileSet: JavaScriptArtifactFileSet,
|
||||
now: () => number,
|
||||
): JavaScriptArtifactAnalysis => {
|
||||
const state = emptyArtifactAnalysis();
|
||||
const context = {
|
||||
state,
|
||||
deadline:
|
||||
now() + JAVASCRIPT_APPLICATION_RESOURCE_LIMITS.maxParseMilliseconds,
|
||||
now,
|
||||
};
|
||||
const context = { state };
|
||||
for (const file of fileSet.files) analyzeArtifactFile(file, context);
|
||||
return finalizeArtifactAnalysis(state);
|
||||
};
|
||||
@@ -87,7 +78,7 @@ const finalizeArtifactAnalysis = (
|
||||
"Static paths and relationships may remain unresolved when expressions are dynamic or obfuscated.",
|
||||
...(truncatedScopes === 0
|
||||
? []
|
||||
: ["One or more static-analysis scopes reached an approved bound."]),
|
||||
: ["Some static-analysis scopes were truncated."]),
|
||||
],
|
||||
};
|
||||
};
|
||||
@@ -105,13 +96,6 @@ const analyzeArtifactFile = (
|
||||
state.files.push({ file, javascript: null, semantic: null });
|
||||
return;
|
||||
}
|
||||
const remainingNodes =
|
||||
JAVASCRIPT_APPLICATION_RESOURCE_LIMITS.maxAstNodes - state.visitedNodes;
|
||||
if (remainingNodes <= 0 || context.now() > context.deadline) {
|
||||
state.files.push({ file, javascript: null, semantic: null });
|
||||
state.truncatedScopes += 1;
|
||||
return;
|
||||
}
|
||||
const parsed = parseJavaScriptSource(file.text.value);
|
||||
if (parsed === null) {
|
||||
const analysis = failedJavaScriptStaticAnalysis();
|
||||
@@ -123,9 +107,9 @@ const analyzeArtifactFile = (
|
||||
file.text.value,
|
||||
parsed,
|
||||
{
|
||||
maxAstNodes: remainingNodes,
|
||||
deadline: context.deadline,
|
||||
now: context.now,
|
||||
maxAstNodes: Number.POSITIVE_INFINITY,
|
||||
deadline: Number.POSITIVE_INFINITY,
|
||||
now: () => 0,
|
||||
},
|
||||
);
|
||||
const staticFindings = findingCount(analysis);
|
||||
@@ -156,10 +140,7 @@ const addSourceMap = (
|
||||
file: JavaScriptArtifactFile,
|
||||
context: ArtifactAnalysisContext,
|
||||
): void => {
|
||||
const sourceMap = parseSourceMap(file, {
|
||||
deadline: context.deadline,
|
||||
now: context.now,
|
||||
});
|
||||
const sourceMap = parseSourceMap(file);
|
||||
context.state.sourceMaps.push(sourceMap);
|
||||
};
|
||||
|
||||
@@ -265,43 +246,23 @@ const htmlBaseHref = (text: string): string | null => {
|
||||
|
||||
const parseSourceMap = (
|
||||
file: JavaScriptArtifactFile,
|
||||
context: {
|
||||
readonly deadline: number;
|
||||
readonly now: () => number;
|
||||
},
|
||||
): JavaScriptSourceMapObservation => {
|
||||
if (!file.text.included) return unavailableSourceMap(file);
|
||||
if (context.now() > context.deadline)
|
||||
return truncatedSourceMap(
|
||||
file,
|
||||
null,
|
||||
"Parse deadline reached before source-map decoding.",
|
||||
);
|
||||
let value: unknown;
|
||||
try {
|
||||
value = JSON.parse(file.text.value);
|
||||
} catch {
|
||||
return invalidSourceMap(file, "Source map is not valid JSON.");
|
||||
}
|
||||
if (context.now() > context.deadline)
|
||||
return truncatedSourceMap(
|
||||
file,
|
||||
null,
|
||||
"Parse deadline elapsed during source-map decoding.",
|
||||
);
|
||||
const maps = flattenSourceMaps(value);
|
||||
if (maps === undefined)
|
||||
return invalidSourceMap(file, "Source map is not a bounded version 3 map.");
|
||||
return collectSourceMapOriginals(file, maps, context);
|
||||
return invalidSourceMap(file, "Source map is not a version 3 map.");
|
||||
return collectSourceMapOriginals(file, maps);
|
||||
};
|
||||
|
||||
const collectSourceMapOriginals = (
|
||||
file: JavaScriptArtifactFile,
|
||||
maps: readonly Readonly<Record<string, unknown>>[],
|
||||
context: {
|
||||
readonly deadline: number;
|
||||
readonly now: () => number;
|
||||
},
|
||||
): JavaScriptSourceMapObservation => {
|
||||
const sources: JavaScriptSourceMapOriginal[] = [];
|
||||
for (const map of maps) {
|
||||
@@ -313,15 +274,6 @@ const collectSourceMapOriginals = (
|
||||
: [];
|
||||
const root = typeof map.sourceRoot === "string" ? map.sourceRoot : "";
|
||||
for (const [index, raw] of names.entries()) {
|
||||
if (sources.length % 1_024 === 0 && context.now() > context.deadline)
|
||||
return {
|
||||
path: file.path,
|
||||
sha256: file.sha256,
|
||||
status: "truncated",
|
||||
sources,
|
||||
omitted_sources: null,
|
||||
limitation: "Parse deadline elapsed during source-map traversal.",
|
||||
};
|
||||
if (typeof raw !== "string")
|
||||
return invalidSourceMap(
|
||||
file,
|
||||
@@ -357,8 +309,7 @@ const unavailableSourceMap = (
|
||||
status: "invalid",
|
||||
sources: [],
|
||||
omitted_sources: 0,
|
||||
limitation:
|
||||
"Source-map text was unavailable within the selected input limits.",
|
||||
limitation: "Source-map text could not be decoded as UTF-8.",
|
||||
};
|
||||
};
|
||||
|
||||
@@ -381,19 +332,6 @@ const flattenSourceMaps = (
|
||||
return maps;
|
||||
};
|
||||
|
||||
const truncatedSourceMap = (
|
||||
file: JavaScriptArtifactFile,
|
||||
omitted: number | null,
|
||||
limitation: string,
|
||||
): JavaScriptSourceMapObservation => ({
|
||||
path: file.path,
|
||||
sha256: file.sha256,
|
||||
status: "truncated",
|
||||
sources: [],
|
||||
omitted_sources: omitted,
|
||||
limitation,
|
||||
});
|
||||
|
||||
const invalidSourceMap = (
|
||||
file: JavaScriptArtifactFile,
|
||||
limitation: string,
|
||||
|
||||
@@ -14,7 +14,6 @@ import {
|
||||
} from "../artifacts/ArtifactReader.js";
|
||||
import { streamChunkToBuffer } from "../artifacts/StreamBytes.js";
|
||||
import type { ArtifactInventorySnapshot } from "./ArtifactInventory.js";
|
||||
import { JAVASCRIPT_APPLICATION_RESOURCE_LIMITS } from "./JavaScriptArtifactReconstructionInput.js";
|
||||
|
||||
/** Relevant file categories projected from the complete artifact inventory. */
|
||||
export type JavaScriptArtifactFileKind =
|
||||
@@ -25,7 +24,7 @@ export type JavaScriptArtifactFileKind =
|
||||
| "source-map"
|
||||
| "native-addon";
|
||||
|
||||
/** One content-addressed local artifact file and its bounded text availability. */
|
||||
/** One content-addressed local artifact file and its text availability. */
|
||||
export interface JavaScriptArtifactFile {
|
||||
readonly path: string;
|
||||
readonly container_sha256: string;
|
||||
@@ -38,11 +37,7 @@ export interface JavaScriptArtifactFile {
|
||||
| { readonly included: true; readonly value: string }
|
||||
| {
|
||||
readonly included: false;
|
||||
readonly reason:
|
||||
| "not-applicable"
|
||||
| "file-limit"
|
||||
| "byte-limit"
|
||||
| "invalid-utf8";
|
||||
readonly reason: "not-applicable" | "invalid-utf8";
|
||||
};
|
||||
}
|
||||
|
||||
@@ -58,10 +53,7 @@ export interface JavaScriptArtifactContainer {
|
||||
export interface JavaScriptArtifactFileSet {
|
||||
readonly files: readonly JavaScriptArtifactFile[];
|
||||
readonly containers: readonly JavaScriptArtifactContainer[];
|
||||
readonly text_files_selected: number;
|
||||
readonly text_bytes_read: number;
|
||||
readonly omitted_text_files: number;
|
||||
readonly limit_omitted_text_files: number;
|
||||
readonly invalid_utf8_files: number;
|
||||
}
|
||||
|
||||
@@ -73,15 +65,9 @@ interface ExpectedFile {
|
||||
readonly kind: JavaScriptArtifactFileKind;
|
||||
}
|
||||
|
||||
interface Selection {
|
||||
readonly selected: boolean;
|
||||
readonly reason: "file-limit" | "byte-limit" | null;
|
||||
}
|
||||
|
||||
interface ReadContext {
|
||||
readonly expected: ReadonlyMap<string, ExpectedFile>;
|
||||
readonly expectedContainers: ReadonlyMap<string, JavaScriptArtifactContainer>;
|
||||
readonly selections: ReadonlyMap<string, Selection>;
|
||||
readonly registry: ArtifactPathRegistry;
|
||||
readonly files: JavaScriptArtifactFile[];
|
||||
readonly containers: JavaScriptArtifactContainer[];
|
||||
@@ -94,10 +80,9 @@ interface ReadTextInput {
|
||||
readonly reader: ArtifactReader;
|
||||
readonly entry: ArtifactEntry;
|
||||
readonly expected: ExpectedFile;
|
||||
readonly selection: Selection | undefined;
|
||||
}
|
||||
|
||||
/** Read only selected textual entries through an already-inventoried reader. */
|
||||
/** Read all relevant textual entries through an already-inventoried reader. */
|
||||
export const readJavaScriptArtifactFiles = async (
|
||||
reader: ArtifactReader,
|
||||
snapshot: ArtifactInventorySnapshot,
|
||||
@@ -105,11 +90,9 @@ export const readJavaScriptArtifactFiles = async (
|
||||
): Promise<JavaScriptArtifactFileSet> => {
|
||||
const inventory = expectedInventory(snapshot);
|
||||
const expected = inventory.files;
|
||||
const selections = selectTextFiles(expected);
|
||||
const context: ReadContext = {
|
||||
expected,
|
||||
expectedContainers: inventory.containers,
|
||||
selections,
|
||||
registry: new ArtifactPathRegistry(),
|
||||
files: [],
|
||||
containers: [],
|
||||
@@ -122,22 +105,12 @@ export const readJavaScriptArtifactFiles = async (
|
||||
compareCodePoints(left.path, right.path),
|
||||
);
|
||||
assertExpectedFilesWereVisited(expected, files);
|
||||
const selected = [...selections.values()].filter(
|
||||
({ selected: isSelected }) => isSelected,
|
||||
).length;
|
||||
return {
|
||||
files,
|
||||
containers: context.containers.sort((left, right) =>
|
||||
compareCodePoints(left.path, right.path),
|
||||
),
|
||||
text_files_selected: selected,
|
||||
text_bytes_read: context.textBytes,
|
||||
omitted_text_files: [...selections.values()].filter(
|
||||
({ selected: isSelected }) => !isSelected,
|
||||
).length,
|
||||
limit_omitted_text_files: [...selections.values()].filter(
|
||||
({ reason }) => reason === "file-limit" || reason === "byte-limit",
|
||||
).length,
|
||||
invalid_utf8_files: context.invalidUtf8,
|
||||
};
|
||||
};
|
||||
@@ -195,12 +168,10 @@ const visitReader = async (
|
||||
}
|
||||
const expected = context.expected.get(path);
|
||||
if (expected === undefined) continue;
|
||||
const selection = context.selections.get(path);
|
||||
const text = await readTextIfSelected(context, {
|
||||
const text = await readText(context, {
|
||||
reader: frame.reader,
|
||||
entry,
|
||||
expected,
|
||||
selection,
|
||||
});
|
||||
context.files.push({
|
||||
path,
|
||||
@@ -238,20 +209,14 @@ const expectedContainer = (
|
||||
return container;
|
||||
};
|
||||
|
||||
const readTextIfSelected = async (
|
||||
const readText = async (
|
||||
context: ReadContext,
|
||||
input: ReadTextInput,
|
||||
): Promise<JavaScriptArtifactFile["text"]> => {
|
||||
if (input.expected.kind === "native-addon")
|
||||
return { included: false, reason: "not-applicable" };
|
||||
if (input.selection?.selected !== true)
|
||||
return {
|
||||
included: false,
|
||||
reason: input.selection?.reason ?? "file-limit",
|
||||
};
|
||||
const bytes = await readBounded(
|
||||
const bytes = await readAll(
|
||||
await input.reader.open(input.entry, context.signal),
|
||||
JAVASCRIPT_APPLICATION_RESOURCE_LIMITS.maxTextFileBytes,
|
||||
context.signal,
|
||||
);
|
||||
const digest = createHash("sha256").update(bytes).digest("hex");
|
||||
@@ -310,42 +275,8 @@ const expectedInventory = (
|
||||
return { files, containers };
|
||||
};
|
||||
|
||||
const selectTextFiles = (
|
||||
files: ReadonlyMap<string, ExpectedFile>,
|
||||
): ReadonlyMap<string, Selection> => {
|
||||
const selected = new Map<string, Selection>();
|
||||
let fileCount = 0;
|
||||
let bytes = 0;
|
||||
const ordered = [...files.values()].sort((left, right) => {
|
||||
const priority = filePriority(left.kind) - filePriority(right.kind);
|
||||
return priority === 0 ? compareCodePoints(left.path, right.path) : priority;
|
||||
});
|
||||
for (const file of ordered) {
|
||||
if (file.kind === "native-addon") continue;
|
||||
if (
|
||||
fileCount >= JAVASCRIPT_APPLICATION_RESOURCE_LIMITS.maxTextFiles ||
|
||||
file.bytes > JAVASCRIPT_APPLICATION_RESOURCE_LIMITS.maxTextFileBytes
|
||||
) {
|
||||
selected.set(file.path, { selected: false, reason: "file-limit" });
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
bytes + file.bytes >
|
||||
JAVASCRIPT_APPLICATION_RESOURCE_LIMITS.maxTotalTextBytes
|
||||
) {
|
||||
selected.set(file.path, { selected: false, reason: "byte-limit" });
|
||||
continue;
|
||||
}
|
||||
selected.set(file.path, { selected: true, reason: null });
|
||||
fileCount += 1;
|
||||
bytes += file.bytes;
|
||||
}
|
||||
return selected;
|
||||
};
|
||||
|
||||
const readBounded = async (
|
||||
const readAll = async (
|
||||
stream: Readable,
|
||||
maximum: number,
|
||||
signal?: AbortSignal,
|
||||
): Promise<Buffer> => {
|
||||
const chunks: Buffer[] = [];
|
||||
@@ -354,13 +285,6 @@ const readBounded = async (
|
||||
abortIfNeeded(signal);
|
||||
const chunk = streamChunkToBuffer(raw);
|
||||
bytes += chunk.length;
|
||||
if (bytes > maximum) {
|
||||
stream.destroy();
|
||||
throw new ArtifactReaderFailure(
|
||||
"integrity",
|
||||
"Artifact entry grew after inventory and exceeded the selected text read size",
|
||||
);
|
||||
}
|
||||
chunks.push(chunk);
|
||||
}
|
||||
return Buffer.concat(chunks, bytes);
|
||||
@@ -391,23 +315,6 @@ const relevantKind = (path: string): JavaScriptArtifactFileKind | undefined => {
|
||||
return undefined;
|
||||
};
|
||||
|
||||
const filePriority = (kind: JavaScriptArtifactFileKind): number => {
|
||||
switch (kind) {
|
||||
case "package-json":
|
||||
return 0;
|
||||
case "html":
|
||||
return 1;
|
||||
case "javascript":
|
||||
return 2;
|
||||
case "json":
|
||||
return 3;
|
||||
case "source-map":
|
||||
return 4;
|
||||
case "native-addon":
|
||||
return 5;
|
||||
}
|
||||
};
|
||||
|
||||
const isFilesystemAsar = (entry: ArtifactEntry, path: string): boolean =>
|
||||
entry.kind === "file" &&
|
||||
path.toLowerCase().endsWith(".asar") &&
|
||||
|
||||
@@ -6,10 +6,7 @@ import {
|
||||
import type { JavaScriptArtifactAnalysis } from "./JavaScriptArtifactAnalysisTypes.js";
|
||||
import type { JavaScriptArtifactFileSet } from "./JavaScriptArtifactFiles.js";
|
||||
import { JavaScriptArtifactGraphAccumulator } from "./JavaScriptArtifactGraphAccumulator.js";
|
||||
import {
|
||||
javaScriptAnalysisLimits,
|
||||
type JavaScriptArtifactGraphContext,
|
||||
} from "./JavaScriptArtifactGraphContext.js";
|
||||
import type { JavaScriptArtifactGraphContext } from "./JavaScriptArtifactGraphContext.js";
|
||||
import {
|
||||
addJavaScriptHtmlRoles,
|
||||
addJavaScriptSourceMapOriginals,
|
||||
@@ -22,7 +19,6 @@ import {
|
||||
import {
|
||||
completeApplicationCoverage,
|
||||
partialApplicationCoverage,
|
||||
truncatedApplicationCoverage,
|
||||
} from "../domain/javascriptApplicationEvidenceSchemas.js";
|
||||
import {
|
||||
addJavaScriptArtifactContainers,
|
||||
@@ -31,14 +27,13 @@ import {
|
||||
addJavaScriptPackageNodes,
|
||||
createJavaScriptArtifactRootNode,
|
||||
} from "./JavaScriptArtifactGraphStructure.js";
|
||||
import { JAVASCRIPT_APPLICATION_RESOURCE_LIMITS } from "./JavaScriptArtifactReconstructionInput.js";
|
||||
import { addElectronBoundaries } from "./ElectronBoundaryGraph.js";
|
||||
import {
|
||||
classifyElectronIpcPairings,
|
||||
collectElectronIpcRecords,
|
||||
} from "./ElectronBoundaryAnalysis.js";
|
||||
|
||||
/** Project bounded artifact and AST facts into JavaScript Application Graph. */
|
||||
/** Project artifact and AST facts into JavaScript Application Graph. */
|
||||
export const buildJavaScriptArtifactGraph = (
|
||||
snapshot: ArtifactInventorySnapshot,
|
||||
fileSet: JavaScriptArtifactFileSet,
|
||||
@@ -85,7 +80,6 @@ export const buildJavaScriptArtifactGraph = (
|
||||
};
|
||||
|
||||
const graphCoverage = (context: JavaScriptArtifactGraphContext) => {
|
||||
const exactLimitOmissions = context.fileSet.limit_omitted_text_files;
|
||||
const sourceMapPolicyGap = context.analysis.source_maps.some(
|
||||
({ status }) => status === "invalid",
|
||||
);
|
||||
@@ -104,40 +98,9 @@ const graphCoverage = (context: JavaScriptArtifactGraphContext) => {
|
||||
malformedStructuredData ||
|
||||
partialJavaScript;
|
||||
if (context.analysis.truncated_scopes > 0)
|
||||
return truncatedApplicationCoverage(
|
||||
reconstructionLimits(),
|
||||
truncationOmittedCount(context),
|
||||
);
|
||||
if (exactLimitOmissions > 0)
|
||||
return truncatedApplicationCoverage(
|
||||
reconstructionLimits(),
|
||||
exactLimitOmissions,
|
||||
);
|
||||
if (unknownGap)
|
||||
return partialApplicationCoverage(reconstructionLimits(), null);
|
||||
return completeApplicationCoverage(reconstructionLimits());
|
||||
};
|
||||
|
||||
const truncationOmittedCount = (
|
||||
context: JavaScriptArtifactGraphContext,
|
||||
): number | null => {
|
||||
const staticTruncations = context.analysis.files.filter(
|
||||
({ javascript }) => javascript?.parse_status === "truncated",
|
||||
);
|
||||
if (staticTruncations.length > 0) return null;
|
||||
const semanticTruncations = context.analysis.files.flatMap(({ semantic }) =>
|
||||
semantic?.ir.coverage.status === "truncated" ? [semantic.ir.coverage] : [],
|
||||
);
|
||||
const sourceMapTruncations = context.analysis.source_maps.filter(
|
||||
({ status }) => status === "truncated",
|
||||
);
|
||||
if (sourceMapTruncations.length > 0) return null;
|
||||
if (semanticTruncations.length !== context.analysis.truncated_scopes)
|
||||
return null;
|
||||
const omissions = semanticTruncations.map(({ omittedCount }) => omittedCount);
|
||||
return omissions.some((omitted) => omitted === null)
|
||||
? null
|
||||
: omissions.reduce<number>((total, omitted) => total + (omitted ?? 0), 0);
|
||||
return partialApplicationCoverage([], null);
|
||||
if (unknownGap) return partialApplicationCoverage([], null);
|
||||
return completeApplicationCoverage();
|
||||
};
|
||||
|
||||
const graphLimitations = (
|
||||
@@ -202,17 +165,3 @@ const graphLimitations = (
|
||||
]),
|
||||
];
|
||||
};
|
||||
|
||||
const reconstructionLimits = () => [
|
||||
{
|
||||
name: "max-text-files",
|
||||
value: JAVASCRIPT_APPLICATION_RESOURCE_LIMITS.maxTextFiles,
|
||||
unit: "items" as const,
|
||||
},
|
||||
{
|
||||
name: "max-total-text-bytes",
|
||||
value: JAVASCRIPT_APPLICATION_RESOURCE_LIMITS.maxTotalTextBytes,
|
||||
unit: "bytes" as const,
|
||||
},
|
||||
...javaScriptAnalysisLimits(),
|
||||
];
|
||||
|
||||
@@ -2,7 +2,6 @@ import type { ArtifactInventorySnapshot } from "./ArtifactInventory.js";
|
||||
import {
|
||||
completeApplicationCoverage,
|
||||
partialApplicationCoverage,
|
||||
truncatedApplicationCoverage,
|
||||
type ApplicationGraphEvidence,
|
||||
} from "../domain/javascriptApplicationEvidenceSchemas.js";
|
||||
import type { ApplicationNode } from "../domain/javascriptApplicationGraph.js";
|
||||
@@ -214,10 +213,8 @@ export const javascriptAnalysisCoverage = (
|
||||
>,
|
||||
): JavaScriptArtifactGraphCoverage =>
|
||||
analysis.parse_status === "complete"
|
||||
? completeApplicationCoverage(javaScriptAnalysisLimits())
|
||||
: analysis.parse_status === "truncated"
|
||||
? truncatedApplicationCoverage(javaScriptAnalysisLimits(), null)
|
||||
: partialApplicationCoverage(javaScriptAnalysisLimits(), null);
|
||||
? completeApplicationCoverage()
|
||||
: partialApplicationCoverage([], null);
|
||||
|
||||
/** Stable identity for a fact scoped to one exact artifact. */
|
||||
export const artifactLocalIdentity = (
|
||||
@@ -228,8 +225,8 @@ export const artifactLocalIdentity = (
|
||||
strategy: "artifact-local-key" as const,
|
||||
stability: "artifact-version" as const,
|
||||
artifact_sha256: sha256,
|
||||
namespace: namespace.slice(0, 512),
|
||||
key: key.slice(0, 4_096),
|
||||
namespace,
|
||||
key,
|
||||
});
|
||||
|
||||
/** Select the graph node kind for one inventoried relevant file. */
|
||||
@@ -344,17 +341,3 @@ export const sourceNodeFor = (
|
||||
moduleKey === null
|
||||
? context.sourceModuleNodes.get(path)
|
||||
: context.moduleNodes.get(moduleLookupKey(path, moduleKey));
|
||||
|
||||
/** Actual parser safety bounds committed to graph evidence coverage. */
|
||||
export const javaScriptAnalysisLimits = () => [
|
||||
{
|
||||
name: "max-ast-nodes",
|
||||
value: 2_000_000,
|
||||
unit: "items" as const,
|
||||
},
|
||||
{
|
||||
name: "max-parse-milliseconds",
|
||||
value: 30_000,
|
||||
unit: "milliseconds" as const,
|
||||
},
|
||||
];
|
||||
|
||||
@@ -8,9 +8,8 @@ import {
|
||||
import { artifactObservationEvidence } from "./JavaScriptArtifactGraphEvidence.js";
|
||||
import {
|
||||
completeApplicationCoverage,
|
||||
truncatedApplicationCoverage,
|
||||
partialApplicationCoverage,
|
||||
} from "../domain/javascriptApplicationEvidenceSchemas.js";
|
||||
import { javaScriptAnalysisLimits } from "./JavaScriptArtifactGraphContext.js";
|
||||
import { resolveArtifactPathByContext } from "./JavaScriptArtifactPathResolution.js";
|
||||
|
||||
/** Project HTML renderer entrypoints and their local script assets. */
|
||||
@@ -94,10 +93,7 @@ export const addJavaScriptSourceMapOriginals = (
|
||||
if (mapFile === undefined || mapNode === undefined) continue;
|
||||
const coverage =
|
||||
sourceMap.status === "truncated"
|
||||
? truncatedApplicationCoverage(
|
||||
javaScriptAnalysisLimits(),
|
||||
sourceMap.omitted_sources,
|
||||
)
|
||||
? partialApplicationCoverage([], null)
|
||||
: completeApplicationCoverage();
|
||||
for (const original of sourceMap.sources) {
|
||||
const node = context.accumulator.addNode({
|
||||
|
||||
@@ -5,8 +5,32 @@ import {
|
||||
type ResolveArtifactPathInput,
|
||||
} from "./JavaScriptArtifactPathResolution.js";
|
||||
import type { JavaScriptArtifactFile } from "./JavaScriptArtifactFiles.js";
|
||||
import { artifactLocalIdentity } from "./JavaScriptArtifactGraphContext.js";
|
||||
import { applicationNodeIdentitySchema } from "../domain/javascriptApplicationEvidenceSchemas.js";
|
||||
import { analyzeJavaScriptStaticSource } from "../domain/javascriptStaticAnalysis.js";
|
||||
|
||||
describe("artifact-local graph identity", () => {
|
||||
it("preserves complete long namespace and key values", () => {
|
||||
const digest = "a".repeat(64);
|
||||
const namespacePrefix = "n".repeat(512);
|
||||
const keyPrefix = "k".repeat(4_096);
|
||||
const first = artifactLocalIdentity(
|
||||
digest,
|
||||
`${namespacePrefix}a`,
|
||||
`${keyPrefix}a`,
|
||||
);
|
||||
const second = artifactLocalIdentity(
|
||||
digest,
|
||||
`${namespacePrefix}b`,
|
||||
`${keyPrefix}b`,
|
||||
);
|
||||
|
||||
expect(applicationNodeIdentitySchema.parse(first)).toEqual(first);
|
||||
expect(applicationNodeIdentitySchema.parse(second)).toEqual(second);
|
||||
expect(first).not.toEqual(second);
|
||||
});
|
||||
});
|
||||
|
||||
describe("contextual JavaScript artifact path resolution", () => {
|
||||
it.each([
|
||||
["main.js", "app/main.js"],
|
||||
@@ -249,7 +273,7 @@ describe("contextual JavaScript module identity", () => {
|
||||
window.loadURL("file:///renderer/index.html");
|
||||
`,
|
||||
{
|
||||
maxAstNodes: 10_000,
|
||||
maxAstNodes: Number.POSITIVE_INFINITY,
|
||||
deadline: Number.POSITIVE_INFINITY,
|
||||
now: () => 0,
|
||||
},
|
||||
@@ -315,6 +339,6 @@ const file = (
|
||||
unpacked: false,
|
||||
text:
|
||||
text === null
|
||||
? { included: false, reason: "file-limit" }
|
||||
? { included: false, reason: "invalid-utf8" }
|
||||
: { included: true, value: text },
|
||||
});
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { lstat, realpath } from "node:fs/promises";
|
||||
import { performance } from "node:perf_hooks";
|
||||
|
||||
import { AsarArtifactReader } from "../artifacts/AsarArtifactReader.js";
|
||||
import {
|
||||
@@ -34,10 +33,7 @@ export interface JavaScriptArtifactReconstructionResult {
|
||||
readonly statistics: {
|
||||
readonly relevant_files: number;
|
||||
readonly nested_asar_containers: number;
|
||||
readonly text_files_selected: number;
|
||||
readonly text_bytes_read: number;
|
||||
readonly omitted_text_files: number;
|
||||
readonly limit_omitted_text_files: number;
|
||||
readonly invalid_utf8_files: number;
|
||||
readonly parsed_javascript_files: number;
|
||||
readonly visited_ast_nodes: number;
|
||||
@@ -67,9 +63,7 @@ export const reconstructJavaScriptArtifact = async (
|
||||
const reader = createReader(path, format);
|
||||
try {
|
||||
const files = await readJavaScriptArtifactFiles(reader, snapshot, signal);
|
||||
const analysis = analyzeJavaScriptArtifactFiles(files, () =>
|
||||
performance.now(),
|
||||
);
|
||||
const analysis = analyzeJavaScriptArtifactFiles(files);
|
||||
abortIfNeeded(signal);
|
||||
const graph = buildJavaScriptArtifactGraph(snapshot, files, analysis);
|
||||
const semanticGraph = buildJavaScriptSemanticGraph({
|
||||
@@ -89,10 +83,7 @@ export const reconstructJavaScriptArtifact = async (
|
||||
statistics: {
|
||||
relevant_files: files.files.length,
|
||||
nested_asar_containers: files.containers.length,
|
||||
text_files_selected: files.text_files_selected,
|
||||
text_bytes_read: files.text_bytes_read,
|
||||
omitted_text_files: files.omitted_text_files,
|
||||
limit_omitted_text_files: files.limit_omitted_text_files,
|
||||
invalid_utf8_files: files.invalid_utf8_files,
|
||||
parsed_javascript_files: analysis.files.filter(
|
||||
({ javascript }) => javascript !== null,
|
||||
|
||||
@@ -1,14 +1,5 @@
|
||||
import { z } from "zod";
|
||||
|
||||
/** Resource safeguards for local artifact traversal and static parsing. */
|
||||
export const JAVASCRIPT_APPLICATION_RESOURCE_LIMITS = {
|
||||
maxTextFiles: 5_000,
|
||||
maxTotalTextBytes: 128 * 1_024 * 1_024,
|
||||
maxTextFileBytes: 8 * 1_024 * 1_024,
|
||||
maxAstNodes: 2_000_000,
|
||||
maxParseMilliseconds: 30_000,
|
||||
} as const;
|
||||
|
||||
/** Local ASAR/directory reconstruction request. */
|
||||
export const javascriptArtifactReconstructionInputSchema = z.strictObject({
|
||||
input_path: z.string().min(1),
|
||||
|
||||
@@ -179,7 +179,6 @@ export const prepareReplayPlan = async (
|
||||
: {
|
||||
path: input.reproducer_export.path,
|
||||
include_sources: input.reproducer_export.include_sources,
|
||||
authority: "evidence_write" as const,
|
||||
},
|
||||
};
|
||||
const publicPlan: ReplayPlan = {
|
||||
|
||||
@@ -105,11 +105,7 @@ export const runControlledReplayValidated = async (
|
||||
),
|
||||
);
|
||||
|
||||
const exportContext = await authorizeReproducerExport(
|
||||
dependencies.authority,
|
||||
input,
|
||||
prepared.value,
|
||||
);
|
||||
const exportContext = await authorizeReproducerExport(input);
|
||||
if (!exportContext.ok) return exportContext;
|
||||
|
||||
const executed = await executeValidatedReplay({
|
||||
@@ -201,45 +197,12 @@ interface ReproducerExportContext {
|
||||
readonly includeSources: boolean;
|
||||
}
|
||||
|
||||
const authorizeReproducerExport = async (
|
||||
authority: PermissionAuthority | undefined,
|
||||
const authorizeReproducerExport = (
|
||||
input: ControlledReplayExecutionInput,
|
||||
prepared: Awaited<ReturnType<typeof prepareReplayPlan>>,
|
||||
): Promise<Result<ReproducerExportContext | undefined, AnalysisError>> => {
|
||||
): Result<ReproducerExportContext | undefined, AnalysisError> => {
|
||||
if (input.reproducer_export === undefined) return ok(undefined);
|
||||
if (authority === undefined)
|
||||
return err(
|
||||
new AnalysisCapabilityUnavailableError(
|
||||
"rea-javascript-replay",
|
||||
OPERATION,
|
||||
"JavaScript replay permission policy is not configured",
|
||||
),
|
||||
);
|
||||
const exportAuthorized = await authority.authorize(
|
||||
{
|
||||
capability: "evidence_write",
|
||||
roots: [input.reproducer_export.path],
|
||||
executables: [],
|
||||
environment_names: [],
|
||||
network: "none",
|
||||
mount: false,
|
||||
operation_identity: `${OPERATION}:reproducer:${prepared.publicPlan.plan_digest}`,
|
||||
},
|
||||
"write",
|
||||
);
|
||||
if (!exportAuthorized.ok)
|
||||
return err(projectPermissionFailure(exportAuthorized.error));
|
||||
const canonicalExportPath = exportAuthorized.value.request.roots[0];
|
||||
if (canonicalExportPath !== input.reproducer_export.path)
|
||||
return err(
|
||||
new AnalysisInputError(`${OPERATION}:reproducer_export:path`, {
|
||||
cause: new TypeError(
|
||||
`Use the canonical reproducer path: ${canonicalExportPath ?? "unavailable"}`,
|
||||
),
|
||||
}),
|
||||
);
|
||||
return ok({
|
||||
path: canonicalExportPath,
|
||||
path: input.reproducer_export.path,
|
||||
includeSources: input.reproducer_export.include_sources === true,
|
||||
});
|
||||
};
|
||||
|
||||
@@ -4,7 +4,6 @@ import { access, realpath } from "node:fs/promises";
|
||||
import { delimiter, join } from "node:path";
|
||||
|
||||
const TOOL_NAMES = ["node", "npm", "npx"] as const;
|
||||
const MAX_CANDIDATES_PER_TOOL = 16;
|
||||
const MAX_OUTPUT_BYTES = 65_536;
|
||||
const MAX_DIAGNOSTIC_BYTES = 4_096;
|
||||
const PROBE_CONCURRENCY = 4;
|
||||
@@ -142,7 +141,6 @@ const discoverToolCandidates = async (
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (candidates.length >= MAX_CANDIDATES_PER_TOOL) break;
|
||||
}
|
||||
return candidates;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user