Skip to content

Commit 5f5ef66

Browse files
committed
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.
1 parent 7223c00 commit 5f5ef66

3 files changed

Lines changed: 137 additions & 29 deletions

File tree

‎lib/mint/http2.ex‎

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,11 @@ defmodule Mint.HTTP2 do
100100
`promised_headers` are headers that tell the client *what request* the promised response
101101
will respond to. The idea is that the server tells the client a request the client will
102102
want to make and then preemptively sends a response for that request. Promised headers
103-
will always include `:method`, `:path`, and `:authority`.
103+
will always include `:method`, `:scheme`, `:path`, and `:authority`.
104+
105+
Mint only accepts pushes for the connection's own origin: the promised `:scheme` and
106+
`:authority` must match the connection's scheme, host, and port. Mint resets pushes for
107+
any other origin with a `PROTOCOL_ERROR` and doesn't return them to the caller.
104108
105109
next_message =
106110
receive do
@@ -2725,7 +2729,7 @@ defmodule Mint.HTTP2 do
27252729
conn = reset_promised_stream(conn, promised_stream_id, :refused_stream)
27262730
{conn, responses}
27272731

2728-
debug_data = promised_headers_error(headers) ->
2732+
debug_data = promised_headers_error(conn, headers) ->
27292733
log(conn, :debug, "Resetting promised stream #{promised_stream_id}: #{debug_data}")
27302734
conn = reset_promised_stream(conn, promised_stream_id, :protocol_error)
27312735
{conn, responses}
@@ -2804,7 +2808,9 @@ defmodule Mint.HTTP2 do
28042808
# content, and RFC 9113 8.4 and 8.3.1 require the :method, :scheme, :authority and
28052809
# :path pseudo-headers. Field names and values follow the same rules as response
28062810
# headers, except that "te" is allowed with the "trailers" value (RFC 9113 8.2.2).
2807-
defp promised_headers_error(headers) do
2811+
# RFC 9113 8.4.1 also requires the client to check that the server is authoritative
2812+
# for the promised request. Mint only accepts the connection's own origin.
2813+
defp promised_headers_error(conn, headers) do
28082814
case validate_promised_fields(headers, _pseudo = %{}, _regular? = false) do
28092815
{:error, debug_data} ->
28102816
debug_data
@@ -2828,14 +2834,43 @@ defmodule Mint.HTTP2 do
28282834

28292835
true ->
28302836
case content_length(headers) do
2831-
{:ok, content_length} when content_length in [nil, 0] -> nil
2832-
{:ok, _content_length} -> "promised request must not have content"
2833-
{:error, _reason} -> "invalid content-length header in promised request"
2837+
{:ok, content_length} when content_length in [nil, 0] ->
2838+
promised_origin_error(conn, pseudo)
2839+
2840+
{:ok, _content_length} ->
2841+
"promised request must not have content"
2842+
2843+
{:error, _reason} ->
2844+
"invalid content-length header in promised request"
28342845
end
28352846
end
28362847
end
28372848
end
28382849

2850+
defp promised_origin_error(conn, pseudo) do
2851+
cond do
2852+
String.downcase(pseudo[":scheme"], :ascii) != conn.scheme ->
2853+
"promised request scheme #{inspect(pseudo[":scheme"])} is not the connection's " <>
2854+
"scheme #{inspect(conn.scheme)}"
2855+
2856+
normalize_authority(pseudo[":authority"], conn.scheme) !=
2857+
normalize_authority(conn.authority, conn.scheme) ->
2858+
"promised request authority #{inspect(pseudo[":authority"])} is not the " <>
2859+
"connection's authority #{inspect(conn.authority)}"
2860+
2861+
true ->
2862+
nil
2863+
end
2864+
end
2865+
2866+
# Hosts are case-insensitive and an explicit default port names the same origin as
2867+
# no port (RFC 3986 6.2.2.1 and 6.2.3).
2868+
defp normalize_authority(authority, scheme) do
2869+
authority
2870+
|> String.downcase(:ascii)
2871+
|> String.replace_suffix(":#{URI.default_port(scheme)}", "")
2872+
end
2873+
28392874
defp validate_promised_fields([], pseudo, _regular?), do: {:ok, pseudo}
28402875

28412876
defp validate_promised_fields([{":" <> _ = name, value} | rest], pseudo, regular?) do

‎test/mint/http2/conn_test.exs‎

Lines changed: 94 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,7 +1279,7 @@ defmodule Mint.HTTP2Test do
12791279

12801280
assert_recv_frames [headers(stream_id: stream_id)]
12811281

1282-
hbf = server_encode_headers(promised_headers() ++ for(i <- 1..40, do: {"h#{i}", "v"}))
1282+
hbf = server_encode_headers(promised_headers(conn) ++ for(i <- 1..40, do: {"h#{i}", "v"}))
12831283

12841284
assert {:ok, %HTTP2{} = conn, []} =
12851285
stream_frames(conn, [
@@ -2295,7 +2295,7 @@ defmodule Mint.HTTP2Test do
22952295

22962296
assert_recv_frames [headers(stream_id: stream_id)]
22972297

2298-
promised_hbf = server_encode_headers(promised_headers())
2298+
promised_hbf = server_encode_headers(promised_headers(conn))
22992299
hbf1 = server_encode_headers([{":status", "200"}])
23002300
hbf2 = server_encode_headers([{":status", "200"}])
23012301
trailer_hbf = server_encode_headers([{"x-trailer", "some value"}])
@@ -2337,7 +2337,7 @@ defmodule Mint.HTTP2Test do
23372337
{:done, promised_ref}
23382338
] = responses
23392339

2340-
assert promised_headers == promised_headers()
2340+
assert promised_headers == promised_headers(conn)
23412341
assert HTTP2.open?(conn)
23422342
end
23432343

@@ -2424,7 +2424,7 @@ defmodule Mint.HTTP2Test do
24242424
stream_frames(conn, [
24252425
push_promise(
24262426
stream_id: stream_id,
2427-
hbf: server_encode_headers(promised_headers()),
2427+
hbf: server_encode_headers(promised_headers(conn)),
24282428
promised_stream_id: 2,
24292429
flags: set_flags(:push_promise, [:end_headers])
24302430
)
@@ -2459,7 +2459,7 @@ defmodule Mint.HTTP2Test do
24592459
stream_frames(conn, [
24602460
push_promise(
24612461
stream_id: stream_id,
2462-
hbf: server_encode_headers(promised_headers()),
2462+
hbf: server_encode_headers(promised_headers(conn)),
24632463
promised_stream_id: 2,
24642464
flags: set_flags(:push_promise, [:end_headers])
24652465
)
@@ -2486,7 +2486,7 @@ defmodule Mint.HTTP2Test do
24862486
stream_frames(conn, [
24872487
push_promise(
24882488
stream_id: stream_id,
2489-
hbf: server_encode_headers(promised_headers()),
2489+
hbf: server_encode_headers(promised_headers(conn)),
24902490
promised_stream_id: 2,
24912491
flags: set_flags(:push_promise, [:end_headers])
24922492
),
@@ -2626,7 +2626,7 @@ defmodule Mint.HTTP2Test do
26262626

26272627
assert_recv_frames [headers(stream_id: stream_id)]
26282628

2629-
promised_headers = promised_headers() ++ unquote(fields)
2629+
promised_headers = promised_headers(conn) ++ unquote(fields)
26302630

26312631
assert {:ok, %HTTP2{} = conn, [{:push_promise, ^ref, _promised_ref, ^promised_headers}]} =
26322632
stream_frames(conn, [
@@ -2643,6 +2643,77 @@ defmodule Mint.HTTP2Test do
26432643
end
26442644
end
26452645

2646+
test "a PUSH_PROMISE for another origin resets the promised stream", %{conn: conn} do
2647+
{conn, _ref} = open_request(conn)
2648+
2649+
assert_recv_frames [headers(stream_id: stream_id)]
2650+
2651+
other_origins = [
2652+
{"https", "example.com"},
2653+
{"https", "localhost:1"},
2654+
{"http", conn.authority}
2655+
]
2656+
2657+
frames =
2658+
for {{scheme, authority}, index} <- Enum.with_index(other_origins, 1) do
2659+
promised_headers =
2660+
[{":method", "GET"}, {":scheme", scheme}, {":authority", authority}, {":path", "/"}]
2661+
2662+
push_promise(
2663+
stream_id: stream_id,
2664+
hbf: server_encode_headers(promised_headers),
2665+
promised_stream_id: index * 2,
2666+
flags: set_flags(:push_promise, [:end_headers])
2667+
)
2668+
end
2669+
2670+
assert {:ok, %HTTP2{} = conn, []} = stream_frames(conn, frames)
2671+
2672+
assert_recv_frames [
2673+
rst_stream(stream_id: 2, error_code: :protocol_error),
2674+
rst_stream(stream_id: 4, error_code: :protocol_error),
2675+
rst_stream(stream_id: 6, error_code: :protocol_error)
2676+
]
2677+
2678+
refute Map.has_key?(conn.streams, 2)
2679+
refute Map.has_key?(conn.streams, 4)
2680+
refute Map.has_key?(conn.streams, 6)
2681+
assert HTTP2.open?(conn)
2682+
end
2683+
2684+
test "a PUSH_PROMISE for the connection's origin is accepted regardless of case " <>
2685+
"and an explicit default port",
2686+
%{conn: conn} do
2687+
# The authority of a connection on the scheme's default port has no port.
2688+
conn = %{conn | authority: "localhost"}
2689+
{conn, ref} = open_request(conn)
2690+
2691+
assert_recv_frames [headers(stream_id: stream_id)]
2692+
2693+
frames =
2694+
for {{scheme, authority}, index} <-
2695+
Enum.with_index([{"https", "LocalHost"}, {"HTTPS", "localhost:443"}], 1) do
2696+
promised_headers =
2697+
[{":method", "GET"}, {":scheme", scheme}, {":authority", authority}, {":path", "/"}]
2698+
2699+
push_promise(
2700+
stream_id: stream_id,
2701+
hbf: server_encode_headers(promised_headers),
2702+
promised_stream_id: index * 2,
2703+
flags: set_flags(:push_promise, [:end_headers])
2704+
)
2705+
end
2706+
2707+
assert {:ok, %HTTP2{} = conn,
2708+
[
2709+
{:push_promise, ^ref, _, [_, _, {":authority", "LocalHost"}, _]},
2710+
{:push_promise, ^ref, _, [_, _, {":authority", "localhost:443"}, _]}
2711+
]} = stream_frames(conn, frames)
2712+
2713+
refute_receive {:ssl, _socket, _data}, 100
2714+
assert HTTP2.open?(conn)
2715+
end
2716+
26462717
for {name, promised_stream_id} <- [
26472718
{"zero", 0},
26482719
{"odd", 3},
@@ -2658,7 +2729,7 @@ defmodule Mint.HTTP2Test do
26582729
stream_frames(conn, [
26592730
push_promise(
26602731
stream_id: stream_id,
2661-
hbf: server_encode_headers(promised_headers()),
2732+
hbf: server_encode_headers(promised_headers(conn)),
26622733
promised_stream_id: 4,
26632734
flags: set_flags(:push_promise, [:end_headers])
26642735
)
@@ -2668,7 +2739,7 @@ defmodule Mint.HTTP2Test do
26682739
stream_frames(conn, [
26692740
push_promise(
26702741
stream_id: stream_id,
2671-
hbf: server_encode_headers(promised_headers()),
2742+
hbf: server_encode_headers(promised_headers(conn)),
26722743
promised_stream_id: unquote(promised_stream_id),
26732744
flags: set_flags(:push_promise, [:end_headers])
26742745
)
@@ -2690,7 +2761,7 @@ defmodule Mint.HTTP2Test do
26902761
stream_frames(conn, [
26912762
push_promise(
26922763
stream_id: stream_id,
2693-
hbf: server_encode_headers(promised_headers()),
2764+
hbf: server_encode_headers(promised_headers(conn)),
26942765
promised_stream_id: 2,
26952766
flags: set_flags(:push_promise, [:end_headers])
26962767
)
@@ -2704,7 +2775,7 @@ defmodule Mint.HTTP2Test do
27042775
stream_frames(conn, [
27052776
push_promise(
27062777
stream_id: 2,
2707-
hbf: server_encode_headers(promised_headers()),
2778+
hbf: server_encode_headers(promised_headers(conn)),
27082779
promised_stream_id: 4,
27092780
flags: set_flags(:push_promise, [:end_headers])
27102781
)
@@ -2724,7 +2795,7 @@ defmodule Mint.HTTP2Test do
27242795
stream_frames(conn, [
27252796
push_promise(
27262797
stream_id: stream_id,
2727-
hbf: server_encode_headers(promised_headers()),
2798+
hbf: server_encode_headers(promised_headers(conn)),
27282799
promised_stream_id: 4,
27292800
flags: set_flags(:push_promise, [:end_headers])
27302801
)
@@ -2824,7 +2895,7 @@ defmodule Mint.HTTP2Test do
28242895
assert_recv_frames [headers(stream_id: stream_id)]
28252896

28262897
# Promised headers.
2827-
headers = promised_headers() ++ [{"foo", "bar"}, {"baz", "bong"}]
2898+
headers = promised_headers(conn) ++ [{"foo", "bar"}, {"baz", "bong"}]
28282899

28292900
<<hbf1::1-bytes, hbf2::1-bytes, hbf3::binary>> = server_encode_headers(headers)
28302901

@@ -2859,7 +2930,7 @@ defmodule Mint.HTTP2Test do
28592930
] = responses
28602931

28612932
assert is_reference(promised_ref)
2862-
assert headers == promised_headers() ++ [{"foo", "bar"}, {"baz", "bong"}]
2933+
assert headers == promised_headers(conn) ++ [{"foo", "bar"}, {"baz", "bong"}]
28632934

28642935
assert {:ok, %HTTP2{} = conn, responses} =
28652936
stream_frames(conn, [
@@ -2892,7 +2963,7 @@ defmodule Mint.HTTP2Test do
28922963

28932964
assert_recv_frames [headers(stream_id: stream_id)]
28942965

2895-
hbf = server_encode_headers(promised_headers())
2966+
hbf = server_encode_headers(promised_headers(conn))
28962967

28972968
assert {:error, %HTTP2{} = conn, error, []} =
28982969
stream_frames(conn, [
@@ -2917,7 +2988,7 @@ defmodule Mint.HTTP2Test do
29172988

29182989
assert_recv_frames [headers(stream_id: stream_id)]
29192990

2920-
promised_headers_hbf = server_encode_headers(promised_headers())
2991+
promised_headers_hbf = server_encode_headers(promised_headers(conn))
29212992
normal_headers_hbf = server_encode_headers([{":status", "200"}])
29222993

29232994
assert {:error, %HTTP2{} = conn, error, _responses} =
@@ -2956,7 +3027,7 @@ defmodule Mint.HTTP2Test do
29563027

29573028
assert_recv_frames [headers(stream_id: stream_id)]
29583029

2959-
promised_headers_hbf = server_encode_headers(promised_headers())
3030+
promised_headers_hbf = server_encode_headers(promised_headers(conn))
29603031
normal_headers_hbf = server_encode_headers([{":status", "200"}])
29613032

29623033
assert {:ok, %HTTP2{} = conn, responses} =
@@ -3009,7 +3080,7 @@ defmodule Mint.HTTP2Test do
30093080
stream_frames(conn, [
30103081
push_promise(
30113082
stream_id: stream_id,
3012-
hbf: server_encode_headers(promised_headers()),
3083+
hbf: server_encode_headers(promised_headers(conn)),
30133084
promised_stream_id: 2,
30143085
flags: set_flags(:push_promise, [:end_headers])
30153086
)
@@ -3039,7 +3110,7 @@ defmodule Mint.HTTP2Test do
30393110

30403111
assert_recv_frames [headers(stream_id: stream_id)]
30413112

3042-
promised_headers_hbf = server_encode_headers(promised_headers())
3113+
promised_headers_hbf = server_encode_headers(promised_headers(conn))
30433114

30443115
# The server promises many more streams than the client's limit but never
30453116
# follows up with the response HEADERS for any of them. Each promise must
@@ -3091,7 +3162,7 @@ defmodule Mint.HTTP2Test do
30913162
stream_frames(conn, [
30923163
push_promise(
30933164
stream_id: stream_id,
3094-
hbf: server_encode_headers(promised_headers()),
3165+
hbf: server_encode_headers(promised_headers(conn)),
30953166
promised_stream_id: 6,
30963167
flags: set_flags(:push_promise, [:end_headers])
30973168
)
@@ -4415,7 +4486,7 @@ defmodule Mint.HTTP2Test do
44154486
stream_frames(conn, [
44164487
push_promise(
44174488
stream_id: stream_id,
4418-
hbf: server_encode_headers(promised_headers()),
4489+
hbf: server_encode_headers(promised_headers(conn)),
44194490
promised_stream_id: 2,
44204491
flags: set_flags(:push_promise, [:end_headers])
44214492
)
@@ -4906,8 +4977,8 @@ defmodule Mint.HTTP2Test do
49064977
headers
49074978
end
49084979

4909-
defp promised_headers do
4910-
[{":method", "GET"}, {":scheme", "https"}, {":authority", "localhost"}, {":path", "/"}]
4980+
defp promised_headers(conn) do
4981+
[{":method", "GET"}, {":scheme", "https"}, {":authority", conn.authority}, {":path", "/"}]
49114982
end
49124983

49134984
defp open_request(conn, body \\ nil) do

‎test/mint/http2/fuzz_test.exs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ defmodule Mint.HTTP2.FuzzTest do
282282
refs: refs,
283283
promised: [],
284284
next_promised: 2,
285+
authority: conn.authority,
285286
closed: closed_sid,
286287
server: Process.get(:fuzz_server),
287288
chunks: Stream.cycle(chunk_sizes),
@@ -789,6 +790,7 @@ defmodule Mint.HTTP2.FuzzTest do
789790
do: %{state | next_promised: promised + 2, promised: [promised | state.promised]},
790791
else: state
791792

793+
headers = List.keyreplace(headers, ":authority", 0, {":authority", state.authority})
792794
{server, hbf} = TestServer.encode_headers(server, headers)
793795

794796
bytes =

0 commit comments

Comments
 (0)