Skip to content

Commit

Permalink
Do not fail on cookies that are not URI escaped
Browse files Browse the repository at this point in the history
 * Closes #360

Conflicts:
	test/spec_request.rb
  • Loading branch information
James Tucker committed Jan 4, 2013
1 parent 0df2674 commit 9a98b44
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
6 changes: 2 additions & 4 deletions lib/rack/request.rb
Expand Up @@ -260,12 +260,10 @@ def cookies
# 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.
Utils.parse_query(string, ';,').each { |k,v| hash[k] = Array === v ? v.first : v }
cookies = Utils.parse_query(string, ';,') { |s| Rack::Utils.unescape(s) rescue s }
cookies.each { |k,v| hash[k] = Array === v ? v.first : v }
@env["rack.request.cookie_string"] = string
hash
rescue => error
error.message.replace "cannot parse Cookie header: #{error.message}"
raise
end

def xhr?
Expand Down
6 changes: 4 additions & 2 deletions lib/rack/utils.rb
Expand Up @@ -63,12 +63,14 @@ class << self
# and ';' characters. You can also use this to parse
# cookies by changing the characters used in the second
# parameter (which defaults to '&;').
def parse_query(qs, d = nil)
def parse_query(qs, d = nil, &unescaper)
unescaper ||= method(:unescape)

params = KeySpaceConstrainedParams.new

(qs || '').split(d ? /[#{d}] */n : DEFAULT_SEP).each do |p|
next if p.empty?
k, v = p.split('=', 2).map { |x| unescape(x) }
k, v = p.split('=', 2).map(&unescaper)
next unless k || v

if cur = params[k]
Expand Down
4 changes: 2 additions & 2 deletions test/spec_request.rb
Expand Up @@ -411,9 +411,9 @@
req.cookies.should.equal 'foo' => 'bar'
end

should "raise any errors on every request" do
should "pass through non-uri escaped cookies as-is" do
req = Rack::Request.new Rack::MockRequest.env_for("", "HTTP_COOKIE" => "foo=%")
2.times { proc { req.cookies }.should.raise(ArgumentError) }
req.cookies["foo"].should == "%"
end

should "parse cookies according to RFC 2109" do
Expand Down

0 comments on commit 9a98b44

Please sign in to comment.