Skip to content

Commit 15a9391

Browse files
committed
Return 400 response for chunked requests with unexpected data after chunk
Fixes #133
1 parent 2b38d56 commit 15a9391

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/webrick/httprequest.rb

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

577-
read_line(socket) # skip CRLF
577+
line = read_line(socket) # skip CRLF
578+
unless line == "\r\n"
579+
raise HTTPStatus::BadRequest, "extra data after chunk `#{line}'."
580+
end
581+
578582
chunk_size, = read_chunk_size(socket)
579583
end
580584
read_header(socket) # trailer + CRLF

test/webrick/test_httprequest.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,29 @@ def test_bad_chunked
423423
end
424424
end
425425

426+
def test_bad_chunked_extra_data
427+
msg = <<~HTTP
428+
POST /path HTTP/1.1\r
429+
Transfer-Encoding: chunked\r
430+
\r
431+
3\r
432+
ABCthis-all-gets-ignored\r
433+
0\r
434+
\r
435+
HTTP
436+
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
437+
req.parse(StringIO.new(msg))
438+
assert_raise(WEBrick::HTTPStatus::BadRequest){ req.body }
439+
440+
# chunked req.body_reader
441+
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
442+
req.parse(StringIO.new(msg))
443+
dst = StringIO.new
444+
assert_raise(WEBrick::HTTPStatus::BadRequest) do
445+
IO.copy_stream(req.body_reader, dst)
446+
end
447+
end
448+
426449
def test_null_byte_in_header
427450
msg = <<~HTTP.gsub("\n", "\r\n")
428451
POST /path HTTP/1.1\r

0 commit comments

Comments
 (0)