Skip to content

gh-135993: Fix IPv6 bug in set_ok_port and return_ok_port #136076

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

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open
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
7 changes: 3 additions & 4 deletions Lib/http/cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,10 +656,9 @@ def request_path(request):
return path

def request_port(request):
host = request.host
i = host.find(':')
if i >= 0:
port = host[i+1:]
match = cut_port_re.search(request.host)
if match:
port = match.group(0).removeprefix(':')
try:
int(port)
except ValueError:
Expand Down
40 changes: 40 additions & 0 deletions Lib/test/test_http_cookiejar.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,6 +1262,46 @@ def test_missing_final_slash(self):
c.add_cookie_header(req)
self.assertTrue(req.has_header("Cookie"))

def test_set_ok_port(self):
pol = DefaultCookiePolicy()
c = CookieJar(policy=pol)
headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; port=1234"]
req = urllib.request.Request("http://127.0.0.1:1234")
res = FakeResponse(headers, "http://127.0.0.1:1234")
self.assertTrue(pol.set_ok_port(c.make_cookies(res, req)[0], req))

req = urllib.request.Request("http://acme.com:1234")
res = FakeResponse(headers, "http://acme.com:1234")
self.assertTrue(pol.set_ok_port(c.make_cookies(res, req)[0], req))

req = urllib.request.Request("http://[::1]:1234")
res = FakeResponse(headers, "http://[::1]:1234")
self.assertTrue(pol.set_ok_port(c.make_cookies(res, req)[0], req))

req = urllib.request.Request("http://[::1]:1235")
res = FakeResponse(headers, "http://[::1]:1235")
self.assertFalse(pol.set_ok_port(c.make_cookies(res, req)[0], req))

def test_return_ok_port(self):
pol = DefaultCookiePolicy()
c = CookieJar(policy=pol)
headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/; port=1234"]
req = urllib.request.Request("http://127.0.0.1:1234")
res = FakeResponse(headers, "http://127.0.0.1:1234")
self.assertTrue(pol.return_ok_port(c.make_cookies(res, req)[0], req))

req = urllib.request.Request("http://acme.com:1234")
res = FakeResponse(headers, "http://acme.com:1234")
self.assertTrue(pol.return_ok_port(c.make_cookies(res, req)[0], req))

req = urllib.request.Request("http://[::1]:1234")
res = FakeResponse(headers, "http://[::1]:1234")
self.assertTrue(pol.return_ok_port(c.make_cookies(res, req)[0], req))

req = urllib.request.Request("http://[::1]:1235")
res = FakeResponse(headers, "http://[::1]:1235")
self.assertFalse(pol.return_ok_port(c.make_cookies(res, req)[0], req))

def test_domain_mirror(self):
pol = DefaultCookiePolicy(rfc2965=True)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:mod:`http.cookiejar`: Fix a bug that occurs when :class:`~http.cookiejar.DefaultCookiePolicy`
attempts to parse the port of an IPv6 address in func ``request_port()``.
Loading