fix: report malformed application evidence as input errors

This commit is contained in:
morluto
2026-10-10 03:52:35 +08:00
parent 55a46f7c71
commit 218c13160e
15 changed files with 613 additions and 165 deletions
+13 -7
View File
@@ -140,6 +140,18 @@ export const evidenceSchema = evidenceBaseSchema.superRefine(
validateAnalysisProfileProvider,
);
const authenticatedEvidenceSchema = evidenceSchema.superRefine(
(evidence, context) => {
const { evidence_id: evidenceId, ...withoutId } = evidence;
if (computeEvidenceId(withoutId) !== evidenceId)
context.addIssue({
code: "custom",
path: ["evidence_id"],
message: "Evidence semantic identifier does not match its record",
});
},
);
/** Complete observation whose normalized payload retains its operation-specific type. */
export type Evidence<Result extends JsonValue = JsonValue> = z.infer<
typeof evidenceSchema
@@ -224,13 +236,7 @@ export const parseEvidence = (input: unknown): Evidence => {
? immutableEvidenceSnapshots.get(input)
: undefined;
if (immutable !== undefined) return immutable;
const evidence = evidenceSchema.parse(input);
const { evidence_id: evidenceId, ...withoutId } = evidence;
if (computeEvidenceId(withoutId) !== evidenceId)
throw new TypeError(
"Evidence semantic identifier does not match its record",
);
return evidence;
return authenticatedEvidenceSchema.parse(input);
};
/** Authenticate and seal a ledger-owned snapshot; external mutable values are copied first. */
+14 -1
View File
@@ -243,7 +243,20 @@ export const parseEvidenceBundle = (input: unknown): EvidenceBundle => {
throw new TypeError("Evidence bundle contains duplicate record IDs");
validateUnknownGraph(parsed.unknowns, parsed.records);
const canonical = createEvidenceBundle(
parsed.records.map(parseEvidence),
parsed.records.map((record, index) => {
try {
return parseEvidence(record);
} catch (cause: unknown) {
if (cause instanceof z.ZodError)
throw new z.ZodError(
cause.issues.map((issue) => ({
...issue,
path: ["records", index, ...issue.path],
})),
);
throw cause;
}
}),
parsed.unknowns,
);
if (JSON.stringify(parsed) !== JSON.stringify(canonical))