summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorNataliia Solomko <natalirs1985@gmail.com>2026-11-03 18:27:23 +0200
committerNataliia Solomko <natalirs1985@gmail.com>2026-11-04 11:25:49 +0200
commit53dc51f02ac94a6a1a32efb454ecdc994aa99bfd (patch)
treea601b4f337d9b1f63167ca860ac295d353b1711c /src
parenta100a7f62325beac2c9c3a75778bca09da3e9d08 (diff)
downloadvyos-1x-53dc51f02ac94a6a1a32efb454ecdc994aa99bfd.tar.gz
vyos-1x-53dc51f02ac94a6a1a32efb454ecdc994aa99bfd.zip
remote: T9568: Do not send HTTP credentials to another host after a redirect
Downloads that were redirected to a different host sent the user's credentials to that host. Credentials are now dropped when the redirect leaves the original host.
Diffstat (limited to 'src')
-rw-r--r--src/tests/test_remote.py67
1 files changed, 65 insertions, 2 deletions
diff --git a/src/tests/test_remote.py b/src/tests/test_remote.py
index d0805cdff..1a4cfecc0 100644
--- a/src/tests/test_remote.py
+++ b/src/tests/test_remote.py
@@ -16,12 +16,15 @@ import os
import tempfile
import threading
import unittest
+from base64 import b64encode
from http.server import BaseHTTPRequestHandler
from http.server import HTTPServer
from urllib.parse import urlsplit
from vyos.remote import HttpC
+AUTH_HEADER = 'Basic ' + b64encode(b'vyos:secret').decode()
+
class HeadRejectHandler(BaseHTTPRequestHandler):
body = b'192.0.2.1\n'
@@ -50,14 +53,54 @@ class Head501Handler(HeadRejectHandler):
head_status = 501
+class RedirectHandler(BaseHTTPRequestHandler):
+ body = b'192.0.2.1\n'
+ location = '/redirected.txt'
+
+ def _respond(self, send_body):
+ self.server.requests.append(
+ (self.command, self.path, self.headers.get('Authorization')))
+ if self.path == '/list.txt':
+ self.send_response(302)
+ self.send_header('Location',
+ self.location.format(port=self.server.server_address[1]))
+ self.end_headers()
+ return
+ self.send_response(200)
+ self.send_header('Content-Type', 'text/plain')
+ self.send_header('Content-Length', str(len(self.body)))
+ self.end_headers()
+ if send_body:
+ self.wfile.write(self.body)
+
+ def do_HEAD(self):
+ self._respond(send_body=False)
+
+ def do_GET(self):
+ self._respond(send_body=True)
+
+ def log_message(self, format, *args):
+ pass
+
+
+class SameOriginRedirectHandler(RedirectHandler):
+ location = '/redirected.txt'
+
+
+class CrossHostRedirectHandler(RedirectHandler):
+ # Same server, but 'localhost' is a different host from '127.0.0.1'
+ location = 'http://localhost:{port}/redirected.txt'
+
+
class TestHttpCDownload(unittest.TestCase):
- def _download_from_server(self, handler):
+ def _download_from_server(self, handler, userinfo=''):
server = HTTPServer(('127.0.0.1', 0), handler)
+ server.requests = []
port = server.server_address[1]
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
- url = f'http://127.0.0.1:{port}/list.txt'
+ url = f'http://{userinfo}127.0.0.1:{port}/list.txt'
with tempfile.NamedTemporaryFile(delete=False) as tmp:
tmp_path = tmp.name
@@ -72,12 +115,32 @@ class TestHttpCDownload(unittest.TestCase):
thread.join(timeout=1)
os.unlink(tmp_path)
+ return server.requests
+
def test_download_without_head_support_405(self):
self._download_from_server(Head405Handler)
def test_download_without_head_support_501(self):
self._download_from_server(Head501Handler)
+ def test_download_redirect_to_other_host_drops_auth(self):
+ requests = self._download_from_server(CrossHostRedirectHandler,
+ userinfo='vyos:secret@')
+ self.assertEqual(requests, [
+ ('HEAD', '/list.txt', AUTH_HEADER),
+ ('HEAD', '/redirected.txt', None),
+ ('GET', '/redirected.txt', None),
+ ])
+
+ def test_download_redirect_to_same_origin_keeps_auth(self):
+ requests = self._download_from_server(SameOriginRedirectHandler,
+ userinfo='vyos:secret@')
+ self.assertEqual(requests, [
+ ('HEAD', '/list.txt', AUTH_HEADER),
+ ('HEAD', '/redirected.txt', AUTH_HEADER),
+ ('GET', '/redirected.txt', AUTH_HEADER),
+ ])
+
if __name__ == '__main__':
unittest.main() \ No newline at end of file