From c234647acdefee075538dca76a3379bebbc46100 Mon Sep 17 00:00:00 2001 From: tong_1001 Date: Mon, 31 May 2021 10:46:13 +0800 Subject: [PATCH] fix urllib.request.proxy_bypass_environment() --- ...llib.request.proxy_bypass_environmen.patch | 137 ++++++++++++++++++ python3.spec | 10 +- 2 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 backport-bpo-39057-Fix-urllib.request.proxy_bypass_environmen.patch diff --git a/backport-bpo-39057-Fix-urllib.request.proxy_bypass_environmen.patch b/backport-bpo-39057-Fix-urllib.request.proxy_bypass_environmen.patch new file mode 100644 index 0000000..3132542 --- /dev/null +++ b/backport-bpo-39057-Fix-urllib.request.proxy_bypass_environmen.patch @@ -0,0 +1,137 @@ +From fc84d501b9d77701cbdd485de45e200bf027c0e9 Mon Sep 17 00:00:00 2001 +From: "Miss Islington (bot)" + <31488909+miss-islington@users.noreply.github.com> +Date: Sun, 5 Jan 2020 04:32:00 -0800 +Subject: [PATCH] bpo-39057: Fix urllib.request.proxy_bypass_environment(). + (GH-17619) + +Ignore leading dots and no longer ignore a trailing newline. +(cherry picked from commit 6a265f0d0c0a4b3b8fecf4275d49187a384167f4) + +Co-authored-by: Serhiy Storchaka +--- + Lib/test/test_urllib.py | 22 ++++++++++++++++++++ + Lib/urllib/parse.py | 4 ++-- + Lib/urllib/request.py | 24 ++++++++++++---------- + .../2019-12-15-21-47-54.bpo-39057.FOxn-w.rst | 2 ++ + 4 files changed, 39 insertions(+), 13 deletions(-) + create mode 100644 Misc/NEWS.d/next/Library/2019-12-15-21-47-54.bpo-39057.FOxn-w.rst + +diff --git a/Lib/test/test_urllib.py b/Lib/test/test_urllib.py +index 801f0fd..e9c656c 100644 +--- a/Lib/test/test_urllib.py ++++ b/Lib/test/test_urllib.py +@@ -261,14 +261,36 @@ def test_proxy_bypass_environment_host_match(self): + self.assertTrue(bypass('localhost')) + self.assertTrue(bypass('LocalHost')) # MixedCase + self.assertTrue(bypass('LOCALHOST')) # UPPERCASE ++ self.assertTrue(bypass('.localhost')) + self.assertTrue(bypass('newdomain.com:1234')) ++ self.assertTrue(bypass('.newdomain.com:1234')) + self.assertTrue(bypass('foo.d.o.t')) # issue 29142 ++ self.assertTrue(bypass('d.o.t')) + self.assertTrue(bypass('anotherdomain.com:8888')) ++ self.assertTrue(bypass('.anotherdomain.com:8888')) + self.assertTrue(bypass('www.newdomain.com:1234')) + self.assertFalse(bypass('prelocalhost')) + self.assertFalse(bypass('newdomain.com')) # no port + self.assertFalse(bypass('newdomain.com:1235')) # wrong port + ++ def test_proxy_bypass_environment_always_match(self): ++ bypass = urllib.request.proxy_bypass_environment ++ self.env.set('NO_PROXY', '*') ++ self.assertTrue(bypass('newdomain.com')) ++ self.assertTrue(bypass('newdomain.com:1234')) ++ self.env.set('NO_PROXY', '*, anotherdomain.com') ++ self.assertTrue(bypass('anotherdomain.com')) ++ self.assertFalse(bypass('newdomain.com')) ++ self.assertFalse(bypass('newdomain.com:1234')) ++ ++ def test_proxy_bypass_environment_newline(self): ++ bypass = urllib.request.proxy_bypass_environment ++ self.env.set('NO_PROXY', ++ 'localhost, anotherdomain.com, newdomain.com:1234') ++ self.assertFalse(bypass('localhost\n')) ++ self.assertFalse(bypass('anotherdomain.com:8888\n')) ++ self.assertFalse(bypass('newdomain.com:1234\n')) ++ + + class ProxyTests_withOrderedEnv(unittest.TestCase): + +diff --git a/Lib/urllib/parse.py b/Lib/urllib/parse.py +index d497925..0b39b6e 100644 +--- a/Lib/urllib/parse.py ++++ b/Lib/urllib/parse.py +@@ -1054,9 +1054,9 @@ def _splitport(host): + """splitport('host:port') --> 'host', 'port'.""" + global _portprog + if _portprog is None: +- _portprog = re.compile('(.*):([0-9]*)$', re.DOTALL) ++ _portprog = re.compile('(.*):([0-9]*)', re.DOTALL) + +- match = _portprog.match(host) ++ match = _portprog.fullmatch(host) + if match: + host, port = match.groups() + if port: +diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py +index 51c4759..6f6577b 100644 +--- a/Lib/urllib/request.py ++++ b/Lib/urllib/request.py +@@ -2500,24 +2500,26 @@ def proxy_bypass_environment(host, proxies=None): + try: + no_proxy = proxies['no'] + except KeyError: +- return 0 ++ return False + # '*' is special case for always bypass + if no_proxy == '*': +- return 1 ++ return True ++ host = host.lower() + # strip port off host + hostonly, port = _splitport(host) + # check if the host ends with any of the DNS suffixes +- no_proxy_list = [proxy.strip() for proxy in no_proxy.split(',')] +- for name in no_proxy_list: ++ for name in no_proxy.split(','): ++ name = name.strip() + if name: + name = name.lstrip('.') # ignore leading dots +- name = re.escape(name) +- pattern = r'(.+\.)?%s$' % name +- if (re.match(pattern, hostonly, re.I) +- or re.match(pattern, host, re.I)): +- return 1 ++ name = name.lower() ++ if hostonly == name or host == name: ++ return True ++ name = '.' + name ++ if hostonly.endswith(name) or host.endswith(name): ++ return True + # otherwise, don't bypass +- return 0 ++ return False + + + # This code tests an OSX specific data structure but is testable on all +@@ -2643,7 +2645,7 @@ def getproxies_registry(): + for p in proxyServer.split(';'): + protocol, address = p.split('=', 1) + # See if address has a type:// prefix +- if not re.match('^([^/:]+)://', address): ++ if not re.match('(?:[^/:]+)://', address): + address = '%s://%s' % (protocol, address) + proxies[protocol] = address + else: +diff --git a/Misc/NEWS.d/next/Library/2019-12-15-21-47-54.bpo-39057.FOxn-w.rst b/Misc/NEWS.d/next/Library/2019-12-15-21-47-54.bpo-39057.FOxn-w.rst +new file mode 100644 +index 0000000..24a1744 +--- /dev/null ++++ b/Misc/NEWS.d/next/Library/2019-12-15-21-47-54.bpo-39057.FOxn-w.rst +@@ -0,0 +1,2 @@ ++:func:`urllib.request.proxy_bypass_environment` now ignores leading dots and ++no longer ignores a trailing newline. +-- +1.8.3.1 + diff --git a/python3.spec b/python3.spec index 8a38f2a..e44c739 100644 --- a/python3.spec +++ b/python3.spec @@ -3,7 +3,7 @@ Summary: Interpreter of the Python3 programming language URL: https://www.python.org/ Version: 3.8.5 -Release: 10 +Release: 11 License: Python %global branchversion 3.8 @@ -100,6 +100,7 @@ Patch254: CVE-2021-3177.patch Patch255: backport-CVE-2021-23336.patch Patch256: backport-Remove-thread-objects-which-finished-process-its-request.patch Patch257: backport-Fix-reference-leak-when-Thread-is-never-joined.patch +Patch6000: backport-bpo-39057-Fix-urllib.request.proxy_bypass_environmen.patch Provides: python%{branchversion} = %{version}-%{release} Provides: python(abi) = %{branchversion} @@ -197,6 +198,7 @@ rm Lib/ensurepip/_bundled/*.whl %patch255 -p1 %patch256 -p1 %patch257 -p1 +%patch6000 -p1 rm configure pyconfig.h.in @@ -804,6 +806,12 @@ export BEP_GTDLIST="$BEP_GTDLIST_TMP" %{_mandir}/*/* %changelog +* Mon May 31 2021 shixuantong - 3.8.5-11 +- Type:bugfix +- ID:NA +- SUG:NA +- DESC:fix urllib.request.proxy_bypass_environment() + * Sun May 23 2021 shixuantong - 3.8.5-10 - Type:bugfix - ID:NA -- Gitee