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

Only strip space and horizontal tab in headers #141

Merged
merged 2 commits into from
Jul 11, 2024
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
19 changes: 11 additions & 8 deletions lib/webrick/httputils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,20 +178,23 @@ def parse_header(raw)
field.downcase!
header[field] = HEADER_CLASSES[field].new unless header.has_key?(field)
header[field] << value
when /^\s+([^\r\n\0]*?)\r\n/om
when /^[ \t]+([^\r\n\0]*?)\r\n/om
unless field
raise HTTPStatus::BadRequest, "bad header '#{line}'."
end
value = line
value.lstrip!
value.gsub!(/\A[ \t]+/, '')
value.slice!(-2..-1)
header[field][-1] << " " << value
else
raise HTTPStatus::BadRequest, "bad header '#{line}'."
end
}
header.each{|key, values|
values.each(&:strip!)
values.each{|value|
value.gsub!(/\A[ \t]+/, '')
value.gsub!(/[ \t]+\z/, '')
}
}
header
end
Expand All @@ -202,7 +205,7 @@ def parse_header(raw)

def split_header_value(str)
str.scan(%r'\G((?:"(?:\\.|[^"])+?"|[^",]++)+)
(?:,\s*|\Z)'xn).flatten
(?:,[ \t]*|\Z)'xn).flatten
end
module_function :split_header_value

Expand Down Expand Up @@ -230,9 +233,9 @@ def parse_range_header(ranges_specifier)
def parse_qvalues(value)
tmp = []
if value
parts = value.split(/,\s*/)
parts = value.split(/,[ \t]*/)
parts.each {|part|
if m = %r{^([^\s,]+?)(?:;\s*q=(\d+(?:\.\d+)?))?$}.match(part)
if m = %r{^([^ \t,]+?)(?:;[ \t]*q=(\d+(?:\.\d+)?))?$}.match(part)
val = m[1]
q = (m[2] or 1).to_f
tmp.push([val, q])
Expand Down Expand Up @@ -331,8 +334,8 @@ def <<(str)
elsif str == CRLF
@header = HTTPUtils::parse_header(@raw_header.join)
if cd = self['content-disposition']
if /\s+name="(.*?)"/ =~ cd then @name = $1 end
if /\s+filename="(.*?)"/ =~ cd then @filename = $1 end
if /[ \t]+name="(.*?)"/ =~ cd then @name = $1 end
if /[ \t]+filename="(.*?)"/ =~ cd then @filename = $1 end
end
else
@raw_header << str
Expand Down
40 changes: 30 additions & 10 deletions test/webrick/test_httprequest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def test_invalid_content_length_header
HTTP
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
assert_raise(WEBrick::HTTPStatus::BadRequest){
req.parse(StringIO.new(msg.gsub(/^ {8}/, "").gsub("\n", "\r\n")))
req.parse(StringIO.new(msg))
}
end
end
Expand All @@ -102,7 +102,7 @@ def test_bare_lf_request_line
HTTP
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
assert_raise(WEBrick::HTTPStatus::BadRequest){
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
req.parse(StringIO.new(msg))
}
end

Expand All @@ -114,10 +114,31 @@ def test_bare_lf_header
HTTP
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
assert_raise(WEBrick::HTTPStatus::BadRequest){
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
req.parse(StringIO.new(msg))
}
end

def test_header_vt_ff_whitespace
msg = <<~HTTP
GET / HTTP/1.1\r
Foo: \x0b1\x0c\r
\r
HTTP
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
req.parse(StringIO.new(msg))
assert_equal("\x0b1\x0c", req["Foo"])

msg = <<~HTTP
GET / HTTP/1.1\r
Foo: \x0b1\x0c\r
\x0b2\x0c\r
\r
HTTP
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
req.parse(StringIO.new(msg))
assert_equal("\x0b1\x0c \x0b2\x0c", req["Foo"])
end

def test_bare_cr_request_line
msg = <<~HTTP.gsub("\n", "\r\n")
GET / HTTP/1.1\r\r
Expand All @@ -126,7 +147,7 @@ def test_bare_cr_request_line
HTTP
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
assert_raise(WEBrick::HTTPStatus::BadRequest){
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
req.parse(StringIO.new(msg))
}
end

Expand All @@ -138,7 +159,7 @@ def test_bare_cr_header
HTTP
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
assert_raise(WEBrick::HTTPStatus::BadRequest){
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
req.parse(StringIO.new(msg))
}
end

Expand All @@ -150,7 +171,7 @@ def test_invalid_request_lines
HTTP
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
assert_raise(WEBrick::HTTPStatus::BadRequest){
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
req.parse(StringIO.new(msg))
}

msg = <<~HTTP.gsub("\n", "\r\n")
Expand All @@ -160,7 +181,7 @@ def test_invalid_request_lines
HTTP
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
assert_raise(WEBrick::HTTPStatus::BadRequest){
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
req.parse(StringIO.new(msg))
}

msg = <<~HTTP.gsub("\n", "\r\n")
Expand All @@ -170,7 +191,7 @@ def test_invalid_request_lines
HTTP
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
assert_raise(WEBrick::HTTPStatus::BadRequest){
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
req.parse(StringIO.new(msg))
}

msg = <<~HTTP.gsub("\n", "\r\n")
Expand All @@ -180,7 +201,7 @@ def test_invalid_request_lines
HTTP
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
assert_raise(WEBrick::HTTPStatus::BadRequest){
req.parse(StringIO.new(msg.gsub(/^ {6}/, "")))
req.parse(StringIO.new(msg))
}
end

Expand Down Expand Up @@ -406,7 +427,6 @@ def test_null_byte_in_header
Evil: evil\x00\r
\r
HTTP
msg.gsub!(/^ {6}/, "")
req = WEBrick::HTTPRequest.new(WEBrick::Config::HTTP)
assert_raise(WEBrick::HTTPStatus::BadRequest){ req.parse(StringIO.new(msg)) }
end
Expand Down