Skip to content

Commit 19be555

Browse files
authored
Merge commit from fork
1 parent d996244 commit 19be555

3 files changed

Lines changed: 83 additions & 12 deletions

File tree

‎lib/mint/http.ex‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,9 @@ defmodule Mint.HTTP do
227227
*Available since v1.5.0*.
228228
229229
* `:max_header_list_size` - (positive integer or `:infinity`) the maximum size, in
230-
**bytes**, of an HTTP/1 response header section or chunked trailer section. Defaults to
231-
256 KiB. This option is only used for HTTP/1 connections. *Available since 1.9.2*.
230+
**bytes**, of an HTTP/1 response line, header section, or chunked trailer section.
231+
Defaults to 256 KiB. This option is only used for HTTP/1 connections.
232+
*Available since 1.9.2*.
232233
233234
* `:proxy` - a `{scheme, address, port, opts}` tuple that identifies a proxy to
234235
connect to. See the "Proxying" section below for more information.

‎lib/mint/http1.ex‎

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ defmodule Mint.HTTP1 do
4747
4848
* `:invalid_status_line` - when the HTTP/1 status line is invalid.
4949
50+
* `{:response_line_too_long, size, max_size}` - when a response status line,
51+
chunk-size line, or chunk-extension line exceeds the configured size limit.
52+
`size` is the number of bytes received and `max_size` is the configured maximum.
53+
5054
* `{:invalid_request_target, target}` - when the request target is invalid.
5155
5256
* `{:invalid_request_method, method}` - when the request method is invalid.
@@ -148,8 +152,9 @@ defmodule Mint.HTTP1 do
148152
This is only available for HTTP/1.1 connections. *Available since v1.8.0*.
149153
150154
* `:max_header_list_size` - (`t:pos_integer/0` or `:infinity`) the maximum number of
151-
bytes allowed in a response header section or chunked trailer section. This includes
152-
header names, values, and line delimiters. Defaults to 256 KiB. *Available since 1.9.2*.
155+
bytes allowed in a response status line, chunk-size line, chunk-extension line,
156+
header section, or chunked trailer section. This includes header names, values,
157+
and line delimiters. Defaults to 256 KiB. *Available since 1.9.2*.
153158
154159
* `:stream_headers` - (`t:boolean/0`) if set to `true`, response headers and trailer headers
155160
will be emitted as they are parsed, rather than buffered until the complete header section
@@ -727,8 +732,7 @@ defmodule Mint.HTTP1 do
727732
decode(:headers, conn, rest, responses)
728733

729734
:more ->
730-
conn = put_in(conn.buffer, data)
731-
{:ok, conn, responses}
735+
buffer_response_line(conn, data, responses)
732736

733737
:error ->
734738
{:error, conn, wrap_error(:invalid_status_line), responses}
@@ -874,9 +878,7 @@ defmodule Mint.HTTP1 do
874878
defp decode_body({:chunked, nil}, conn, data, request_ref, responses) do
875879
case Parse.chunk_size(data) do
876880
:more ->
877-
conn = put_in(conn.buffer, data)
878-
conn = put_in(conn.request.body, {:chunked, nil})
879-
{:ok, conn, responses}
881+
buffer_response_line(conn, data, responses, {:chunked, nil})
880882

881883
{:ok, 0, rest} ->
882884
# Manually collapse the body buffer since we're done with the body
@@ -897,9 +899,7 @@ defmodule Mint.HTTP1 do
897899
decode_body({:chunked, size}, conn, rest, request_ref, responses)
898900

899901
:more ->
900-
conn = put_in(conn.buffer, data)
901-
conn = put_in(conn.request.body, {:chunked, :metadata, size})
902-
{:ok, conn, responses}
902+
buffer_response_line(conn, data, responses, {:chunked, :metadata, size})
903903
end
904904
end
905905

@@ -1075,6 +1075,34 @@ defmodule Mint.HTTP1 do
10751075
end
10761076
end
10771077

1078+
defp buffer_response_line(conn, data, responses, body_state \\ nil) do
1079+
case check_response_line_size(conn, byte_size(data)) do
1080+
:ok ->
1081+
conn = put_in(conn.buffer, data)
1082+
1083+
conn =
1084+
if body_state == nil do
1085+
conn
1086+
else
1087+
put_in(conn.request.body, body_state)
1088+
end
1089+
1090+
{:ok, conn, responses}
1091+
1092+
{:error, reason} ->
1093+
{:error, conn, wrap_error(reason), responses}
1094+
end
1095+
end
1096+
1097+
defp check_response_line_size(%{max_header_list_size: :infinity}, _size), do: :ok
1098+
1099+
defp check_response_line_size(%{max_header_list_size: max_size}, size)
1100+
when size <= max_size,
1101+
do: :ok
1102+
1103+
defp check_response_line_size(%{max_header_list_size: max_size}, size),
1104+
do: {:error, {:response_line_too_long, size, max_size}}
1105+
10781106
defp store_header(%{content_length: nil} = request, "content-length", value) do
10791107
with {:ok, content_length} <- Parse.content_length_header(value),
10801108
do: {:ok, %{request | content_length: content_length}}
@@ -1312,6 +1340,10 @@ defmodule Mint.HTTP1 do
13121340
"invalid status line"
13131341
end
13141342

1343+
def format_error({:response_line_too_long, size, max_size}) do
1344+
"the response line (#{size} bytes) exceeds the maximum allowed size of #{max_size} bytes"
1345+
end
1346+
13151347
def format_error(:invalid_header) do
13161348
"invalid header"
13171349
end

‎test/mint/http1/conn_test.exs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ defmodule Mint.HTTP1Test do
5050
HTTP1.stream(conn, {:tcp, conn.socket, " 200 OK\r\n"})
5151
end
5252

53+
test "limits an incomplete response status line", %{port: port} do
54+
assert {:ok, conn} = HTTP1.connect(:http, "localhost", port, max_header_list_size: 64)
55+
{:ok, conn, _ref} = HTTP1.request(conn, "GET", "/", [], nil)
56+
57+
line = "HTTP/1.1 200 " <> String.duplicate("x", 51)
58+
assert byte_size(line) == 64
59+
assert {:ok, conn, []} = HTTP1.stream(conn, {:tcp, conn.socket, line})
60+
61+
assert {:error, _conn, %HTTPError{reason: {:response_line_too_long, 65, 64}}, []} =
62+
HTTP1.stream(conn, {:tcp, conn.socket, "x"})
63+
end
64+
5365
test "headers", %{conn: conn} do
5466
{:ok, conn, ref} = HTTP1.request(conn, "GET", "/", [], nil)
5567

@@ -403,6 +415,32 @@ defmodule Mint.HTTP1Test do
403415
assert IO.iodata_to_binary(conn.request.data_buffer) == ""
404416
end
405417

418+
test "limits an incomplete chunk-size line", %{port: port} do
419+
assert {:ok, conn} = HTTP1.connect(:http, "localhost", port, max_header_list_size: 64)
420+
{:ok, conn, _ref} = HTTP1.request(conn, "GET", "/", [], nil)
421+
response = "HTTP/1.1 200 OK\r\ntransfer-encoding: chunked\r\n\r\n"
422+
assert {:ok, conn, [_status, _headers]} = HTTP1.stream(conn, {:tcp, conn.socket, response})
423+
424+
assert {:ok, conn, []} =
425+
HTTP1.stream(conn, {:tcp, conn.socket, String.duplicate("f", 64)})
426+
427+
assert {:error, _conn, %HTTPError{reason: {:response_line_too_long, 65, 64}}, []} =
428+
HTTP1.stream(conn, {:tcp, conn.socket, "f"})
429+
end
430+
431+
test "limits an incomplete chunk-extension line", %{port: port} do
432+
assert {:ok, conn} = HTTP1.connect(:http, "localhost", port, max_header_list_size: 64)
433+
{:ok, conn, _ref} = HTTP1.request(conn, "GET", "/", [], nil)
434+
response = "HTTP/1.1 200 OK\r\ntransfer-encoding: chunked\r\n\r\n5;"
435+
assert {:ok, conn, [_status, _headers]} = HTTP1.stream(conn, {:tcp, conn.socket, response})
436+
437+
assert {:ok, conn, []} =
438+
HTTP1.stream(conn, {:tcp, conn.socket, String.duplicate("x", 63)})
439+
440+
assert {:error, _conn, %HTTPError{reason: {:response_line_too_long, 65, 64}}, []} =
441+
HTTP1.stream(conn, {:tcp, conn.socket, "x"})
442+
end
443+
406444
test "body with chunked transfer-encoding with metadata and trailers", %{conn: conn} do
407445
{:ok, conn, ref} = HTTP1.request(conn, "GET", "/", [], nil)
408446

0 commit comments

Comments
 (0)