diff options
author | Paul Bailey <paul.m.bailey@gmail.com> | 2018-10-08 18:49:29 (GMT) |
---|---|---|
committer | Andrew Svetlov <andrew.svetlov@gmail.com> | 2018-10-08 18:49:29 (GMT) |
commit | 4c339970570d07916bee6ade51f4e9781d51627a (patch) | |
tree | d389f9afd8cdb4e2a02e4d941d983cbecccb07e6 /Lib | |
parent | af5658ae93b0a87ab4420a7dc30a07fa5a83e252 (diff) | |
download | cpython-4c339970570d07916bee6ade51f4e9781d51627a.zip cpython-4c339970570d07916bee6ade51f4e9781d51627a.tar.gz cpython-4c339970570d07916bee6ade51f4e9781d51627a.tar.bz2 |
bpo-34911: Added support for secure websocket cookies (GH-9734)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/http/cookiejar.py | 4 | ||||
-rw-r--r-- | Lib/test/test_http_cookiejar.py | 55 |
2 files changed, 58 insertions, 1 deletions
diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index e0f1032..0ba8200 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -878,6 +878,7 @@ class DefaultCookiePolicy(CookiePolicy): strict_ns_domain=DomainLiberal, strict_ns_set_initial_dollar=False, strict_ns_set_path=False, + secure_protocols=("https", "wss") ): """Constructor arguments should be passed as keyword arguments only.""" self.netscape = netscape @@ -890,6 +891,7 @@ class DefaultCookiePolicy(CookiePolicy): self.strict_ns_domain = strict_ns_domain self.strict_ns_set_initial_dollar = strict_ns_set_initial_dollar self.strict_ns_set_path = strict_ns_set_path + self.secure_protocols = secure_protocols if blocked_domains is not None: self._blocked_domains = tuple(blocked_domains) @@ -1116,7 +1118,7 @@ class DefaultCookiePolicy(CookiePolicy): return True def return_ok_secure(self, cookie, request): - if cookie.secure and request.type != "https": + if cookie.secure and request.type not in self.secure_protocols: _debug(" secure cookie with non-secure request") return False return True diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index 6fee4df..9687259 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -984,6 +984,61 @@ class CookieTests(unittest.TestCase): c._cookies["www.acme.com"]["/"]["foo2"].secure, "secure cookie registered non-secure") + def test_secure_block(self): + pol = DefaultCookiePolicy() + c = CookieJar(policy=pol) + + headers = ["Set-Cookie: session=narf; secure; path=/"] + req = urllib.request.Request("https://www.acme.com/") + res = FakeResponse(headers, "https://www.acme.com/") + c.extract_cookies(res, req) + self.assertEqual(len(c), 1) + + req = urllib.request.Request("https://www.acme.com/") + c.add_cookie_header(req) + self.assertTrue(req.has_header("Cookie")) + + req = urllib.request.Request("http://www.acme.com/") + c.add_cookie_header(req) + self.assertFalse(req.has_header("Cookie")) + + # secure websocket protocol + req = urllib.request.Request("wss://www.acme.com/") + c.add_cookie_header(req) + self.assertTrue(req.has_header("Cookie")) + + # non-secure websocket protocol + req = urllib.request.Request("ws://www.acme.com/") + c.add_cookie_header(req) + self.assertFalse(req.has_header("Cookie")) + + def test_custom_secure_protocols(self): + pol = DefaultCookiePolicy(secure_protocols=["foos"]) + c = CookieJar(policy=pol) + + headers = ["Set-Cookie: session=narf; secure; path=/"] + req = urllib.request.Request("https://www.acme.com/") + res = FakeResponse(headers, "https://www.acme.com/") + c.extract_cookies(res, req) + self.assertEqual(len(c), 1) + + # test https removed from secure protocol list + req = urllib.request.Request("https://www.acme.com/") + c.add_cookie_header(req) + self.assertFalse(req.has_header("Cookie")) + + req = urllib.request.Request("http://www.acme.com/") + c.add_cookie_header(req) + self.assertFalse(req.has_header("Cookie")) + + req = urllib.request.Request("foos://www.acme.com/") + c.add_cookie_header(req) + self.assertTrue(req.has_header("Cookie")) + + req = urllib.request.Request("foo://www.acme.com/") + c.add_cookie_header(req) + self.assertFalse(req.has_header("Cookie")) + def test_quote_cookie_value(self): c = CookieJar(policy=DefaultCookiePolicy(rfc2965=True)) interact_2965(c, "http://www.acme.com/", r'foo=\b"a"r; Version=1') |