mirror of
https://github.com/samsonjs/http-cookie.git
synced 2026-04-27 14:57:46 +00:00
Make HTTP::Cookie.parse() reject cookies longer than 4096 bytes.
This limit is defined as HTTP::Cookie::MAX_LENGTH.
This commit is contained in:
parent
10e9ca5c60
commit
84d375e3b7
2 changed files with 22 additions and 0 deletions
|
|
@ -9,6 +9,9 @@ end
|
||||||
|
|
||||||
# This class is used to represent an HTTP Cookie.
|
# This class is used to represent an HTTP Cookie.
|
||||||
class HTTP::Cookie
|
class HTTP::Cookie
|
||||||
|
# Maximum number of bytes per cookie (RFC 6265 6.1 requires 4096 at least)
|
||||||
|
MAX_LENGTH = 4096
|
||||||
|
|
||||||
PERSISTENT_PROPERTIES = %w[
|
PERSISTENT_PROPERTIES = %w[
|
||||||
name value
|
name value
|
||||||
domain for_domain path
|
domain for_domain path
|
||||||
|
|
@ -153,6 +156,11 @@ class HTTP::Cookie
|
||||||
|
|
||||||
[].tap { |cookies|
|
[].tap { |cookies|
|
||||||
set_cookie.split(/,(?=[^;,]*=)|,$/).each { |c|
|
set_cookie.split(/,(?=[^;,]*=)|,$/).each { |c|
|
||||||
|
if c.bytesize > MAX_LENGTH
|
||||||
|
logger.warn("Cookie definition too long: #{c}") if logger
|
||||||
|
next
|
||||||
|
end
|
||||||
|
|
||||||
cookie_elem = c.split(/;+/)
|
cookie_elem = c.split(/;+/)
|
||||||
first_elem = cookie_elem.shift
|
first_elem = cookie_elem.shift
|
||||||
first_elem.strip!
|
first_elem.strip!
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
require File.expand_path('helper', File.dirname(__FILE__))
|
require File.expand_path('helper', File.dirname(__FILE__))
|
||||||
|
|
||||||
class TestHTTPCookie < Test::Unit::TestCase
|
class TestHTTPCookie < Test::Unit::TestCase
|
||||||
|
|
@ -79,6 +80,19 @@ class TestHTTPCookie < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_parse_too_long_cookie
|
||||||
|
uri = URI.parse 'http://example'
|
||||||
|
|
||||||
|
cookie_str = "foo=#{'クッキー' * 340}; path=/ab/"
|
||||||
|
assert_equal(HTTP::Cookie::MAX_LENGTH - 1, cookie_str.bytesize)
|
||||||
|
|
||||||
|
assert_equal 1, HTTP::Cookie.parse(cookie_str, :origin => uri).size
|
||||||
|
|
||||||
|
assert_equal 1, HTTP::Cookie.parse(cookie_str.sub(';', 'x;'), :origin => uri).size
|
||||||
|
|
||||||
|
assert_equal 0, HTTP::Cookie.parse(cookie_str.sub(';', 'xx;'), :origin => uri).size
|
||||||
|
end
|
||||||
|
|
||||||
def test_parse_quoted
|
def test_parse_quoted
|
||||||
cookie_str =
|
cookie_str =
|
||||||
"quoted=\"value\"; Expires=Sun, 06 Nov 2011 00:11:18 GMT; Path=/"
|
"quoted=\"value\"; Expires=Sun, 06 Nov 2011 00:11:18 GMT; Path=/"
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue