Skip to content

Commit 45f2e84

Browse files
committed
Return 400 response for chunked requests with unexpected data after chunk
Fixes #133
1 parent a27d7ed commit 45f2e84

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

lib/webrick/httprequest.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,11 @@ def read_chunked(socket, block)
566566
block.call(data)
567567
end while (chunk_size -= sz) > 0
568568

569-
read_line(socket) # skip CRLF
569+
line = read_line(socket) # skip CRLF
570+
unless line == "\r\n"
571+
raise HTTPStatus::BadRequest, "extra data after chunk `#{line}'."
572+
end
573+
570574
chunk_size, = read_chunk_size(socket)
571575
end
572576
read_header(socket) # trailer + CRLF

test/webrick/test_httprequest.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,30 @@ def test_bad_chunked
312312
end
313313
end
314314

315+
def test_bad_chunked_extra_data
316+
msg = <<-_end_of_message_
317+
POST /path HTTP/1.1\r
318+
Transfer-Encoding: chunked\r
319+
\r
320+
3\r
321+
ABCthis-all-gets-ignored\r
322+
0\r
323+
\r
324+
_end_of_message_
325+
msg.gsub!(/^ {6}/, "")
326+
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
327+
req.parse(StringIO.new(msg))
328+
assert_raise(WEBrick::HTTPStatus::BadRequest){ req.body }
329+
330+
# chunked req.body_reader
331+
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
332+
req.parse(StringIO.new(msg))
333+
dst = StringIO.new
334+
assert_raise(WEBrick::HTTPStatus::BadRequest) do
335+
IO.copy_stream(req.body_reader, dst)
336+
end
337+
end
338+
315339
def test_null_byte_in_header
316340
msg = <<-_end_of_message_
317341
POST /path HTTP/1.1\r

0 commit comments

Comments
 (0)