Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect processing of incorrect multipart/form-data requests #67

Open
sigsergv opened this issue Feb 15, 2019 · 1 comment
Open

Incorrect processing of incorrect multipart/form-data requests #67

sigsergv opened this issue Feb 15, 2019 · 1 comment

Comments

@sigsergv
Copy link

simple_bridge hangs when processing incorrectly passed muiltipart/form-data POST body.

How to reproduce:

  1. start using standard make run_inets
  2. execute command curl -X POST -H 'Content-Type: multipart/form-data; boundary=12345' --data-binary "@test-data-bad" "http://localhost:8000/"

Expected result: Error 4xx or 5xx.

Actual result: request never ends (or stop after some timeout).

On large amount of such requests code consumes all CPU resources very quickly.

I've attached test data and sample code in test.sh simple_bridge_post_form.tar.gz

In "bad" payload I use \n instead of \r\n.

As a dirty workaround I use this code in simple_bridge_multipart:

--- a/simple_bridge/src/simple_bridge_multipart.erl
+++ b/simple_bridge/src/simple_bridge_multipart.erl
@@ -230,6 +230,13 @@ get_next_line(Data, Acc, Part, State) when Data == undefined orelse Data == <<>>
 
 read_chunk(State = #state { req=Req, length=Length, bytes_read=BytesRead }) ->
     BytesToRead = lists:min([Length - BytesRead, ?CHUNKSIZE]),
+    if
+        BytesToRead==0 ->
+            error_logger:error_msg("LineEnding failed, state: ~p~n", [State]),
+            erlang:throw({unexpected, line_end, 0});
+        true ->
+            ok
+    end,
     Data = sbw:recv_from_socket(BytesToRead, ?IDLE_TIMEOUT, Req),
     NewBytesRead = BytesRead + size(Data),
     ok=crash_if_too_big(NewBytesRead, State),

@choptastic
Copy link
Member

choptastic commented Feb 15, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants