Skip to content

Add maximum_header_count limit. #43

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: main
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: 7 additions & 1 deletion lib/protocol/http1/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ module HTTP1
VALID_FIELD_VALUE = /\A#{FIELD_VALUE}\z/.freeze

DEFAULT_MAXIMUM_LINE_LENGTH = 8192
MAXIMUM_HEADER_COUNT = 128

class Connection
CRLF = "\r\n"
HTTP10 = "HTTP/1.0"
HTTP11 = "HTTP/1.1"

def initialize(stream, persistent: true, state: :idle, maximum_line_length: DEFAULT_MAXIMUM_LINE_LENGTH)
def initialize(stream, persistent: true, state: :idle, maximum_line_length: DEFAULT_MAXIMUM_LINE_LENGTH, maximum_header_count: MAXIMUM_HEADER_COUNT)
@stream = stream

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

@maximum_line_length = maximum_line_length
@maximum_header_count = maximum_header_count
end

attr :stream
Expand Down Expand Up @@ -381,6 +383,10 @@ def read_headers
fields = []

while line = read_line
if @maximum_header_count and fields.size > @maximum_header_count
raise HeaderCountError, "Too many headers: #{fields.size} > #{@maximum_header_count}!"
end

# Empty line indicates end of headers:
break if line.empty?

Expand Down
3 changes: 3 additions & 0 deletions lib/protocol/http1/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class ProtocolError < Error
class LineLengthError < Error
end

class HeaderCountError < Error
end

# The request was not able to be parsed correctly, or failed some kind of validation.
class BadRequest < Error
end
Expand Down
Loading