Skip to content

Commit

Permalink
When parsing cookies, only decode the values
Browse files Browse the repository at this point in the history
Patch utils to fix cookie parsing

[CVE-2020-8184]
  • Loading branch information
fletchto99 authored and tenderlove committed Jun 15, 2020
1 parent c9ff970 commit 1f5763d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/rack/utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,12 @@ def parse_cookies_header(header)
# The syntax for cookie headers only supports semicolons
# User Agent -> Server ==
# Cookie: SID=31d4d96e407aad42; lang=en-US
cookies = parse_query(header, ';') { |s| unescape(s) rescue s }
cookies.each_with_object({}) { |(k, v), hash| hash[k] = Array === v ? v.first : v }
return {} unless header
header.split(/[;] */n).each_with_object({}) do |cookie, cookies|
next if cookie.empty?
key, value = cookie.split('=', 2)
cookies[key] = (unescape(value) rescue value) unless cookies.key?(key)
end
end

def add_cookie_to_header(header, key, value)
Expand Down
4 changes: 4 additions & 0 deletions test/spec_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,10 @@ def initialize(*)

env = Rack::MockRequest.env_for("", "HTTP_COOKIE" => "foo=bar").freeze
Rack::Utils.parse_cookies(env).must_equal({ "foo" => "bar" })

env = Rack::MockRequest.env_for("", "HTTP_COOKIE" => "%66oo=baz;foo=bar")
cookies = Rack::Utils.parse_cookies(env)
cookies.must_equal({ "%66oo" => "baz", "foo" => "bar" })
end

it "adds new cookies to nil header" do
Expand Down

2 comments on commit 1f5763d

@utkarsh2102
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @tenderlove,

Thanks for this!
Can this be patch be backported to previous versions?
With my Debian LTS Security hat on, I'd be very delighted if this can be backported to 1.6-stable branch?

@orhantoy
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened this PR #1677 to backport the fix to 1.6.

Please sign in to comment.