Skip to content

Commit 46c8cb8

Browse files
committed
fix: include rejected redirect target in error
Assisted-by: Codex (model: GPT-5, autonomous)
1 parent 469b9bd commit 46c8cb8

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/specify_cli/authentication/http.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def _hostname_in_hosts(hostname: str, hosts: tuple[str, ...]) -> bool:
6464
def _validate_strict_redirect(_old_url: str, new_url: str) -> None:
6565
if not is_https_or_localhost_http(new_url):
6666
raise urllib.error.URLError(
67-
"unsafe redirect: target must use HTTPS with a hostname, "
67+
f"unsafe redirect to {new_url}: target must use HTTPS with a hostname, "
6868
"or HTTP for localhost (127.0.0.1, ::1)"
6969
)
7070

tests/test_authentication.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,25 @@ def test_redirect_rejects_https_downgrade(self):
882882
handler.redirect_request(req, io.BytesIO(b""), 302, "Found", {},
883883
"http://evil.example.com/archive.zip")
884884

885+
def test_strict_redirect_error_describes_target_and_allowed_localhost(self):
886+
from specify_cli.authentication.http import _StripAuthOnRedirect
887+
from urllib.request import Request
888+
import io
889+
import urllib.error
890+
891+
handler = _StripAuthOnRedirect(("example.com",))
892+
req = Request("https://example.com/archive.zip")
893+
894+
with pytest.raises(urllib.error.URLError) as exc_info:
895+
handler.redirect_request(req, io.BytesIO(b""), 302, "Found", {},
896+
"http://evil.example.com/archive.zip")
897+
898+
error_message = str(exc_info.value)
899+
assert "http://evil.example.com/archive.zip" in error_message
900+
assert "localhost" in error_message
901+
assert "127.0.0.1" in error_message
902+
assert "::1" in error_message
903+
885904

886905
# ---------------------------------------------------------------------------
887906
# _fetch_latest_release_tag delegation

0 commit comments

Comments
 (0)