Heuristic:A5c ai Babysitter Canonicalization Fail Closed Pattern
| Knowledge Sources | |
|---|---|
| Domains | Security, Policy_Enforcement |
| Last Updated | 2026-10-23 00:00 GMT |
Overview
Canonical argument serialization must fail closed by throwing on non-finite numbers (NaN, ±Infinity) rather than silently coercing them to `null` via `JSON.stringify`.
Description
The `canonicalizeArgs` function is the single shared canonicalizer used by both the proxy producer and every policy gate. It must be total (defined for every JSON-representable value), loss-preserving (never fold distinct inputs to the same bytes), and byte-identical across paths. The critical heuristic is the fail-closed pattern: when encountering a non-finite number (`NaN`, `+Infinity`, `-Infinity`), the canonicalizer throws an explicit error rather than allowing `JSON.stringify` to silently coerce it to `null` — which would be a lossy fold, mapping distinct values to the same bytes. Similarly, `bigint`, `function`, and `symbol` types throw because they are not JSON-representable.
Usage
Use this heuristic when implementing or debugging proof-based policy enforcement canonicalization. The symptom of a violation is a policy gate accepting or rejecting incorrectly because two distinct argument values canonicalize to the same bytes (a collision due to silent coercion).
The Insight (Rule of Thumb)
- Action: Throw an explicit error for any value that is not JSON-representable (`NaN`, `±Infinity`, `bigint`, `function`, `symbol`).
- Value: `throw new Error('canonicalizeArgs: non-finite number is not JSON-representable')` for non-finite numbers; `throw new Error('canonicalizeArgs: value of type ${t} is not JSON-representable')` for other types.
- Trade-off: The canonicalizer never silently coerces. This means some inputs that `JSON.stringify` would accept (by mapping to `null`) are rejected. The security trade-off favors explicit denial over silent loss.
- Compatibility: Object keys are deep-sorted (order-independent), array order is preserved, string bytes and value types are preserved. No lowercase, trim, or unicode-fold.
Reasoning
`JSON.stringify` silently converts `NaN` and `±Infinity` to `null`. If the canonicalizer allowed this, two distinct argument values (e.g., `NaN` and `null`, or `Infinity` and `null`) would produce identical canonical bytes, causing the same policy hash. This breaks the loss-preserving property and creates a security hole where a denied argument could canonicalize to the same hash as an allowed one. The fail-closed pattern ensures that any input the canonicalizer cannot faithfully represent is explicitly rejected, preserving the integrity of policy hashes.
Code Evidence
Fail-closed logic from `packages/adapters/policy/src/canonicalize-args.ts:34-71`:
export function canonicalizeArgs(value: unknown): string {
return JSON.stringify(canonicalValue(value));
}
function canonicalValue(value: unknown): unknown {
if (value === null || value === undefined) return value;
const t = typeof value;
if (t === 'number') {
if (!Number.isFinite(value as number)) {
throw new Error('canonicalizeArgs: non-finite number is not JSON-representable');
}
return value;
}
if (t === 'string' || t === 'boolean') return value;
if (Array.isArray(value)) {
return value.map((el) => canonicalValue(el));
}
if (t === 'object') {
const obj = value as Record<string, unknown>;
const sorted: Record<string, unknown> = {};
for (const key of Object.keys(obj).sort()) {
sorted[key] = canonicalValue(obj[key]);
}
return sorted;
}
// bigint / function / symbol are not JSON-representable → deny (fail closed).
throw new Error(`canonicalizeArgs: value of type ${t} is not JSON-representable`);
}
Overriding rule from `packages/adapters/policy/src/canonicalize-args.ts:21-23`:
// OVERRIDING RULE: fail closed. The only throw path is the explicit deny for
// non-representable input (non-finite number); it MUST NOT silently coerce.