From 8ca4f5a6ae07db7cc50f7d5344dad82e70bf9a01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8B=82=E7=94=B7=E9=A3=8E?= Date: Sat, 22 May 2021 00:10:25 +0800 Subject: [PATCH 1/9] bpo-42627: Fix wrong parsing of Windows registry proxy settings https://bugs.python.org/issue42627 --- Lib/urllib/request.py | 56 ++++++++++++++++++++++++++++++------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 8363905f20fa35..c75e4909749aa8 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -2674,22 +2674,46 @@ def getproxies_registry(): # Returned as Unicode but problems if not converted to ASCII proxyServer = str(winreg.QueryValueEx(internetSettings, 'ProxyServer')[0]) - if '=' in proxyServer: - # Per-protocol settings - for p in proxyServer.split(';'): - protocol, address = p.split('=', 1) - # See if address has a type:// prefix - if not re.match('(?:[^/:]+)://', address): - address = '%s://%s' % (protocol, address) - proxies[protocol] = address - else: - # Use one setting for all protocols - if proxyServer[:5] == 'http:': - proxies['http'] = proxyServer - else: - proxies['http'] = 'http://%s' % proxyServer - proxies['https'] = 'https://%s' % proxyServer - proxies['ftp'] = 'ftp://%s' % proxyServer + + if '=' not in proxyServer and ';' not in proxyServer: + # Use one setting for all protocols. + # Chromium treats this as a separate category, and some software + # uses the ALL_PROXY environment variable for a similar purpose, + # so arguably this should be 'all={}'.format(proxyServer), + # but this is more backward compatible. + proxyServer = 'http={0};https={0};ftp={0}'.format(proxyServer) + + for p in proxyServer.split(';'): + # Chromium and WinInet are inconsistent in their treatment of + # invalid strings with the wrong number of = characters. It + # probably doesn't matter. + protocol, addresses = p.split('=', 1) + protocol = protocol.strip() + + # Chromium supports more than one proxy per protocol. I don't + # know how many clients support the same, but handling it is at + # least no worse than leaving the commas uninterpreted. + for address in addresses.split(','): + if protocol in {'http', 'https', 'ftp', 'socks'}: + # See if address has a type:// prefix + if not re.match('(?:[^/:]+)://', address): + if protocol == 'socks': + # Chromium notes that the correct protocol here + # is SOCKS4, but "socks://" is interpreted + # as SOCKS5 elsewhere. I don't know whether + # prepending socks4:// here would break code. + address = 'socks://' + address + else: + address = 'http://' + address + + # A string like 'http=foo;http=bar' will produce a + # comma-separated list, while previously 'bar' would + # override 'foo'. That could potentially break something. + if protocol not in proxies: + proxies[protocol] = address + else: + proxies[protocol] += ',' + address + internetSettings.Close() except (OSError, ValueError, TypeError): # Either registry key not found etc, or the value in an From 5b7d88ca8185f5213670649714f1d9101fc295df Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sat, 22 May 2021 07:58:59 +0000 Subject: [PATCH 2/9] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst diff --git a/Misc/NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst b/Misc/NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst new file mode 100644 index 00000000000000..9cd08b71537c35 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst @@ -0,0 +1 @@ +Fix wrong parsing of Windows registry proxy settings \ No newline at end of file From dc2e781052f02554858036290161a2a0288a3603 Mon Sep 17 00:00:00 2001 From: CrazyBoyFeng Date: Fri, 28 May 2021 14:17:00 +0800 Subject: [PATCH 3/9] Remove support for multi proxy per one protocol Keep backward compatibility --- Lib/urllib/request.py | 44 +++++++++---------------------------------- 1 file changed, 9 insertions(+), 35 deletions(-) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index c75e4909749aa8..4d4a4fb6aeb8df 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -2674,46 +2674,20 @@ def getproxies_registry(): # Returned as Unicode but problems if not converted to ASCII proxyServer = str(winreg.QueryValueEx(internetSettings, 'ProxyServer')[0]) - if '=' not in proxyServer and ';' not in proxyServer: # Use one setting for all protocols. - # Chromium treats this as a separate category, and some software - # uses the ALL_PROXY environment variable for a similar purpose, - # so arguably this should be 'all={}'.format(proxyServer), - # but this is more backward compatible. proxyServer = 'http={0};https={0};ftp={0}'.format(proxyServer) - for p in proxyServer.split(';'): - # Chromium and WinInet are inconsistent in their treatment of - # invalid strings with the wrong number of = characters. It - # probably doesn't matter. - protocol, addresses = p.split('=', 1) + protocol, address = p.split('=', 1) protocol = protocol.strip() - - # Chromium supports more than one proxy per protocol. I don't - # know how many clients support the same, but handling it is at - # least no worse than leaving the commas uninterpreted. - for address in addresses.split(','): - if protocol in {'http', 'https', 'ftp', 'socks'}: - # See if address has a type:// prefix - if not re.match('(?:[^/:]+)://', address): - if protocol == 'socks': - # Chromium notes that the correct protocol here - # is SOCKS4, but "socks://" is interpreted - # as SOCKS5 elsewhere. I don't know whether - # prepending socks4:// here would break code. - address = 'socks://' + address - else: - address = 'http://' + address - - # A string like 'http=foo;http=bar' will produce a - # comma-separated list, while previously 'bar' would - # override 'foo'. That could potentially break something. - if protocol not in proxies: - proxies[protocol] = address - else: - proxies[protocol] += ',' + address - + if '://' not in address: + # Add type:// prefix to address without specifying type + if protocol in ('http', 'https', 'ftp'): + # The default proxy type of Windows is HTTP + address = 'http://' + address + elif protocol == 'socks': + address = 'socks://' + address + proxies[protocol] = address internetSettings.Close() except (OSError, ValueError, TypeError): # Either registry key not found etc, or the value in an From 33f07b22f621114eefc184aae209b39c06f804b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8B=82=E7=94=B7=E9=A3=8E?= Date: Tue, 8 Jun 2021 14:02:02 +0800 Subject: [PATCH 4/9] Use SOCKS proxy for HTTP(S) protocols To compat with customary formats from software such as `requests[socks]` --- Lib/urllib/request.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 4d4a4fb6aeb8df..422d2eaf8d629e 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -2679,8 +2679,8 @@ def getproxies_registry(): proxyServer = 'http={0};https={0};ftp={0}'.format(proxyServer) for p in proxyServer.split(';'): protocol, address = p.split('=', 1) - protocol = protocol.strip() - if '://' not in address: + # See if address has a type:// prefix + if not re.match('(?:[^/:]+)://', address): # Add type:// prefix to address without specifying type if protocol in ('http', 'https', 'ftp'): # The default proxy type of Windows is HTTP @@ -2688,6 +2688,12 @@ def getproxies_registry(): elif protocol == 'socks': address = 'socks://' + address proxies[protocol] = address + # Use SOCKS proxy for HTTP(S) protocols + if proxies['socks']: + # The default SOCKS proxy type of Windows is SOCKS4 + address = re.sub(r'^socks://', 'socks4://', proxies['socks']) + proxies['http'] = proxies['http'] or address + proxies['https'] = proxies['https'] or address internetSettings.Close() except (OSError, ValueError, TypeError): # Either registry key not found etc, or the value in an From 37fd713f37d88448623f01018e73dfa373e112dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8B=82=E7=94=B7=E9=A3=8E?= Date: Tue, 8 Jun 2021 18:45:41 +0800 Subject: [PATCH 5/9] Fix `ValueError` --- Lib/urllib/request.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 422d2eaf8d629e..69784415452263 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -2689,7 +2689,7 @@ def getproxies_registry(): address = 'socks://' + address proxies[protocol] = address # Use SOCKS proxy for HTTP(S) protocols - if proxies['socks']: + if proxies.get('socks'): # The default SOCKS proxy type of Windows is SOCKS4 address = re.sub(r'^socks://', 'socks4://', proxies['socks']) proxies['http'] = proxies['http'] or address From e74efd8ac52db023cb4d0659157f0b40b1174af6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8B=82=E7=94=B7=E9=A3=8E?= Date: Wed, 8 Sep 2021 13:49:08 +0800 Subject: [PATCH 6/9] Fix `ValueError` https://github.com/python/cpython/pull/26307#discussion_r703885833 --- Lib/urllib/request.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/urllib/request.py b/Lib/urllib/request.py index 03a5d562399229..fe85122ae66a75 100644 --- a/Lib/urllib/request.py +++ b/Lib/urllib/request.py @@ -2692,8 +2692,8 @@ def getproxies_registry(): if proxies.get('socks'): # The default SOCKS proxy type of Windows is SOCKS4 address = re.sub(r'^socks://', 'socks4://', proxies['socks']) - proxies['http'] = proxies['http'] or address - proxies['https'] = proxies['https'] or address + proxies['http'] = proxies.get('http') or address + proxies['https'] = proxies.get('https') or address internetSettings.Close() except (OSError, ValueError, TypeError): # Either registry key not found etc, or the value in an From e0367b0123b36e3856ad7872ac13078bd087326a Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Tue, 10 May 2022 23:50:37 +0100 Subject: [PATCH 7/9] Update Misc/NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst --- .../next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst b/Misc/NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst index 9cd08b71537c35..1986ba785a9724 100644 --- a/Misc/NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst +++ b/Misc/NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst @@ -1 +1 @@ -Fix wrong parsing of Windows registry proxy settings \ No newline at end of file +Fix incorrect parsing of Windows registry proxy settings \ No newline at end of file From 4faf5291e39c5389d56672a5a202559411f21dc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8B=82=E7=94=B7=E9=A3=8E?= Date: Wed, 11 May 2022 13:57:41 +0800 Subject: [PATCH 8/9] Update 2021-05-22-07-58-59.bpo-42627.EejtD0.rst Add a `CRLF` at the end of file --- .../next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst b/Misc/NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst index 1986ba785a9724..c3b48c540d6b4a 100644 --- a/Misc/NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst +++ b/Misc/NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst @@ -1 +1 @@ -Fix incorrect parsing of Windows registry proxy settings \ No newline at end of file +Fix incorrect parsing of Windows registry proxy settings From 783deb91ff0032ecb18320615c9babeacf076ceb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8B=82=E7=94=B7=E9=A3=8E?= Date: Wed, 11 May 2022 14:07:17 +0800 Subject: [PATCH 9/9] Update 2021-05-22-07-58-59.bpo-42627.EejtD0.rst Use LF for line separator --- .../next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst b/Misc/NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst index c3b48c540d6b4a..f165b9ced05d90 100644 --- a/Misc/NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst +++ b/Misc/NEWS.d/next/Library/2021-05-22-07-58-59.bpo-42627.EejtD0.rst @@ -1 +1 @@ -Fix incorrect parsing of Windows registry proxy settings +Fix incorrect parsing of Windows registry proxy settings