Skip to content

The agent that grows with you

fix(pm): only loopback-origin downloads may redirect to loopback

ethernetf9ec21538f6e
diff --git a/pm/downloader.py b/pm/downloader.py
index 5ee83b71811..382bbfbce6e 100644
--- a/pm/downloader.py
+++ b/pm/downloader.py
@@ -54,7 +54,11 @@ class _HttpsRedirectHandler(urllib.request.HTTPRedirectHandler):
carry the payload in the clear."""
def redirect_request(self, req, fp, code, msg, headers, newurl):
- if not (newurl.startswith("https://") or newurl.startswith(_LOOPBACK)):
+ # Plain-http loopback exists for test servers, and only a download that
+ # started on loopback may stay there: an https origin bouncing to a local
+ # listener would hand an unpinned model file to whatever is bound there.
+ loopback = req.full_url.startswith(_LOOPBACK) and newurl.startswith(_LOOPBACK)
+ if not (newurl.startswith("https://") or loopback):
raise DownloadError(f"refusing redirect to non-https url: {newurl}")
# urllib preserves our User-Agent and range headers. Do not re-add
# arbitrary headers after its redirect policy has processed them.
diff --git a/tests/pm/test_downloader.py b/tests/pm/test_downloader.py
index f89b45cd908..a6899bc932b 100644
--- a/tests/pm/test_downloader.py
+++ b/tests/pm/test_downloader.py
@@ -186,6 +186,12 @@ def test_redirect_to_non_https_refused():
# An https redirect resolves through the default handler.
assert handler.redirect_request(req, None, 302, "Found", {},
"https://example.com/b") is not None
+ # Loopback is a test-server affordance: an https origin may not land there.
+ with pytest.raises(DownloadError):
+ handler.redirect_request(req, None, 302, "Found", {}, "http://127.0.0.1:8000/b")
+ local = urllib.request.Request("http://127.0.0.1:8000/a")
+ assert handler.redirect_request(local, None, 302, "Found", {},
+ "http://localhost:8000/b") is not None
# ── pause ─────────────────────────────────────────────────────