Respond with 400 for malformed HTTP/2 request headers#364
Open
apoorvdarshan wants to merge 1 commit into
Open
Conversation
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.
Summary
In HTTP/2 mode, a request with a malformed header value — for example a value with leading or trailing whitespace, which RFC 9113 8.2.1 forbids — causes the whole connection to be torn down with a GOAWAY and no HTTP response. The client sees an abnormal connection failure instead of the 400 (Bad Request) the RFC recommends. This fixes issue #348.
The
h2library raises a connection-levelh2.exceptions.ProtocolErrorwhile parsing such a request.H2Protocol.handle()caught it and only flushed the queued GOAWAY before closing, so no response ever reached the offending stream.Fix
src/hypercorn/protocol/h2.py: onProtocolError, send a 400 (Bad Request) for the offending stream before flushing and closing (RFC 9113 8.2.1).Because
h2has already transitioned the connection to a closed state, its send API can no longer be used, so a minimalHEADERSframe is written directly. The header block is a single fully-indexed HPACK reference to the static-table entry:status: 400(index 12, byte0x8c), so no encoder state is required and it is safe to emit on a fresh connection. The offending stream is taken fromconnection.highest_inbound_stream_id; when that is0(a framing-level error with no request stream, e.g. garbage bytes) the behaviour is unchanged — the connection is simply closed.Tests
test_protocol_handle_malformed_request_sends_400, which drives a realh2client throughH2Protocol, injects a hand-crafted HEADERS frame with a whitespace-surrounded header value, and asserts the client receives a:status: 400response (and the connection is then closed). The test fails on the pristine code (client sees onlyConnectionTerminated) and passes with the fix.test_protocol_handle_protocol_error(garbage input → justClosed()) still passes, confirming the framing-error path is unchanged.black,isort,flake8, andmypyare clean on the changed files.Added a CHANGELOG entry.
Disclosure: prepared with AI assistance; reviewed and verified locally.