Skip to content

Added extra header validation for total size of headers #219

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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/net/http/header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,19 @@
module Net::HTTPHeader
MAX_KEY_LENGTH = 1024
MAX_FIELD_LENGTH = 65536
MAX_HEADER_LENGTH = 1024 * 1024 # 1 MiB

def initialize_http_header(initheader) #:nodoc:
@header = {}
return unless initheader

total_header_size = 0
initheader.each do |key, value|
total_header_size += (key.to_s.bytesize + (value ? value.to_s.bytesize : 0))
if total_header_size > MAX_HEADER_LENGTH
raise ArgumentError, "headers too large (#{total_header_size} bytes exceeds #{MAX_HEADER_LENGTH} bytes limit)"
end

warn "net/http: duplicated HTTP header: #{key}", uplevel: 3 if key?(key) and $VERBOSE
if value.nil?
warn "net/http: nil HTTP header: #{key}", uplevel: 3 if $VERBOSE
Expand Down
14 changes: 14 additions & 0 deletions test/net/http/test_httpheader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,20 @@ def test_initialize_with_symbol
assert_equal "abc", @c["foo"]
end

def test_initialize_with_max_header_length_exceeded
field_value = 'x' * (Net::HTTPHeader::MAX_FIELD_LENGTH - 100)
num_headers = (Net::HTTPHeader::MAX_HEADER_LENGTH / Net::HTTPHeader::MAX_FIELD_LENGTH) + 2

large_headers = {}
num_headers.times do |i|
large_headers["Header#{i}"] = field_value
end

assert_raise(ArgumentError) do
@c.initialize_http_header(large_headers)
end
end

def test_size
assert_equal 0, @c.size
@c['a'] = 'a'
Expand Down