Repository navigation
Conversation
A chunk-size line with an extension (`4;foo=bar`) was rejected as an invalid chunk size, and a non-empty trailer section after the last chunk as an improperly terminated body. RFC 9112 requires recipients to ignore unrecognized chunk extensions (section 7.1.1), and allows a trailer section after the last chunk (section 7.1.2). Because the same code reads response bodies, simple_httpclient also failed to fetch chunked responses with trailers, such as those sent by Node's response.addTrailers(). Extensions are now checked against the chunk-ext grammar and ignored. Trailer lines are read up to max_header_size, validated like header lines and discarded. Each trailer line counts towards the IOLoop yield threshold like a chunk, and an http_trailers benchmark covers the worst case of many short lines.
measure() waited for the handler to finish, so a scenario that the server under test rejects waited forever. compare_benchmarks runs head's benchmarks against the base branch too, and the new http_trailers scenario is rejected there, which hung the CI job until its timeout. Once the client sees the connection closed, give the handler a second to finish and otherwise raise, so the benchmark is reported as new.
|
The benchmark comparison job hung until its timeout: |
|
This is a big change with potential security implications, so I want to review it carefully. First, I'd like to understand the motivation a little better. Is this just about finding something from the RFCs that we don't support or do you have a use case where chunk extensions and/or trailers are being used? (Is it legal to send trailers in response to a request that did not include |
|
Thanks for taking the time to look at this, and for being upfront about the caution. That's fair for framing code, and I'd much rather it get a careful look than a quick one. On the motivation, to be honest: this didn't start from a production use case. It came out of reading On On scope: parse-and-discard is all I had in mind. I'm not planning to expose the values, and I think discarding is the safer default anyway (§7.1.2 explicitly allows it). Given all that, I'd understand if the full change doesn't seem worth the review risk. If it helps, I'm happy to cut it down: for example to only accepting (and discarding) a trailer section without the chunk-extension parsing, or only accepting trailers on the client side, where the concrete failure is. Whatever shape is easiest for you to review. And if you'd rather leave this alone for now, that's completely fine too. |
|
Thanks for the transparency. We've had a lot of churn and review backlog recently, so I'm going to let this one wait a while. |
HTTP1Connection._read_chunked_bodyrejects two parts of the chunked coding that RFC 9112 allows.4;foo=bar) failsparse_hex_intand comes back as "invalid chunk size". Section 7.1.1 says "A recipient MUST ignore unrecognized chunk extensions". The method has carried aTODO: "chunk extensions"for this.0\r\nX-Trailer: value\r\n\r\n) is "improperly terminated chunked request", because the code expects the final CRLF right after0\r\n. Section 7.1.2 allows a trailer section there.The same method reads response bodies, so
simple_httpclientfails on chunked responses that end with trailers. Here is Node'sresponse.addTrailers(), for example:SimpleAsyncHTTPClient().fetch("http://127.0.0.1:8000/")raisesHTTPStreamClosedError: Connection closed. With this change it returns200 b'hello world'. On the server side, llhttp, Go's net/http, h11, puma, aiohttp and fasthttp all accept5;a=band a trailer section; tornado was the only one that answered 400.Since this is framing code, I kept it strict:
;must match thechunk-extgrammar:*( BWS ";" BWS token [ BWS "=" BWS ( token / quoted-string ) ] ). That rule is now in_ABNF, along withquoted-string. An extension that doesn't match is rejected as "invalid chunk extension". That covers CTLs, a stray"and empty names, among other cases.;. The grammar allows it, but llhttp, h11, puma, aiohttp, fasthttp and Go all reject5 ;x. So5 ;xis still an invalid chunk size, and the size stays unambiguous. The comment says so; allowing it later would be a one-line change.HTTPHeaders.parse_line, then discarded. The whole section is limited tomax_header_size.0\r\n\nGET /b HTTP/1.1\r\n...becomes a "no colon" error instead of swallowing the next request._CHUNK_YIELD_COSTtowards_YIELD_THRESHOLD, as a chunk does, so a section of many short lines doesn't monopolize the IOLoop.Tests:
httpserver_test: a request using extensions (including a quoted;and a valueless one) and trailers; five malformed extensions; and an invalid trailer line.http1connection_test: a response with an extension and aServer-Timingtrailer; and a 10000-line trailer section, which checks that the IOLoop keeps ticking.Each test fails without the change. The yield test reports
0 not greater than 10when the yield is removed.Benchmark: I added an
http_trailersscenario todos_benchmark.py, with manya:lines andmax_header_sizeraised, and toci_benchmark.pywith the same 8.0 budget ashttp_chunked. On my machine, in µs per byte:http_trailershttp_chunkedheaders_manyChecks:
python -m tornado.test.runtestspasses, with 1540 tests and 89 skipped (no pycurl/pycares/twisted here).black --check tornado demos,flake8andmypy --platform linux/windows tornadopass, using the tool versions pinned inrequirements.txt. I did not runtox -e docsor the-fullconfigurations.