fix(contracts): report malformed validator results as THROWN instead of throwing (07-06)
invokeValidator spread result.issues outside its try and treated any truthy success as valid, so a hand-written validator could throw past the executor or pass unchecked data. The result is now read inside the try and checked. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
710436bc2e
commit
0bcee348c5
@@ -72,22 +72,29 @@ export function invokeValidator<T>(
|
||||
validator: RuntimeValidator<T>,
|
||||
value: unknown,
|
||||
): ValidatorInvocation<T> {
|
||||
let result: RuntimeValidationResult<T>;
|
||||
// 07-06. Reading the result stays inside the try: a hand-written validator
|
||||
// may return any shape, including throwing getters.
|
||||
try {
|
||||
result = validator.safeParse(value);
|
||||
const result: unknown = validator.safeParse(value);
|
||||
if (!result || typeof result !== "object") throw new TypeError();
|
||||
const candidate = result as Readonly<Record<string, unknown>>;
|
||||
if (candidate.success === true) {
|
||||
return Object.freeze({
|
||||
outcome: "VALID" as const,
|
||||
value: candidate.data as T,
|
||||
});
|
||||
}
|
||||
const issues = candidate.issues;
|
||||
if (candidate.success !== false || !Array.isArray(issues)) {
|
||||
throw new TypeError();
|
||||
}
|
||||
return Object.freeze({
|
||||
outcome: "INVALID" as const,
|
||||
issues: Object.freeze([...(issues as RuntimeValidationIssue[])]),
|
||||
});
|
||||
} catch {
|
||||
return Object.freeze({ outcome: "THROWN" as const });
|
||||
}
|
||||
if (!result || typeof result !== "object" || !("success" in result)) {
|
||||
return Object.freeze({ outcome: "THROWN" as const });
|
||||
}
|
||||
if (result.success) {
|
||||
return Object.freeze({ outcome: "VALID" as const, value: result.data });
|
||||
}
|
||||
return Object.freeze({
|
||||
outcome: "INVALID" as const,
|
||||
issues: Object.freeze([...(result.issues ?? [])]),
|
||||
});
|
||||
}
|
||||
|
||||
export type MappingResult<T> =
|
||||
|
||||
@@ -3,7 +3,9 @@ import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
ContractContributionError,
|
||||
composeContractContributions,
|
||||
invokeValidator,
|
||||
type InstalledContractContribution,
|
||||
type RuntimeValidator,
|
||||
} from "../../src/contracts/external-contract-runtime.ts";
|
||||
import { TEST_CONTRACT_CONTRIBUTION } from "../helpers/external-contract-fixture.ts";
|
||||
|
||||
@@ -83,3 +85,34 @@ describe("external contract contribution composition", () => {
|
||||
expect(error.reason).toContain("method");
|
||||
});
|
||||
});
|
||||
|
||||
describe("validator invocation (07-06)", () => {
|
||||
const returning = (result: unknown): RuntimeValidator<unknown> => ({
|
||||
schemaId: "Malformed",
|
||||
safeParse: () => result as never,
|
||||
});
|
||||
|
||||
it.each([
|
||||
["non-array issues", { success: false, issues: 5 }],
|
||||
["object issues", { success: false, issues: {} }],
|
||||
["non-boolean success", { success: "no", data: "unchecked" }],
|
||||
["throwing issues getter", {
|
||||
success: false,
|
||||
get issues(): never {
|
||||
throw new Error("hostile");
|
||||
},
|
||||
}],
|
||||
])("reports THROWN for a result with %s", (_label, result) => {
|
||||
expect(invokeValidator(returning(result), {})).toEqual({ outcome: "THROWN" });
|
||||
});
|
||||
|
||||
it("keeps well-formed results", () => {
|
||||
expect(invokeValidator(returning({ success: true, data: 1 }), {})).toEqual({
|
||||
outcome: "VALID",
|
||||
value: 1,
|
||||
});
|
||||
expect(
|
||||
invokeValidator(returning({ success: false, issues: [{ path: [], code: "X" }] }), {}),
|
||||
).toEqual({ outcome: "INVALID", issues: [{ path: [], code: "X" }] });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user