You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
urllib.request resolves the host before checking it against the system's proxy bypass list [Security: LOW, minor info leak] #115197
When system proxy bypass list is set, the urllib.request library on macOS and Windows resolves the hostname to an IP address and the IP address to a hostname (on Windows) before checking it against the system proxy bypass list (see here and here).
This causes DNS leak and HTTP requests to hang while waiting for DNS timeout in some air-gaped environments. This behavior also differs from other system applications (tested on macOS Sonoma with Safari and Windows Server 2022 with the Edge browser).
Test process on macOS and Windows:
Creating an A record from <my-test-domain>.net to <my-test-ip>.
macOS with Safari:
In the system network setting:
"Web proxy (HTTP)" is set to 172.16.0.1:8000
"Secure web proxy (HTTPS)" is set to 172.16.0.1:8000
"Bypass proxy settings" is set to <my-test-ip>
In Safari:
visiting http://<my-test-ip>: does not use the proxy
visiting http://<my-test-domain>.net: uses the proxy
Windows Server 2022 with Edge browser:
in system network setting:
"HTTP proxy" is set to 172.16.0.1:8000
"Do not use proxy server" is set to <my-test-ip>
In Edge browser:
visiting http://<my-test-ip>: does not use the proxy
visiting http://<my-test-domain>.net: uses the proxy
urllib.request on Windows also resolves the IP address back to FQDN before check, here's a test for that:
Windows Server 2022 with Edge browser:
Update the Host file so the IP address can be resolved back to FQDN (socket.getfqdn("<my-test-ip>") == "<my-test-domain>.net").
In system network setting:
"HTTP proxy" is set to 172.16.0.1:8000
"Do not use proxy server" is set to .net
In Edge browser:
visiting http://<my-test-ip>: uses the proxy
visiting http://<my-test-domain>.net: does not use the proxy
Yes, please add a pull request. If it's a relatively simple fix, then this will be less controversial.
For what it's worth, we don't usually consider web browsers to be the canonical behaviour for our APIs. If another low-level library similar to urllib is doing it, that is helpful guidance (e.g. urllib3).
Yes, please add a pull request. If it's a relatively simple fix, then this will be less controversial.
For what it's worth, we don't usually consider web browsers to be the canonical behaviour for our APIs. If another low-level library similar to urllib is doing it, that is helpful guidance (e.g. urllib3).
@zooba Yes, the solution is simply not to resolve the host before checking it against the system proxy bypass list. I don't recall any HTTP tools, for example, curl, has this behavior. I did some research and found that the requests library has already created a patch inside requests to stop resolving for Windows, but it is still unfixed for macOS. Fixing this behavior in Python could definitely help.
Gitlab blog about no_proxy: https://about.gitlab.com/blog/2021/01/27/we-need-to-talk-no-proxy/, the links above are based on that blog post (but point to main branch except for wget which I didn't change). The blog points out some other inconstencies between the implementations as well.
Given the references above most major tools seem to not resolve names, which t.b.h. is not something I had expected.
@ronaldoussoren Thank you for the investigation. This aligns more or less with what I thought. Even Python itself does not resolve the hostname when handling the no_proxy environment variable, just like wget, go, and curl.
"""Test if proxies should not be used for a particular host.
Checks the proxy dict for the value of no_proxy, which should
be a list of comma separated DNS suffixes, or '*' for all hosts.
"""
ifproxiesisNone:
proxies=getproxies_environment()
# don't bypass, if no_proxy isn't specified
try:
no_proxy=proxies['no']
exceptKeyError:
returnFalse
# '*' is special case for always bypass
ifno_proxy=='*':
returnTrue
host=host.lower()
# strip port off host
hostonly, port=_splitport(host)
# check if the host ends with any of the DNS suffixes
fornameinno_proxy.split(','):
name=name.strip()
ifname:
name=name.lstrip('.') # ignore leading dots
name=name.lower()
ifhostonly==nameorhost==name:
returnTrue
name='.'+name
ifhostonly.endswith(name) orhost.endswith(name):
returnTrue
# otherwise, don't bypass
returnFalse
The affected code here is slightly different, as Python handles the proxy bypass list sourced from the operating system. To my knowledge, Ruby, curl, and go do not even attempt to read the proxy bypass list from the operating system. And there is essentially no documentation related to the operating system's proxy bypass list on Windows and macOS. This is why I ran experiments with the operating system's default browser and attempted to test the "canonical" interpretation of the system proxy bypass list. From the results, it appears that macOS and Windows do not resolve the hostname.
Names are not what we get from a resolution process, so that would argue that at least on Windows, we should check the host name without resolving it.
Whether we can safely make that change in terms of backwards compatibility is a separate question. My instinct is that we should check both for now and plan to stop checking addresses after a deprecation period (and probably add some way for users to stop checking them now). But I'm not sure if that's best. @gpshead, I expect you know more about this area (proxies) than I do - any thoughts?
I agree with the majority of tools: A proxy bypass lists should always be a simple string match without name resolution. In proxy environments the point of the proxy is that it is entirely up to the proxy to do all resolving and remote connecting (or MITMing, denying, rejecting, etc). So we should not be doing any name resolution (or reverse name lookups of an address) in order to determine if a proxy should be used.
@zooba I can update the code to print a deprecation warning when the hostname does not match directly, but the resolved result does, ensuring backward compatibility for now. However, based on the current evidence, I personally believe this should be categorized as a bug. How would you advise handling this issue?
Activity
weiiwang01 commentedon Feb 9, 2024
I have created a fix for this problem internally. If it's okay, I can submit that as a pull request.
zooba commentedon Feb 9, 2024
Yes, please add a pull request. If it's a relatively simple fix, then this will be less controversial.
For what it's worth, we don't usually consider web browsers to be the canonical behaviour for our APIs. If another low-level library similar to urllib is doing it, that is helpful guidance (e.g. urllib3).
weiiwang01 commentedon Feb 9, 2024
@zooba Yes, the solution is simply not to resolve the host before checking it against the system proxy bypass list. I don't recall any HTTP tools, for example, curl, has this behavior. I did some research and found that the
requestslibrary has already created a patch insiderequeststo stop resolving for Windows, but it is still unfixed for macOS. Fixing this behavior in Python could definitely help.ronaldoussoren commentedon Feb 9, 2024
Some other datapoints.
Ruby resolves hostnames, see https://github.com/ruby/ruby/blob/08b77dd682f342943bb64f9d823040cd350240ba/lib/uri/generic.rb#L1490-L1554
wget does not, see https://github.com/jay/wget/blob/099d8ee3da3a6eea5635581ae517035165f400a5/src/retr.c#L1266-L1274
go does not, see https://github.com/golang/go/blob/2057ad02bd8387378a2d1fd637e955e126f698bf/src/vendor/golang.org/x/net/http/httpproxy/proxy.go#L172-L205
curl does not, see https://github.com/curl/curl/blob/476adfeac019edec71d67e144f184f4dbf1a46fb/lib/noproxy.c#L122-L263
Gitlab blog about no_proxy: https://about.gitlab.com/blog/2021/01/27/we-need-to-talk-no-proxy/, the links above are based on that blog post (but point to main branch except for wget which I didn't change). The blog points out some other inconstencies between the implementations as well.
Given the references above most major tools seem to not resolve names, which t.b.h. is not something I had expected.
weiiwang01 commentedon Feb 9, 2024
@ronaldoussoren Thank you for the investigation. This aligns more or less with what I thought. Even Python itself does not resolve the hostname when handling the
no_proxyenvironment variable, just likewget,go, andcurl.cpython/Lib/urllib/request.py
Lines 2516 to 2548 in 31633f4
The affected code here is slightly different, as Python handles the proxy bypass list sourced from the operating system. To my knowledge,
Ruby,curl, andgodo not even attempt to read the proxy bypass list from the operating system. And there is essentially no documentation related to the operating system's proxy bypass list on Windows and macOS. This is why I ran experiments with the operating system's default browser and attempted to test the "canonical" interpretation of the system proxy bypass list. From the results, it appears that macOS and Windows do not resolve the hostname.zooba commentedon Feb 9, 2024
The
WINHTTP_PROXY_INFOdocumentation says that "proxy bypass list contains one or more server names".Names are not what we get from a resolution process, so that would argue that at least on Windows, we should check the host name without resolving it.
Whether we can safely make that change in terms of backwards compatibility is a separate question. My instinct is that we should check both for now and plan to stop checking addresses after a deprecation period (and probably add some way for users to stop checking them now). But I'm not sure if that's best. @gpshead, I expect you know more about this area (proxies) than I do - any thoughts?
weiiwang01 commentedon Feb 9, 2024
@zooba Thank you for the inspiration on how to find the documentation. I discovered that the corresponding documentation for macOS
kSCPropNetProxiesExceptionsListsays that "Host name patterns that should bypass the proxy."Does the "name" here imply that macOS shouldn't resolve the name as well?
gpshead commentedon Feb 9, 2024
I agree with the majority of tools: A proxy bypass lists should always be a simple string match without name resolution. In proxy environments the point of the proxy is that it is entirely up to the proxy to do all resolving and remote connecting (or MITMing, denying, rejecting, etc). So we should not be doing any name resolution (or reverse name lookups of an address) in order to determine if a proxy should be used.
@sethmlarson may also have an opinion.
ronaldoussoren commentedon Feb 10, 2024
TLDR: Indeed, on macOS we shouldn't resolve the name to match system behaviour.
I did some testing with Apple's system libraries and those don't resolve names when determining if a proxy should be used.
That is, given the following script (requires PyObjC to be installed):
I get the following output with a proxy configured an 34.35.121.47 added to the no-proxy list (the other two are part of the default configuration):
This shows that the "http://www.bol.com" will be proxied, even though its IP address is on the proxy list.
That means that we shouldn't resolve names to match system behaviour.
Not that any of this is clearly documented (sigh...).
weiiwang01 commentedon Feb 12, 2024
@zooba I can update the code to print a deprecation warning when the hostname does not match directly, but the resolved result does, ensuring backward compatibility for now. However, based on the current evidence, I personally believe this should be categorized as a bug. How would you advise handling this issue?
15 remaining items
gh-115197: Stop resolving host in urllib.request proxy bypass (GH-115210
pythongh-115197: Stop resolving host in urllib.request proxy bypass (p…
pythongh-115197: Stop resolving host in urllib.request proxy bypass (p…
pythongh-115197: Stop resolving host in urllib.request proxy bypass (p…
pythongh-115197: Stop resolving host in urllib.request proxy bypass (p…
pythongh-115197: Stop resolving host in urllib.request proxy bypass (p…
gpshead commentedon Feb 28, 2024
thanks!
[3.12] gh-115197: Stop resolving host in urllib.request proxy bypass (G…
[3.11] gh-115197: Stop resolving host in urllib.request proxy bypass (G…
pythongh-115197: Stop resolving host in urllib.request proxy bypass (p…
[3.10] gh-115197: Stop resolving host in urllib.request proxy bypass (G…
[3.9] gh-115197: Stop resolving host in urllib.request proxy bypass (G…
[3.8] gh-115197: Stop resolving host in urllib.request proxy bypass (G…
pythongh-115197: Stop resolving host in urllib.request proxy bypass (p…
pythongh-115197: Stop resolving host in urllib.request proxy bypass (p…