Skip to content

Commit c245f7f

Browse files
unakhsbt
authored andcommitted
Limit header length
1 parent e037967 commit c245f7f

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

lib/net/http/header.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,8 @@
179179
# - #each_value: Passes each string field value to the block.
180180
#
181181
module Net::HTTPHeader
182+
MAX_KEY_LENGTH = 1024
183+
MAX_FIELD_LENGTH = 65536
182184

183185
def initialize_http_header(initheader) #:nodoc:
184186
@header = {}
@@ -189,6 +191,12 @@ def initialize_http_header(initheader) #:nodoc:
189191
warn "net/http: nil HTTP header: #{key}", uplevel: 3 if $VERBOSE
190192
else
191193
value = value.strip # raise error for invalid byte sequences
194+
if key.bytesize > MAX_KEY_LENGTH
195+
raise ArgumentError, "too long (#{key.bytesize} bytes) header: #{key[0, 30].inspect}..."
196+
end
197+
if value.bytesize > MAX_FIELD_LENGTH
198+
raise ArgumentError, "header #{key} has too long field vallue: #{value.bytesize}"
199+
end
192200
if value.count("\r\n") > 0
193201
raise ArgumentError, "header #{key} has field value #{value.inspect}, this cannot include CR/LF"
194202
end

0 commit comments

Comments
 (0)