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

Prevent request smuggling #146

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/webrick/httprequest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,10 @@ def parse_host_request_line(host)
def read_body(socket, block)
return unless socket
if tc = self['transfer-encoding']
if self['content-length']
raise HTTPStatus::BadRequest, "request with both transfer-encoding and content-length, possible request smuggling"
end

case tc
when /\Achunked\z/io then read_chunked(socket, block)
else raise HTTPStatus::NotImplemented, "Transfer-Encoding: #{tc}."
Expand Down
18 changes: 18 additions & 0 deletions test/webrick/test_httprequest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,24 @@ def test_duplicate_content_length_header
}
end

def test_content_length_and_transfer_encoding_headers_smuggling
msg = <<~HTTP.gsub("\n", "\r\n")
POST /user HTTP/1.1
Content-Length: 28
Transfer-Encoding: chunked

0

GET /admin HTTP/1.1

HTTP
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
req.parse(StringIO.new(msg))
assert_raise(WEBrick::HTTPStatus::BadRequest){
req.body
}
end

def test_parse_headers
msg = <<~HTTP.gsub("\n", "\r\n")
GET /path HTTP/1.1
Expand Down