Skip to content

Commit f79a60a

Browse files
committed
Apply the HTTP/2 header table size to the decoding table as a limit
HPAX.resize/2 sets the table's own maximum size along with the maximum the protocol permits. For the decoding table that maximum belongs to the server's encoder, which declares it with a dynamic table size update (RFC 7541 4.2), so raising SETTINGS_HEADER_TABLE_SIZE also raised it and kept entries the server had evicted. An indexed reference to one of them decoded to a stale header instead of failing with a compression error. HPAX.protocol_resize/2 changes only the permitted maximum and follows it down when it drops below what the encoder declared. After a reduction the server has to declare its new size at the start of the next field block, which RFC 9113 4.3.1 requires the client to enforce, so a block that doesn't is now a connection error with COMPRESSION_ERROR.
1 parent 6184a56 commit f79a60a

4 files changed

Lines changed: 139 additions & 3 deletions

File tree

‎lib/mint/http2.ex‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2221,7 +2221,7 @@ defmodule Mint.HTTP2 do
22212221
update_client_initial_window_size(conn, initial_window_size)
22222222

22232223
{:header_table_size, header_table_size}, conn ->
2224-
conn = update_in(conn.decode_table, &HPAX.resize(&1, header_table_size))
2224+
conn = update_in(conn.decode_table, &HPAX.protocol_resize(&1, header_table_size))
22252225
put_in(conn.client_settings.header_table_size, header_table_size)
22262226

22272227
{setting, value}, conn when setting in unquote(valid_client_settings_without_iws) ->

‎mix.exs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ defmodule Mint.MixProject do
7777
defp deps do
7878
[
7979
{:castore, "~> 0.1.0 or ~> 1.0", optional: true},
80-
{:hpax, "~> 0.1.1 or ~> 0.2.0 or ~> 1.0"},
80+
{:hpax, github: "elixir-mint/hpax"},
8181

8282
# Dev/test dependencies
8383
{:dialyxir, "~> 1.4", only: [:dev, :test], runtime: false},

‎mix.lock‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"erlex": {:hex, :erlex, "0.2.9", "7debbbaa9f4f368b8cd648983e0f1d7963028508e9c59e9d4ed504e94ef52a55", [:mix], [], "hexpm", "8cfffc0ec7159e6d73de2ab28a588064de80f88b2798d5cbe4482cbbc200178b"},
66
"ex_doc": {:hex, :ex_doc, "0.40.3", "4a972ffe64bc07dc605af487e98fc19b72a4185f55ca031b94c0552d6071c1d9", [:mix], [{:earmark_parser, "~> 1.4.44", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.0", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14 or ~> 1.0", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1 or ~> 1.0", [hex: :makeup_erlang, repo: "hexpm", optional: false]}, {:makeup_html, ">= 0.1.0", [hex: :makeup_html, repo: "hexpm", optional: true]}], "hexpm", "2756e357742fecd9749b489b85d67c9ce99c465f2e75728d9e6dc8d704b973de"},
77
"excoveralls": {:hex, :excoveralls, "0.18.5", "e229d0a65982613332ec30f07940038fe451a2e5b29bce2a5022165f0c9b157e", [:mix], [{:castore, "~> 1.0", [hex: :castore, repo: "hexpm", optional: true]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "523fe8a15603f86d64852aab2abe8ddbd78e68579c8525ae765facc5eae01562"},
8-
"hpax": {:hex, :hpax, "1.0.3", "ed67ef51ad4df91e75cc6a1494f851850c0bd98ebc0be6e81b026e765ee535aa", [:mix], [], "hexpm", "8eab6e1cfa8d5918c2ce4ba43588e894af35dbd8e91e6e55c817bca5847df34a"},
8+
"hpax": {:git, "https://github.com/elixir-mint/hpax.git", "f7d69fc7b33310c4da9165d44ba56cee97df4989", []},
99
"jason": {:hex, :jason, "1.4.4", "b9226785a9aa77b6857ca22832cffa5d5011a667207eb2a0ad56adb5db443b8a", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "c5eb0cab91f094599f94d55bc63409236a8ec69a21a67814529e8d5f6cc90b3b"},
1010
"makeup": {:hex, :makeup, "1.2.1", "e90ac1c65589ef354378def3ba19d401e739ee7ee06fb47f94c687016e3713d1", [:mix], [{:nimble_parsec, "~> 1.4", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "d36484867b0bae0fea568d10131197a4c2e47056a6fbe84922bf6ba71c8d17ce"},
1111
"makeup_elixir": {:hex, :makeup_elixir, "1.0.1", "e928a4f984e795e41e3abd27bfc09f51db16ab8ba1aebdba2b3a575437efafc2", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "7284900d412a3e5cfd97fdaed4f5ed389b8f2b4cb49efc0eb3bd10e2febf9507"},

‎test/mint/http2/conn_test.exs‎

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2271,6 +2271,122 @@ defmodule Mint.HTTP2Test do
22712271
])
22722272
end
22732273

2274+
test "a raised header table size leaves the table size to the server", %{conn: conn} do
2275+
{conn, ref} = open_request(conn)
2276+
2277+
assert_recv_frames [headers(stream_id: stream_id)]
2278+
2279+
# The server picks a 64 byte table and stores ":status: 404" in it, which takes 42 bytes.
2280+
assert {:ok, %HTTP2{} = conn, responses} =
2281+
stream_frames(conn, [
2282+
headers(
2283+
stream_id: stream_id,
2284+
hbf: <<0x3F, 0x21, 0x48, 3, "404">>,
2285+
flags: set_flags(:headers, [:end_headers, :end_stream])
2286+
)
2287+
])
2288+
2289+
assert [{:status, ^ref, 404}, {:headers, ^ref, []}, {:done, ^ref}] = responses
2290+
2291+
{:ok, conn} = HTTP2.put_settings(conn, header_table_size: 8192)
2292+
assert_recv_frames [settings(params: [header_table_size: 8192])]
2293+
2294+
assert {:ok, %HTTP2{} = conn, []} =
2295+
stream_frames(conn, [settings(flags: set_flags(:settings, [:ack]), params: [])])
2296+
2297+
# The server didn't ask for more room, so storing ":status: 500" evicts ":status: 404".
2298+
{conn, ref} = open_request(conn)
2299+
2300+
assert_recv_frames [headers(stream_id: stream_id)]
2301+
2302+
assert {:ok, %HTTP2{} = conn, responses} =
2303+
stream_frames(conn, [
2304+
headers(
2305+
stream_id: stream_id,
2306+
hbf: <<0x48, 3, "500">>,
2307+
flags: set_flags(:headers, [:end_headers, :end_stream])
2308+
)
2309+
])
2310+
2311+
assert [{:status, ^ref, 500}, {:headers, ^ref, []}, {:done, ^ref}] = responses
2312+
2313+
{conn, _ref} = open_request(conn)
2314+
2315+
assert_recv_frames [headers(stream_id: stream_id)]
2316+
2317+
assert {:error, %HTTP2{} = conn, error, []} =
2318+
stream_frames(conn, [
2319+
headers(
2320+
stream_id: stream_id,
2321+
hbf: <<0xBF>>,
2322+
flags: set_flags(:headers, [:end_headers, :end_stream])
2323+
)
2324+
])
2325+
2326+
assert_http2_error error, {:compression_error, debug_data}
2327+
assert debug_data =~ "unable to decode headers: {:index_not_found, 63}"
2328+
2329+
assert_recv_frames [goaway(error_code: :compression_error)]
2330+
2331+
refute HTTP2.open?(conn)
2332+
end
2333+
2334+
test "a lowered header table size has to be signalled by the server", %{conn: conn} do
2335+
conn = fill_server_header_table(conn)
2336+
2337+
{:ok, conn} = HTTP2.put_settings(conn, header_table_size: 0)
2338+
assert_recv_frames [settings(params: [header_table_size: 0])]
2339+
2340+
assert {:ok, %HTTP2{} = conn, []} =
2341+
stream_frames(conn, [settings(flags: set_flags(:settings, [:ack]), params: [])])
2342+
2343+
{conn, _ref} = open_request(conn)
2344+
2345+
assert_recv_frames [headers(stream_id: stream_id)]
2346+
2347+
assert {:error, %HTTP2{} = conn, error, []} =
2348+
stream_frames(conn, [
2349+
headers(
2350+
stream_id: stream_id,
2351+
hbf: <<0x88>>,
2352+
flags: set_flags(:headers, [:end_headers, :end_stream])
2353+
)
2354+
])
2355+
2356+
assert_http2_error error, {:compression_error, debug_data}
2357+
assert debug_data =~ "unable to decode headers: :missing_size_update"
2358+
2359+
assert_recv_frames [goaway(error_code: :compression_error)]
2360+
2361+
refute HTTP2.open?(conn)
2362+
end
2363+
2364+
test "a response that signals the lowered header table size is accepted", %{conn: conn} do
2365+
conn = fill_server_header_table(conn)
2366+
2367+
{:ok, conn} = HTTP2.put_settings(conn, header_table_size: 0)
2368+
assert_recv_frames [settings(params: [header_table_size: 0])]
2369+
2370+
assert {:ok, %HTTP2{} = conn, []} =
2371+
stream_frames(conn, [settings(flags: set_flags(:settings, [:ack]), params: [])])
2372+
2373+
{conn, ref} = open_request(conn)
2374+
2375+
assert_recv_frames [headers(stream_id: stream_id)]
2376+
2377+
assert {:ok, %HTTP2{} = conn, responses} =
2378+
stream_frames(conn, [
2379+
headers(
2380+
stream_id: stream_id,
2381+
hbf: <<0x20, 0x88>>,
2382+
flags: set_flags(:headers, [:end_headers, :end_stream])
2383+
)
2384+
])
2385+
2386+
assert [{:status, ^ref, 200}, {:headers, ^ref, []}, {:done, ^ref}] = responses
2387+
assert HTTP2.open?(conn)
2388+
end
2389+
22742390
@tag connect_options: [
22752391
receive_window_update_threshold: 8,
22762392
client_settings: [initial_window_size: 16]
@@ -3051,6 +3167,26 @@ defmodule Mint.HTTP2Test do
30513167
end
30523168
end
30533169

3170+
defp fill_server_header_table(conn) do
3171+
{conn, ref} = open_request(conn)
3172+
3173+
assert_recv_frames [headers(stream_id: stream_id)]
3174+
3175+
# ":status: 404" stored with incremental indexing, 42 bytes in the server's table.
3176+
assert {:ok, %HTTP2{} = conn, responses} =
3177+
stream_frames(conn, [
3178+
headers(
3179+
stream_id: stream_id,
3180+
hbf: <<0x48, 3, "404">>,
3181+
flags: set_flags(:headers, [:end_headers, :end_stream])
3182+
)
3183+
])
3184+
3185+
assert [{:status, ^ref, 404}, {:headers, ^ref, []}, {:done, ^ref}] = responses
3186+
3187+
conn
3188+
end
3189+
30543190
defp start_server_async(_context) do
30553191
{:ok, port, server_socket_task} = TestServer.listen_and_accept()
30563192
%{server_port: port, server_socket_task: server_socket_task}

0 commit comments

Comments
 (0)