Skip to content

Commit

Permalink
Fix possible HTTP Response Splitting
Browse files Browse the repository at this point in the history
Ignore any response header with \r or \n in their value

See GHSA-84j7-475p-hp8v
  • Loading branch information
macournoyer committed May 17, 2021
1 parent 026285b commit dd24bf5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/thin/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Thin
class Headers
HEADER_FORMAT = "%s: %s\r\n".freeze
ALLOWED_DUPLICATES = %w(set-cookie set-cookie2 warning www-authenticate).freeze
CR_OR_LF = /[\r\n]/.freeze

def initialize
@sent = {}
Expand All @@ -20,7 +21,7 @@ def []=(key, value)
value = case value
when Time
value.httpdate
when NilClass
when NilClass, CR_OR_LF
return
else
value.to_s
Expand Down
20 changes: 20 additions & 0 deletions spec/headers_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,24 @@
@headers['Modified-At'] = time
expect(@headers.to_s).to include("Modified-At: #{time.httpdate}")
end

it 'should format Integer values correctly' do
@headers['X-Number'] = 32
expect(@headers.to_s).to include("X-Number: 32")
end

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

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

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

0 comments on commit dd24bf5

Please sign in to comment.