Browse Source

Fix two edge cases when the request-line provided is invalid

Loïc Hoguin 10 years ago
parent
commit
403895a641
2 changed files with 6 additions and 0 deletions
  1. 4 0
      src/cowboy_protocol.erl
  2. 2 0
      test/http_SUITE.erl

+ 4 - 0
src/cowboy_protocol.erl

@@ -136,6 +136,8 @@ wait_request(Buffer, State=#state{socket=Socket, transport=Transport,
 %% Empty lines must be using \r\n.
 %% Empty lines must be using \r\n.
 parse_request(<< $\n, _/binary >>, State, _) ->
 parse_request(<< $\n, _/binary >>, State, _) ->
 	error_terminate(400, State);
 	error_terminate(400, State);
+parse_request(<< $\s, _/bits >>, State, _) ->
+	error_terminate(400, State);
 %% We limit the length of the Request-line to MaxLength to avoid endlessly
 %% We limit the length of the Request-line to MaxLength to avoid endlessly
 %% reading from the socket and eventually crashing.
 %% reading from the socket and eventually crashing.
 parse_request(Buffer, State=#state{max_request_line_length=MaxLength,
 parse_request(Buffer, State=#state{max_request_line_length=MaxLength,
@@ -170,6 +172,8 @@ parse_method(<< C, Rest/bits >>, State, SoFar) ->
 
 
 parse_uri(<< $\r, _/bits >>, State, _) ->
 parse_uri(<< $\r, _/bits >>, State, _) ->
 	error_terminate(400, State);
 	error_terminate(400, State);
+parse_uri(<< $\s, _/bits >>, State, Method) ->
+	error_terminate(400, State);
 parse_uri(<< "* ", Rest/bits >>, State, Method) ->
 parse_uri(<< "* ", Rest/bits >>, State, Method) ->
 	parse_version(Rest, State, Method, <<"*">>, <<>>);
 	parse_version(Rest, State, Method, <<"*">>, <<>>);
 parse_uri(<< "http://", Rest/bits >>, State, Method) ->
 parse_uri(<< "http://", Rest/bits >>, State, Method) ->

+ 2 - 0
test/http_SUITE.erl

@@ -256,6 +256,8 @@ The document has moved
 		{400, "\n"},
 		{400, "\n"},
 		{400, "Garbage\r\n\r\n"},
 		{400, "Garbage\r\n\r\n"},
 		{400, "\r\n\r\n\r\n\r\n\r\n\r\n"},
 		{400, "\r\n\r\n\r\n\r\n\r\n\r\n"},
+		{400, " / HTTP/1.1\r\nHost: localhost\r\n\r\n"},
+		{400, "GET  HTTP/1.1\r\nHost: localhost\r\n\r\n"},
 		{400, "GET / HTTP/1.1\r\nHost: ninenines.eu\r\n\r\n"},
 		{400, "GET / HTTP/1.1\r\nHost: ninenines.eu\r\n\r\n"},
 		{400, "GET http://proxy/ HTTP/1.1\r\n\r\n"},
 		{400, "GET http://proxy/ HTTP/1.1\r\n\r\n"},
 		{400, "GET / HTTP/1.1\r\nHost: localhost:bad_port\r\n\r\n"},
 		{400, "GET / HTTP/1.1\r\nHost: localhost:bad_port\r\n\r\n"},