fix: reject requests with malformed request-line#100
Open
kugland wants to merge 2 commits into
Open
Conversation
Add test cases for two forms of invalid request-line: - a literal space in the request-target (e.g. GET /foo bar HTTP/1.1), which is invalid per RFC 9112 and should return 400 Bad Request; - extra tokens after the HTTP version (e.g. GET / HTTP/1.1 HTTP/1.1), which should likewise be rejected.
In parse_request(), after parsing the HTTP version token, reject the request if: - the token is not HTTP/1.0 or HTTP/1.1 (catches literal spaces in the request-target, which cause the URL parser to stop early and treat the next word as the version); - a non-whitespace character follows the version on the same line (catches extra tokens such as GET / HTTP/1.1 HTTP/1.1). Also stop the version-token loop at '\n' so bare-LF requests are handled correctly, and force conn_close=1 in process_request() when parse_request() fails so the 400 reply always closes the connection.
0ea96d1 to
99a9c25
Compare
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.
Fixes #99.
parse_request()stopped URL parsing at the first space, then treated the next token as the HTTP version. This allowed a literal space in the request-target to go undetected — the server would silently truncate the URL and continue. Additionally, extra tokens after the HTTP version (e.g.GET / HTTP/1.1 HTTP/1.1) were silently ignored.Changes
darkhttpd.cHTTP/1.0orHTTP/1.1as valid version tokens (previously accepted anyHTTP/prefix). An unrecognised token means the URL contained a literal space → 400.\nso bare-LF requests are handled symmetrically with CRLF.conn_close = 1inprocess_request()whenparse_request()fails, so the 400 reply always closes the connection (previously, ifHTTP/1.1had been parsed before the failure, the connection was incorrectly kept alive).devel/test.pyGET /foo bar HTTP/1.1→ 400.TestMalformedRequestLine.test_extra_token_after_version: sendsGET / HTTP/1.1 HTTP/1.1via raw socket → 400.