diff --git a/lib/http/cookie_jar.rb b/lib/http/cookie_jar.rb index 0889c07..0bacf68 100644 --- a/lib/http/cookie_jar.rb +++ b/lib/http/cookie_jar.rb @@ -9,8 +9,6 @@ end class HTTP::CookieJar include Enumerable - # add_cookie wants something resembling a URI. - attr_reader :jar def initialize @@ -23,6 +21,9 @@ class HTTP::CookieJar # Add a +cookie+ to the jar and return self. def add(cookie) + if cookie.domain.nil? || cookie.path.nil? + raise ArgumentError, "a cookie with unknown domain or path cannot be added" + end normal_domain = cookie.domain_name.hostname ((@jar[normal_domain] ||= {})[cookie.path] ||= {})[cookie.name] = cookie diff --git a/test/test_http_cookie_jar.rb b/test/test_http_cookie_jar.rb index aba2910..3a0a818 100644 --- a/test/test_http_cookie_jar.rb +++ b/test/test_http_cookie_jar.rb @@ -171,6 +171,18 @@ class TestHTTPCookieJar < Test::Unit::TestCase assert_equal(1, @jar.cookies(url).length) end + def test_add_rejects_cookies_with_unknown_domain_or_path + cookie = HTTP::Cookie.new(cookie_values.reject { |k,v| [:origin, :domain].include?(k) }) + assert_raises(ArgumentError) { + @jar.add(cookie) + } + + cookie = HTTP::Cookie.new(cookie_values.reject { |k,v| [:origin, :path].include?(k) }) + assert_raises(ArgumentError) { + @jar.add(cookie) + } + end + def test_add_does_not_reject_cookies_from_a_nested_subdomain url = URI 'http://y.x.foo.com'