Section 1.48

Robustness principle and strict parsing

Definition

The robustness principle, also called Postel’s law, is the protocol design rule “be conservative in what you do, be liberal in what you accept from others” [429]: receivers tolerate input that departs from the specification. Strict parsing is the opposite rule for input from a less trusted party: the format defines exactly which byte sequences are valid and what each means, and the receiver rejects all others instead of repairing them. The difference that matters: under strict parsing every accepted message has one meaning, shared by every component that reads it.

The idea

Early Internet software was written by many teams from specifications that left gaps. The principle appears, in the words quoted above, in the 1981 specification of TCP (Transmission Control Protocol), RFC 793, edited by Jon Postel, after whom the principle is named. Tolerance let programs that disagreed in small ways still work together. Tolerance has a cost when an attacker writes the input. A tolerant receiver must guess what a malformed message was meant to say, and two tolerant programs can guess differently.

That difference is the danger. Suppose one component checks a request and another component carries it out. The checker reads the text target=A;target=B, takes the first target, A, which the sender may use, and approves. The executor reads the same text and takes the last target, B, which the sender may not use. Both components accept the message, each is consistent with itself, and the sender reaches B. Two parsers that give the same input different meanings form a parser differential. HTTP request smuggling is a real case, where HTTP is the Hypertext Transfer Protocol of the web: a front-end server finds where a request ends from one header field and a back-end server from another, so an attacker hides a second request inside the first.

A contract with two clauses that contradict each other is a fair picture. If the clerk who approves the contract reads one clause and the officer who enforces it reads the other, the signature on the contract does not decide which clause applies. The picture stops being accurate in one respect: a court can settle a contradiction afterwards, whereas a program acts at once on whatever meaning its parser chose.

How it works

Strict parsing starts from a precise format and handles input in three steps.

  1. Specify the format: the size or length prefix §4.30 of every field, the order of the fields, the range of every number, the supported versions, and an explicit rule for each of the four cases below.

  2. Recognize, then act: check the whole message against the format and build an in-memory value from it before any part of it is used. A message that fails any check is rejected as a whole. Never act on the first part of a message while the rest is unchecked.

  3. One parse, one meaning: every later component uses the value the parser built, never the raw bytes. If two components must parse independently, they must accept exactly the same messages and give each the same meaning.

Treating valid input as a precisely defined formal language, and recognizing it completely before processing it, is the approach called language-theoretic security (LangSec).

Four cases need an explicit rule.

Reserved fields are bits or bytes that the current version leaves unused so that a later version can give them a meaning. They serve forward compatibility, the ability of old receivers to handle messages from newer senders. The sender sets reserved fields to zero, and the receiver has two choices. Ignoring them lets a later version use them without breaking old receivers, but an old receiver then silently ignores the new meaning, which is dangerous when that meaning is a security requirement. Rejecting non-zero values is safe but blocks that form of extension. A meaning that old receivers must not ignore belongs behind a new version number, which old receivers reject, or in a field marked critical.

Authentication §1.6 does not repair ambiguity. A message authentication code §5.7 or a digital signature §5.14 shows that the exact bytes came from a holder of the key. If those bytes have two meanings, both meanings are authenticated. Checking that a message is authentic and checking that it has one meaning are separate jobs, and a format needs both.

Worked example

A format defines a record of exactly 5 bytes: a 1-byte operation code §2.8, a 2-byte target number and a 2-byte amount, each number written with its most significant byte first. Operation 0x01 means read and 0x02 means write. A receiver gets these 10 bytes, written in hexadecimal:

01 00 07 00 64 02 00 07 FF FF

The first record is 01 00 07 00 64: operation 1, read; target 0x0007, which is 7; amount 0x0064, which is 6×16+4=1006 \times 16 + 4 = 100. The checker reads one record, finds that the sender may read target 7, and approves. The executor processes records until the bytes run out, so it also runs 02 00 07 FF FF: operation 2, write; target 7; amount 0xFFFF, which is 65,53565{,}535. The sender has written to target 7 without approval.

A strict parser checks the length first. A message is exactly one record, so its length must be 5 bytes. The length is 10, and 10≠510 \ne 5, so the whole message is rejected before either component acts. If the format is meant to carry several records, it must say so, for example with a leading count, and the checker must then approve every record that the executor will run.

Reserved bits work the same way. A 1-byte flags field defines bits 0 to 2, and bits 3 to 7 are reserved and must be zero. The reserved bits form the mask 0xF8, binary 11111000. A version-2 sender sets bit 3, which version 2 defines as “encrypt the reply”, together with bit 0, so the flags byte is 23+20=8+1=92^3 + 2^0 = 8 + 1 = 9, that is 0x09. A strict version-1 receiver computes the bitwise AND of 0x09 and 0xF8, which keeps only the bits set in both. The result is 0x08, not zero, so the receiver rejects the message; the sender learns that its requirement was not understood. A tolerant version-1 receiver ignores bit 3 and sends the reply unencrypted.

Stated precisely

Let MM be the set of byte strings that the format declares valid, and for each xx in MM let mean(x)\mathrm{mean}(x) be the meaning the format assigns to xx. A parser pp is strict when it accepts exactly the strings in MM and returns mean(x)\mathrm{mean}(x) for each accepted xx. Two parsers pp and qq agree when, for every byte string xx, either both reject xx, or both accept it and p(x)=q(x)p(x) = q(x).

The security requirement at a trust boundary §1.3 is agreement between every component that checks a message and every component that acts on it: whatever the checker approves, the executor accepts with the same meaning. Strict parsing in both components gives agreement, because both then compute mean\mathrm{mean}. Where messages are compared, hashed or signed as bytes, a second requirement applies: each meaning has exactly one valid encoding, so that for xx and yy in MM, mean(x)=mean(y)\mathrm{mean}(x) = \mathrm{mean}(y) implies x=yx = y.

Limits and common mistakes

The robustness principle keeps a place where no attacker writes the input and working together matters more than precision. Even there it has a cost. RFC 9413, Maintaining Robust Protocols (2023) [431], describes how errors that receivers tolerate become entrenched as a de facto standard, so that every later implementation must reproduce them to work with the others.

Strict does not mean impossible to extend. A format can be strict and extensible at once through explicit extension points: fields that carry their length, a critical marker and a version number.

Using the same parser library in two components is evidence of agreement, not proof. Two versions or two settings of one library can differ, and a component that later re-parses or normalizes the raw bytes, for example by decoding escaped characters, can reintroduce a difference.

Strictness must cover the whole route. A strict parser at the entry is undone by a later component that re-reads the original bytes with a tolerant parser of its own.

Duplicate names are not only a problem of text formats. In a 2013 Android flaw, an application package, which is a ZIP archive (a file format that packs several named files into one), could contain two files with the same name; the signature check examined one copy and the installer used the other [352].

A strict parser still reads bytes that an attacker chose, so it must itself be safe for every input: rejecting a malformed message must not overrun a buffer. A parser that, after an error, tries again with looser rules is a tolerant parser.

Questions answered

Should unknown fields, trailing bytes, or duplicate keys be accepted?

At a trust boundary, where a more trusted component acts on input from a less trusted party, the default answer is no for all three, unless the format states an explicit rule that gives every accepted message exactly one meaning. Two components can treat the same tolerated input differently, and an attacker exploits that difference.

Authenticating the message does not remove the need for these rules. A valid tag or signature covers the bytes, including both copies of a duplicated key, not one meaning. The robustness principle, “be liberal in what you accept”, helps independent implementations work together, but it is the wrong default for input that an attacker can choose.

Related entries: Serialization (§4.28), Deserialization (§4.29), Length-prefix framing (§4.30), Authentication coverage (§5.41), Duplicate JSON key (§15.20), Type-length-value (TLV) (§4.31), Trust boundary (§1.3). Further reading: [29, 352, 429, 430, 431].