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 scoping for HTTPS urls. #343

Merged
merged 2 commits into from
Feb 8, 2022
Merged
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
2 changes: 1 addition & 1 deletion src/treq/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def _scoped_cookiejar_from_dict(url_object, cookie_dict):
(url_object.scheme == "https" and url_object.port == 443)
or (url_object.scheme == "http" and url_object.port == 80)
)
port = str(url_object.port)
port = str(url_object.port) if port_specified else None
domain = url_object.host
netscape_domain = domain if '.' in domain else domain + '.local'

Expand Down
48 changes: 46 additions & 2 deletions src/treq/test/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,10 +327,10 @@ def test_session_persistence_between_requests(self):
sid_4 = self.successResultOf(resp.content())
self.assertEqual(sid_3, sid_4)

def test_different_domains(self):
def test_cookies_not_sent_to_different_domains(self):
"""
Cookies manually specified as part of a dictionary are not relayed
through redirects.
through redirects to different domains.

(This is really more of a test for scoping of cookies within treq
itself, rather than just for testing.)
Expand All @@ -345,6 +345,50 @@ def test_different_domains(self):
received = self.successResultOf(resp.json())
self.assertNotIn('not-across-redirect', received.get('Cookie', [''])[0])

def test_cookies_sent_for_same_domain(self):
"""
Cookies manually specified as part of a dictionary are relayed
through redirects to the same domain.

(This is really more of a test for scoping of cookies within treq
itself, rather than just for testing.)
"""
rsrc = _RedirectResource()
stub = StubTreq(rsrc)
d = stub.request(
"GET", "https://example.org/",
cookies={'sent-to-same-domain': 'yes'}
)
resp = self.successResultOf(d)
received = self.successResultOf(resp.json())
self.assertIn('sent-to-same-domain', received.get('Cookie', [''])[0])

def test_cookies_sent_with_explicit_port(self):
"""
Cookies will be sent for URLs that specify a non-default port for their scheme.

(This is really more of a test for scoping of cookies within treq
itself, rather than just for testing.)
"""
rsrc = _RedirectResource()
stub = StubTreq(rsrc)

d = stub.request(
"GET", "http://example.org:8080/redirected",
cookies={'sent-to-non-default-port': 'yes'}
)
resp = self.successResultOf(d)
received = self.successResultOf(resp.json())
self.assertIn('sent-to-non-default-port', received.get('Cookie', [''])[0])

d = stub.request(
"GET", "https://example.org:8443/redirected",
cookies={'sent-to-non-default-port': 'yes'}
)
resp = self.successResultOf(d)
received = self.successResultOf(resp.json())
self.assertIn('sent-to-non-default-port', received.get('Cookie', [''])[0])


class HasHeadersTests(TestCase):
"""
Expand Down