Skip to content

Commit 3d3fb64

Browse files
authored
Merge commit from fork
1 parent 0d5b5ea commit 3d3fb64

4 files changed

Lines changed: 60 additions & 8 deletions

File tree

‎lib/plug/parsers/multipart.ex‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,15 @@ defmodule Plug.Parsers.MULTIPART do
328328
end
329329
end
330330

331+
# Charge the framing too, otherwise parts without headers and
332+
# headers without name or value would cost nothing.
333+
#
334+
# Each part has 6 bytes of framing, "--" and "\r\n\r\n", plus the
335+
# boundary, which we estimate at 30 bytes. Each header has at least
336+
# 3 bytes besides its name and value: ":" and "\r\n".
331337
defp headers_size(headers) do
332-
Enum.reduce(headers, 0, fn {key, value}, acc ->
333-
acc + byte_size(key) + byte_size(value)
338+
Enum.reduce(headers, 6 + 30, fn {key, value}, acc ->
339+
acc + byte_size(key) + byte_size(value) + 3
334340
end)
335341
end
336342

‎src/plug_multipart.erl‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,9 @@ skip_preamble(Stream, Boundary) ->
187187
case binary:match(Stream, <<"\r\n--", Boundary/bits >>) of
188188
%% No boundary, need more data.
189189
nomatch ->
190-
%% We can safely skip the size of the stream
191-
%% minus the last 3 bytes which may be a partial boundary.
192-
SkipSize = byte_size(Stream) - 3,
190+
%% We can safely skip the size of the stream minus the
191+
%% last bytes which may be a partial "\r\n--" Boundary.
192+
SkipSize = byte_size(Stream) - byte_size(Boundary) - 3,
193193
case SkipSize > 0 of
194194
false ->
195195
more;
@@ -205,10 +205,11 @@ skip_preamble(Stream, Boundary) ->
205205
<< "--", Stream3/bits >> ->
206206
{done, Stream3};
207207
_ ->
208-
case binary:match(Stream, <<"\r\n\r\n">>) of
209-
%% We don't have the full headers.
208+
case binary:match(Stream2, <<"\r\n\r\n">>) of
209+
%% We don't have the full headers. Keep the boundary
210+
%% so it is found again once more data arrives.
210211
nomatch ->
211-
{more, Stream2};
212+
{more, binary:part(Stream, Start, byte_size(Stream) - Start)};
212213
_ ->
213214
before_parse_headers(Stream2)
214215
end

‎test/plug/conn_test.exs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,6 +1012,27 @@ defmodule Plug.ConnTest do
10121012
assert {:error, :too_large, _conn} = read_part_headers(conn, length: 1_000)
10131013
end
10141014

1015+
test "multipart preambles are skipped when split across reads" do
1016+
part =
1017+
"\r\n--deadbeef\r\ncontent-disposition: form-data; name=\"x\"\r\n\r\nabc\r\n--deadbeef--\r\n"
1018+
1019+
for preamble <- ["PPPPPPPPPPPPPPPPPPPP", "P\r\n\r\nPPPPPPPPPPPPPPP"], size <- 1..40 do
1020+
assert {:ok, [{"content-disposition", "form-data; name=\"x\""}], _rest} =
1021+
parse_split_headers("", preamble <> part, size)
1022+
end
1023+
end
1024+
1025+
# Feeds the multipart parser the way read_part_headers/2 does
1026+
defp parse_split_headers(data, body, size) do
1027+
{chunk, body} = String.split_at(body, size)
1028+
1029+
case :plug_multipart.parse_headers(data <> chunk, "deadbeef") do
1030+
:more -> parse_split_headers(data <> chunk, body, size)
1031+
{:more, rest} -> parse_split_headers(rest, body, size)
1032+
other -> other
1033+
end
1034+
end
1035+
10151036
test "query_params/1 and fetch_query_params/1" do
10161037
conn = conn(:get, "/foo?a=b&c=d")
10171038
assert conn.query_params == %Plug.Conn.Unfetched{aspect: :query_params}

‎test/plug/parsers_test.exs‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,30 @@ defmodule Plug.ParsersTest do
436436
assert Plug.Exception.status(exception) == 413
437437
end
438438

439+
test "raises on many multipart parts without headers" do
440+
multipart =
441+
"--deadbeef\r\n\r\n" <>
442+
String.duplicate("\r\n--deadbeef\r\n\r\n", 10_000) <> "\r\n--deadbeef--\r\n"
443+
444+
assert_raise Plug.Parsers.RequestTooLargeError, fn ->
445+
conn(:post, "/", multipart)
446+
|> put_req_header("content-type", "multipart/form-data; boundary=deadbeef")
447+
|> parse(length: 10_000)
448+
end
449+
end
450+
451+
test "raises on many empty multipart headers" do
452+
multipart =
453+
String.duplicate("--deadbeef\r\n" <> String.duplicate(":\r\n", 1_000) <> "\r\n\r\n", 10) <>
454+
"--deadbeef--\r\n"
455+
456+
assert_raise Plug.Parsers.RequestTooLargeError, fn ->
457+
conn(:post, "/", multipart)
458+
|> put_req_header("content-type", "multipart/form-data; boundary=deadbeef")
459+
|> parse(length: 10_000)
460+
end
461+
end
462+
439463
test "raises when cumulative multipart headers exceed the parser length" do
440464
multipart =
441465
[

0 commit comments

Comments
 (0)