From ce370fefeafddbc13682d6c46a8f1f3e6837a07f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eric=20Meadows-J=C3=B6nsson?= Date: Mon, 16 Nov 2026 17:42:29 +0100 Subject: [PATCH] Reset HTTP/2 pushes for other origins Mint.HTTP2 checked that a PUSH_PROMISE carried non-empty :scheme and :authority pseudo-headers but never compared them to the connection, so a server on https://a.example could promise a response for https://b.example and Mint returned it to the caller as a :push_promise. RFC 9113 8.4.1 requires the client to check that the server is authoritative for a pushed request and to treat a push that fails the check as a stream error of type PROTOCOL_ERROR. A caller that stores pushed responses under the promised URL would otherwise store another origin's content. Pushes are now accepted only for the connection's own origin: the promised :scheme must equal the connection's scheme and the promised :authority must equal the connection's authority, ignoring case and an explicit default port. Other pushes reset the promised stream with PROTOCOL_ERROR, like the other invalid promised requests. RFC 9113 10.1 also allows pushes for any origin covered by the server's certificate, which Mint doesn't track per connection, so those are reset as well. --- lib/mint/http2.ex | 47 ++++++++++++-- test/mint/http2/conn_test.exs | 117 +++++++++++++++++++++++++++------- test/mint/http2/fuzz_test.exs | 2 + 3 files changed, 137 insertions(+), 29 deletions(-) diff --git a/lib/mint/http2.ex b/lib/mint/http2.ex index 9ad856e0..c1a2c2c2 100644 --- a/lib/mint/http2.ex +++ b/lib/mint/http2.ex @@ -100,7 +100,11 @@ defmodule Mint.HTTP2 do `promised_headers` are headers that tell the client *what request* the promised response will respond to. The idea is that the server tells the client a request the client will want to make and then preemptively sends a response for that request. Promised headers - will always include `:method`, `:path`, and `:authority`. + will always include `:method`, `:scheme`, `:path`, and `:authority`. + + Mint only accepts pushes for the connection's own origin: the promised `:scheme` and + `:authority` must match the connection's scheme, host, and port. Mint resets pushes for + any other origin with a `PROTOCOL_ERROR` and doesn't return them to the caller. next_message = receive do @@ -2725,7 +2729,7 @@ defmodule Mint.HTTP2 do conn = reset_promised_stream(conn, promised_stream_id, :refused_stream) {conn, responses} - debug_data = promised_headers_error(headers) -> + debug_data = promised_headers_error(conn, headers) -> log(conn, :debug, "Resetting promised stream #{promised_stream_id}: #{debug_data}") conn = reset_promised_stream(conn, promised_stream_id, :protocol_error) {conn, responses} @@ -2804,7 +2808,9 @@ defmodule Mint.HTTP2 do # content, and RFC 9113 8.4 and 8.3.1 require the :method, :scheme, :authority and # :path pseudo-headers. Field names and values follow the same rules as response # headers, except that "te" is allowed with the "trailers" value (RFC 9113 8.2.2). - defp promised_headers_error(headers) do + # RFC 9113 8.4.1 also requires the client to check that the server is authoritative + # for the promised request. Mint only accepts the connection's own origin. + defp promised_headers_error(conn, headers) do case validate_promised_fields(headers, _pseudo = %{}, _regular? = false) do {:error, debug_data} -> debug_data @@ -2828,14 +2834,43 @@ defmodule Mint.HTTP2 do true -> case content_length(headers) do - {:ok, content_length} when content_length in [nil, 0] -> nil - {:ok, _content_length} -> "promised request must not have content" - {:error, _reason} -> "invalid content-length header in promised request" + {:ok, content_length} when content_length in [nil, 0] -> + promised_origin_error(conn, pseudo) + + {:ok, _content_length} -> + "promised request must not have content" + + {:error, _reason} -> + "invalid content-length header in promised request" end end end end + defp promised_origin_error(conn, pseudo) do + cond do + String.downcase(pseudo[":scheme"], :ascii) != conn.scheme -> + "promised request scheme #{inspect(pseudo[":scheme"])} is not the connection's " <> + "scheme #{inspect(conn.scheme)}" + + normalize_authority(pseudo[":authority"], conn.scheme) != + normalize_authority(conn.authority, conn.scheme) -> + "promised request authority #{inspect(pseudo[":authority"])} is not the " <> + "connection's authority #{inspect(conn.authority)}" + + true -> + nil + end + end + + # Hosts are case-insensitive and an explicit default port names the same origin as + # no port (RFC 3986 6.2.2.1 and 6.2.3). + defp normalize_authority(authority, scheme) do + authority + |> String.downcase(:ascii) + |> String.replace_suffix(":#{URI.default_port(scheme)}", "") + end + defp validate_promised_fields([], pseudo, _regular?), do: {:ok, pseudo} defp validate_promised_fields([{":" <> _ = name, value} | rest], pseudo, regular?) do diff --git a/test/mint/http2/conn_test.exs b/test/mint/http2/conn_test.exs index 0cc4ae4c..036bbd3c 100644 --- a/test/mint/http2/conn_test.exs +++ b/test/mint/http2/conn_test.exs @@ -1279,7 +1279,7 @@ defmodule Mint.HTTP2Test do assert_recv_frames [headers(stream_id: stream_id)] - hbf = server_encode_headers(promised_headers() ++ for(i <- 1..40, do: {"h#{i}", "v"})) + hbf = server_encode_headers(promised_headers(conn) ++ for(i <- 1..40, do: {"h#{i}", "v"})) assert {:ok, %HTTP2{} = conn, []} = stream_frames(conn, [ @@ -2295,7 +2295,7 @@ defmodule Mint.HTTP2Test do assert_recv_frames [headers(stream_id: stream_id)] - promised_hbf = server_encode_headers(promised_headers()) + promised_hbf = server_encode_headers(promised_headers(conn)) hbf1 = server_encode_headers([{":status", "200"}]) hbf2 = server_encode_headers([{":status", "200"}]) trailer_hbf = server_encode_headers([{"x-trailer", "some value"}]) @@ -2337,7 +2337,7 @@ defmodule Mint.HTTP2Test do {:done, promised_ref} ] = responses - assert promised_headers == promised_headers() + assert promised_headers == promised_headers(conn) assert HTTP2.open?(conn) end @@ -2424,7 +2424,7 @@ defmodule Mint.HTTP2Test do stream_frames(conn, [ push_promise( stream_id: stream_id, - hbf: server_encode_headers(promised_headers()), + hbf: server_encode_headers(promised_headers(conn)), promised_stream_id: 2, flags: set_flags(:push_promise, [:end_headers]) ) @@ -2459,7 +2459,7 @@ defmodule Mint.HTTP2Test do stream_frames(conn, [ push_promise( stream_id: stream_id, - hbf: server_encode_headers(promised_headers()), + hbf: server_encode_headers(promised_headers(conn)), promised_stream_id: 2, flags: set_flags(:push_promise, [:end_headers]) ) @@ -2486,7 +2486,7 @@ defmodule Mint.HTTP2Test do stream_frames(conn, [ push_promise( stream_id: stream_id, - hbf: server_encode_headers(promised_headers()), + hbf: server_encode_headers(promised_headers(conn)), promised_stream_id: 2, flags: set_flags(:push_promise, [:end_headers]) ), @@ -2626,7 +2626,7 @@ defmodule Mint.HTTP2Test do assert_recv_frames [headers(stream_id: stream_id)] - promised_headers = promised_headers() ++ unquote(fields) + promised_headers = promised_headers(conn) ++ unquote(fields) assert {:ok, %HTTP2{} = conn, [{:push_promise, ^ref, _promised_ref, ^promised_headers}]} = stream_frames(conn, [ @@ -2643,6 +2643,77 @@ defmodule Mint.HTTP2Test do end end + test "a PUSH_PROMISE for another origin resets the promised stream", %{conn: conn} do + {conn, _ref} = open_request(conn) + + assert_recv_frames [headers(stream_id: stream_id)] + + other_origins = [ + {"https", "example.com"}, + {"https", "localhost:1"}, + {"http", conn.authority} + ] + + frames = + for {{scheme, authority}, index} <- Enum.with_index(other_origins, 1) do + promised_headers = + [{":method", "GET"}, {":scheme", scheme}, {":authority", authority}, {":path", "/"}] + + push_promise( + stream_id: stream_id, + hbf: server_encode_headers(promised_headers), + promised_stream_id: index * 2, + flags: set_flags(:push_promise, [:end_headers]) + ) + end + + assert {:ok, %HTTP2{} = conn, []} = stream_frames(conn, frames) + + assert_recv_frames [ + rst_stream(stream_id: 2, error_code: :protocol_error), + rst_stream(stream_id: 4, error_code: :protocol_error), + rst_stream(stream_id: 6, error_code: :protocol_error) + ] + + refute Map.has_key?(conn.streams, 2) + refute Map.has_key?(conn.streams, 4) + refute Map.has_key?(conn.streams, 6) + assert HTTP2.open?(conn) + end + + test "a PUSH_PROMISE for the connection's origin is accepted regardless of case " <> + "and an explicit default port", + %{conn: conn} do + # The authority of a connection on the scheme's default port has no port. + conn = %{conn | authority: "localhost"} + {conn, ref} = open_request(conn) + + assert_recv_frames [headers(stream_id: stream_id)] + + frames = + for {{scheme, authority}, index} <- + Enum.with_index([{"https", "LocalHost"}, {"HTTPS", "localhost:443"}], 1) do + promised_headers = + [{":method", "GET"}, {":scheme", scheme}, {":authority", authority}, {":path", "/"}] + + push_promise( + stream_id: stream_id, + hbf: server_encode_headers(promised_headers), + promised_stream_id: index * 2, + flags: set_flags(:push_promise, [:end_headers]) + ) + end + + assert {:ok, %HTTP2{} = conn, + [ + {:push_promise, ^ref, _, [_, _, {":authority", "LocalHost"}, _]}, + {:push_promise, ^ref, _, [_, _, {":authority", "localhost:443"}, _]} + ]} = stream_frames(conn, frames) + + refute_receive {:ssl, _socket, _data}, 100 + assert HTTP2.open?(conn) + end + for {name, promised_stream_id} <- [ {"zero", 0}, {"odd", 3}, @@ -2658,7 +2729,7 @@ defmodule Mint.HTTP2Test do stream_frames(conn, [ push_promise( stream_id: stream_id, - hbf: server_encode_headers(promised_headers()), + hbf: server_encode_headers(promised_headers(conn)), promised_stream_id: 4, flags: set_flags(:push_promise, [:end_headers]) ) @@ -2668,7 +2739,7 @@ defmodule Mint.HTTP2Test do stream_frames(conn, [ push_promise( stream_id: stream_id, - hbf: server_encode_headers(promised_headers()), + hbf: server_encode_headers(promised_headers(conn)), promised_stream_id: unquote(promised_stream_id), flags: set_flags(:push_promise, [:end_headers]) ) @@ -2690,7 +2761,7 @@ defmodule Mint.HTTP2Test do stream_frames(conn, [ push_promise( stream_id: stream_id, - hbf: server_encode_headers(promised_headers()), + hbf: server_encode_headers(promised_headers(conn)), promised_stream_id: 2, flags: set_flags(:push_promise, [:end_headers]) ) @@ -2704,7 +2775,7 @@ defmodule Mint.HTTP2Test do stream_frames(conn, [ push_promise( stream_id: 2, - hbf: server_encode_headers(promised_headers()), + hbf: server_encode_headers(promised_headers(conn)), promised_stream_id: 4, flags: set_flags(:push_promise, [:end_headers]) ) @@ -2724,7 +2795,7 @@ defmodule Mint.HTTP2Test do stream_frames(conn, [ push_promise( stream_id: stream_id, - hbf: server_encode_headers(promised_headers()), + hbf: server_encode_headers(promised_headers(conn)), promised_stream_id: 4, flags: set_flags(:push_promise, [:end_headers]) ) @@ -2824,7 +2895,7 @@ defmodule Mint.HTTP2Test do assert_recv_frames [headers(stream_id: stream_id)] # Promised headers. - headers = promised_headers() ++ [{"foo", "bar"}, {"baz", "bong"}] + headers = promised_headers(conn) ++ [{"foo", "bar"}, {"baz", "bong"}] <> = server_encode_headers(headers) @@ -2859,7 +2930,7 @@ defmodule Mint.HTTP2Test do ] = responses assert is_reference(promised_ref) - assert headers == promised_headers() ++ [{"foo", "bar"}, {"baz", "bong"}] + assert headers == promised_headers(conn) ++ [{"foo", "bar"}, {"baz", "bong"}] assert {:ok, %HTTP2{} = conn, responses} = stream_frames(conn, [ @@ -2892,7 +2963,7 @@ defmodule Mint.HTTP2Test do assert_recv_frames [headers(stream_id: stream_id)] - hbf = server_encode_headers(promised_headers()) + hbf = server_encode_headers(promised_headers(conn)) assert {:error, %HTTP2{} = conn, error, []} = stream_frames(conn, [ @@ -2917,7 +2988,7 @@ defmodule Mint.HTTP2Test do assert_recv_frames [headers(stream_id: stream_id)] - promised_headers_hbf = server_encode_headers(promised_headers()) + promised_headers_hbf = server_encode_headers(promised_headers(conn)) normal_headers_hbf = server_encode_headers([{":status", "200"}]) assert {:error, %HTTP2{} = conn, error, _responses} = @@ -2956,7 +3027,7 @@ defmodule Mint.HTTP2Test do assert_recv_frames [headers(stream_id: stream_id)] - promised_headers_hbf = server_encode_headers(promised_headers()) + promised_headers_hbf = server_encode_headers(promised_headers(conn)) normal_headers_hbf = server_encode_headers([{":status", "200"}]) assert {:ok, %HTTP2{} = conn, responses} = @@ -3009,7 +3080,7 @@ defmodule Mint.HTTP2Test do stream_frames(conn, [ push_promise( stream_id: stream_id, - hbf: server_encode_headers(promised_headers()), + hbf: server_encode_headers(promised_headers(conn)), promised_stream_id: 2, flags: set_flags(:push_promise, [:end_headers]) ) @@ -3039,7 +3110,7 @@ defmodule Mint.HTTP2Test do assert_recv_frames [headers(stream_id: stream_id)] - promised_headers_hbf = server_encode_headers(promised_headers()) + promised_headers_hbf = server_encode_headers(promised_headers(conn)) # The server promises many more streams than the client's limit but never # follows up with the response HEADERS for any of them. Each promise must @@ -3091,7 +3162,7 @@ defmodule Mint.HTTP2Test do stream_frames(conn, [ push_promise( stream_id: stream_id, - hbf: server_encode_headers(promised_headers()), + hbf: server_encode_headers(promised_headers(conn)), promised_stream_id: 6, flags: set_flags(:push_promise, [:end_headers]) ) @@ -4415,7 +4486,7 @@ defmodule Mint.HTTP2Test do stream_frames(conn, [ push_promise( stream_id: stream_id, - hbf: server_encode_headers(promised_headers()), + hbf: server_encode_headers(promised_headers(conn)), promised_stream_id: 2, flags: set_flags(:push_promise, [:end_headers]) ) @@ -4906,8 +4977,8 @@ defmodule Mint.HTTP2Test do headers end - defp promised_headers do - [{":method", "GET"}, {":scheme", "https"}, {":authority", "localhost"}, {":path", "/"}] + defp promised_headers(conn) do + [{":method", "GET"}, {":scheme", "https"}, {":authority", conn.authority}, {":path", "/"}] end defp open_request(conn, body \\ nil) do diff --git a/test/mint/http2/fuzz_test.exs b/test/mint/http2/fuzz_test.exs index 62700894..14cb2aec 100644 --- a/test/mint/http2/fuzz_test.exs +++ b/test/mint/http2/fuzz_test.exs @@ -282,6 +282,7 @@ defmodule Mint.HTTP2.FuzzTest do refs: refs, promised: [], next_promised: 2, + authority: conn.authority, closed: closed_sid, server: Process.get(:fuzz_server), chunks: Stream.cycle(chunk_sizes), @@ -789,6 +790,7 @@ defmodule Mint.HTTP2.FuzzTest do do: %{state | next_promised: promised + 2, promised: [promised | state.promised]}, else: state + headers = List.keyreplace(headers, ":authority", 0, {":authority", state.authority}) {server, hbf} = TestServer.encode_headers(server, headers) bytes =