diff --git a/src/whichproxy/env.py b/src/whichproxy/env.py index 0de5cc0..d50f9d4 100644 --- a/src/whichproxy/env.py +++ b/src/whichproxy/env.py @@ -86,6 +86,8 @@ def redact_proxy(url: str) -> str: if not parts.username and not parts.password: return url host = parts.hostname or "" + if ":" in host: + host = f"[{host}]" if parts.port: host = f"{host}:{parts.port}" user = parts.username or "" diff --git a/src/whichproxy/match.py b/src/whichproxy/match.py index 0a795d4..85810aa 100644 --- a/src/whichproxy/match.py +++ b/src/whichproxy/match.py @@ -1,5 +1,6 @@ from __future__ import annotations +import fnmatch import ipaddress from dataclasses import dataclass from urllib.parse import urlsplit @@ -66,6 +67,32 @@ def _curl(host: str, tokens: list[str], proxy: str) -> ModelResult: return ModelResult("curl", route, detail) +def normalize_no_proxy_token(raw: str) -> str: + """Strip ``[IPv6]`` brackets and a trailing ``:port`` from a NO_PROXY token. + + ``[::1]`` and ``127.0.0.1:8080`` appear in real env files; matching must + use the host (or CIDR) curl/Go actually compare against. + """ + text = (raw or "").strip() + if text.startswith("[") and "]" in text: + inner = text[1 : text.index("]")] + rest = text[text.index("]") + 1 :] + if rest.startswith("/") or not rest: + return inner + rest + if rest.startswith(":") and rest[1:].split("/", 1)[0].isdigit(): + suffix = rest.split(":", 1)[1] + slash = suffix.find("/") + return inner if slash < 0 else inner + suffix[slash:] + return inner + if "/" in text: + return text + if text.count(":") == 1 and not text.startswith(":"): + host, port = text.rsplit(":", 1) + if port.isdigit(): + return host + return text + + def _curl_match(host: str, tokens: list[str]) -> str | None: for token in tokens: raw = token.strip() @@ -73,12 +100,27 @@ def _curl_match(host: str, tokens: list[str]) -> str | None: continue if raw == "*": return "matched *" + # macOS / many tools ship ``*.local``; modern curl treats ``*`` as a wildcard. + if "*" in raw: + pattern = raw.lower().rstrip(".") + if fnmatch.fnmatch(host, pattern): + return f"wildcard {raw}" + continue + if "/" in raw: + try: + network = ipaddress.ip_network(raw, strict=False) + addr = ipaddress.ip_address(host) + except ValueError: + continue + if addr in network: + return f"CIDR {raw}" + continue if raw.startswith("."): suffix = raw.lower().lstrip(".") if host.endswith("." + suffix): return f"subdomain of {raw}" continue - lowered = raw.lower().rstrip(".") + lowered = normalize_no_proxy_token(raw).lower().rstrip(".") if host == lowered: return f"exact {raw}" return None @@ -110,14 +152,14 @@ def _go_match(host: str, tokens: list[str]) -> str | None: return "matched *" if "/" in raw: try: - network = ipaddress.ip_network(raw, strict=False) + network = ipaddress.ip_network(normalize_no_proxy_token(raw), strict=False) addr = ipaddress.ip_address(host) except ValueError: continue if addr in network: return f"CIDR {raw}" continue - lowered = raw.lower() + lowered = normalize_no_proxy_token(raw).lower() if lowered.startswith("."): suffix = lowered.lstrip(".") if host.endswith("." + suffix): diff --git a/tests/test_env.py b/tests/test_env.py index ae5748c..d14f8b0 100644 --- a/tests/test_env.py +++ b/tests/test_env.py @@ -43,6 +43,13 @@ def test_redact_proxy_leaves_plain_url() -> None: assert redact_proxy(PROXY_URL) == PROXY_URL +def test_redact_proxy_ipv6_keeps_brackets() -> None: + raw = "http://user:s3cretPASS@[::1]:7897" + hidden = redact_proxy(raw) + assert hidden == "http://user:***@[::1]:7897" + assert "s3cretPASS" not in hidden + + def test_dangerous_no_proxy_hits_finds_openai() -> None: hits = dangerous_no_proxy_hits("localhost,openai.com,127.0.0.1") assert "openai.com" in hits diff --git a/tests/test_match.py b/tests/test_match.py index 2c311d9..65bb4b6 100644 --- a/tests/test_match.py +++ b/tests/test_match.py @@ -59,3 +59,25 @@ def test_evaluate_applies_normalize_host() -> None: results = {item.model: item for item in evaluate("https://api.openai.com:443", _env(".openai.com"))} assert results["curl"].route == "DIRECT" assert results["go"].route == "DIRECT" + + +def test_curl_star_local_wildcard_matches_mdns_host() -> None: + inside = _by_model("printer.local", "*.local") + apex = _by_model("local", "*.local") + assert inside["curl"].route == "DIRECT" + assert "wildcard" in inside["curl"].reason + assert apex["curl"].route == "PROXY" + # Go has no ``*.suffix`` wildcard; only a bare ``*`` token. + assert inside["go"].route == "PROXY" + + +def test_curl_and_go_match_bracketed_ipv6_loopback() -> None: + results = _by_model("::1", "[::1]") + assert results["curl"].route == "DIRECT" + assert results["go"].route == "DIRECT" + + +def test_curl_and_go_strip_port_from_noproxy_token() -> None: + results = _by_model("127.0.0.1", "127.0.0.1:8080") + assert results["curl"].route == "DIRECT" + assert results["go"].route == "DIRECT"