THRIFT-6266: Read the THttpServer Content-Length against its grammar - #3864
Merged
Merged
Conversation
Client: py THttpServer converted Content-Length with int(), which accepts more than the field's grammar of one or more decimal digits (RFC 9110 8.6). int() takes a leading sign, underscores between digits, and whitespace such as a no-break space, a vertical tab or a form feed around the number. Each of these was read as a valid length, and the request was served. "-0" passed as zero, and the request then failed without a response. The value is now stripped of the spaces and tabs that surround a field value without being part of it (RFC 9110 5.5). What remains must consist of digits only. Anything else is answered with the 400 already sent for a malformed length. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
JIRA: THRIFT-6266
Client: py
THttpServerconvertedContent-Lengthwith a bareint(). The field's grammar is1*DIGIT(RFC 9110 §8.6), butint()also accepts:+17);1_7);\xa0, vertical tab, form feed, NEL.http.serverdecodes headers as Latin-1, so all of these reach the handler.Each of these was taken as a length of 17 and the request was served.
-0passed as zero, and the request then failed without a response.Change
The value is stripped of spaces and tabs, which surround a field value without being part of it (RFC 9110 §5.5). What remains has to match
[0-9]+; anything else gets the existing400 Invalid Content-Length. TheValueErrorhandler stays: from Python 3.11 on,int()refuses more than 4300 digits.A note on the ticket's examples:
" 12 "is still accepted, because the whitespace around a field value is not part of the value.http.serveralready drops the leading part. The D binding does the same since2f7714ebf. A list form such as17, 17is refused, which RFC 9110 permits.Tests
New
lib/py/test/test_http_server_content_length.py, registered in both lists inlib/py/Makefile.am. It sends raw requests to a runningTHttpServerand checks the status and whether the processor ran.17,017, and17with spaces or tabs on either side.\xa0,\x0b,\x0cor\x85before or after the digits; an empty value,0x11,1e1,17.0,1 7,-0; a list of lengths.Results:
-0cases fail.ValueErrorhandler (on Python 3.12).test_http_server_body_size.pypass on Python 3.10, 3.12 and 3.14.🤖 Generated with Claude Code