summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorNataliia Solomko <natalirs1985@gmail.com>2026-11-04 11:48:49 +0200
committerNataliia Solomko <natalirs1985@gmail.com>2026-11-04 11:54:32 +0200
commitdc20a42c8f14e63747a931f101f37858f6f4b494 (patch)
tree2a7f842a7de6657cf1f6c26937fea58ff800826e /python
parent53dc51f02ac94a6a1a32efb454ecdc994aa99bfd (diff)
downloadvyos-1x-dc20a42c8f14e63747a931f101f37858f6f4b494.tar.gz
vyos-1x-dc20a42c8f14e63747a931f101f37858f6f4b494.zip
remote: T9568: Do not use .netrc credentials for redirect targets
A "default" entry in .netrc matches any host, so a download that was redirected to a different host could send those credentials there. Credentials from .netrc are now only used for the original host.
Diffstat (limited to 'python')
-rw-r--r--python/vyos/remote.py31
1 files changed, 29 insertions, 2 deletions
diff --git a/python/vyos/remote.py b/python/vyos/remote.py
index 81acd0913..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()})
@@ -446,7 +473,7 @@ class HttpC:
size = None
# Do not send credentials to a different host after a redirect
if s.should_strip_auth(self.urlstring, final_urlstring):
- s.auth = None
+ s.auth = NoAuth()
with s.get(final_urlstring, stream=True, timeout=self.timeout) as r:
r.raise_for_status()
if size is None: