|
| 1 | +--- |
| 2 | +title: Body and Framing |
| 3 | +description: "Content-Length, chunked transfer encoding, trailers, and why CL+TE conflicts cause request smuggling." |
| 4 | +weight: 5 |
| 5 | +--- |
| 6 | + |
| 7 | +HTTP/1.1 messages optionally carry a **message body** after the header section. The critical question for any parser is: **where does the body end?** Getting this wrong is the root cause of HTTP request smuggling. |
| 8 | + |
| 9 | +## When Is a Body Present? |
| 10 | + |
| 11 | +- **Requests** — a body is present if `Content-Length` or `Transfer-Encoding` is set. `GET`, `HEAD`, `DELETE`, and `OPTIONS` typically have no body (though the spec doesn't forbid it). |
| 12 | +- **Responses** — all responses to `HEAD` requests and all `1xx`, `204`, and `304` responses have no body. Everything else may have a body. |
| 13 | + |
| 14 | +## Content-Length |
| 15 | + |
| 16 | +The `Content-Length` header declares the exact size of the body in bytes as a decimal integer: |
| 17 | + |
| 18 | +```http |
| 19 | +POST /data HTTP/1.1 |
| 20 | +Host: example.com |
| 21 | +Content-Type: text/plain |
| 22 | +Content-Length: 13 |
| 23 | +
|
| 24 | +Hello, World! |
| 25 | +``` |
| 26 | + |
| 27 | +The parser reads exactly 13 bytes after the empty line, then the next bytes are the start of the next message (on a persistent connection) or the connection ends. |
| 28 | + |
| 29 | +### Rules |
| 30 | + |
| 31 | +- The value **MUST** be a non-negative decimal integer. |
| 32 | +- **No leading zeros** — `Content-Length: 007` is invalid. |
| 33 | +- **No signs** — `Content-Length: +13` or `Content-Length: -1` are invalid. |
| 34 | +- **No whitespace** within the value — `Content-Length: 1 3` is invalid. |
| 35 | +- If `Content-Length` **doesn't match** the actual body size, the message is malformed. The server SHOULD close the connection. |
| 36 | +- **Multiple `Content-Length` headers** are allowed only if all values are identical. If they differ, the message is malformed and MUST be rejected. |
| 37 | + |
| 38 | +### Why Strictness Matters |
| 39 | + |
| 40 | +Lenient parsing of `Content-Length` is a common source of vulnerabilities: |
| 41 | + |
| 42 | +- `Content-Length: 0x0d` — if parsed as hex, this is 13 bytes. If parsed as decimal, it's invalid. A parser mismatch between front-end and back-end enables smuggling. |
| 43 | +- `Content-Length: 13, 14` — a list of two differing values. One parser might take the first, another the last. |
| 44 | + |
| 45 | +## Chunked Transfer Encoding |
| 46 | + |
| 47 | +When the total body size is unknown at the time headers are sent (streaming, server-generated content, compression), HTTP/1.1 uses **chunked transfer encoding**. |
| 48 | + |
| 49 | +### Format |
| 50 | + |
| 51 | +``` |
| 52 | +chunk-size (hex) CRLF |
| 53 | +chunk-data CRLF |
| 54 | +... |
| 55 | +0 CRLF |
| 56 | +[ trailer-section ] |
| 57 | +CRLF |
| 58 | +``` |
| 59 | + |
| 60 | +Each chunk starts with the chunk size in hexadecimal, followed by CRLF, then exactly that many bytes of data, followed by CRLF. A zero-length chunk signals the end of the body. |
| 61 | + |
| 62 | +### Full Example |
| 63 | + |
| 64 | +```http |
| 65 | +HTTP/1.1 200 OK |
| 66 | +Transfer-Encoding: chunked |
| 67 | +
|
| 68 | +4\r\n |
| 69 | +Wiki\r\n |
| 70 | +7\r\n |
| 71 | +pedia i\r\n |
| 72 | +B\r\n |
| 73 | +n chunks.\r\n |
| 74 | +0\r\n |
| 75 | +\r\n |
| 76 | +``` |
| 77 | + |
| 78 | +Decoded body: `Wikipedia in chunks.` |
| 79 | + |
| 80 | +### Chunk Extensions |
| 81 | + |
| 82 | +A chunk-size may be followed by semicolon-separated extensions: |
| 83 | + |
| 84 | +``` |
| 85 | +a;ext-name=ext-value\r\n |
| 86 | +0123456789\r\n |
| 87 | +``` |
| 88 | + |
| 89 | +Most servers and proxies **ignore** chunk extensions. They exist for potential use cases like per-chunk checksums or metadata, but are rarely used in practice. Some security tools test whether servers handle unexpected extensions safely. |
| 90 | + |
| 91 | +### Trailers |
| 92 | + |
| 93 | +After the final zero-length chunk, **trailer fields** may appear — headers sent after the body: |
| 94 | + |
| 95 | +```http |
| 96 | +HTTP/1.1 200 OK |
| 97 | +Transfer-Encoding: chunked |
| 98 | +Trailer: Checksum |
| 99 | +
|
| 100 | +4\r\n |
| 101 | +data\r\n |
| 102 | +0\r\n |
| 103 | +Checksum: abc123\r\n |
| 104 | +\r\n |
| 105 | +``` |
| 106 | + |
| 107 | +Trailers are useful for: |
| 108 | +- **Checksums/signatures** — computed as the body streams. |
| 109 | +- **Processing status** — whether the server completed successfully. |
| 110 | +- **Metadata** — anything that can't be determined until after the body is generated. |
| 111 | + |
| 112 | +The `Trailer` header in the response declares which trailer fields to expect (though this is advisory, not enforced). |
| 113 | + |
| 114 | +### Rules |
| 115 | + |
| 116 | +- Chunk sizes **MUST** be hexadecimal, case-insensitive (`a` and `A` are both valid). |
| 117 | +- A zero-length chunk **MUST** be present to terminate the body. |
| 118 | +- After the zero-length chunk, the trailer section and final CRLF complete the message. |
| 119 | + |
| 120 | +## Content-Length vs Transfer-Encoding |
| 121 | + |
| 122 | +A message **MUST NOT** contain both `Content-Length` and `Transfer-Encoding`. |
| 123 | + |
| 124 | +RFC 9112 §6.1 is explicit: |
| 125 | + |
| 126 | +> If a message is received with both a Transfer-Encoding and a Content-Length header field, the Transfer-Encoding overrides the Content-Length. Such a message might indicate an attempt to perform request smuggling or response splitting and **ought to be handled as an error**. |
| 127 | +
|
| 128 | +### The Request Smuggling Problem |
| 129 | + |
| 130 | +This ambiguity is the **root cause of HTTP request smuggling**. Consider a message with both headers: |
| 131 | + |
| 132 | +```http |
| 133 | +POST / HTTP/1.1 |
| 134 | +Host: example.com |
| 135 | +Content-Length: 6 |
| 136 | +Transfer-Encoding: chunked |
| 137 | +
|
| 138 | +0\r\n |
| 139 | +\r\n |
| 140 | +GPOST |
| 141 | +``` |
| 142 | + |
| 143 | +- A parser that uses **Transfer-Encoding** sees a zero-length chunk → body ends immediately. The remaining bytes (`GPOST`) are the start of the next request. |
| 144 | +- A parser that uses **Content-Length** reads 6 bytes (`0\r\n\r\nG`) as the body. `POST` becomes part of the next request with a different method. |
| 145 | + |
| 146 | +If a front-end proxy uses one interpretation and a back-end server uses another, the attacker controls where one request ends and the next begins. This can: |
| 147 | +- **Bypass access controls** — smuggle a request to an internal endpoint. |
| 148 | +- **Poison caches** — make the cache store an attacker-controlled response for a victim's URL. |
| 149 | +- **Hijack connections** — capture another user's request. |
| 150 | + |
| 151 | +### How Servers Should Handle It |
| 152 | + |
| 153 | +Strict servers should: |
| 154 | +1. **Reject** messages with both `Content-Length` and `Transfer-Encoding` with a 400 response. |
| 155 | +2. If not rejecting, **always prioritize `Transfer-Encoding`** and ignore `Content-Length`. |
| 156 | +3. **Never trust `Content-Length`** when `Transfer-Encoding` is present. |
| 157 | + |
| 158 | +This is one of the most critical compliance checks that Http11Probe performs. |
| 159 | + |
| 160 | +## Transfer-Encoding Obfuscation |
| 161 | + |
| 162 | +Attackers may try to hide `Transfer-Encoding` from one parser while making another recognize it: |
| 163 | + |
| 164 | +```http |
| 165 | +Transfer-Encoding: chunked |
| 166 | +Transfer-Encoding : chunked |
| 167 | +Transfer-Encoding: xchunked |
| 168 | +Transfer-Encoding: chunked\r\n (extra space) |
| 169 | +Transfer-Encoding: |
| 170 | + chunked |
| 171 | +``` |
| 172 | + |
| 173 | +Each of these variants exploits differences in how parsers handle: |
| 174 | +- Whitespace before the colon (forbidden by RFC 9112 §5.1). |
| 175 | +- Unknown transfer coding names. |
| 176 | +- Obs-fold (deprecated line folding). |
| 177 | +- Leading/trailing whitespace in the value. |
| 178 | + |
| 179 | +Strict, RFC-compliant parsing eliminates these attack surfaces. |
0 commit comments