fix(evidence): route web script export and WASM comparison through shared Evidence inputs (#15385)

* fix(evidence): resolve web script and WASM comparison sources through the shared Evidence input owner

export_web_scripts accepts its capture as declared Evidence input: a
complete record, a retained reference, or a retained inspect_analysis_view
summary, which reads its retained parent. compare_wasm_interfaces admits
both sides through resolveEvidenceRequest, so a malformed side is a
bounded evidence_integrity_mismatch and a summary reads its parent. The
summary-to-parent rule lives in the shared resolver as completeProducers.
A catalog test checks that every declared Evidence input resolves a
retained reference through the shared resolver.

Refs #15341

* fix(evidence): retain an inline web script capture before publishing and commit the schema summary
This commit is contained in:
morluto authored and GitHub committed 2026-11-30 20:42:45 -05:00
1 parent 1fd22f9783
commit fbfcad3152
16 files changed
+702 -122

No files matched your search

+127 -30
View File
@@ -12,7 +12,21 @@ import {
artifactReaderTargetFailure,
} from "../resources/index.js";
import { publishWebScripts } from "./PublishWebScripts.js";
import { selectScriptCapture } from "../domain/webScriptCaptureAdapters.js";
import {
selectEvidenceScriptCapture,
selectScriptCapture,
UNSUPPORTED_SCRIPT_CAPTURE_MESSAGE,
} from "../domain/webScriptCaptureAdapters.js";
import { readEvidence } from "../domain/evidenceValidation.js";
import { invariantViolation } from "../domain/internalInvariant.js";
import { unknownTarget } from "../domain/targetIdentity.js";
import type { EvidenceInputPath } from "../domain/evidenceInputContract.js";
import {
resolveEvidenceRequest,
type EvidenceLookup,
} from "./EvidenceInputResolver.js";
import { recordEvidenceSources } from "./investigation/recordEvidenceSources.js";
import type { EvidenceWriter } from "./investigation/InvestigationRecordPort.js";
import {
AnalysisAccessDeniedError,
AnalysisInputError,
@@ -26,13 +40,13 @@ import { WindowsChangeTimeConfigurationError } from "../config/windowsChangeTime
import { ConfigurationError } from "../domain/configurationErrors.js";
import { createEvidence, type Evidence } from "../domain/evidence.js";
import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js";
import { analysisInputErrorFromIssues } from "../domain/inputIssueProjection.js";
import { ProviderAdapterError } from "../domain/providerAdapterError.js";
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
import { err, ok, type Result } from "../domain/result.js";
import { safeParseJson } from "../domain/safeJson.js";
import {
exportWebScriptsInputSchema,
WEB_SCRIPT_CAPTURE_PRODUCERS,
type ExportWebScriptsInput,
} from "../domain/webScriptExport.js";
import { WebScriptExportError } from "../domain/webScriptExportError.js";
@@ -68,43 +82,67 @@ interface CaptureRead {
}
interface PreparedCapture {
readonly bytes: Buffer;
/** The selected file's bytes; undefined for a capture supplied as Evidence. */
readonly bytes: Buffer | undefined;
readonly loaded: ReturnType<typeof selectScriptCapture>;
}
/** Export one local capture through the shared CLI/MCP application workflow. */
/**
* Export one capture through the shared CLI/MCP application workflow. A
* capture supplied as Evidence resolves through the shared Evidence input
* owner: a retained reference, or a summary that names its retained parent.
* The session retains that capture before anything is written, so a full
* retention allowance refuses the export instead of hiding a completed one,
* and a later failure never leaves the export without its source.
*/
export const exportWebScripts = async (
rawInput: unknown,
resources: ArtifactResourceScope,
options: ExecutionOptions = {},
evidence: {
readonly lookup?: EvidenceLookup | undefined;
readonly recordEvidence?: EvidenceWriter["recordEvidence"] | undefined;
} = {},
): Promise<Result<Evidence, AnalysisError>> => {
const input = exportWebScriptsInputSchema.safeParse(rawInput);
return input.success
? exportWebScriptsValidated(input.data, resources, options)
: err(
analysisInputErrorFromIssues(OPERATION, input.error.issues, rawInput),
);
};
/** Publish input already parsed by a named adapter contract. */
export const exportWebScriptsValidated = async (
input: ExportWebScriptsInput,
resources: ArtifactResourceScope,
options: ExecutionOptions = {},
): Promise<Result<Evidence, AnalysisError>> => {
const resolved = resolveEvidenceRequest(
OPERATION,
rawInput,
exportWebScriptsInputSchema,
evidence.lookup,
{
...(options.signal === undefined ? {} : { signal: options.signal }),
completeProducers: WEB_SCRIPT_CAPTURE_PRODUCERS,
readEvidence: admitCapture,
},
);
if (!resolved.ok) return resolved;
const { capture, ...selection } = resolved.value.input;
const input: ExportWebScriptsInput = selection;
if (capture !== undefined) {
const retained = recordEvidenceSources(evidence.recordEvidence, [capture]);
if (!retained.ok) return retained;
}
try {
if (options.signal?.aborted)
return err(cancelledBy(OPERATION, options.signal));
const prepared = await prepareCapture(
input.capture_path,
resources,
options.signal,
);
const prepared =
capture !== undefined
? evidenceCapture(capture)
: input.capture_path !== undefined
? await prepareCapture(input.capture_path, resources, options.signal)
: undefined;
if (prepared === undefined)
throw invariantViolation(
"The export_web_scripts input admits exactly one capture selector",
);
if (!prepared.ok) return prepared;
const { bytes, loaded } = prepared.value;
const { loaded } = prepared.value;
if (options.signal?.aborted)
return err(cancelledBy(OPERATION, options.signal));
const sha256 = sha256Hex(bytes);
const sha256 =
prepared.value.bytes === undefined
? null
: sha256Hex(prepared.value.bytes);
const result = await publishWebScripts(input, loaded, sha256, {
resources,
...(options.signal === undefined ? {} : { signal: options.signal }),
@@ -117,13 +155,25 @@ export const exportWebScriptsValidated = async (
);
return ok(
createEvidence(
{ path: input.capture_path, sha256, format: "file" },
input.capture_path === undefined || sha256 === null
? unknownTarget("derived-from-linked-evidence")
: { path: input.capture_path, sha256, format: "file" },
PROVIDER,
{
predicateType: "rea.web-script-export",
operation: OPERATION,
parameters: {
capture_path: input.capture_path,
...(input.capture_path === undefined
? {}
: { capture_path: input.capture_path }),
...(capture === undefined
? {}
: {
capture: {
kind: "retained-evidence",
evidence_id: capture.evidence_id,
},
}),
output_directory: input.output_directory,
},
result,
@@ -142,6 +192,49 @@ export const exportWebScriptsValidated = async (
}
};
/** Authenticate a capture record and require one of its producers. */
const admitCapture = (
input: unknown,
path: EvidenceInputPath,
): Result<Evidence, AnalysisError> => {
const record = readEvidence(input);
if (
!record.ok ||
(WEB_SCRIPT_CAPTURE_PRODUCERS as readonly string[]).includes(
record.value.operation,
)
)
return record;
const message = `Expected inspect_web_page or capture_browser_scenario Evidence, received ${record.value.operation}`;
return err(
new AnalysisInputError(OPERATION, { userMessage: message }, [
{ path: [...path], reason: "invalid_value", message },
]),
);
};
/** The capture an admitted record retains; it has no file bytes of its own. */
const evidenceCapture = (
evidence: Evidence,
): Result<PreparedCapture, AnalysisError> => {
const loaded = selectEvidenceScriptCapture(evidence);
return loaded === undefined
? err(
new AnalysisInputError(
OPERATION,
{ userMessage: UNSUPPORTED_SCRIPT_CAPTURE_MESSAGE },
[
{
path: ["capture"],
reason: "invalid_format",
message: UNSUPPORTED_SCRIPT_CAPTURE_MESSAGE,
},
],
),
)
: ok({ bytes: undefined, loaded });
};
const prepareCapture = async (
path: string,
resources: ArtifactResourceScope,
@@ -189,7 +282,11 @@ const mapExportFailure = (
? cancelledBy(OPERATION, carriedReason(cause.cancellation.reason), {
partialObservation: cause.partialObservation,
})
: (artifactReaderTargetFailure(OPERATION, input.capture_path, cause) ??
: (artifactReaderTargetFailure(
OPERATION,
input.capture_path ?? input.output_directory,
cause,
) ??
new WebScriptExportError(
artifactOperationReason(cause),
input.output_directory,
@@ -207,14 +304,14 @@ const mapExportFailure = (
)
return new WebScriptExportError(
"io",
`${input.capture_path} → ${input.output_directory}`,
`${input.capture_path ?? "capture Evidence"} → ${input.output_directory}`,
cause.message,
{ cause },
);
return new ProviderAdapterError(PROVIDER.id, OPERATION, {
cause,
diagnostics: {
capture_path: input.capture_path,
capture_path: input.capture_path ?? null,
output_directory: input.output_directory,
error_name: cause instanceof Error ? cause.name : "UnknownError",
error_message: