Skip to content

The agent that grows with you

fix(agent): a malformed port in base_url no longer drops the keepalive transport

teknium18121b8f438e6
diff --git a/agent/proxy_bypass.py b/agent/proxy_bypass.py
index 9b5194acd4e..64cc22bd220 100644
--- a/agent/proxy_bypass.py
+++ b/agent/proxy_bypass.py
@@ -27,13 +27,18 @@ def first_proxy_env_value() -> str:
def split_host_port(value: str) -> tuple[str, int | None]:
"""``(host, port)`` from a URL (scheme optional: ``//host/path``), ``[v6]:port``,
- ``host:port`` or bare host; host lowercased."""
+ ``host:port`` or bare host; host lowercased. A malformed URL port (``host:abc``,
+ ``host:99999``) yields ``(host, None)`` rather than raising."""
raw = str(value or "").strip()
if not raw:
return "", None
if "://" in raw or raw.startswith("//"):
parsed = urlsplit(raw)
- host, port = parsed.hostname or "", parsed.port
+ host = parsed.hostname or ""
+ try:
+ port = parsed.port
+ except ValueError: # ``host:abc`` / ``host:99999``: keep the host, drop the port
+ port = None
elif raw.startswith("[") and "]" in raw:
host, _, rest = raw[1:].partition("]")
port = int(rest[1:]) if rest.startswith(":") and rest[1:].isdigit() else None
diff --git a/tests/agent/test_proxy_bypass_shared.py b/tests/agent/test_proxy_bypass_shared.py
index 5bf7a70d5be..2ffb34021c8 100644
--- a/tests/agent/test_proxy_bypass_shared.py
+++ b/tests/agent/test_proxy_bypass_shared.py
@@ -46,3 +46,14 @@ def test_non_matching_host_keeps_the_proxy_on_both_paths(proxy_env):
assert resolve_proxy_url(target_hosts="api.telegram.org") == "http://proxy.corp:3128"
assert not is_host_excluded_by_no_proxy("slack.com")
assert is_host_excluded_by_no_proxy("files.slack.com", "slack.com") # explicit value wins
+ assert not should_bypass_proxy("notslack.com", no_proxy_value="*.slack.com")
+
+
+def test_malformed_port_in_base_url_keeps_the_proxy_instead_of_raising(proxy_env):
+ """A ``host:notaport`` base_url must not raise out of the bypass check: the caller's
+ blanket ``except`` would otherwise drop the shared keepalive transport entirely."""
+ proxy_env.setenv("NO_PROXY", "other.example")
+ assert _get_proxy_for_base_url("http://host:notaport/v1") == "http://proxy.corp:3128"
+ assert _get_proxy_for_base_url("http://host:99999/v1") == "http://proxy.corp:3128"
+ proxy_env.setenv("NO_PROXY", "host")
+ assert _get_proxy_for_base_url("http://host:notaport/v1") is None # host still matched