From 388d5336422434bc759954d4c828f85d9ab1f511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Meadows-J=C3=B6nsson?= Date: Sun, 20 Sep 2026 00:08:58 +0200 Subject: [PATCH] Validate HTTP/2 response header fields and pseudo-headers 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. --- lib/mint/http2.ex | 120 +++++++++++++++++++++++--- test/mint/http2/conn_test.exs | 155 +++++++++++++++++++++++++++++++++- 2 files changed, 261 insertions(+), 14 deletions(-) diff --git a/lib/mint/http2.ex b/lib/mint/http2.ex index 21527787..673f58b8 100644 --- a/lib/mint/http2.ex +++ b/lib/mint/http2.ex @@ -366,6 +366,18 @@ defmodule Mint.HTTP2 do * `:request_is_not_streaming` - when you try to send data (with `stream_request_body/3`) on a request that is not open for streaming. + * `:missing_status_header` - when a response has no `:status` pseudo-header. + + * `{:invalid_status_header, value}` - when the `:status` pseudo-header of a response + is not a three-digit status code. `value` is the received value. + + * `{:invalid_header_name, name}` - when a response header name is invalid, for + example because it contains uppercase characters. + + * `{:invalid_header_value, name, value}` - when a response header value is invalid, + for example because it contains control characters. `name` is the name of the + header and `value` is the invalid value. + * `:unprocessed` - when a request was closed because it was not processed by the server. When this error is returned, it means that the server hasn't processed the request at all, so it's safe to retry the given request on a different or new connection. @@ -1988,22 +2000,25 @@ defmodule Mint.HTTP2 do defp handle_decoded_headers_for_stream(conn, responses, stream, headers, end_stream?) do %{ref: ref, received_first_headers?: received_first_headers?} = stream + case validate_response_headers(headers, _trailers? = received_first_headers?) do + :ok -> + handle_valid_headers_for_stream(conn, responses, stream, headers, end_stream?) + + {:error, reason} -> + conn = close_stream!(conn, stream.id, :protocol_error) + {conn, [{:error, ref, wrap_error(reason)} | responses]} + end + end + + defp handle_valid_headers_for_stream(conn, responses, stream, headers, end_stream?) do + %{ref: ref, received_first_headers?: received_first_headers?} = stream + case headers do # Interim response (1xx), which is made of only one HEADERS plus zero or more CONTINUATIONs. # There can be zero or more interim responses before a "proper" response. # https://httpwg.org/specs/rfc9113.html#HttpFraming [{":status", <> = status} | headers] -> cond do - received_first_headers? -> - conn = close_stream!(conn, stream.id, :protocol_error) - - debug_data = - "informational response (1xx) must appear before final response, got a #{status} status" - - error = wrap_error({:protocol_error, debug_data}) - responses = [{:error, stream.ref, error} | responses] - {conn, responses} - end_stream? -> conn = close_stream!(conn, stream.id, :protocol_error) debug_data = "informational response (1xx) must not have the END_STREAM flag set" @@ -2093,9 +2108,80 @@ defmodule Mint.HTTP2 do end end + # RFC 9113 8.2.1 and 8.3: pseudo-header fields come before regular fields, only + # :status is defined for responses and it appears exactly once, trailers carry + # no pseudo-header fields, and field names and values are limited to the + # characters allowed by the RFC. + defp validate_response_headers(headers, trailers?) do + validate_response_headers(headers, trailers?, _status? = false, _regular? = false) + end + + defp validate_response_headers([], _trailers?, _status?, _regular?), do: :ok + + defp validate_response_headers([{":" <> _ = name, value} | rest], trailers?, status?, regular?) do + cond do + trailers? -> + {:error, {:protocol_error, "pseudo-header #{inspect(name)} is not allowed in trailers"}} + + regular? -> + {:error, + {:protocol_error, + "pseudo-header #{inspect(name)} must appear before regular header fields"}} + + name != ":status" -> + {:error, {:protocol_error, "undefined pseudo-header #{inspect(name)} in response"}} + + status? -> + {:error, {:protocol_error, "the :status pseudo-header appears more than once"}} + + not valid_status?(value) -> + {:error, {:invalid_status_header, value}} + + true -> + validate_response_headers(rest, trailers?, true, regular?) + end + end + + defp validate_response_headers([{name, value} | rest], trailers?, status?, _regular?) do + cond do + not valid_field_name?(name) -> {:error, {:invalid_header_name, name}} + not valid_field_value?(value) -> {:error, {:invalid_header_value, name, value}} + true -> validate_response_headers(rest, trailers?, status?, true) + end + end + + # RFC 9110 15: status-code = 3DIGIT + defp valid_status?(<>) when a in ?0..?9 and b in ?0..?9 and c in ?0..?9, do: true + defp valid_status?(_other), do: false + + # RFC 9113 8.2.1: a field name must not contain characters in 0x00-0x20, 0x41-0x5A + # (uppercase letters) or 0x7F-0xFF, and only a pseudo-header field can contain a colon. + defp valid_field_name?(<<>>), do: false + defp valid_field_name?(name), do: field_name_chars?(name) + + defp field_name_chars?(<>) + when char in 0x00..0x20 or char in ?A..?Z or char in 0x7F..0xFF or char == ?:, + do: false + + defp field_name_chars?(<<_char, rest::binary>>), do: field_name_chars?(rest) + defp field_name_chars?(<<>>), do: true + + # RFC 9113 8.2.1: a field value must not contain NUL, LF or CR and must not start or + # end with SP or HTAB. + defp valid_field_value?(<>) when char in [?\s, ?\t], do: false + + defp valid_field_value?(value) do + byte_size(value) == 0 or + (:binary.last(value) not in [?\s, ?\t] and field_value_chars?(value)) + end + + defp field_value_chars?(<>) when char in [0, ?\n, ?\r], do: false + defp field_value_chars?(<<_char, rest::binary>>), do: field_value_chars?(rest) + defp field_value_chars?(<<>>), do: true + defp join_cookie_headers(headers) do # If we have 0 or 1 Cookie headers, we just use the old list of headers. - case Enum.split_with(headers, fn {name, _value} -> Headers.lower_raw(name) == "cookie" end) do + case Enum.split_with(headers, fn {name, _value} -> name == "cookie" end) do {[], _headers} -> headers @@ -2702,6 +2788,18 @@ defmodule Mint.HTTP2 do "the :status pseudo-header (which is required in HTTP/2) is missing from the response" end + def format_error({:invalid_status_header, value}) do + "invalid :status pseudo-header in the response: #{inspect(value)}" + end + + def format_error({:invalid_header_name, name}) do + "invalid header name in the response: #{inspect(name)}" + end + + def format_error({:invalid_header_value, name, value}) do + "invalid value for header #{inspect(name)} in the response: #{inspect(value)}" + end + def format_error({:server_closed_request, error_code}) do "server closed request with error code #{inspect(error_code)}" end diff --git a/test/mint/http2/conn_test.exs b/test/mint/http2/conn_test.exs index b323be46..12f4a901 100644 --- a/test/mint/http2/conn_test.exs +++ b/test/mint/http2/conn_test.exs @@ -1129,7 +1129,7 @@ defmodule Mint.HTTP2Test do {":status", "200"}, {"accept", "text/plain"}, {"cookie", "a=b"}, - {"Cookie", "c=d; e=f"}, + {"cookie", "c=d; e=f"}, {"content-type", "application/json"}, {"cookie", "g=h"}, {"x-header", "value"} @@ -1195,6 +1195,156 @@ defmodule Mint.HTTP2Test do end end + describe "response header validation" do + for status <- ["abc", "", "+200", "2000", "20", "200 ", " 200", "1ab"] do + test "an invalid :status of #{inspect(status)} is a stream error", %{conn: conn} do + {conn, ref} = open_request(conn) + + assert_recv_frames [headers(stream_id: stream_id)] + + assert {:ok, %HTTP2{} = conn, responses} = + stream_frames(conn, [ + {:headers, stream_id, [{":status", unquote(status)}], [:end_headers]} + ]) + + assert [{:error, ^ref, error}] = responses + assert_http2_error error, {:invalid_status_header, unquote(status)} + + assert_recv_frames [rst_stream(stream_id: ^stream_id, error_code: :protocol_error)] + assert HTTP2.open?(conn) + end + end + + for name <- ["Foo", "fo o", "", "foo:bar", "f\x7Fo", "f\xC3\xA4"] do + test "an invalid header name #{inspect(name)} is a stream error", %{conn: conn} do + {conn, ref} = open_request(conn) + + assert_recv_frames [headers(stream_id: stream_id)] + + assert {:ok, %HTTP2{} = conn, responses} = + stream_frames(conn, [ + {:headers, stream_id, [{":status", "200"}, {unquote(name), "bar"}], + [:end_headers, :end_stream]} + ]) + + assert [{:error, ^ref, error}] = responses + assert_http2_error error, {:invalid_header_name, unquote(name)} + + assert_recv_frames [rst_stream(stream_id: ^stream_id, error_code: :protocol_error)] + assert HTTP2.open?(conn) + end + end + + for value <- ["b\0ar", "b\rar", "b\nar", " bar", "bar ", "bar\t"] do + test "an invalid header value #{inspect(value)} is a stream error", %{conn: conn} do + {conn, ref} = open_request(conn) + + assert_recv_frames [headers(stream_id: stream_id)] + + assert {:ok, %HTTP2{} = conn, responses} = + stream_frames(conn, [ + {:headers, stream_id, [{":status", "200"}, {"foo", unquote(value)}], + [:end_headers, :end_stream]} + ]) + + assert [{:error, ^ref, error}] = responses + assert_http2_error error, {:invalid_header_value, "foo", unquote(value)} + + assert_recv_frames [rst_stream(stream_id: ^stream_id, error_code: :protocol_error)] + assert HTTP2.open?(conn) + end + end + + test "header values may be empty or contain obs-text and inner whitespace", %{conn: conn} do + {conn, ref} = open_request(conn) + + assert_recv_frames [headers(stream_id: stream_id)] + + headers = [ + {":status", "200"}, + {"empty", ""}, + {"foo", "b\xC3\xA4r \tbaz"}, + {"a-b_c.d!", "v"} + ] + + assert {:ok, %HTTP2{} = conn, responses} = + stream_frames(conn, [{:headers, stream_id, headers, [:end_headers, :end_stream]}]) + + assert responses == [ + {:status, ref, 200}, + {:headers, ref, tl(headers)}, + {:done, ref} + ] + + assert HTTP2.open?(conn) + end + + for {label, headers, debug_data} <- [ + {"a pseudo-header after a regular header", [{"foo", "bar"}, {":status", "200"}], + "must appear before regular header fields"}, + {"an undefined pseudo-header", [{":status", "200"}, {":path", "/"}], + "undefined pseudo-header \":path\""}, + {"a duplicate :status pseudo-header", [{":status", "200"}, {":status", "404"}], + "appears more than once"} + ] do + test "#{label} is a stream error", %{conn: conn} do + {conn, ref} = open_request(conn) + + assert_recv_frames [headers(stream_id: stream_id)] + + assert {:ok, %HTTP2{} = conn, responses} = + stream_frames(conn, [ + {:headers, stream_id, unquote(headers), [:end_headers, :end_stream]} + ]) + + assert [{:error, ^ref, error}] = responses + assert_http2_error error, {:protocol_error, debug_data} + assert debug_data =~ unquote(debug_data) + + assert_recv_frames [rst_stream(stream_id: ^stream_id, error_code: :protocol_error)] + assert HTTP2.open?(conn) + end + end + + test "a pseudo-header in trailers is a stream error", %{conn: conn} do + {conn, ref} = open_request(conn) + + assert_recv_frames [headers(stream_id: stream_id)] + + assert {:ok, %HTTP2{} = conn, responses} = + stream_frames(conn, [ + {:headers, stream_id, [{":status", "200"}], [:end_headers]}, + {:headers, stream_id, [{":status", "500"}, {"x-trailer", "v"}], + [:end_headers, :end_stream]} + ]) + + assert [{:status, ^ref, 200}, {:headers, ^ref, []}, {:error, ^ref, error}] = responses + assert_http2_error error, {:protocol_error, debug_data} + assert debug_data =~ "not allowed in trailers" + + assert_recv_frames [rst_stream(stream_id: ^stream_id, error_code: :protocol_error)] + assert HTTP2.open?(conn) + end + + test "an invalid header name in trailers is a stream error", %{conn: conn} do + {conn, ref} = open_request(conn) + + assert_recv_frames [headers(stream_id: stream_id)] + + assert {:ok, %HTTP2{} = conn, responses} = + stream_frames(conn, [ + {:headers, stream_id, [{":status", "200"}], [:end_headers]}, + {:headers, stream_id, [{"X-Trailer", "v"}], [:end_headers, :end_stream]} + ]) + + assert [{:status, ^ref, 200}, {:headers, ^ref, []}, {:error, ^ref, error}] = responses + assert_http2_error error, {:invalid_header_name, "X-Trailer"} + + assert_recv_frames [rst_stream(stream_id: ^stream_id, error_code: :protocol_error)] + assert HTTP2.open?(conn) + end + end + describe "interim responses (1xx)" do test "multiple before a single HEADERS", %{conn: conn} do info_status1 = Enum.random(100..199) @@ -1313,8 +1463,7 @@ defmodule Mint.HTTP2Test do assert_http2_error error, {:protocol_error, debug_data} - assert debug_data =~ - "informational response (1xx) must appear before final response, got a 101 status" + assert debug_data =~ "pseudo-header \":status\" is not allowed in trailers" assert HTTP2.open?(conn) end