From e3cfeb676b98662f2905dc6043d16ba82cbd226b Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 00:55:27 +0800 Subject: [PATCH 01/40] Update cookiejar.py --- Lib/http/cookiejar.py | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 68cf16c93cc1c8..45905a54dc692f 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -29,14 +29,15 @@ 'FileCookieJar', 'LWPCookieJar', 'LoadError', 'MozillaCookieJar'] import os -import copy -import datetime import re +import copy import time +import datetime import urllib.parse, urllib.request import threading as _threading import http.client # only for the default HTTP port from calendar import timegm +from ipaddress import ip_address debug = False # set to True to enable debugging via the logging module logger = None @@ -533,14 +534,21 @@ def parse_ns_headers(ns_headers): IPV4_RE = re.compile(r"\.\d+$", re.ASCII) +def is_ip(text): + """Return True if text is a valid IP address.""" + # This function is a replacement of regex `IPV4_RE` in previous versions. + try: + ip_address(text) + return True + except ValueError: + return False def is_HDN(text): """Return True if text is a host domain name.""" # XXX # This may well be wrong. Which RFC is HDN defined in, if any (for # the purposes of RFC 2965)? - # For the current implementation, what about IPv6? Remember to look - # at other uses of IPV4_RE also, if change this. - if IPV4_RE.search(text): + # Both IPv4 and IPv6 are supported. + if is_ip(text): return False if text == "": return False @@ -593,7 +601,7 @@ def liberal_is_HDN(text): For accepting/blocking domains. """ - if IPV4_RE.search(text): + if is_ip(text): return False return True @@ -607,9 +615,10 @@ def user_domain_match(A, B): B = B.lower() if not (liberal_is_HDN(A) and liberal_is_HDN(B)): if A == B: - # equal IP addresses + # equal IPv4 addresses return True return False + # A and B may be HDNs or a IPv6 addresses now initial_dot = B.startswith(".") if initial_dot and A.endswith(B): return True @@ -641,7 +650,8 @@ def eff_request_host(request): """ erhn = req_host = request_host(request) - if "." not in req_host: + if "." not in req_host and '[' not in req_host: + # detect '[' mainly for IPv6 addr like [::1] erhn = req_host + ".local" return req_host, erhn From 8c5320a68930a0db0ddc9e37372dac967a6d7449 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Fri, 20 Jun 2025 17:03:53 +0000 Subject: [PATCH 02/40] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20b?= =?UTF-8?q?lurb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst diff --git a/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst b/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst new file mode 100644 index 00000000000000..12d0f804d7b063 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst @@ -0,0 +1 @@ +:mod: `http.cookiejar` now supports IPv6 address in :attr:`blocked_domains` and :attr:`allowed_domains` of ``DefaultCookiePolicy`` From 4c452f9c931415848a29a18fe0db58da4413b8c0 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 01:06:55 +0800 Subject: [PATCH 03/40] Update 2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst --- .../next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst b/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst index 12d0f804d7b063..37db5e79d60eb3 100644 --- a/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst +++ b/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst @@ -1 +1 @@ -:mod: `http.cookiejar` now supports IPv6 address in :attr:`blocked_domains` and :attr:`allowed_domains` of ``DefaultCookiePolicy`` +:mod: `http.cookiejar` now supports IPv6 address in `blocked_domains` and `allowed_domains` of class `DefaultCookiePolicy` From 60304ce84303ebdfd2f98f459bda906b4a8d8723 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 01:07:06 +0800 Subject: [PATCH 04/40] Update 2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst --- .../next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst b/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst index 37db5e79d60eb3..25c2928e511cd3 100644 --- a/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst +++ b/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst @@ -1 +1 @@ -:mod: `http.cookiejar` now supports IPv6 address in `blocked_domains` and `allowed_domains` of class `DefaultCookiePolicy` +:mod:`http.cookiejar` now supports IPv6 address in `blocked_domains` and `allowed_domains` of class `DefaultCookiePolicy` From 714d4d8d27603954d6ede8627be16b42b0537a83 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 01:09:43 +0800 Subject: [PATCH 05/40] Update 2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst --- .../next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst b/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst index 25c2928e511cd3..2745d9426e7441 100644 --- a/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst +++ b/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst @@ -1 +1 @@ -:mod:`http.cookiejar` now supports IPv6 address in `blocked_domains` and `allowed_domains` of class `DefaultCookiePolicy` +:mod:`http.cookiejar` now supports IPv6 address in blocked_domains and allowed_domains of class DefaultCookiePolicy From 4c9ab02f4451ba8ad7fe5cfa180ceb425f096e25 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 01:19:24 +0800 Subject: [PATCH 06/40] Update Lib/http/cookiejar.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/http/cookiejar.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 45905a54dc692f..3a567717561e0f 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -547,7 +547,6 @@ def is_HDN(text): # XXX # This may well be wrong. Which RFC is HDN defined in, if any (for # the purposes of RFC 2965)? - # Both IPv4 and IPv6 are supported. if is_ip(text): return False if text == "": From 2c9f6a4d0ed409257cd8aca74aa2f988ac766898 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 01:22:23 +0800 Subject: [PATCH 07/40] Update Lib/http/cookiejar.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/http/cookiejar.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 3a567717561e0f..a54d383f60d99c 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -536,7 +536,6 @@ def parse_ns_headers(ns_headers): IPV4_RE = re.compile(r"\.\d+$", re.ASCII) def is_ip(text): """Return True if text is a valid IP address.""" - # This function is a replacement of regex `IPV4_RE` in previous versions. try: ip_address(text) return True From a5198eef73c5b9bd196c9d5e13a619cb6c971845 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 01:22:30 +0800 Subject: [PATCH 08/40] Update Lib/http/cookiejar.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/http/cookiejar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index a54d383f60d99c..ef54b572a4235c 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -538,9 +538,9 @@ def is_ip(text): """Return True if text is a valid IP address.""" try: ip_address(text) - return True except ValueError: return False + return True def is_HDN(text): """Return True if text is a host domain name.""" # XXX From baf62d1ef042901ba09b1a83ea8817951a78b5a9 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 01:22:51 +0800 Subject: [PATCH 09/40] Update Lib/http/cookiejar.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/http/cookiejar.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index ef54b572a4235c..878c6a50619134 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -599,9 +599,7 @@ def liberal_is_HDN(text): For accepting/blocking domains. """ - if is_ip(text): - return False - return True + return not is_ip(text) def user_domain_match(A, B): """For blocking/accepting domains. From 0783fc1bccb2c325c68dddf4d6eb6913e703cade Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 01:28:02 +0800 Subject: [PATCH 10/40] Update cookiejar.py --- Lib/http/cookiejar.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 878c6a50619134..126db0ce07c87b 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -29,15 +29,14 @@ 'FileCookieJar', 'LWPCookieJar', 'LoadError', 'MozillaCookieJar'] import os -import re import copy -import time import datetime +import re +import time import urllib.parse, urllib.request import threading as _threading import http.client # only for the default HTTP port from calendar import timegm -from ipaddress import ip_address debug = False # set to True to enable debugging via the logging module logger = None @@ -536,6 +535,7 @@ def parse_ns_headers(ns_headers): IPV4_RE = re.compile(r"\.\d+$", re.ASCII) def is_ip(text): """Return True if text is a valid IP address.""" + from ipaddress import ip_address try: ip_address(text) except ValueError: @@ -611,10 +611,8 @@ def user_domain_match(A, B): B = B.lower() if not (liberal_is_HDN(A) and liberal_is_HDN(B)): if A == B: - # equal IPv4 addresses return True return False - # A and B may be HDNs or a IPv6 addresses now initial_dot = B.startswith(".") if initial_dot and A.endswith(B): return True From dffb204a89bdf16ba24feea35cb0909329cd5750 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 01:32:44 +0800 Subject: [PATCH 11/40] add IPV6 regex --- Lib/http/cookiejar.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 126db0ce07c87b..27cff9b6a8bb7e 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -644,8 +644,9 @@ def eff_request_host(request): """ erhn = req_host = request_host(request) - if "." not in req_host and '[' not in req_host: - # detect '[' mainly for IPv6 addr like [::1] + IPV6_RE = re.compile(r"^\[.*\]$") + if "." not in req_host and not IPV6_RE.search(req_host): + # avoid adding .local at the end of a IPV6 address erhn = req_host + ".local" return req_host, erhn From 056b44de18a0a61c75239b474552c46fc577dc07 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 01:34:47 +0800 Subject: [PATCH 12/40] Update Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst b/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst index 2745d9426e7441..a6a2b5a3285362 100644 --- a/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst +++ b/Misc/NEWS.d/next/Library/2025-06-20-17-03-51.gh-issue-135768.DhUJWf.rst @@ -1 +1,2 @@ -:mod:`http.cookiejar` now supports IPv6 address in blocked_domains and allowed_domains of class DefaultCookiePolicy +:mod:`http.cookiejar`: fix allowed and blocked IPv6 domains +in :class:`~http.cookiejar.DefaultCookiePolicy`. From 37c193365c13953323fc768d0207cb6f49899755 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 01:41:35 +0800 Subject: [PATCH 13/40] add string method instead of regex to detect IPV6 --- Lib/http/cookiejar.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 27cff9b6a8bb7e..83cd734c9c3557 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -644,8 +644,8 @@ def eff_request_host(request): """ erhn = req_host = request_host(request) - IPV6_RE = re.compile(r"^\[.*\]$") - if "." not in req_host and not IPV6_RE.search(req_host): + is_IPV6 = req_host.startswith('[') and req_host.endswith(']') + if "." not in req_host and not is_IPV6: # avoid adding .local at the end of a IPV6 address erhn = req_host + ".local" return req_host, erhn From 779e4437bb9c5e6dcddd7cd065ad7f6bd9ca03b1 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 01:52:43 +0800 Subject: [PATCH 14/40] update is_ip --- Lib/http/cookiejar.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 83cd734c9c3557..e69cbaef83f4ba 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -532,11 +532,15 @@ def parse_ns_headers(ns_headers): return result -IPV4_RE = re.compile(r"\.\d+$", re.ASCII) -def is_ip(text): +IPV4_RE = re.compile(r"\.\d+$", re.ASCII) # kept for backwards compatibility +def is_ip(text: str): """Return True if text is a valid IP address.""" from ipaddress import ip_address try: + if text.startswith('['): + text = text[1:] + if text.endswith(']'): + text = text[:-1] ip_address(text) except ValueError: return False @@ -610,9 +614,11 @@ def user_domain_match(A, B): A = A.lower() B = B.lower() if not (liberal_is_HDN(A) and liberal_is_HDN(B)): + # equal IPV4 addresses if A == B: return True return False + # A and B may be HDNs or a IPv6 addresses now initial_dot = B.startswith(".") if initial_dot and A.endswith(B): return True From 1c00c395de0f565eefbc5615fcc5ee0fde292fa5 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 01:54:23 +0800 Subject: [PATCH 15/40] update comments --- Lib/http/cookiejar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index e69cbaef83f4ba..858c3b0be0132d 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -614,8 +614,8 @@ def user_domain_match(A, B): A = A.lower() B = B.lower() if not (liberal_is_HDN(A) and liberal_is_HDN(B)): - # equal IPV4 addresses if A == B: + # equal IPV4 addresses return True return False # A and B may be HDNs or a IPv6 addresses now From 4c12cec7fe680ef682cf5fefc9b05f8fa7b656e0 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 02:16:09 +0800 Subject: [PATCH 16/40] Update test_http_cookiejar.py --- Lib/test/test_http_cookiejar.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index 04cb440cd4ccf6..fa6c4b63012c83 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -1168,6 +1168,12 @@ def test_domain_block(self): c.add_cookie_header(req) self.assertFalse(req.has_header("Cookie")) + pol.set_blocked_domains(["[::1]"]) + req = urllib.request.Request("http://[::1]:8080") + res = FakeResponse(headers, "http://[::1]:8080") + c.extract_cookies(res, req) + self.assertEqual(len(c), 0) + def test_secure(self): for ns in True, False: for whitespace in " ", "": From 0cf11d77afd0cc40624f7ac87ae299b4d14a896a Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 08:34:25 +0800 Subject: [PATCH 17/40] Update Lib/http/cookiejar.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/http/cookiejar.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 858c3b0be0132d..c60aa3a73cd6e3 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -536,11 +536,8 @@ def parse_ns_headers(ns_headers): def is_ip(text: str): """Return True if text is a valid IP address.""" from ipaddress import ip_address + text = text.removeprefix('[').removesuffix(']') try: - if text.startswith('['): - text = text[1:] - if text.endswith(']'): - text = text[:-1] ip_address(text) except ValueError: return False From 88a6af110148e9a684e03825270b6f6e40686f3d Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 08:34:57 +0800 Subject: [PATCH 18/40] Update Lib/http/cookiejar.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/http/cookiejar.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index c60aa3a73cd6e3..61c75758d96886 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -615,7 +615,6 @@ def user_domain_match(A, B): # equal IPV4 addresses return True return False - # A and B may be HDNs or a IPv6 addresses now initial_dot = B.startswith(".") if initial_dot and A.endswith(B): return True From d8411c094aff37eb1fb754508406508d8751fd57 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 08:42:19 +0800 Subject: [PATCH 19/40] Update cookiejar.py --- Lib/http/cookiejar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 61c75758d96886..802629e3764939 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -612,7 +612,7 @@ def user_domain_match(A, B): B = B.lower() if not (liberal_is_HDN(A) and liberal_is_HDN(B)): if A == B: - # equal IPV4 addresses + # equal IP addresses return True return False initial_dot = B.startswith(".") From 7646042c793e528bfdda93c6c19c095170f77063 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 13:48:04 +0800 Subject: [PATCH 20/40] more test added --- Lib/test/test_http_cookiejar.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index fa6c4b63012c83..27004164995e39 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -860,6 +860,8 @@ def test_is_HDN(self): self.assertTrue(is_HDN("foo.bar.com")) self.assertTrue(is_HDN("1foo2.3bar4.5com")) self.assertFalse(is_HDN("192.168.1.1")) + self.assertFalse(is_HDN("[::1]")) + self.assertFalse(is_HDN("[2001:db8:85a3::8a2e:370:7334]")) self.assertFalse(is_HDN("")) self.assertFalse(is_HDN(".")) self.assertFalse(is_HDN(".foo.bar.com")) @@ -875,9 +877,14 @@ def test_reach(self): self.assertEqual(reach("."), ".") self.assertEqual(reach(""), "") self.assertEqual(reach("192.168.0.1"), "192.168.0.1") + self.assertEqual(reach("[::1]"), "[::1]") + self.assertEqual(reach("[2001:db8:85a3::8a2e:370:7334]"), "[2001:db8:85a3::8a2e:370:7334]") def test_domain_match(self): self.assertTrue(domain_match("192.168.1.1", "192.168.1.1")) + self.assertTrue(domain_match("[::1]", "[::1]")) + self.assertFalse(domain_match("[::1]", "::1")) + self.assertTrue(domain_match("[2001:db8:85a3::8a2e:370:7334]", "[2001:db8:85a3::8a2e:370:7334]")) self.assertFalse(domain_match("192.168.1.1", ".168.1.1")) self.assertTrue(domain_match("x.y.com", "x.Y.com")) self.assertTrue(domain_match("x.y.com", ".Y.com")) @@ -906,6 +913,9 @@ def test_domain_match(self): self.assertFalse(user_domain_match("x.y.com", "")) self.assertFalse(user_domain_match("x.y.com", ".")) self.assertTrue(user_domain_match("192.168.1.1", "192.168.1.1")) + self.assertTrue(user_domain_match("[::1]", "[::1]")) + self.assertFalse(user_domain_match("[::1]", "::1")) + self.assertTrue(domain_match("[2001:db8:85a3::8a2e:370:7334]", "[2001:db8:85a3::8a2e:370:7334]")) # not both HDNs, so must string-compare equal to match self.assertFalse(user_domain_match("192.168.1.1", ".168.1.1")) self.assertFalse(user_domain_match("192.168.1.1", ".")) @@ -1168,12 +1178,30 @@ def test_domain_block(self): c.add_cookie_header(req) self.assertFalse(req.has_header("Cookie")) + c.clear() + pol.set_blocked_domains(["[::1]"]) req = urllib.request.Request("http://[::1]:8080") res = FakeResponse(headers, "http://[::1]:8080") c.extract_cookies(res, req) self.assertEqual(len(c), 0) + pol.set_blocked_domains(["[2001:db8:85a3::8a2e:370:7334]"]) + req = urllib.request.Request("http://[2001:db8:85a3::8a2e:370:7334]:8080") + res = FakeResponse(headers, "http://[2001:db8:85a3::8a2e:370:7334]:8080") + c.extract_cookies(res, req) + self.assertEqual(len(c), 0) + + pol.set_blocked_domains([""]) + req = urllib.request.Request("http://[::1]:8080") + res = FakeResponse(headers, "http://[::1]:8080") + c.extract_cookies(res, req) + self.assertEqual(len(c), 1) + + c.clear() + + + def test_secure(self): for ns in True, False: for whitespace in " ", "": From 9eb52e3af81ae5b82181f60684a49ac579ae9581 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 13:48:32 +0800 Subject: [PATCH 21/40] Update test_http_cookiejar.py --- Lib/test/test_http_cookiejar.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index 27004164995e39..49a33b5b7ccdcc 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -1198,10 +1198,6 @@ def test_domain_block(self): c.extract_cookies(res, req) self.assertEqual(len(c), 1) - c.clear() - - - def test_secure(self): for ns in True, False: for whitespace in " ", "": From 09f5a61a0728f76b00354a952ae0ef1c6adbec64 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 13:57:44 +0800 Subject: [PATCH 22/40] Update test_http_cookiejar.py --- Lib/test/test_http_cookiejar.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index 49a33b5b7ccdcc..f82bbc5245452f 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -912,11 +912,11 @@ def test_domain_match(self): self.assertFalse(user_domain_match("x.y.com", ".m")) self.assertFalse(user_domain_match("x.y.com", "")) self.assertFalse(user_domain_match("x.y.com", ".")) + # not both HDNs, so must string-compare equal to match self.assertTrue(user_domain_match("192.168.1.1", "192.168.1.1")) self.assertTrue(user_domain_match("[::1]", "[::1]")) - self.assertFalse(user_domain_match("[::1]", "::1")) self.assertTrue(domain_match("[2001:db8:85a3::8a2e:370:7334]", "[2001:db8:85a3::8a2e:370:7334]")) - # not both HDNs, so must string-compare equal to match + self.assertFalse(user_domain_match("[::1]", "::1")) self.assertFalse(user_domain_match("192.168.1.1", ".168.1.1")) self.assertFalse(user_domain_match("192.168.1.1", ".")) # empty string is a special case From d440b543f23d6f597c0d1fa20ee2a6cf5695bc60 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 14:10:37 +0800 Subject: [PATCH 23/40] Update test_http_cookiejar.py --- Lib/test/test_http_cookiejar.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index f82bbc5245452f..a8d5b0d3b79197 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -16,7 +16,7 @@ CookieJar, DefaultCookiePolicy, LWPCookieJar, MozillaCookieJar, LoadError, lwp_cookie_str, DEFAULT_HTTP_PORT, escape_path, reach, is_HDN, domain_match, user_domain_match, request_path, - request_port, request_host) + request_port, request_host, is_ip) mswindows = (sys.platform == "win32") @@ -867,6 +867,18 @@ def test_is_HDN(self): self.assertFalse(is_HDN(".foo.bar.com")) self.assertFalse(is_HDN("..foo")) self.assertFalse(is_HDN("foo.")) + + def test_is_ip(self): + self.assertTrue(is_ip('[::1]')) + self.assertTrue(is_ip('::1')) + self.assertTrue(is_ip('[2001:db8:85a3::8a2e:370:7334]')) + self.assertTrue(is_ip('2001:db8:85a3::8a2e:370:7334')) + self.assertTrue(is_ip('192.168.0.1')) + self.assertFalse(is_ip('256.256.256.256')) + self.assertFalse(is_ip('[::2001:db8:85a3::]')) + self.assertFalse(is_ip('acme.com')) + self.assertFalse(is_ip('')) + def test_reach(self): self.assertEqual(reach("www.acme.com"), ".acme.com") @@ -915,7 +927,7 @@ def test_domain_match(self): # not both HDNs, so must string-compare equal to match self.assertTrue(user_domain_match("192.168.1.1", "192.168.1.1")) self.assertTrue(user_domain_match("[::1]", "[::1]")) - self.assertTrue(domain_match("[2001:db8:85a3::8a2e:370:7334]", "[2001:db8:85a3::8a2e:370:7334]")) + self.assertTrue(domain_match("2001:db8::", "2001:db8::")) self.assertFalse(user_domain_match("[::1]", "::1")) self.assertFalse(user_domain_match("192.168.1.1", ".168.1.1")) self.assertFalse(user_domain_match("192.168.1.1", ".")) From f9aa74f3147f8f220e91f9ce772fb4dffc975d44 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 14:12:47 +0800 Subject: [PATCH 24/40] Update test_http_cookiejar.py --- Lib/test/test_http_cookiejar.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index a8d5b0d3b79197..f9bdeb7efb0f73 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -879,9 +879,8 @@ def test_is_ip(self): self.assertFalse(is_ip('acme.com')) self.assertFalse(is_ip('')) - def test_reach(self): - self.assertEqual(reach("www.acme.com"), ".acme.com") + self.assertEqual(reach("www.acme.com"), ".acme.coEm") self.assertEqual(reach("acme.com"), "acme.com") self.assertEqual(reach("acme.local"), ".local") self.assertEqual(reach(".local"), ".local") From 7f65ca91cb289630251c69cddf66ec72b64cb282 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 14:14:01 +0800 Subject: [PATCH 25/40] Update test_http_cookiejar.py --- Lib/test/test_http_cookiejar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index f9bdeb7efb0f73..56832544d24e72 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -867,7 +867,7 @@ def test_is_HDN(self): self.assertFalse(is_HDN(".foo.bar.com")) self.assertFalse(is_HDN("..foo")) self.assertFalse(is_HDN("foo.")) - + def test_is_ip(self): self.assertTrue(is_ip('[::1]')) self.assertTrue(is_ip('::1')) From dd04e81d42c269a8cc1fc4dd80e4b97233aa15ae Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 14:30:36 +0800 Subject: [PATCH 26/40] Update test_http_cookiejar.py --- Lib/test/test_http_cookiejar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index 56832544d24e72..ec3d048f5ddc35 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -880,7 +880,7 @@ def test_is_ip(self): self.assertFalse(is_ip('')) def test_reach(self): - self.assertEqual(reach("www.acme.com"), ".acme.coEm") + self.assertEqual(reach("www.acme.com"), ".acme.com") self.assertEqual(reach("acme.com"), "acme.com") self.assertEqual(reach("acme.local"), ".local") self.assertEqual(reach(".local"), ".local") From e553fe8ad751ca56ee8fa1c33f9e2bf99378203d Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 19:04:00 +0800 Subject: [PATCH 27/40] Update Lib/http/cookiejar.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/http/cookiejar.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 802629e3764939..686b392c56c638 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -532,7 +532,9 @@ def parse_ns_headers(ns_headers): return result -IPV4_RE = re.compile(r"\.\d+$", re.ASCII) # kept for backwards compatibility +# only kept for backwards compatibilty. +IPV4_RE = re.compile(r"\.\d+$", re.ASCII) + def is_ip(text: str): """Return True if text is a valid IP address.""" from ipaddress import ip_address From af9d29e63e5d8bf6626df09c6eb4d460ad0f6edc Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 19:06:32 +0800 Subject: [PATCH 28/40] Update Lib/test/test_http_cookiejar.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Lib/test/test_http_cookiejar.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index ec3d048f5ddc35..a5d40d3bae907b 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -874,6 +874,7 @@ def test_is_ip(self): self.assertTrue(is_ip('[2001:db8:85a3::8a2e:370:7334]')) self.assertTrue(is_ip('2001:db8:85a3::8a2e:370:7334')) self.assertTrue(is_ip('192.168.0.1')) + self.assertFalse(is_ip('256.256.256.256')) self.assertFalse(is_ip('[::2001:db8:85a3::]')) self.assertFalse(is_ip('acme.com')) From 56ac5454f043505bd6d78e228025a248e6464056 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 19:24:35 +0800 Subject: [PATCH 29/40] Update cookiejar.py --- Lib/http/cookiejar.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 686b392c56c638..779735885de045 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -648,7 +648,15 @@ def eff_request_host(request): """ erhn = req_host = request_host(request) - is_IPV6 = req_host.startswith('[') and req_host.endswith(']') + if req_host.startswith('[') and req_host.endswith(']'): + from ipaddress import IPv6Address + try: + IPv6Address(req_host.removeprefix('[').removesuffix(']')) + is_IPV6 = True + except ValueError: + is_IPV6 = False + else: + is_IPV6 = False if "." not in req_host and not is_IPV6: # avoid adding .local at the end of a IPV6 address erhn = req_host + ".local" From f90b3542e6ecb70679b4d317afed68e97f65fa72 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 19:25:33 +0800 Subject: [PATCH 30/40] Update test_http_cookiejar.py --- Lib/test/test_http_cookiejar.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index a5d40d3bae907b..55bd8c64954ee3 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -874,7 +874,6 @@ def test_is_ip(self): self.assertTrue(is_ip('[2001:db8:85a3::8a2e:370:7334]')) self.assertTrue(is_ip('2001:db8:85a3::8a2e:370:7334')) self.assertTrue(is_ip('192.168.0.1')) - self.assertFalse(is_ip('256.256.256.256')) self.assertFalse(is_ip('[::2001:db8:85a3::]')) self.assertFalse(is_ip('acme.com')) @@ -890,13 +889,15 @@ def test_reach(self): self.assertEqual(reach(""), "") self.assertEqual(reach("192.168.0.1"), "192.168.0.1") self.assertEqual(reach("[::1]"), "[::1]") - self.assertEqual(reach("[2001:db8:85a3::8a2e:370:7334]"), "[2001:db8:85a3::8a2e:370:7334]") + self.assertEqual(reach("[2001:db8:85a3::8a2e:370:7334]"), + "[2001:db8:85a3::8a2e:370:7334]") def test_domain_match(self): self.assertTrue(domain_match("192.168.1.1", "192.168.1.1")) self.assertTrue(domain_match("[::1]", "[::1]")) self.assertFalse(domain_match("[::1]", "::1")) - self.assertTrue(domain_match("[2001:db8:85a3::8a2e:370:7334]", "[2001:db8:85a3::8a2e:370:7334]")) + self.assertTrue(domain_match("[2001:db8:85a3::8a2e:370:7334]", + "[2001:db8:85a3::8a2e:370:7334]")) self.assertFalse(domain_match("192.168.1.1", ".168.1.1")) self.assertTrue(domain_match("x.y.com", "x.Y.com")) self.assertTrue(domain_match("x.y.com", ".Y.com")) @@ -1190,7 +1191,11 @@ def test_domain_block(self): c.add_cookie_header(req) self.assertFalse(req.has_header("Cookie")) - c.clear() + def test_block_ip_domains(self): + pol = DefaultCookiePolicy( + rfc2965=True, blocked_domains=[]) + c = CookieJar(policy=pol) + headers = ["Set-Cookie: CUSTOMER=WILE_E_COYOTE; path=/"] pol.set_blocked_domains(["[::1]"]) req = urllib.request.Request("http://[::1]:8080") From 3d1cc918553d1fcbcb5776380ae8ee54181dcd1e Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 19:27:07 +0800 Subject: [PATCH 31/40] Update cookiejar.py --- Lib/http/cookiejar.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 779735885de045..a6bd4365b32257 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -656,7 +656,7 @@ def eff_request_host(request): except ValueError: is_IPV6 = False else: - is_IPV6 = False + is_IPV6 = False if "." not in req_host and not is_IPV6: # avoid adding .local at the end of a IPV6 address erhn = req_host + ".local" From 08797505b9a40b301ad560bf3b49e7e517aaf78f Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 19:34:35 +0800 Subject: [PATCH 32/40] update is_ip --- Lib/http/cookiejar.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index a6bd4365b32257..3612dddcab6f2f 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -537,12 +537,19 @@ def parse_ns_headers(ns_headers): def is_ip(text: str): """Return True if text is a valid IP address.""" - from ipaddress import ip_address - text = text.removeprefix('[').removesuffix(']') + from ipaddress import IPv4Address, IPv6Address + # check for IPv4 address try: - ip_address(text) + IPv4Address(text) except ValueError: - return False + # check for IPv6 address in [] + if text.startswith('[') and text.endswith(']'): + try: + IPv6Address(text.removeprefix('[').removesuffix(']')) + except ValueError: + return False + else: + return False # not a IPv6 address in [] return True def is_HDN(text): """Return True if text is a host domain name.""" From 7731e7fe0c8c2837108b16b727c60d176d1eca66 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 19:35:50 +0800 Subject: [PATCH 33/40] delete test for ::1 --- Lib/test/test_http_cookiejar.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index 55bd8c64954ee3..a5e51407f71391 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -870,7 +870,6 @@ def test_is_HDN(self): def test_is_ip(self): self.assertTrue(is_ip('[::1]')) - self.assertTrue(is_ip('::1')) self.assertTrue(is_ip('[2001:db8:85a3::8a2e:370:7334]')) self.assertTrue(is_ip('2001:db8:85a3::8a2e:370:7334')) self.assertTrue(is_ip('192.168.0.1')) From 9c4a91fe39006b64c645d545bf59777f338ce7b4 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 19:39:55 +0800 Subject: [PATCH 34/40] Update test_http_cookiejar.py --- Lib/test/test_http_cookiejar.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index a5e51407f71391..a4878679a0c09a 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -873,6 +873,7 @@ def test_is_ip(self): self.assertTrue(is_ip('[2001:db8:85a3::8a2e:370:7334]')) self.assertTrue(is_ip('2001:db8:85a3::8a2e:370:7334')) self.assertTrue(is_ip('192.168.0.1')) + self.assertFalse(is_ip('256.256.256.256')) self.assertFalse(is_ip('[::2001:db8:85a3::]')) self.assertFalse(is_ip('acme.com')) From 1b0f228486d7d5a7369bc56e1241061771f99e5a Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 19:48:30 +0800 Subject: [PATCH 35/40] Update test_http_cookiejar.py --- Lib/test/test_http_cookiejar.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index a4878679a0c09a..43dda5a1781378 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -871,7 +871,6 @@ def test_is_HDN(self): def test_is_ip(self): self.assertTrue(is_ip('[::1]')) self.assertTrue(is_ip('[2001:db8:85a3::8a2e:370:7334]')) - self.assertTrue(is_ip('2001:db8:85a3::8a2e:370:7334')) self.assertTrue(is_ip('192.168.0.1')) self.assertFalse(is_ip('256.256.256.256')) From 8f2c9c3fc1cf201f8c83083477f843561a8f96f7 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 21:46:35 +0800 Subject: [PATCH 36/40] Change is_ip to is_ip_like --- Lib/http/cookiejar.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 3612dddcab6f2f..1c2cacc25bbb62 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -535,8 +535,8 @@ def parse_ns_headers(ns_headers): # only kept for backwards compatibilty. IPV4_RE = re.compile(r"\.\d+$", re.ASCII) -def is_ip(text: str): - """Return True if text is a valid IP address.""" +def is_ip_like(text: str): + """Return True if text is a valid hostname in the form of IP address.""" from ipaddress import IPv4Address, IPv6Address # check for IPv4 address try: From e01652e4522aac571a5b3ef714603f87521aab01 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 21:48:04 +0800 Subject: [PATCH 37/40] Update test script for is_ip_like --- Lib/test/test_http_cookiejar.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index 43dda5a1781378..907ef68200da3f 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -16,7 +16,7 @@ CookieJar, DefaultCookiePolicy, LWPCookieJar, MozillaCookieJar, LoadError, lwp_cookie_str, DEFAULT_HTTP_PORT, escape_path, reach, is_HDN, domain_match, user_domain_match, request_path, - request_port, request_host, is_ip) + request_port, request_host, is_ip_like) mswindows = (sys.platform == "win32") @@ -868,15 +868,15 @@ def test_is_HDN(self): self.assertFalse(is_HDN("..foo")) self.assertFalse(is_HDN("foo.")) - def test_is_ip(self): - self.assertTrue(is_ip('[::1]')) - self.assertTrue(is_ip('[2001:db8:85a3::8a2e:370:7334]')) - self.assertTrue(is_ip('192.168.0.1')) + def test_is_ip_like(self): + self.assertTrue(is_ip_like('[::1]')) + self.assertTrue(is_ip_like('[2001:db8:85a3::8a2e:370:7334]')) + self.assertTrue(is_ip_like('192.168.0.1')) - self.assertFalse(is_ip('256.256.256.256')) - self.assertFalse(is_ip('[::2001:db8:85a3::]')) - self.assertFalse(is_ip('acme.com')) - self.assertFalse(is_ip('')) + self.assertFalse(is_ip_like('256.256.256.256')) + self.assertFalse(is_ip_like('[::2001:db8:85a3::]')) + self.assertFalse(is_ip_like('acme.com')) + self.assertFalse(is_ip_like('')) def test_reach(self): self.assertEqual(reach("www.acme.com"), ".acme.com") From 3d59ed9a33026487932d8999accfa26419bd29ef Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sat, 21 Jun 2025 22:18:43 +0800 Subject: [PATCH 38/40] Update cookiejar.py --- Lib/http/cookiejar.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index 1c2cacc25bbb62..aefc4d7886633d 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -556,7 +556,7 @@ def is_HDN(text): # XXX # This may well be wrong. Which RFC is HDN defined in, if any (for # the purposes of RFC 2965)? - if is_ip(text): + if is_ip_like(text): return False if text == "": return False @@ -609,7 +609,7 @@ def liberal_is_HDN(text): For accepting/blocking domains. """ - return not is_ip(text) + return not is_ip_like(text) def user_domain_match(A, B): """For blocking/accepting domains. @@ -659,12 +659,12 @@ def eff_request_host(request): from ipaddress import IPv6Address try: IPv6Address(req_host.removeprefix('[').removesuffix(']')) - is_IPV6 = True + is_ipV6 = True except ValueError: - is_IPV6 = False + is_ipV6 = False else: - is_IPV6 = False - if "." not in req_host and not is_IPV6: + is_ipV6 = False + if "." not in req_host and not is_ipV6: # avoid adding .local at the end of a IPV6 address erhn = req_host + ".local" return req_host, erhn From befc9e26fc7067ed7c299f7d6e34e4ce50aff7c7 Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sun, 22 Jun 2025 16:48:03 +0800 Subject: [PATCH 39/40] Update test_http_cookiejar.py --- Lib/test/test_http_cookiejar.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Lib/test/test_http_cookiejar.py b/Lib/test/test_http_cookiejar.py index 907ef68200da3f..f40607ee4ce2e2 100644 --- a/Lib/test/test_http_cookiejar.py +++ b/Lib/test/test_http_cookiejar.py @@ -16,7 +16,7 @@ CookieJar, DefaultCookiePolicy, LWPCookieJar, MozillaCookieJar, LoadError, lwp_cookie_str, DEFAULT_HTTP_PORT, escape_path, reach, is_HDN, domain_match, user_domain_match, request_path, - request_port, request_host, is_ip_like) + request_port, request_host, is_ip_like_hostname) mswindows = (sys.platform == "win32") @@ -868,15 +868,15 @@ def test_is_HDN(self): self.assertFalse(is_HDN("..foo")) self.assertFalse(is_HDN("foo.")) - def test_is_ip_like(self): - self.assertTrue(is_ip_like('[::1]')) - self.assertTrue(is_ip_like('[2001:db8:85a3::8a2e:370:7334]')) - self.assertTrue(is_ip_like('192.168.0.1')) + def test_is_ip_like_hostname(self): + self.assertTrue(is_ip_like_hostname('[::1]')) + self.assertTrue(is_ip_like_hostname('[2001:db8:85a3::8a2e:370:7334]')) + self.assertTrue(is_ip_like_hostname('192.168.0.1')) - self.assertFalse(is_ip_like('256.256.256.256')) - self.assertFalse(is_ip_like('[::2001:db8:85a3::]')) - self.assertFalse(is_ip_like('acme.com')) - self.assertFalse(is_ip_like('')) + self.assertFalse(is_ip_like_hostname('256.256.256.256')) + self.assertFalse(is_ip_like_hostname('[::2001:db8:85a3::]')) + self.assertFalse(is_ip_like_hostname('acme.com')) + self.assertFalse(is_ip_like_hostname('')) def test_reach(self): self.assertEqual(reach("www.acme.com"), ".acme.com") From 57fca8b77a2cf4f75147cfd5e91ffc0c10d46aee Mon Sep 17 00:00:00 2001 From: Weilin Du <108666168+LamentXU123@users.noreply.github.com> Date: Sun, 22 Jun 2025 16:48:39 +0800 Subject: [PATCH 40/40] Update cookiejar.py --- Lib/http/cookiejar.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Lib/http/cookiejar.py b/Lib/http/cookiejar.py index aefc4d7886633d..bb77cbbb5d5865 100644 --- a/Lib/http/cookiejar.py +++ b/Lib/http/cookiejar.py @@ -535,7 +535,7 @@ def parse_ns_headers(ns_headers): # only kept for backwards compatibilty. IPV4_RE = re.compile(r"\.\d+$", re.ASCII) -def is_ip_like(text: str): +def is_ip_like_hostname(text: str): """Return True if text is a valid hostname in the form of IP address.""" from ipaddress import IPv4Address, IPv6Address # check for IPv4 address @@ -556,7 +556,7 @@ def is_HDN(text): # XXX # This may well be wrong. Which RFC is HDN defined in, if any (for # the purposes of RFC 2965)? - if is_ip_like(text): + if is_ip_like_hostname(text): return False if text == "": return False @@ -609,7 +609,7 @@ def liberal_is_HDN(text): For accepting/blocking domains. """ - return not is_ip_like(text) + return not is_ip_like_hostname(text) def user_domain_match(A, B): """For blocking/accepting domains.