Skip to content

Commit 7223c00

Browse files
bmalumericmj
andauthored
Validate HTTP/2 request methods, targets and header fields (#545)
* Validate HTTP/2 request methods, targets and header fields Mint.HTTP2.request/5 lowercased the header names it was given and HPACK-encoded everything else as is, and stream_request_body/3 did the same for trailers. A method, :path, CONNECT target, header name or header value containing CR, LF or NUL, a header name containing a space or a colon, or a value with leading or trailing whitespace was sent to the server unchanged. Mint.HTTP1 rejects all of these, and Mint.HTTP2 rejects them in responses since #504, so the behaviour of a request depended on which protocol ALPN negotiated. RFC 9113 8.2.1 forbids these characters in field names and values and says a sender must not generate such a message. HTTP/2 framing isn't confused by them, but an intermediary that forwards the request over HTTP/1.1 without re-validating the fields turns an injected CRLF into request header injection or request splitting on its back-end connection, so an application that is safe against an HTTP/1 origin isn't once the origin or a CDN in front of it enables HTTP/2. Requests and trailers are now validated before a stream is opened, with the field rules already used for responses: the method must be a token (RFC 9110 9.1), :path and the CONNECT :authority must be non-empty field values (RFC 9113 8.3.1), pseudo-header names passed by the caller must be valid field names, and trailers can't carry pseudo-headers (RFC 9113 8.1). Invalid input fails with :invalid_request_method, :invalid_request_target, :invalid_header_name or :invalid_header_value, like in Mint.HTTP1, and the connection stays open without the request taking up a stream. The messages for the two header errors no longer mention the response, since they now apply to requests as well. * Validate caller-supplied HTTP/2 method fields * Clarify HTTP/2 request field validation documentation --------- Co-authored-by: Eric Meadows-Jönsson <eric.meadows.jonsson@gmail.com>
1 parent d54ba54 commit 7223c00

2 files changed

Lines changed: 232 additions & 7 deletions

File tree

‎lib/mint/http2.ex‎

Lines changed: 92 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,12 +382,19 @@ defmodule Mint.HTTP2 do
382382
* `{:invalid_status_header, value}` - when the `:status` pseudo-header of a response
383383
is not a three-digit status code. `value` is the received value.
384384
385-
* `{:invalid_header_name, name}` - when a response header name is invalid, for
386-
example because it contains uppercase characters.
385+
* `{:invalid_request_method, method}` - when the request method is not a valid
386+
HTTP token. `method` is the given method.
387387
388-
* `{:invalid_header_value, name, value}` - when a response header value is invalid,
389-
for example because it contains control characters. `name` is the name of the
390-
header and `value` is the invalid value.
388+
* `{:invalid_request_target, target}` - when the request target (the `:path`
389+
or `:authority` pseudo-header) is empty or contains characters that are not allowed
390+
in a header value. `target` is the given target.
391+
392+
* `{:invalid_header_name, name}` - when a request or response header name is invalid,
393+
for example because it contains a space or control characters.
394+
395+
* `{:invalid_header_value, name, value}` - when a request or response header value is
396+
invalid, for example because it contains control characters. `name` is the name of
397+
the header and `value` is the invalid value.
391398
392399
* `{:invalid_content_length_header, value}` - when the `content-length` header of a
393400
response is not a non-negative integer. `value` is the received value.
@@ -619,6 +626,7 @@ defmodule Mint.HTTP2 do
619626
|> add_default_headers(body)
620627
|> sort_pseudo_headers_to_front()
621628

629+
validate_request_headers!(conn, headers)
622630
{conn, stream_id, ref} = open_stream(conn, method)
623631
{conn, payload} = encode_request_payload(conn, stream_id, headers, body)
624632
conn = send!(conn, payload)
@@ -1434,6 +1442,7 @@ defmodule Mint.HTTP2 do
14341442
end
14351443

14361444
trailer_headers = Headers.to_raw(trailers, _case_sensitive = false)
1445+
validate_request_trailers!(conn, trailer_headers)
14371446
enabled_flags = [:end_headers, :end_stream]
14381447
{conn, payload} = encode_header_block(conn, stream_id, trailer_headers, enabled_flags)
14391448
conn = put_in(conn.streams[stream_id].state, :half_closed_local)
@@ -1741,6 +1750,74 @@ defmodule Mint.HTTP2 do
17411750
end)
17421751
end
17431752

1753+
# RFC 9113 8.2.1 and 8.3: a request whose field names or values contain characters
1754+
# outside the allowed set is malformed, and a sender must not generate it. The
1755+
# headers are validated after lowercasing and adding the pseudo-headers, so the
1756+
# method, the request target and caller-provided pseudo-headers are covered too.
1757+
# HTTP/2 framing isn't confused by these bytes, but an intermediary that forwards
1758+
# the request over HTTP/1.1 could be.
1759+
defp validate_request_headers!(conn, headers) do
1760+
Enum.each(headers, fn
1761+
{":method", value} ->
1762+
unless valid_method?(value) do
1763+
throw({:mint, conn, wrap_error({:invalid_request_method, value})})
1764+
end
1765+
1766+
{pseudo_header, value} when pseudo_header in [":path", ":authority"] ->
1767+
unless valid_request_target?(value) do
1768+
throw({:mint, conn, wrap_error({:invalid_request_target, value})})
1769+
end
1770+
1771+
{":" <> name = pseudo_header, value} ->
1772+
unless valid_field_name?(name) do
1773+
throw({:mint, conn, wrap_error({:invalid_header_name, pseudo_header})})
1774+
end
1775+
1776+
unless valid_field_value?(value) do
1777+
throw({:mint, conn, wrap_error({:invalid_header_value, pseudo_header, value})})
1778+
end
1779+
1780+
{name, value} ->
1781+
validate_request_field!(conn, name, value)
1782+
end)
1783+
end
1784+
1785+
# RFC 9113 8.1: trailers must not include pseudo-header fields, and the field
1786+
# character rules apply to them as to any other field.
1787+
defp validate_request_trailers!(conn, trailers) do
1788+
Enum.each(trailers, fn {name, value} -> validate_request_field!(conn, name, value) end)
1789+
end
1790+
1791+
defp validate_request_field!(conn, name, value) do
1792+
cond do
1793+
not valid_field_name?(name) ->
1794+
throw({:mint, conn, wrap_error({:invalid_header_name, name})})
1795+
1796+
not valid_field_value?(value) ->
1797+
throw({:mint, conn, wrap_error({:invalid_header_value, name, value})})
1798+
1799+
true ->
1800+
:ok
1801+
end
1802+
end
1803+
1804+
# RFC 9110 9.1: method = token.
1805+
defp valid_method?(<<>>), do: false
1806+
defp valid_method?(method), do: token_chars?(method)
1807+
1808+
defp token_chars?(<<char, rest::binary>>)
1809+
when char in ?0..?9 or char in ?a..?z or char in ?A..?Z or char in ~c"!#$%&'*+-.^_`|~",
1810+
do: token_chars?(rest)
1811+
1812+
defp token_chars?(<<_char, _rest::binary>>), do: false
1813+
defp token_chars?(<<>>), do: true
1814+
1815+
# RFC 9113 8.3.1: :path must not be empty for http and https URIs, and :authority
1816+
# carries the target of a CONNECT request. Both are field values, so RFC 9113 8.2.1
1817+
# forbids NUL, CR and LF in them and leading or trailing whitespace.
1818+
defp valid_request_target?(<<>>), do: false
1819+
defp valid_request_target?(target), do: valid_field_value?(target)
1820+
17441821
## Frame handling
17451822

17461823
defp maybe_concat_and_handle_new_data(conn, data) do
@@ -3153,12 +3230,20 @@ defmodule Mint.HTTP2 do
31533230
"invalid :status pseudo-header in the response: #{inspect(value)}"
31543231
end
31553232

3233+
def format_error({:invalid_request_method, method}) do
3234+
"invalid request method: #{inspect(method)}"
3235+
end
3236+
3237+
def format_error({:invalid_request_target, target}) do
3238+
"invalid request target: #{inspect(target)}"
3239+
end
3240+
31563241
def format_error({:invalid_header_name, name}) do
3157-
"invalid header name in the response: #{inspect(name)}"
3242+
"invalid header name: #{inspect(name)}"
31583243
end
31593244

31603245
def format_error({:invalid_header_value, name, value}) do
3161-
"invalid value for header #{inspect(name)} in the response: #{inspect(value)}"
3246+
"invalid value for header #{inspect(name)}: #{inspect(value)}"
31623247
end
31633248

31643249
def format_error({:invalid_content_length_header, value}) do

‎test/mint/http2/conn_test.exs‎

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,139 @@ defmodule Mint.HTTP2Test do
14791479
end
14801480
end
14811481

1482+
describe "request header validation" do
1483+
for method <- ["", "GET /admin", "GET\t", "G\r\nET", "GET\0"] do
1484+
test "an invalid method #{inspect(method)} returns an error", %{conn: conn} do
1485+
assert {:error, %HTTP2{} = conn, error} =
1486+
HTTP2.request(conn, unquote(method), "/", [], nil)
1487+
1488+
assert_http2_error error, {:invalid_request_method, unquote(method)}
1489+
assert_request_was_not_sent(conn)
1490+
end
1491+
1492+
test "an invalid explicitly passed :method #{inspect(method)} returns an error", %{
1493+
conn: conn
1494+
} do
1495+
assert {:error, ^conn, error} =
1496+
HTTP2.request(conn, "GET", "/", [{":method", unquote(method)}], nil)
1497+
1498+
assert_http2_error error, {:invalid_request_method, unquote(method)}
1499+
assert_request_was_not_sent(conn)
1500+
end
1501+
end
1502+
1503+
for target <- ["", "/p\r\nath", "/pa\nth", "/pa\0th", " /path", "/path "] do
1504+
test "an invalid request target #{inspect(target)} returns an error", %{conn: conn} do
1505+
assert {:error, %HTTP2{} = conn, error} =
1506+
HTTP2.request(conn, "GET", unquote(target), [], nil)
1507+
1508+
assert_http2_error error, {:invalid_request_target, unquote(target)}
1509+
assert_request_was_not_sent(conn)
1510+
end
1511+
end
1512+
1513+
for target <- ["", "example.com:443\r\n", "example.com:443\0"] do
1514+
test "an invalid CONNECT target #{inspect(target)} returns an error", %{conn: conn} do
1515+
assert {:error, %HTTP2{} = conn, error} =
1516+
HTTP2.request(conn, "CONNECT", unquote(target), [], :stream)
1517+
1518+
assert_http2_error error, {:invalid_request_target, unquote(target)}
1519+
assert_request_was_not_sent(conn)
1520+
end
1521+
end
1522+
1523+
for name <- [
1524+
"",
1525+
"x-upper case",
1526+
"x\rfoo",
1527+
"x\nfoo",
1528+
"x\0foo",
1529+
"x:foo",
1530+
"x\x7Ffoo",
1531+
"x\xC3\xA4"
1532+
] do
1533+
test "an invalid header name #{inspect(name)} returns an error", %{conn: conn} do
1534+
assert {:error, %HTTP2{} = conn, error} =
1535+
HTTP2.request(conn, "GET", "/", [{unquote(name), "bar"}], nil)
1536+
1537+
assert_http2_error error, {:invalid_header_name, unquote(name)}
1538+
assert_request_was_not_sent(conn)
1539+
end
1540+
end
1541+
1542+
for value <- ["evil\r\nx-injected: 1", "b\rar", "b\nar", "b\0ar", " bar", "bar ", "bar\t"] do
1543+
test "an invalid header value #{inspect(value)} returns an error", %{conn: conn} do
1544+
assert {:error, %HTTP2{} = conn, error} =
1545+
HTTP2.request(conn, "GET", "/", [{"foo", unquote(value)}], nil)
1546+
1547+
assert_http2_error error, {:invalid_header_value, "foo", unquote(value)}
1548+
assert_request_was_not_sent(conn)
1549+
end
1550+
end
1551+
1552+
test "an invalid value in an explicitly passed pseudo-header returns an error",
1553+
%{conn: conn} do
1554+
headers = [{":scheme", "https"}, {":path", "/ws"}, {":protocol", "web\r\nsocket"}]
1555+
1556+
assert {:error, %HTTP2{} = conn, error} =
1557+
HTTP2.request(conn, "CONNECT", "/ws", headers, :stream)
1558+
1559+
assert_http2_error error, {:invalid_header_value, ":protocol", "web\r\nsocket"}
1560+
assert_request_was_not_sent(conn)
1561+
end
1562+
1563+
test "an invalid explicitly passed :path pseudo-header returns an error", %{conn: conn} do
1564+
headers = [{":scheme", "https"}, {":path", "/w\r\ns"}, {":protocol", "websocket"}]
1565+
1566+
assert {:error, %HTTP2{} = conn, error} =
1567+
HTTP2.request(conn, "CONNECT", "/ws", headers, :stream)
1568+
1569+
assert_http2_error error, {:invalid_request_target, "/w\r\ns"}
1570+
assert_request_was_not_sent(conn)
1571+
end
1572+
1573+
test "header values may be empty or contain obs-text and inner whitespace", %{conn: conn} do
1574+
headers = [{"empty", ""}, {"foo", "b\xC3\xA4r \tbaz"}, {"A-b_c.d!", "v"}]
1575+
1576+
assert {:ok, %HTTP2{} = conn, _ref} = HTTP2.request(conn, "GET", "/", headers, nil)
1577+
1578+
assert_recv_frames [headers(hbf: hbf)]
1579+
1580+
assert [
1581+
{":method", "GET"},
1582+
{":path", "/"},
1583+
{":scheme", "https"},
1584+
{":authority", _},
1585+
{"user-agent", _},
1586+
{"empty", ""},
1587+
{"foo", "b\xC3\xA4r \tbaz"},
1588+
{"a-b_c.d!", "v"}
1589+
] = server_decode_headers(hbf)
1590+
1591+
assert HTTP2.open?(conn)
1592+
end
1593+
1594+
for {name, value, reason} <- [
1595+
{"x\r\nfoo", "bar", {:invalid_header_name, "x\r\nfoo"}},
1596+
{":status", "200", {:invalid_header_name, ":status"}},
1597+
{"foo", "b\r\nar", {:invalid_header_value, "foo", "b\r\nar"}}
1598+
] do
1599+
test "an invalid trailer #{inspect({name, value})} returns an error", %{conn: conn} do
1600+
{conn, ref} = open_request(conn, :stream)
1601+
assert_recv_frames [headers(stream_id: _stream_id)]
1602+
1603+
assert {:error, %HTTP2{} = conn, error} =
1604+
HTTP2.stream_request_body(conn, ref, {:eof, [{unquote(name), unquote(value)}]})
1605+
1606+
assert_http2_error error, unquote(Macro.escape(reason))
1607+
1608+
refute_receive {:ssl, _socket, _data}, 100
1609+
assert HTTP2.open_request_count(conn) == 1
1610+
assert HTTP2.open?(conn)
1611+
end
1612+
end
1613+
end
1614+
14821615
describe "response header validation" do
14831616
for status <- ["abc", "", "+200", "2000", "20", "200 ", " 200", "1ab", "000", "099"] do
14841617
test "an invalid :status of #{inspect(status)} is a stream error", %{conn: conn} do
@@ -4782,4 +4915,11 @@ defmodule Mint.HTTP2Test do
47824915
assert is_reference(ref)
47834916
{conn, ref}
47844917
end
4918+
4919+
# A rejected request must not reach the server or take up a stream.
4920+
defp assert_request_was_not_sent(conn) do
4921+
refute_receive {:ssl, _socket, _data}, 100
4922+
assert HTTP2.open_request_count(conn) == 0
4923+
assert HTTP2.open?(conn)
4924+
end
47854925
end

0 commit comments

Comments
 (0)