Skip to content

Commit

Permalink
Raise an exception when a response header contains CR or LF
Browse files Browse the repository at this point in the history
  • Loading branch information
macournoyer committed May 18, 2021
1 parent dd24bf5 commit 6c58953
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
8 changes: 7 additions & 1 deletion lib/thin/headers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module Thin
# Raised when an header is not valid
# and the server can not process it.
class InvalidHeader < StandardError; end

# Store HTTP header name-value pairs direcly to a string
# and allow duplicated entries on some names.
class Headers
Expand All @@ -21,8 +25,10 @@ def []=(key, value)
value = case value
when Time
value.httpdate
when NilClass, CR_OR_LF
when NilClass
return
when CR_OR_LF
raise InvalidHeader, "Header contains CR or LF"
else
value.to_s
end
Expand Down
9 changes: 3 additions & 6 deletions spec/headers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,14 @@
end

it 'should not allow CRLF' do
@headers['Bad'] = "a\r\nSet-Cookie: injected=value"
expect(@headers.to_s).to be_empty
expect { @headers['Bad'] = "a\r\nSet-Cookie: injected=value" }.to raise_error(InvalidHeader)
end

it 'should not allow CR' do
@headers['Bad'] = "a\rSet-Cookie: injected=value"
expect(@headers.to_s).to be_empty
expect { @headers['Bad'] = "a\rSet-Cookie: injected=value" }.to raise_error(InvalidHeader)
end

it 'should not allow LF' do
@headers['Bad'] = "a\nSet-Cookie: injected=value"
expect(@headers.to_s).to be_empty
expect { @headers['Bad'] = "a\nSet-Cookie: injected=value" }.to raise_error(InvalidHeader)
end
end

0 comments on commit 6c58953

Please sign in to comment.