Defending AI Agents
עברית Join waitlist
Menu

On-ramp · Lesson 02

JSON is simple, and its parsers still disagree

A policy says "deny any call whose amount exceeds 1,000". The gateway's JSON parser keeps the first amount; the payments service's keeps the last. The attacker sends {"amount": 10, "amount": 1000000} and the gateway reads 10. Nothing is malformed; two correct programs read one text two ways.

The grammar fits on a card

JSON has six value types: object, array, string, number, true/false, null (RFC 8259, 2017). Keys are strings, strings are double-quoted, there are no comments and no trailing commas. That brevity is why it is the agents' language: tool arguments, model replies and MCP messages are JSON.

What the grammar leaves open

RFC 8259 leaves five places to the implementation, and in each two implementations can differ. Python 3 shows the first two, and the float arithmetic behind the second, in four lines:

import json
print(json.loads('{"amount": 1, "amount": 1000000}'))   # {'amount': 1000000}
print(json.loads('{"n": 1e400}'))                      # {'n': inf}
print(json.loads('{"n": 12345678901234567890}'))       # exact in Python; a double in JavaScript
print(0.1 + 0.2)                                       # 0.30000000000000004
Open point What differs What an attacker does with it
Duplicate keys "The names within an object SHOULD be unique", and behaviour is otherwise unpredictable: first wins, last wins, or an error send two values; the control reads one, the tool the other
Number range big integers and 1e400 become inf, a rounded double or an error a limit checked as 9007199254740993 and executed as ...992
Strings \u0000, lone surrogates, differently normalised Unicode a name that compares unequal to a deny-list entry and opens the denied file
Depth and size no limit stated a deep array that crashes one parser and not another
Extensions "A JSON parser MAY accept non-JSON forms or extensions": NaN, Infinity, comments, trailing commas; Python accepts the first two send "amount": NaN: NaN > 1000 is false, so a check that denies above 1,000 passes it

Type is not on the list: the grammar fixes that "1" is a string and 1 a number. A service that turns "amount": "1000000" into a number does so in its own code, which gives the control one more reading to match.

The rule the course follows

Decide on the same bytes you forward, parsed by a parser that rejects what is ambiguous. Module 3 lesson 6 builds that: the gateway rejects any object with a repeated key, and forwards the re-serialised form it decided on, not the original bytes. Then the only parser the tool service meets is the gateway's own.

Expert: canonical form is a proof obligation

"Re-serialise" only closes the gap if parse then serialise is deterministic, loses nothing the tool reads, and the downstream parser reads the output as the gateway did. RFC 8785 (JSON Canonicalization Scheme) exists because signatures over JSON need that property; most gateways need a weaker one, that the output contains no duplicate keys and numbers in a range both sides agree on. State which one you are claiming.

Check yourself

  1. Why is "the gateway parses with a strict parser" not enough if it forwards the original bytes?
  2. A float field "risk": 0.1 is added ten times with += and compared with 1.0. What will it say, and what does that say about using floats as money?
  3. A tool takes {"path": "notes/q3.txt"}, and the gateway refuses any path equal to the deny-list entry private/café.txt (precomposed é). Using the table's Strings row, describe a second valid JSON value that the comparison lets through and a file system may open as the denied file.
Answers
  1. Strictness only fixes the gateway's own reading. The tool still parses the original bytes with its own parser, and on any input where the two differ (number range, Unicode forms, NaN) the gateway has decided on a value the tool never sees. Only forwarding the re-serialised form makes the gateway's reading the one the tool receives.
  2. Adding 0.1 ten times with += gives 0.9999999999999999, so == 1.0 is False and a check at exactly the limit flips. 0.1 has no exact binary representation and the errors accumulate. sum() (Python 3.12 and later) and numpy.sum order or compensate the additions and give 1.0, so the verdict depends on how the code adds, not on the data. Money belongs in integer minor units or a decimal type, never in floats.
  3. "private/café.txt": e followed by the combining acute accent (U+0301), against the precomposed é (U+00E9) of the entry. The two strings differ code point by code point, so the deny-list comparison lets it through, while a file system that normalises names opens the denied file. Against an allow-list the same mismatch costs only a false denial.