Skip to content

Validate HTTP/2 response header fields and pseudo-headers - #504

Merged
ericmj merged 1 commit into
mainfrom
validate-http2-response-headers
Sep 21, 2026
Merged

ericmj merged 1 commit into
mainfrom
validate-http2-response-headers

Conversation

@ericmj

@ericmj ericmj commented Sep 19, 2026 •

Copy link
Copy Markdown
Member

A :status value that isn't three digits, such as "abc" or "200 ", reached String.to_integer/1 inside stream/2 and raised ArgumentError. Those two calls are the only place in Mint.HTTP2 where server-controlled bytes reach a raising function outside the throw-based error channel, so the exception escapes Mint.HTTP.stream/2 rather than coming back as {:error, conn, reason, responses}. Responses already decoded for other streams in the same call go with it, and so does the conn holding the advanced HPACK decode table, so a caller that rescues is left with a connection that fails with {:compression_error, {:index_not_found, _}} on the next header block.

The same pass now checks the rest of RFC 9113 8.2.1 and 8.3 on responses: uppercase and otherwise invalid field names, values containing NUL, CR or LF or surrounded by SP or HTAB, undefined pseudo-headers, a repeated :status, pseudo-headers after regular fields, and pseudo-headers in trailers. All of those were delivered to the caller as ordinary headers before.

A malformed block is a stream error, so the stream is reset with PROTOCOL_ERROR, the connection stays open, and the caller gets one of {:invalid_status_header, value}, {:invalid_header_name, name}, {:invalid_header_value, name, value} or {:protocol_error, debug_data}. The 1xx-after-final-response branch is removed because a 1xx block arriving after the final response is a pseudo-header in trailers, which the new check catches first.

Each of these is a MUST. RFC 9113 8.2.1 gives the four field-validity rules as "the following minimal validation of field names and values" that implementations MUST perform, and states that a response containing a field violating any of them MUST be treated as malformed. 8.3 says the same for undefined or invalid pseudo-header fields, a repeated pseudo-header field name, a pseudo-header field appearing after a regular field line, and pseudo-header fields in a trailer section; a :status that isn't a three-digit code is an invalid value for the one pseudo-header 8.3.2 defines for responses, since it carries the status code from RFC 9110 15. 8.1.1 then requires that malformed responses be treated as a stream error of type PROTOCOL_ERROR, and that "Clients MUST NOT accept a malformed response", which is what Mint was doing by handing these to the caller.

The one check not on that list is the empty field name, which 8.2.1 covers at SHOULD strength by way of field-name = token in RFC 9110 5.1. Going the other way, the field-name check stays with the character ranges 8.2.1 enumerates and doesn't reject the non-tchar visible characters that 5.1 also prohibits, so it's short of full token validation rather than past it.

A :status value that isn't an integer, such as "abc" or "200 ", raised
ArgumentError from String.to_integer/1 inside stream/2, so a server could
crash the process streaming the response. Uppercase or otherwise invalid
field names, values containing NUL, CR or LF, undefined pseudo-headers,
duplicate :status fields, pseudo-headers after regular fields and
pseudo-headers in trailers were all delivered to the caller as if they
were regular headers.

Response header blocks are now checked against RFC 9113 8.2.1 and 8.3
before they're interpreted. A malformed block is a stream error: the
stream is reset with PROTOCOL_ERROR and the caller gets
{:invalid_status_header, value}, {:invalid_header_name, name},
{:invalid_header_value, name, value} or {:protocol_error, debug_data}.

A 1xx block after the final response is now reported as a pseudo-header
in trailers, which made the dedicated check for it unreachable.
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 388

Coverage increased (+0.2%) to 88.627%

Details

  • Coverage increased (+0.2%) from the base build.
  • Patch coverage: 33 of 33 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 1741
Covered Lines: 1543
Line Coverage: 88.63%
Coverage Strength: 552.25 hits per line

💛 - Coveralls

@ericmj
ericmj marked this pull request as ready for review September 19, 2026 22:30
@ericmj
ericmj marked this pull request as draft September 19, 2026 23:03
@ericmj
ericmj marked this pull request as ready for review September 19, 2026 23:18
@ericmj
ericmj merged commit 4385c9e into main Sep 21, 2026
3 checks passed
@ericmj
ericmj deleted the validate-http2-response-headers branch September 21, 2026 08:29
ericmj added a commit that referenced this pull request Oct 25, 2026
* Validate HTTP/2 request methods, targets and header fields

Mint.HTTP2.request/5 lowercased the header names it was given and
HPACK-encoded everything else as is, and stream_request_body/3 did the
same for trailers. A method, :path, CONNECT target, header name or
header value containing CR, LF or NUL, a header name containing a space
or a colon, or a value with leading or trailing whitespace was sent to
the server unchanged. Mint.HTTP1 rejects all of these, and Mint.HTTP2
rejects them in responses since #504, so the behaviour of a request
depended on which protocol ALPN negotiated.

RFC 9113 8.2.1 forbids these characters in field names and values and
says a sender must not generate such a message. HTTP/2 framing isn't
confused by them, but an intermediary that forwards the request over
HTTP/1.1 without re-validating the fields turns an injected CRLF into
request header injection or request splitting on its back-end
connection, so an application that is safe against an HTTP/1 origin
isn't once the origin or a CDN in front of it enables HTTP/2.

Requests and trailers are now validated before a stream is opened, with
the field rules already used for responses: the method must be a token
(RFC 9110 9.1), :path and the CONNECT :authority must be non-empty
field values (RFC 9113 8.3.1), pseudo-header names passed by the caller
must be valid field names, and trailers can't carry pseudo-headers (RFC
9113 8.1). Invalid input fails with :invalid_request_method,
:invalid_request_target, :invalid_header_name or :invalid_header_value,
like in Mint.HTTP1, and the connection stays open without the request
taking up a stream. The messages for the two header errors no longer
mention the response, since they now apply to requests as well.

* Validate caller-supplied HTTP/2 method fields

* Clarify HTTP/2 request field validation documentation

---------

Co-authored-by: Eric Meadows-Jönsson <eric.meadows.jonsson@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants