Skip to content

Commit 335e338

Browse files
committed
Add maximum_header_count limit.
1 parent 0b176f2 commit 335e338

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

lib/protocol/http1/connection.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ module HTTP1
4646
VALID_FIELD_VALUE = /\A#{FIELD_VALUE}\z/.freeze
4747

4848
DEFAULT_MAXIMUM_LINE_LENGTH = 8192
49+
MAXIMUM_HEADER_COUNT = 128
4950

5051
class Connection
5152
CRLF = "\r\n"
5253
HTTP10 = "HTTP/1.0"
5354
HTTP11 = "HTTP/1.1"
5455

55-
def initialize(stream, persistent: true, state: :idle, maximum_line_length: DEFAULT_MAXIMUM_LINE_LENGTH)
56+
def initialize(stream, persistent: true, state: :idle, maximum_line_length: DEFAULT_MAXIMUM_LINE_LENGTH, maximum_header_count: MAXIMUM_HEADER_COUNT)
5657
@stream = stream
5758

5859
@persistent = persistent
@@ -61,6 +62,7 @@ def initialize(stream, persistent: true, state: :idle, maximum_line_length: DEFA
6162
@count = 0
6263

6364
@maximum_line_length = maximum_line_length
65+
@maximum_header_count = maximum_header_count
6466
end
6567

6668
attr :stream
@@ -381,6 +383,10 @@ def read_headers
381383
fields = []
382384

383385
while line = read_line
386+
if @maximum_header_count and fields.size > @maximum_header_count
387+
raise HeaderCountError, "Too many headers: #{fields.size} > #{@maximum_header_count}!"
388+
end
389+
384390
# Empty line indicates end of headers:
385391
break if line.empty?
386392

lib/protocol/http1/error.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class ProtocolError < Error
1717
class LineLengthError < Error
1818
end
1919

20+
class HeaderCountError < Error
21+
end
22+
2023
# The request was not able to be parsed correctly, or failed some kind of validation.
2124
class BadRequest < Error
2225
end

0 commit comments

Comments
 (0)