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

Raise HTTPStatus::BadRequest for requests with invalid/duplicate content-length headers #120

Merged
merged 1 commit into from
Aug 16, 2023
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
8 changes: 8 additions & 0 deletions lib/webrick/httprequest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,14 @@ def read_header(socket)
end
end
@header = HTTPUtils::parse_header(@raw_header.join)

if (content_length = @header['content-length']) && content_length.length != 0
if content_length.length > 1
raise HTTPStatus::BadRequest, "multiple content-length request headers"
elsif !/\A\d+\z/.match?(content_length[0])
raise HTTPStatus::BadRequest, "invalid content-length request header"
end
end
end

def parse_uri(str, scheme="http")
Expand Down
25 changes: 25 additions & 0 deletions test/webrick/test_httprequest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,31 @@ def test_request_uri_too_large
}
end

def test_invalid_content_length_header
['', ' ', ' +1', ' -1', ' a'].each do |cl|
msg = <<-_end_of_message_
GET / HTTP/1.1
Content-Length:#{cl}
_end_of_message_
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
assert_raise(WEBrick::HTTPStatus::BadRequest){
req.parse(StringIO.new(msg.gsub(/^ {8}/, "")))
}
end
end

def test_duplicate_content_length_header
msg = <<-_end_of_message_
GET / HTTP/1.1
Content-Length: 1
Content-Length: 2
_end_of_message_
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
assert_raise(WEBrick::HTTPStatus::BadRequest){
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
}
end

def test_parse_headers
msg = <<-_end_of_message_
GET /path HTTP/1.1
Expand Down