Defending AI Agents
עברית Join waitlist
Menu

On-ramp · Lesson 10

Two parsers, one message: a check is sound only if it reads what the target reads

Lessons 1, 2, 3, 5 and 7 each ended on the same shape. In each, a gateway decides on one reading of a message and a server acts on another: two Content-Length rules, two answers to a repeated JSON key, NO as a string and as a boolean, a name checked and an address connected to, SNI routed and Host decided. This lesson treats them as one class: what makes a check on a message sound, why that cannot be tested into existence, and which designs close the gap.

The condition, stated once

Let x be the bytes on the wire. The check parses them with P_C and accepts when a predicate \varphi holds of the result; the target parses the same bytes with P_T and acts on its result. The check is sound for \varphi when

\forall x:\quad \varphi\big(P_C(x)\big) \;\Rightarrow\; \varphi\big(P_T(x)\big).

The differential set D = \{x : P_C(x) \neq P_T(x)\} is where soundness can fail. Its size is irrelevant. A gateway whose parser agrees with the server on 99.999% of real traffic is exactly as unsound as one that agrees on half of it, because the attacker does not sample from real traffic: one reachable x \in D with \varphi(P_C(x)) true and \varphi(P_T(x)) false is enough, and it can be sent every time. This is "the attacker moves second" (Module 9, lesson 3) in its purest form. An agreement rate measured on traffic estimates the wrong quantity.

Why agreement cannot be tested into existence

Testing two parsers against each other finds members of D; it cannot show that D is empty. For the formats in this unit, nothing else can show it either. Deciding whether two context-free grammars define the same language is undecidable in general, and none of these formats is simpler than context-free: JSON is context-free, while length-prefixed HTTP bodies and YAML's indentation are beyond it. Sassaman, Patterson, Bratus and Locasto (2013) draw the consequence for input handling: two independently written parsers of a rich format cannot, in general, be proved equivalent, so a design that needs them to agree rests on an assumption no one can discharge. They argue for the opposite design: make the accepted language as simple as possible, and recognise it completely, in one place, before acting on any of it.

Differential testing is still worth running, as a search rather than as evidence. Jabiyev, Sprecher, Onarlioglu and Kirda (2021) generated HTTP requests from a grammar, mutated them, sent each to pairs of real servers and proxies, and compared how each side split the stream into messages. Disagreements on framing turned up across the pairs they tested. Each finding is a counterexample to soundness, but a clean run proves nothing, for the same reason that zero failures in n trials bounds a rate and does not make it zero: the one-sided 95% upper bound is 1 - 0.05^{1/n} \approx 3/n.

Three designs, in order of strength

Design What it guarantees Where the course uses it
One parser. The check parses, decides, and forwards a re-serialisation of what it decided on, never the original bytes. The target sees only the check's output. Soundness then needs only P_T(\text{serialise}(v)) = v on the values the check emits, a far smaller and simpler language. JSON, Module 3 lesson 6; a proxy that terminates HTTP and writes its own request (lesson 6); resolve once and connect to the checked address (lesson 5)
A smaller language. Reject every input on which reasonable parsers are known to disagree. Shrinks D by excluding inputs, not by trusting agreement. a repeated JSON key; a message with both Content-Length and Transfer-Encoding, which RFC 9112 (section 6.3) says "ought to be handled as an error"; a numeric IP in a non-canonical form; a YAML scalar whose type the schema does not name
Agreement by testing. Two parsers, differentially tested. Only that no disagreement has been found yet. nowhere as a control; as a search for bugs in the other two

The first design does not remove the problem; it moves it to a place you control. The re-serialiser must be deterministic, must emit only the subset of the format both sides read identically (no duplicate keys, integers within 2^{53}, one Unicode normal form), and must not drop a field the target reads. RFC 8785 exists because signatures need the strongest version of that promise. A gateway needs a weaker one, and should write down which one it claims (lesson 2).

Where agents add pairs

An agent stack has more parsers in series than a web stack:

  • the harness extracts a tool call from the model's text (Module 1, lesson 8): a parser of output that an attacker can influence;
  • the gateway parses the call's JSON arguments, and the tool server parses them again;
  • an argument that is itself a URL, a path or a shell command is parsed a third time by the tool, often by a library the gateway has never seen.

Each adjacent pair is a (P_C, P_T), and the soundness condition has to hold for every pair that carries a decision. The third bullet is the hard one. A gateway that allows fetch_url to files.acme.example has decided on its own URL parser's reading of the host. Two living specifications describe URLs, RFC 3986 and the WHATWG URL Standard that browsers follow, and they disagree on some inputs (the treatment of a backslash in an http URL is one). A check written against one and a fetcher built on the other have a non-empty D by construction. The one-parser design is the only clean answer: the gateway parses the URL, decides, and hands the tool a host and a path as separate, already-parsed fields, not a string to parse again.

What is open

For JSON there is a canonical form and for HTTP/1.1 a strict reading, so the one-parser design is available. For URLs, file paths across operating systems, shell command lines and natural-language "arguments" there is no canonical form both sides accept, and some tools take only a string. The defence there falls back to the smaller-language design (refuse what is ambiguous) plus containment for what gets through (Module 12). No published work yet measures, across real agent tool servers, how often their argument parsers disagree with the gateways in front of them.

Check yourself

  1. A gateway logs that its JSON parser and the tool's agreed on all of 10 million recorded calls. What does that number bound, and what does it not?
  2. Why does forwarding a re-serialisation help even when the gateway's parser is the less common of the two?
  3. A tool accepts only a shell command string. Which of the three designs are available for a gateway in front of it, and what has to carry the rest of the risk?
Answers
  1. It bounds the rate of disagreement on traffic like the recorded traffic: zero in 10^7 gives a one-sided 95% upper bound of 3.0 \times 10^{-7}. It does not bound the differential set D or show it empty, because the attacker does not sample from recorded traffic: one reachable x \in D, sent every time, breaks soundness.
  2. Because soundness then rests on P_T(\text{serialise}(v)) = v for the values the gateway emits, not on the two parsers agreeing about arbitrary bytes. The gateway can restrict its output to a small subset (no repeated keys, integers within 2^{53}, one Unicode normal form) that common parsers read identically, so which parser is more common no longer matters.
  3. Only the smaller language: accept commands from a narrow, unambiguous grammar (no quoting tricks, metacharacters or expansions) and refuse everything else, with differential testing as a search for bugs in that filter. One parser is not available, because the tool takes a string and parses it again. The rest of the risk falls to containment: a sandbox and least privilege that limit what an accepted command can do (Module 12).