From e3598f14fb3cef93c4a676fe0b348b5720390357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=98=E9=9B=85=E3=81=AE=E5=92=B8=E9=B1=BC?= <3129538298@qq.com> Date: Wed, 26 Aug 2026 17:52:25 +0800 Subject: [PATCH 1/5] fix: curl NO_PROXY wildcards and IPv6 tokens --- src/whichproxy/match.py | 39 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/src/whichproxy/match.py b/src/whichproxy/match.py index 0a795d4..9b7ae9e 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,18 @@ 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 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 +143,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): From 9ba35c3d763e3145d28a7ddae9b4e562274b6278 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=98=E9=9B=85=E3=81=AE=E5=92=B8=E9=B1=BC?= <3129538298@qq.com> Date: Wed, 26 Aug 2026 17:52:47 +0800 Subject: [PATCH 2/5] test: curl wildcard and IPv6 NO_PROXY tokens --- tests/test_match.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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" From 8fa5d0ebabce4a5d547d1631c048f1e0ba678a51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=98=E9=9B=85=E3=81=AE=E5=92=B8=E9=B1=BC?= <3129538298@qq.com> Date: Wed, 26 Aug 2026 17:52:49 +0800 Subject: [PATCH 3/5] fix: keep IPv6 brackets when redacting proxy URLs --- src/whichproxy/env.py | 2 ++ 1 file changed, 2 insertions(+) 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 "" From c4e9690af92a1907fcdc22f57fd01687ac2cf6a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=98=E9=9B=85=E3=81=AE=E5=92=B8=E9=B1=BC?= <3129538298@qq.com> Date: Wed, 26 Aug 2026 17:54:33 +0800 Subject: [PATCH 4/5] test: redact IPv6 proxy URLs keep brackets --- tests/test_env.py | 7 +++++++ 1 file changed, 7 insertions(+) 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 From 0947d551aada706ea3d2069a22362d33540efae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=98=E9=9B=85=E3=81=AE=E5=92=B8=E9=B1=BC?= <3129538298@qq.com> Date: Wed, 26 Aug 2026 18:33:45 +0800 Subject: [PATCH 5/5] fix: resolve #3 merge conflicts with main (keep CIDR and wildcard NO_PROXY) --- src/whichproxy/match.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/whichproxy/match.py b/src/whichproxy/match.py index 9b7ae9e..85810aa 100644 --- a/src/whichproxy/match.py +++ b/src/whichproxy/match.py @@ -106,6 +106,15 @@ def _curl_match(host: str, tokens: list[str]) -> str | None: 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):