Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: cookie-av allows arbitrary casing #349

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions lib/rack/test/cookie_jar.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def initialize(raw, uri = nil, default_host = DEFAULT_HOST)
@raw, options = raw.split(/[;,] */n, 2)

@name, @value = parse_query(@raw, ';').to_a.first
@options = parse_query(options, ';')
@options = Hash[parse_query(options, ';').map { |k, v| [k.downcase, v] }]

if domain = @options['domain']
@exact_domain_match = false
Expand Down Expand Up @@ -69,7 +69,7 @@ def secure?
# Whether the cookie has the httponly flag, indicating it is not available via
# a javascript API.
def http_only?
@options.key?('HttpOnly') || @options.key?('httponly')
@options.key?('httponly')
end

# The explicit or implicit path for the cookie.
Expand Down Expand Up @@ -110,11 +110,13 @@ def <=>(other)

# A hash of cookie options, including the cookie value, but excluding the cookie name.
def to_h
@options.merge(
hash = @options.merge(
'value' => @value,
'HttpOnly' => http_only?,
'secure' => secure?
)
hash.delete('httponly')
hash
end
alias to_hash to_h

Expand Down
22 changes: 19 additions & 3 deletions spec/rack/test/cookie_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,28 @@ def cookie.expired?; true end
cookie_string = [
'/',
'csrf_id=ABC123',
'path=/',
'expires=Wed, 01 Jan 2020 08:00:00 GMT',
'path=/cookie',
'HttpOnly'
].join(Rack::Test::CookieJar::DELIMITER)
cookie = Rack::Test::Cookie.new(cookie_string)
cookie.path.must_equal '/'
cookie.path.must_equal '/cookie'
end

it 'attribute names are case-insensitive' do
cookie_string = [
'/',
'csrf_id=ABC123',
'Path=/cookie',
'Expires=Wed, 01 Jan 2020 08:00:00 GMT',
'HttpOnly',
'Secure',
].join(Rack::Test::CookieJar::DELIMITER)
cookie = Rack::Test::Cookie.new(cookie_string)

cookie.path.must_equal '/cookie'
cookie.secure?.must_equal true
cookie.http_only?.must_equal true
cookie.expires.must_equal Time.parse('Wed, 01 Jan 2020 08:00:00 GMT')
end

it 'escapes cookie values' do
Expand Down