mirror of
https://github.com/morluto/rea.git
synced 2026-12-03 21:46:01 +00:00
refactor(evidence): share normalized result validation (#16029)
This commit is contained in:
1 parent
cc339895c7
commit
4a431f9302
5 files changed
+26
-52
No files matched your search
@@ -5,7 +5,7 @@ import {
|
||||
import type { ComparisonEvidenceOperation } from "./comparisonEvidenceIdentity.js";
|
||||
import type { Evidence } from "./evidence.js";
|
||||
import { EvidenceIntegrityError } from "./evidenceErrors.js";
|
||||
import { readEvidencePayload } from "./evidenceValidation.js";
|
||||
import { readEvidenceResult } from "./evidenceValidation.js";
|
||||
import {
|
||||
functionComparisonResultSchema,
|
||||
type FunctionComparisonResult,
|
||||
@@ -51,20 +51,12 @@ export function readComparisonResult(
|
||||
operation: ComparisonEvidenceOperation,
|
||||
): Result<ParsedComparisonEvidence, EvidenceIntegrityError> {
|
||||
if (operation === "compare_process_captures") {
|
||||
const parsed = readEvidencePayload(
|
||||
processCaptureComparisonSchema,
|
||||
evidence.normalized_result,
|
||||
{ within: ["normalized_result"] },
|
||||
);
|
||||
const parsed = readEvidenceResult(processCaptureComparisonSchema, evidence);
|
||||
if (!parsed.ok) return parsed;
|
||||
return ok({ operation, evidence, result: parsed.value });
|
||||
}
|
||||
if (operation === "compare_functions") {
|
||||
const parsed = readEvidencePayload(
|
||||
functionComparisonResultSchema,
|
||||
evidence.normalized_result,
|
||||
{ within: ["normalized_result"] },
|
||||
);
|
||||
const parsed = readEvidenceResult(functionComparisonResultSchema, evidence);
|
||||
if (!parsed.ok) return parsed;
|
||||
if (parsed.value.function_match.method === "same-evidence") {
|
||||
try {
|
||||
@@ -107,19 +99,11 @@ export function readComparisonResult(
|
||||
return ok({ operation, evidence, result: parsed.value });
|
||||
}
|
||||
if (operation === "compare_web_screenshots") {
|
||||
const parsed = readEvidencePayload(
|
||||
webScreenshotDiffSchema,
|
||||
evidence.normalized_result,
|
||||
{ within: ["normalized_result"] },
|
||||
);
|
||||
const parsed = readEvidenceResult(webScreenshotDiffSchema, evidence);
|
||||
if (!parsed.ok) return parsed;
|
||||
return ok({ operation, evidence, result: parsed.value });
|
||||
}
|
||||
const parsed = readEvidencePayload(
|
||||
artifactComparisonResultSchema,
|
||||
evidence.normalized_result,
|
||||
{ within: ["normalized_result"] },
|
||||
);
|
||||
const parsed = readEvidenceResult(artifactComparisonResultSchema, evidence);
|
||||
if (!parsed.ok) return parsed;
|
||||
const links = validateFindingLinks(evidence, [
|
||||
...parsed.value.changes.flatMap(({ evidence_links }) => evidence_links),
|
||||
|
||||
@@ -110,6 +110,15 @@ export const readEvidencePayload = <Output>(
|
||||
: err(integrityFailure(parsed.error, location));
|
||||
};
|
||||
|
||||
/** Validate the normalized result at its fixed location in an admitted record. */
|
||||
export const readEvidenceResult = <Output>(
|
||||
schema: z.ZodType<Output>,
|
||||
evidence: Evidence,
|
||||
): Result<Output, EvidenceIntegrityError> =>
|
||||
readEvidencePayload(schema, evidence.normalized_result, {
|
||||
within: ["normalized_result"],
|
||||
});
|
||||
|
||||
/**
|
||||
* Schema issue facts for an integrity failure. The issue path is reported
|
||||
* only when its position in the record (`within`) is known.
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { evidenceFileTarget, type Evidence } from "../evidence.js";
|
||||
import { AnalysisInputError } from "../analysisErrorCore.js";
|
||||
import { EvidenceIntegrityError } from "../evidenceErrors.js";
|
||||
import { readEvidence, readEvidencePayload } from "../evidenceValidation.js";
|
||||
import { readEvidence, readEvidenceResult } from "../evidenceValidation.js";
|
||||
import { err, ok, type Result } from "../result.js";
|
||||
import type { z } from "zod";
|
||||
|
||||
@@ -59,9 +59,7 @@ export const readManagedInspectionEvidence = <
|
||||
]),
|
||||
);
|
||||
}
|
||||
const payload = readEvidencePayload(schema, evidence.normalized_result, {
|
||||
within: ["normalized_result"],
|
||||
});
|
||||
const payload = readEvidenceResult(schema, evidence);
|
||||
if (!payload.ok) return payload;
|
||||
const result = payload.value;
|
||||
const artifactSha256 = result.artifact.sha256;
|
||||
|
||||
@@ -11,7 +11,7 @@ import { AnalysisInputError } from "../analysisErrorCore.js";
|
||||
import type { AnalysisError } from "../analysisErrorBase.js";
|
||||
import { EvidenceIntegrityError } from "../evidenceErrors.js";
|
||||
import { analysisInputErrorFromIssues } from "../inputIssueProjection.js";
|
||||
import { readEvidence, readEvidencePayload } from "../evidenceValidation.js";
|
||||
import { readEvidence, readEvidenceResult } from "../evidenceValidation.js";
|
||||
import { err, ok, type Result } from "../result.js";
|
||||
import { functionDossierSchema } from "../native/functionDossier.js";
|
||||
import { inspectMachoSchema } from "../native/nativeInspection.js";
|
||||
@@ -74,11 +74,7 @@ const readNativeObservation = (
|
||||
? format
|
||||
: null;
|
||||
if (evidence.operation === "inspect_macho") {
|
||||
const payload = readEvidencePayload(
|
||||
inspectMachoSchema,
|
||||
evidence.normalized_result,
|
||||
{ within: ["normalized_result"] },
|
||||
);
|
||||
const payload = readEvidenceResult(inspectMachoSchema, evidence);
|
||||
if (!payload.ok) return payload;
|
||||
return ok({
|
||||
evidence,
|
||||
@@ -100,11 +96,7 @@ const readNativeObservation = (
|
||||
});
|
||||
}
|
||||
if (evidence.operation === "analyze_function") {
|
||||
const payload = readEvidencePayload(
|
||||
functionDossierSchema,
|
||||
evidence.normalized_result,
|
||||
{ within: ["normalized_result"] },
|
||||
);
|
||||
const payload = readEvidenceResult(functionDossierSchema, evidence);
|
||||
if (!payload.ok) return payload;
|
||||
return ok({
|
||||
evidence,
|
||||
|
||||
@@ -23,7 +23,10 @@ import {
|
||||
} from "./analysisErrorCore.js";
|
||||
import { EvidenceIntegrityError } from "./evidenceErrors.js";
|
||||
import { analysisInputErrorFromIssues } from "./inputIssueProjection.js";
|
||||
import { readEvidencePayload } from "./evidenceValidation.js";
|
||||
import {
|
||||
readEvidencePayload,
|
||||
readEvidenceResult,
|
||||
} from "./evidenceValidation.js";
|
||||
import { functionDossierSchema } from "./native/functionDossier.js";
|
||||
import { assessFunctionCallCoverage } from "./functionCallCoverage.js";
|
||||
import {
|
||||
@@ -319,11 +322,7 @@ const indexScreenshotCaptures = (records: Iterable<Evidence>) => {
|
||||
source.predicate_type !== "rea.web-screenshot"
|
||||
)
|
||||
continue;
|
||||
const parsed = readEvidencePayload(
|
||||
webScreenshotSchema,
|
||||
source.normalized_result,
|
||||
{ within: ["normalized_result"] },
|
||||
);
|
||||
const parsed = readEvidenceResult(webScreenshotSchema, source);
|
||||
if (!parsed.ok) throw parsed.error;
|
||||
const sha256 = parsed.value.artifact.sha256;
|
||||
const capture = {
|
||||
@@ -549,22 +548,14 @@ const readValidatedSourceFacts = (
|
||||
const processSources: ValidatedSourceFacts["processes"][number][] = [];
|
||||
for (const source of sources) {
|
||||
if (claim.kind === "behavioral") {
|
||||
const capture = readEvidencePayload(
|
||||
processCaptureSchema,
|
||||
source.normalized_result,
|
||||
{ within: ["normalized_result"] },
|
||||
);
|
||||
const capture = readEvidenceResult(processCaptureSchema, source);
|
||||
if (!capture.ok) throw capture.error;
|
||||
processSources.push({
|
||||
evidence_id: source.evidence_id,
|
||||
capture: capture.value,
|
||||
});
|
||||
} else if (claim.kind === "structural-function") {
|
||||
const parsedDossier = readEvidencePayload(
|
||||
functionDossierSchema,
|
||||
source.normalized_result,
|
||||
{ within: ["normalized_result"] },
|
||||
);
|
||||
const parsedDossier = readEvidenceResult(functionDossierSchema, source);
|
||||
if (!parsedDossier.ok) throw parsedDossier.error;
|
||||
const dossier = parsedDossier.value;
|
||||
const callCoverage = assessFunctionCallCoverage(
|
||||
|
||||
Reference in new issue
Block a user