|
| 1 | +#!/usr/bin/python3 |
| 2 | +"""Fuzz the canonicalizer that catalog approvals are signed over. |
| 3 | +
|
| 4 | +canonical_json() produces the bytes an approval signature covers, so those bytes |
| 5 | +have to be a faithful, lossless encoding of the input. The property asserted |
| 6 | +here is a round trip: parsing the canonical output must reproduce the input. |
| 7 | +
|
| 8 | +That is stronger than checking for a crash, deliberately. The three RFC 8785 |
| 9 | +bugs found in the sibling agent-manifest canonicalizer in September 2026 were |
| 10 | +all silent. The sharpest was NFC normalization of object keys: two distinct keys |
| 11 | +normalized to one, the output carried that key twice, json.loads kept one of the |
| 12 | +pair, and a field disappeared from a document whose signature claimed to cover |
| 13 | +it. Nothing raised. cmcp's implementation does not have that bug, and this is |
| 14 | +what keeps it that way. |
| 15 | +
|
| 16 | +Structure is built from the fuzz data rather than by mutating JSON text, so the |
| 17 | +budget goes on key and value shapes (combining marks, surrogate pairs, control |
| 18 | +characters, integer boundaries) instead of on producing syntactically valid JSON. |
| 19 | +""" |
| 20 | +import json |
| 21 | +import math |
| 22 | +import sys |
| 23 | + |
| 24 | +import atheris |
| 25 | + |
| 26 | +with atheris.instrument_imports(): |
| 27 | + from cmcp_runtime.catalog.approval import canonical_json |
| 28 | + |
| 29 | +_MAX_DEPTH = 4 |
| 30 | +_MAX_ITEMS = 6 |
| 31 | + |
| 32 | + |
| 33 | +def _build(fdp: atheris.FuzzedDataProvider, depth: int = 0): |
| 34 | + if depth >= _MAX_DEPTH or fdp.remaining_bytes() == 0: |
| 35 | + return fdp.ConsumeUnicodeNoSurrogates(16) |
| 36 | + kind = fdp.ConsumeIntInRange(0, 7) |
| 37 | + if kind == 0: |
| 38 | + return None |
| 39 | + if kind == 1: |
| 40 | + return fdp.ConsumeBool() |
| 41 | + if kind == 2: |
| 42 | + # Straddle the safe-integer boundary on purpose: past it, RFC 8785 maps |
| 43 | + # two distinct integers to the same digits, so the canonicalizer has to |
| 44 | + # refuse rather than emit them. |
| 45 | + return fdp.ConsumeIntInRange(-(2**54), 2**54) |
| 46 | + if kind == 3: |
| 47 | + return fdp.ConsumeFloat() |
| 48 | + if kind == 4: |
| 49 | + return fdp.ConsumeUnicodeNoSurrogates(64) |
| 50 | + if kind == 5: |
| 51 | + return [_build(fdp, depth + 1) for _ in range(fdp.ConsumeIntInRange(0, _MAX_ITEMS))] |
| 52 | + return { |
| 53 | + fdp.ConsumeUnicodeNoSurrogates(24): _build(fdp, depth + 1) |
| 54 | + for _ in range(fdp.ConsumeIntInRange(0, _MAX_ITEMS)) |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | +def _json_equal(a, b) -> bool: |
| 59 | + """Equality up to JSON's number model. |
| 60 | +
|
| 61 | + JSON has one number type, so a float whose shortest form has no fractional |
| 62 | + part re-parses as a Python int and will not compare equal to the float it |
| 63 | + came from. That is correct output, not a defect, so the round trip is |
| 64 | + asserted up to numeric type. |
| 65 | + """ |
| 66 | + if isinstance(a, bool) or isinstance(b, bool): |
| 67 | + return a is b |
| 68 | + if isinstance(a, (int, float)) and isinstance(b, (int, float)): |
| 69 | + return float(a) == float(b) |
| 70 | + if isinstance(a, dict) and isinstance(b, dict): |
| 71 | + return a.keys() == b.keys() and all(_json_equal(a[k], b[k]) for k in a) |
| 72 | + if isinstance(a, list) and isinstance(b, list): |
| 73 | + return len(a) == len(b) and all(_json_equal(x, y) for x, y in zip(a, b)) |
| 74 | + return type(a) is type(b) and a == b |
| 75 | + |
| 76 | + |
| 77 | +def _has_nonfinite(value) -> bool: |
| 78 | + """NaN and Infinity have no JSON form; the canonicalizer rejects them.""" |
| 79 | + if isinstance(value, float): |
| 80 | + return not math.isfinite(value) |
| 81 | + if isinstance(value, dict): |
| 82 | + return any(_has_nonfinite(v) for v in value.values()) |
| 83 | + if isinstance(value, list): |
| 84 | + return any(_has_nonfinite(v) for v in value) |
| 85 | + return False |
| 86 | + |
| 87 | + |
| 88 | +def TestOneInput(data: bytes) -> None: |
| 89 | + fdp = atheris.FuzzedDataProvider(data) |
| 90 | + value = _build(fdp) |
| 91 | + if _has_nonfinite(value): |
| 92 | + return |
| 93 | + try: |
| 94 | + out = canonical_json(value) |
| 95 | + except ValueError: |
| 96 | + # Declared: CatalogApprovalError is a ValueError, and covers floats, |
| 97 | + # integers outside the RFC 8785 safe domain, and unsupported types. |
| 98 | + return |
| 99 | + |
| 100 | + assert _json_equal(json.loads(out), value), f"canonical bytes did not round-trip: {out!r}" |
| 101 | + assert canonical_json(value) == out, "canonicalization is not deterministic" |
| 102 | + |
| 103 | + |
| 104 | +def main() -> None: |
| 105 | + atheris.Setup(sys.argv, TestOneInput) |
| 106 | + atheris.Fuzz() |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == "__main__": |
| 110 | + main() |
0 commit comments