Skip to content

Commit 4385c9e

Browse files
authored
Validate HTTP/2 response header fields and pseudo-headers (#504)
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.
1 parent 9e9ae10 commit 4385c9e

2 files changed

Lines changed: 261 additions & 14 deletions

File tree

‎lib/mint/http2.ex‎

Lines changed: 109 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,18 @@ defmodule Mint.HTTP2 do
366366
* `:request_is_not_streaming` - when you try to send data (with `stream_request_body/3`)
367367
on a request that is not open for streaming.
368368
369+
* `:missing_status_header` - when a response has no `:status` pseudo-header.
370+
371+
* `{:invalid_status_header, value}` - when the `:status` pseudo-header of a response
372+
is not a three-digit status code. `value` is the received value.
373+
374+
* `{:invalid_header_name, name}` - when a response header name is invalid, for
375+
example because it contains uppercase characters.
376+
377+
* `{:invalid_header_value, name, value}` - when a response header value is invalid,
378+
for example because it contains control characters. `name` is the name of the
379+
header and `value` is the invalid value.
380+
369381
* `:unprocessed` - when a request was closed because it was not processed by the server.
370382
When this error is returned, it means that the server hasn't processed the request at all,
371383
so it's safe to retry the given request on a different or new connection.
@@ -1988,22 +2000,25 @@ defmodule Mint.HTTP2 do
19882000
defp handle_decoded_headers_for_stream(conn, responses, stream, headers, end_stream?) do
19892001
%{ref: ref, received_first_headers?: received_first_headers?} = stream
19902002

2003+
case validate_response_headers(headers, _trailers? = received_first_headers?) do
2004+
:ok ->
2005+
handle_valid_headers_for_stream(conn, responses, stream, headers, end_stream?)
2006+
2007+
{:error, reason} ->
2008+
conn = close_stream!(conn, stream.id, :protocol_error)
2009+
{conn, [{:error, ref, wrap_error(reason)} | responses]}
2010+
end
2011+
end
2012+
2013+
defp handle_valid_headers_for_stream(conn, responses, stream, headers, end_stream?) do
2014+
%{ref: ref, received_first_headers?: received_first_headers?} = stream
2015+
19912016
case headers do
19922017
# Interim response (1xx), which is made of only one HEADERS plus zero or more CONTINUATIONs.
19932018
# There can be zero or more interim responses before a "proper" response.
19942019
# https://httpwg.org/specs/rfc9113.html#HttpFraming
19952020
[{":status", <<?1, _, _>> = status} | headers] ->
19962021
cond do
1997-
received_first_headers? ->
1998-
conn = close_stream!(conn, stream.id, :protocol_error)
1999-
2000-
debug_data =
2001-
"informational response (1xx) must appear before final response, got a #{status} status"
2002-
2003-
error = wrap_error({:protocol_error, debug_data})
2004-
responses = [{:error, stream.ref, error} | responses]
2005-
{conn, responses}
2006-
20072022
end_stream? ->
20082023
conn = close_stream!(conn, stream.id, :protocol_error)
20092024
debug_data = "informational response (1xx) must not have the END_STREAM flag set"
@@ -2093,9 +2108,80 @@ defmodule Mint.HTTP2 do
20932108
end
20942109
end
20952110

2111+
# RFC 9113 8.2.1 and 8.3: pseudo-header fields come before regular fields, only
2112+
# :status is defined for responses and it appears exactly once, trailers carry
2113+
# no pseudo-header fields, and field names and values are limited to the
2114+
# characters allowed by the RFC.
2115+
defp validate_response_headers(headers, trailers?) do
2116+
validate_response_headers(headers, trailers?, _status? = false, _regular? = false)
2117+
end
2118+
2119+
defp validate_response_headers([], _trailers?, _status?, _regular?), do: :ok
2120+
2121+
defp validate_response_headers([{":" <> _ = name, value} | rest], trailers?, status?, regular?) do
2122+
cond do
2123+
trailers? ->
2124+
{:error, {:protocol_error, "pseudo-header #{inspect(name)} is not allowed in trailers"}}
2125+
2126+
regular? ->
2127+
{:error,
2128+
{:protocol_error,
2129+
"pseudo-header #{inspect(name)} must appear before regular header fields"}}
2130+
2131+
name != ":status" ->
2132+
{:error, {:protocol_error, "undefined pseudo-header #{inspect(name)} in response"}}
2133+
2134+
status? ->
2135+
{:error, {:protocol_error, "the :status pseudo-header appears more than once"}}
2136+
2137+
not valid_status?(value) ->
2138+
{:error, {:invalid_status_header, value}}
2139+
2140+
true ->
2141+
validate_response_headers(rest, trailers?, true, regular?)
2142+
end
2143+
end
2144+
2145+
defp validate_response_headers([{name, value} | rest], trailers?, status?, _regular?) do
2146+
cond do
2147+
not valid_field_name?(name) -> {:error, {:invalid_header_name, name}}
2148+
not valid_field_value?(value) -> {:error, {:invalid_header_value, name, value}}
2149+
true -> validate_response_headers(rest, trailers?, status?, true)
2150+
end
2151+
end
2152+
2153+
# RFC 9110 15: status-code = 3DIGIT
2154+
defp valid_status?(<<a, b, c>>) when a in ?0..?9 and b in ?0..?9 and c in ?0..?9, do: true
2155+
defp valid_status?(_other), do: false
2156+
2157+
# RFC 9113 8.2.1: a field name must not contain characters in 0x00-0x20, 0x41-0x5A
2158+
# (uppercase letters) or 0x7F-0xFF, and only a pseudo-header field can contain a colon.
2159+
defp valid_field_name?(<<>>), do: false
2160+
defp valid_field_name?(name), do: field_name_chars?(name)
2161+
2162+
defp field_name_chars?(<<char, _rest::binary>>)
2163+
when char in 0x00..0x20 or char in ?A..?Z or char in 0x7F..0xFF or char == ?:,
2164+
do: false
2165+
2166+
defp field_name_chars?(<<_char, rest::binary>>), do: field_name_chars?(rest)
2167+
defp field_name_chars?(<<>>), do: true
2168+
2169+
# RFC 9113 8.2.1: a field value must not contain NUL, LF or CR and must not start or
2170+
# end with SP or HTAB.
2171+
defp valid_field_value?(<<char, _rest::binary>>) when char in [?\s, ?\t], do: false
2172+
2173+
defp valid_field_value?(value) do
2174+
byte_size(value) == 0 or
2175+
(:binary.last(value) not in [?\s, ?\t] and field_value_chars?(value))
2176+
end
2177+
2178+
defp field_value_chars?(<<char, _rest::binary>>) when char in [0, ?\n, ?\r], do: false
2179+
defp field_value_chars?(<<_char, rest::binary>>), do: field_value_chars?(rest)
2180+
defp field_value_chars?(<<>>), do: true
2181+
20962182
defp join_cookie_headers(headers) do
20972183
# If we have 0 or 1 Cookie headers, we just use the old list of headers.
2098-
case Enum.split_with(headers, fn {name, _value} -> Headers.lower_raw(name) == "cookie" end) do
2184+
case Enum.split_with(headers, fn {name, _value} -> name == "cookie" end) do
20992185
{[], _headers} ->
21002186
headers
21012187

@@ -2702,6 +2788,18 @@ defmodule Mint.HTTP2 do
27022788
"the :status pseudo-header (which is required in HTTP/2) is missing from the response"
27032789
end
27042790

2791+
def format_error({:invalid_status_header, value}) do
2792+
"invalid :status pseudo-header in the response: #{inspect(value)}"
2793+
end
2794+
2795+
def format_error({:invalid_header_name, name}) do
2796+
"invalid header name in the response: #{inspect(name)}"
2797+
end
2798+
2799+
def format_error({:invalid_header_value, name, value}) do
2800+
"invalid value for header #{inspect(name)} in the response: #{inspect(value)}"
2801+
end
2802+
27052803
def format_error({:server_closed_request, error_code}) do
27062804
"server closed request with error code #{inspect(error_code)}"
27072805
end

‎test/mint/http2/conn_test.exs‎

Lines changed: 152 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,7 +1129,7 @@ defmodule Mint.HTTP2Test do
11291129
{":status", "200"},
11301130
{"accept", "text/plain"},
11311131
{"cookie", "a=b"},
1132-
{"Cookie", "c=d; e=f"},
1132+
{"cookie", "c=d; e=f"},
11331133
{"content-type", "application/json"},
11341134
{"cookie", "g=h"},
11351135
{"x-header", "value"}
@@ -1195,6 +1195,156 @@ defmodule Mint.HTTP2Test do
11951195
end
11961196
end
11971197

1198+
describe "response header validation" do
1199+
for status <- ["abc", "", "+200", "2000", "20", "200 ", " 200", "1ab"] do
1200+
test "an invalid :status of #{inspect(status)} is a stream error", %{conn: conn} do
1201+
{conn, ref} = open_request(conn)
1202+
1203+
assert_recv_frames [headers(stream_id: stream_id)]
1204+
1205+
assert {:ok, %HTTP2{} = conn, responses} =
1206+
stream_frames(conn, [
1207+
{:headers, stream_id, [{":status", unquote(status)}], [:end_headers]}
1208+
])
1209+
1210+
assert [{:error, ^ref, error}] = responses
1211+
assert_http2_error error, {:invalid_status_header, unquote(status)}
1212+
1213+
assert_recv_frames [rst_stream(stream_id: ^stream_id, error_code: :protocol_error)]
1214+
assert HTTP2.open?(conn)
1215+
end
1216+
end
1217+
1218+
for name <- ["Foo", "fo o", "", "foo:bar", "f\x7Fo", "f\xC3\xA4"] do
1219+
test "an invalid header name #{inspect(name)} is a stream error", %{conn: conn} do
1220+
{conn, ref} = open_request(conn)
1221+
1222+
assert_recv_frames [headers(stream_id: stream_id)]
1223+
1224+
assert {:ok, %HTTP2{} = conn, responses} =
1225+
stream_frames(conn, [
1226+
{:headers, stream_id, [{":status", "200"}, {unquote(name), "bar"}],
1227+
[:end_headers, :end_stream]}
1228+
])
1229+
1230+
assert [{:error, ^ref, error}] = responses
1231+
assert_http2_error error, {:invalid_header_name, unquote(name)}
1232+
1233+
assert_recv_frames [rst_stream(stream_id: ^stream_id, error_code: :protocol_error)]
1234+
assert HTTP2.open?(conn)
1235+
end
1236+
end
1237+
1238+
for value <- ["b\0ar", "b\rar", "b\nar", " bar", "bar ", "bar\t"] do
1239+
test "an invalid header value #{inspect(value)} is a stream error", %{conn: conn} do
1240+
{conn, ref} = open_request(conn)
1241+
1242+
assert_recv_frames [headers(stream_id: stream_id)]
1243+
1244+
assert {:ok, %HTTP2{} = conn, responses} =
1245+
stream_frames(conn, [
1246+
{:headers, stream_id, [{":status", "200"}, {"foo", unquote(value)}],
1247+
[:end_headers, :end_stream]}
1248+
])
1249+
1250+
assert [{:error, ^ref, error}] = responses
1251+
assert_http2_error error, {:invalid_header_value, "foo", unquote(value)}
1252+
1253+
assert_recv_frames [rst_stream(stream_id: ^stream_id, error_code: :protocol_error)]
1254+
assert HTTP2.open?(conn)
1255+
end
1256+
end
1257+
1258+
test "header values may be empty or contain obs-text and inner whitespace", %{conn: conn} do
1259+
{conn, ref} = open_request(conn)
1260+
1261+
assert_recv_frames [headers(stream_id: stream_id)]
1262+
1263+
headers = [
1264+
{":status", "200"},
1265+
{"empty", ""},
1266+
{"foo", "b\xC3\xA4r \tbaz"},
1267+
{"a-b_c.d!", "v"}
1268+
]
1269+
1270+
assert {:ok, %HTTP2{} = conn, responses} =
1271+
stream_frames(conn, [{:headers, stream_id, headers, [:end_headers, :end_stream]}])
1272+
1273+
assert responses == [
1274+
{:status, ref, 200},
1275+
{:headers, ref, tl(headers)},
1276+
{:done, ref}
1277+
]
1278+
1279+
assert HTTP2.open?(conn)
1280+
end
1281+
1282+
for {label, headers, debug_data} <- [
1283+
{"a pseudo-header after a regular header", [{"foo", "bar"}, {":status", "200"}],
1284+
"must appear before regular header fields"},
1285+
{"an undefined pseudo-header", [{":status", "200"}, {":path", "/"}],
1286+
"undefined pseudo-header \":path\""},
1287+
{"a duplicate :status pseudo-header", [{":status", "200"}, {":status", "404"}],
1288+
"appears more than once"}
1289+
] do
1290+
test "#{label} is a stream error", %{conn: conn} do
1291+
{conn, ref} = open_request(conn)
1292+
1293+
assert_recv_frames [headers(stream_id: stream_id)]
1294+
1295+
assert {:ok, %HTTP2{} = conn, responses} =
1296+
stream_frames(conn, [
1297+
{:headers, stream_id, unquote(headers), [:end_headers, :end_stream]}
1298+
])
1299+
1300+
assert [{:error, ^ref, error}] = responses
1301+
assert_http2_error error, {:protocol_error, debug_data}
1302+
assert debug_data =~ unquote(debug_data)
1303+
1304+
assert_recv_frames [rst_stream(stream_id: ^stream_id, error_code: :protocol_error)]
1305+
assert HTTP2.open?(conn)
1306+
end
1307+
end
1308+
1309+
test "a pseudo-header in trailers is a stream error", %{conn: conn} do
1310+
{conn, ref} = open_request(conn)
1311+
1312+
assert_recv_frames [headers(stream_id: stream_id)]
1313+
1314+
assert {:ok, %HTTP2{} = conn, responses} =
1315+
stream_frames(conn, [
1316+
{:headers, stream_id, [{":status", "200"}], [:end_headers]},
1317+
{:headers, stream_id, [{":status", "500"}, {"x-trailer", "v"}],
1318+
[:end_headers, :end_stream]}
1319+
])
1320+
1321+
assert [{:status, ^ref, 200}, {:headers, ^ref, []}, {:error, ^ref, error}] = responses
1322+
assert_http2_error error, {:protocol_error, debug_data}
1323+
assert debug_data =~ "not allowed in trailers"
1324+
1325+
assert_recv_frames [rst_stream(stream_id: ^stream_id, error_code: :protocol_error)]
1326+
assert HTTP2.open?(conn)
1327+
end
1328+
1329+
test "an invalid header name in trailers is a stream error", %{conn: conn} do
1330+
{conn, ref} = open_request(conn)
1331+
1332+
assert_recv_frames [headers(stream_id: stream_id)]
1333+
1334+
assert {:ok, %HTTP2{} = conn, responses} =
1335+
stream_frames(conn, [
1336+
{:headers, stream_id, [{":status", "200"}], [:end_headers]},
1337+
{:headers, stream_id, [{"X-Trailer", "v"}], [:end_headers, :end_stream]}
1338+
])
1339+
1340+
assert [{:status, ^ref, 200}, {:headers, ^ref, []}, {:error, ^ref, error}] = responses
1341+
assert_http2_error error, {:invalid_header_name, "X-Trailer"}
1342+
1343+
assert_recv_frames [rst_stream(stream_id: ^stream_id, error_code: :protocol_error)]
1344+
assert HTTP2.open?(conn)
1345+
end
1346+
end
1347+
11981348
describe "interim responses (1xx)" do
11991349
test "multiple before a single HEADERS", %{conn: conn} do
12001350
info_status1 = Enum.random(100..199)
@@ -1313,8 +1463,7 @@ defmodule Mint.HTTP2Test do
13131463

13141464
assert_http2_error error, {:protocol_error, debug_data}
13151465

1316-
assert debug_data =~
1317-
"informational response (1xx) must appear before final response, got a 101 status"
1466+
assert debug_data =~ "pseudo-header \":status\" is not allowed in trailers"
13181467

13191468
assert HTTP2.open?(conn)
13201469
end

0 commit comments

Comments
 (0)