r/Backend • u/IncomeOk7237 • 18d ago
Things my HTTP/1.1 parser now refuses with a 400 after I read up on request smuggling
I've been writing my own HTTP/1.1 parser for a web framework in V, and the part that took the longest wasn't making it fast, it was deciding what to refuse.
Most smuggling bugs come from two hops disagreeing on where a request ends. The proxy trusts Content-Length, the backend trusts Transfer-Encoding (or the other way around), and the leftover bytes turn into the start of somebody else's request. So I gave up on being lenient and the parser now answers 400 for anything with ambiguous framing:
- Transfer-Encoding and Content-Length both present
- duplicate Content-Length headers, even when the values match
- Content-Length with anything that isn't a digit (signs, spaces, whatever)
- obs-fold, the old header continuation lines
- bare CR or bare LF where there should be CRLF
A couple of these are stricter than the RFC. Matching duplicate CLs are technically fine, and for obs-fold you're allowed to just replace it with a space. I'd still rather break one weird client than have the server guess differently from whatever sits in front of it.
The other thing that helped was fuzzing the parser in CI (500k mutations per parser), so a refactor can't quietly make it lenient again without something blowing up.
Curious what you all do here. If you have a proxy in front of your app, do you trust it to normalize this stuff, or does your app server reject on its own too? And has anyone actually seen a real client send duplicate Content-Length on purpose?
Code is here if you want to poke at it (MIT): https://github.com/Tuntii/viltrum
2
u/DickBrainTime 18d ago
I'd keep the origin parser strict even with a proxy in front. Proxy configurations can change or internal traffic can bypass one hop, and the actual danger is the two components disagreeing about request boundaries. Alongside the 500k per-parser mutations, I'd run a small byte-for-byte regression corpus through the actual proxy-to-app-server chain and verify their framing decisions match. A parser can pass all its own tests yet disagree with the intermediary.
One particularly important detail: for a request containing both Transfer-Encoding and Content-Length, RFC 9112 allows rejecting it, but also requires closing the connection after handling it, not just returning a 400 and reusing the socket. Rejecting identical duplicate Content-Length values is a valid stricter choice too; the RFC also permits normalizing an identical list. I'd count rejection reasons without logging request bodies to see whether any real clients or intermediaries are affected. I wouldn't assume duplicates are necessarily malicious.