8000 When parsing cookies, only decode the values · rack/rack@5ccca47 · GitHub
[go: up one dir, main page]

Skip to content

Commit 5ccca47

Browse files
fletchto99tenderlove
authored andcommitted
When parsing cookies, only decode the values
Patch utils to fix cookie parsing [CVE-2020-8184]
1 parent a5e80f0 commit 5ccca47

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
@@ -212,8 +212,12 @@ def parse_cookies_header(header)
212212
# The syntax for cookie headers only supports semicolons
213213
# User Agent -> Server ==
214214
# Cookie: SID=31d4d96e407aad42; lang=en-US
215-
cookies = parse_query(header, ';') { |s| unescape(s) rescue s }
216-
cookies.each_with_object({}) { |(k, v), hash| hash[k] = Array === v ? v.first : v }
215+
return {} unless header
216+
header.split(/[;] */n).each_with_object({}) do |cookie, cookies|
217+
next if cookie.empty?
218+
key, value = cookie.split('=', 2)
219+
cookies[key] = (unescape(value) rescue value) unless cookies.key?(key)
220+
end
217221
end
218222

219223
def add_cookie_to_header(header, key, value)

test/spec_utils.rb

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

525525
env = Rack::MockRequest.env_for("", "HTTP_COOKIE" => "foo=bar").freeze
526526
Rack::Utils.parse_cookies(env).must_equal({ "foo" => "bar" })
527+
528+
env = Rack::MockRequest.env_for("", "HTTP_COOKIE" => "%66oo=baz;foo=bar")
529+
cookies = Rack::Utils.parse_cookies(env)
530+
cookies.must_equal({ "%66oo" => "baz", "foo" => "bar" })
527531
end
528532

529533
it "adds new cookies to nil header" do

0 commit comments

Comments
 (0)
0