Skip to content

Commit

Permalink
When parsing cookies, only decode the values
Browse files Browse the repository at this point in the history
  • Loading branch information
fletchto99 authored and tenderlove committed Jun 15, 2020
1 parent e7ba1b0 commit a243510
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 @@ -215,8 +215,12 @@ def parse_cookies_header(header)
# the Cookie header such that those with more specific Path attributes
# precede those with less specific. Ordering with respect to other
# attributes (e.g., Domain) is unspecified.
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
module_function :parse_cookies_header

Expand Down
4 changes: 4 additions & 0 deletions test/spec_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,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

0 comments on commit a243510

Please sign in to comment.