How Content-Length and Transfer-Encoding conflicts let two parsers split one byte stream differently, and the RFC 9112 framing rules that close the gap, tested on synthetic requests.
One byte stream, two readings
An HTTP/1.1 connection is a stream of bytes. Nothing in the stream marks where one request ends and the next begins; each parser works that out from the request's own headers. A request body is framed in one of two ways: a Content-Length header gives its size in bytes, or Transfer-Encoding: chunked sends it in pieces, each prefixed by its size in hexadecimal, ending with a zero-size chunk.
Most deployments have at least two parsers on the path: a front end, such as a load balancer, CDN or reverse proxy, and a back-end application server, often sharing one reused connection for many clients' requests. If the two disagree about where a request ends, the bytes that one thinks belong to the current request, the other reads as the start of the next one. An attacker who can make them disagree can prepend content to another user's request, bypass checks the front end applies, or poison a cache. This is HTTP request smuggling, and James Kettle's 2019 research at PortSwigger showed how widely deployed infrastructure was affected.
This guide walks through the classic disagreement, then implements the framing rules from RFC 9112, the current HTTP/1.1 specification, in a small parser tested on synthetic requests. The point is not to replace your server's parser, but to know which settings and behaviours to demand from it.
The disagreement
Take a request that carries both headers. The companion example builds one to a fictional returns endpoint:
POST /returns HTTP/1.1
Host: shop.example
Content-Length: 56
Transfer-Encoding: chunked
0
GET /admin/refunds HTTP/1.1
Host: shop.exampleThe Content-Length of 56 covers every byte after the header section: the zero chunk and the whole second request. A parser that trusts Content-Length reads all 56 bytes as the body of one POST /returns. A parser that trusts chunked encoding reads the zero chunk, decides the body is empty and the request is over, and finds GET /admin/refunds HTTP/1.1 waiting as the next request on the connection.
The example's two deliberately lenient parsers show exactly this. The Content-Length reader returns a 56-byte body and nothing left over. The chunked reader returns an empty body and leaves the second request in the stream. If the front end is the first and the back end is the second, the front end saw one request to /returns, applied its access rules to that, and forwarded the bytes; the back end then serves an admin request the front end never checked.
Other variants work the same way: two Content-Length headers with different values, a Transfer-Encoding value that one parser recognises as chunked and the other does not, or whitespace that one parser tolerates and the other rejects.
What RFC 9112 requires
RFC 9112, published in June 2022, tightened the framing rules precisely because of these attacks. The relevant parts of section 6 for a server receiving a request:
- If
Transfer-Encodingis present and chunked is not the final transfer coding, the body length cannot be determined reliably; the server must respond with 400 and close the connection. - If both
Transfer-EncodingandContent-Lengthare present,Transfer-Encodingoverrides. The RFC says such a message ought to be handled as an error. A server may reject it or process it byTransfer-Encodingalone, and either way must close the connection after responding. An intermediary that forwards it must first removeContent-Length. - If there is no
Transfer-EncodingandContent-Lengthis invalid, the framing is invalid; the server must respond with 400 and close. A list of identical valid values, such as9, 9, can be accepted as one value, and RFC 9110 section 8.6 describes that case. - An HTTP/1.0 message with
Transfer-Encodingmust be treated as having faulty framing, even withContent-Length, and the connection closed after processing it.
Section 5.1 adds a rule that closes another variant: a server must reject with 400 any request with whitespace between a header field name and the colon. Transfer-Encoding : chunked must not be read by one parser as the header and ignored by another.
The common thread is that a recipient must never guess. When the framing is ambiguous, the safe outcomes are to reject, or to choose one framing and close the connection so that nothing after it can be read as a new request.
Implementing the rules
The companion frame() function decides how long a request body is, or rejects it. It has two modes: strict, which rejects both-headers requests outright, and forward, which follows the intermediary rule by dropping Content-Length and using chunked framing.
if te:
if version == 'HTTP/1.0':
raise Reject(400, 'Transfer-Encoding in an HTTP/1.0 message: faulty framing, close after')
codings = [c.strip().lower() for v in te for c in v.split(',') if c.strip()]
if not codings or codings[-1] != 'chunked':
raise Reject(400, 'chunked is not the final transfer coding')
if cl:
if mode == 'strict':
raise Reject(400, 'both Transfer-Encoding and Content-Length')
fields = [(n, v) for n, v in fields if n != 'content-length']Content-Length values must be all digits, so +9 is rejected rather than parsed as 9 by one library and refused by another, and a list of values must all be equal. Chunk sizes must be hexadecimal digits only, so -1 is rejected.
The worked results
Nine synthetic requests go through both modes.
request strict forward
content-length only body 'CASE-6201' body 'CASE-6201'
chunked only body 'CASE-6201' body 'CASE-6201'
both headers reject 400 chunked, empty body, 51 bytes left
gzip after chunked reject 400: chunked not final reject 400
duplicate equal lengths body 'CASE-6201' body 'CASE-6201'
conflicting lengths reject 400 reject 400
signed length reject 400 reject 400
space before colon reject 400 reject 400
TE in HTTP/1.0 reject 400, close reject 400, closeThe forward-mode row for the smuggling request is worth reading carefully. The 51 bytes left over are the hidden GET /admin/refunds request. Forward mode does not make that request disappear; it makes the front end see it as a separate request, which it will now route and check like any other. Smuggling is not the existence of a second request in the bytes, it is two parsers disagreeing about it. Once both agree, the front end's access control applies to what the back end will serve. Closing the connection after such a request, as the RFC requires of a server, removes the rest of the risk.
Hardening a real deployment
The parser above is a teaching model. In a real system, the rules have to hold in every hop, and you rarely control the parsing code.
- Configure the front end to reject requests with both framing headers, with invalid or conflicting
Content-Length, and with malformedTransfer-Encoding, rather than normalising them. Most proxies have a strict mode or a setting for each. - Make the back end equally strict, so that it rejects anything that reaches it with ambiguous framing even if a front end was misconfigured.
- Prefer HTTP/2 or HTTP/3 between the front end and back end where both support it, since those protocols frame messages in binary. Be careful with downgrading: a front end that accepts HTTP/2 and forwards as HTTP/1.1 must not copy a client-supplied
Content-Lengththat disagrees with the actual frame length. - Avoid reusing back-end connections across clients where it is not needed, or at least make sure an error on one request closes the connection.
- Test with a scanner built for desync detection against a staging copy of your exact proxy chain. Configuration differences between environments matter more than code.
Failure modes
A front end that normalises instead of rejecting can create the very disagreement it was meant to prevent, if it removes one header and the back end still sees another through a different path.
Logging and monitoring often miss smuggled requests, because the front end's access log never records them. Back-end logs showing requests that do not match any front-end log line are a signal worth alerting on.
Response-side desync exists too: a back end that frames its responses ambiguously can make a front end hand one user's response to another. The same rules apply in the other direction.
Verification
The companion code has eight tests: single-header requests frame the same body, strict mode rejects both headers, forward mode drops Content-Length and leaves the second request visible, the two lenient parsers disagree on the same bytes, chunked must be final, Content-Length values are validated, whitespace before the colon and Transfer-Encoding in HTTP/1.0 are rejected, and chunk sizes must be hexadecimal. All inputs are synthetic bytes; nothing is sent over a network. Run python -m unittest -v.
Run the example yourself
The framing function, the nine synthetic requests, the lenient parser pair and eight tests. Companion code on GitHub; its README lists the prerequisites, exact commands, expected output and limits.