diff options
| author | John Estabrook <jestabro@vyos.io> | 2026-11-13 08:35:22 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-11-13 08:35:22 -0600 |
| commit | 5665f89e414d3f40db70f9e143184a0062f68b0e (patch) | |
| tree | c01a1e539c8b38a8e286fc0330234639614f1f3a /python | |
| parent | 95afe65e7dc8a0b3d620dda7bdf758e7f0245a45 (diff) | |
| parent | dc20a42c8f14e63747a931f101f37858f6f4b494 (diff) | |
| download | vyos-1x-5665f89e414d3f40db70f9e143184a0062f68b0e.tar.gz vyos-1x-5665f89e414d3f40db70f9e143184a0062f68b0e.zip | |
Merge pull request #5628 from natali-rs1985/T9568rolling
remote: T9568: Do not send HTTP credentials to another host after a redirect
Diffstat (limited to 'python')
| -rw-r--r-- | python/vyos/remote.py | 32 |
1 files changed, 31 insertions, 1 deletions
diff --git a/python/vyos/remote.py b/python/vyos/remote.py index 6a475eb13..d64534ca9 100644 --- a/python/vyos/remote.py +++ b/python/vyos/remote.py @@ -43,6 +43,7 @@ from ftplib import error_reply from ftplib import parse150 from requests import Session from requests.adapters import HTTPAdapter +from requests.auth import AuthBase from urllib.parse import unquote from urllib.parse import urlsplit from urllib.parse import urlunsplit @@ -116,6 +117,32 @@ class SourceAdapter(HTTPAdapter): **pool_kwargs ) + +class NoAuth(AuthBase): + """ + Explicitly send no credentials. Unlike `auth=None`, this also stops + requests from falling back to .netrc. + """ + + def __call__(self, request): + return request + + +class NoRedirectNetrcSession(Session): + """ + requests session that never takes credentials from .netrc for a redirect + target. A .netrc 'default' entry matches any host, so a redirect could + otherwise hand it to a third party. Proxy and CA settings from the + environment are kept, which `trust_env = False` would drop. + """ + + def rebuild_auth(self, prepared_request, response): + headers = prepared_request.headers + if 'Authorization' in headers and self.should_strip_auth( + response.request.url, prepared_request.url + ): + del headers['Authorization'] + @contextmanager def umask(mask: int): """ @@ -411,7 +438,7 @@ class HttpC: self.vrf = vrf def _establish(self): - session = Session() + session = NoRedirectNetrcSession() adapter = SourceAdapter(self.source_pair, vrf=self.vrf) session.mount(self.urlstring, adapter) session.headers.update({'User-Agent': 'VyOS/' + get_version()}) @@ -444,6 +471,9 @@ class HttpC: # In case the server does not supply the header. except KeyError: size = None + # Do not send credentials to a different host after a redirect + if s.should_strip_auth(self.urlstring, final_urlstring): + s.auth = NoAuth() with s.get(final_urlstring, stream=True, timeout=self.timeout) as r: r.raise_for_status() if size is None: |
