Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/whichproxy/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
Expand Down
39 changes: 36 additions & 3 deletions src/whichproxy/match.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import fnmatch
import ipaddress
from dataclasses import dataclass
from urllib.parse import urlsplit
Expand Down Expand Up @@ -66,13 +67,45 @@ 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()
if not raw:
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)
Expand All @@ -87,7 +120,7 @@ def _curl_match(host: str, tokens: list[str]) -> str | None:
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
Expand Down Expand Up @@ -119,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):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions tests/test_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,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"