Skip to content

Commit a243510

Browse files
fletchto99tenderlove
authored andcommitted
When parsing cookies, only decode the values
[CVE-2020-8184]
1 parent e7ba1b0 commit a243510

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/rack/utils.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,12 @@ def parse_cookies_header(header)
215215
# the Cookie header such that those with more specific Path attributes
216216
# precede those with less specific. Ordering with respect to other
217217
# attributes (e.g., Domain) is unspecified.
218-
cookies = parse_query(header, ';,') { |s| unescape(s) rescue s }
219-
cookies.each_with_object({}) { |(k, v), hash| hash[k] = Array === v ? v.first : v }
218+
return {} unless header
219+
header.split(/[;,] */n).each_with_object({}) do |cookie, cookies|
220+
next if cookie.empty?
221+
key, value = cookie.split('=', 2)
222+
cookies[key] = (unescape(value) rescue value) unless cookies.key?(key)
223+
end
220224
end
221225
module_function :parse_cookies_header
222226

test/spec_utils.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,10 @@ def initialize(*)
512512

513513
env = Rack::MockRequest.env_for("", "HTTP_COOKIE" => "foo=bar").freeze
514514
Rack::Utils.parse_cookies(env).must_equal({ "foo" => "bar" })
515+
516+
env = Rack::MockRequest.env_for("", "HTTP_COOKIE" => "%66oo=baz;foo=bar")
517+
cookies = Rack::Utils.parse_cookies(env)
518+
cookies.must_equal({ "%66oo" => "baz", "foo" => "bar" })
515519
end
516520

517521
it "adds new cookies to nil header" do

0 commit comments

Comments
 (0)