Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 41 additions & 6 deletions lib/mint/http2.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
117 changes: 94 additions & 23 deletions test/mint/http2/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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, [
Expand Down Expand Up @@ -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"}])
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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])
)
Expand Down Expand Up @@ -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])
)
Expand All @@ -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])
),
Expand Down Expand Up @@ -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, [
Expand All @@ -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},
Expand All @@ -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])
)
Expand All @@ -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])
)
Expand All @@ -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])
)
Expand All @@ -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])
)
Expand All @@ -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])
)
Expand Down Expand Up @@ -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"}]

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

Expand Down Expand Up @@ -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, [
Expand Down Expand Up @@ -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, [
Expand All @@ -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} =
Expand Down Expand Up @@ -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} =
Expand Down Expand Up @@ -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])
)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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])
)
Expand Down Expand Up @@ -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])
)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions test/mint/http2/fuzz_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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 =
Expand Down
Loading