"Vibe-Trading: Your Personal Trading Agent"
Python ai-agentalgorithmic-tradingbacktestingfintechllmmcpmulti-agentpythonquantitative-financetrading
fix(channels): strip URL userinfo when the port cannot be parsed
cgycoreyb383c93aa6ff
diff --git a/agent/src/channels/config_meta.py b/agent/src/channels/config_meta.pyindex dc6c7dcd6..6bdc409ad 100644--- a/agent/src/channels/config_meta.py+++ b/agent/src/channels/config_meta.py@@ -25,7 +25,6 @@ import loggingimport refrom typing import Anyfrom typing import TypedDict-from urllib.parse import urlsplit, urlunsplitfrom src.channels.registry import load_channel_class@@ -343,21 +342,18 @@ def _mask(value: Any, *, reveal_suffix: bool = True) -> dict[str, Any]:return {"set": True, "masked": masked}+# A URL's userinfo (``user:password@``) lives in its authority, before the+# last '@'. Matching it textually rather than through urllib.parse keeps a+# malformed or out-of-range port from defeating the strip: ``urlsplit(...).port``+# raises on those, and the fail-open fallback returned the credential.+_URL_USERINFO_RE = re.compile(r"([a-zA-Z][a-zA-Z0-9+.\-]*://)(?:[^/\s?#]*@)?(\S*)")++def _strip_url_userinfo(value: Any) -> Any:"""Return a URL string without embedded credentials; non-URLs pass through."""if not isinstance(value, str) or "://" not in value:return value- try:- parts = urlsplit(value)- if parts.username is None and parts.password is None:- return value- host = parts.hostname or ""- port = parts.port # raises ValueError on a malformed port- if port is not None:- host = f"{host}:{port}"- return urlunsplit((parts.scheme, host, parts.path, parts.query, parts.fragment))- except ValueError:- return value+ return _URL_USERINFO_RE.sub(lambda match: match.group(1) + match.group(2), value)def split_values_secrets(diff --git a/agent/src/channels/manager.py b/agent/src/channels/manager.pyindex 2ae730291..fc471aaeb 100644--- a/agent/src/channels/manager.py+++ b/agent/src/channels/manager.py@@ -10,7 +10,6 @@ from collections import defaultdictfrom contextlib import suppressfrom datetime import datetime, timezonefrom typing import Any-from urllib.parse import urlsplit, urlunsplitfrom src.channels.base import BaseChannelfrom src.channels.bus.events import OutboundMessage@@ -44,26 +43,17 @@ _MAX_REPLY_FINGERPRINTS = 4096# Runtime-error bookkeeping for the status surface (Refs #1625)._ERROR_CONTEXTS = frozenset({"start", "send", "reload_stop"})_ERROR_TEXT_LIMIT = 200-_URL_IN_TEXT_RE = re.compile(r"[a-zA-Z][a-zA-Z0-9+.\-]*://\S+")+# A URL's userinfo (``user:password@``) lives in its authority, before the+# last '@'. Matching it textually rather than through urllib.parse keeps a+# malformed or out-of-range port from defeating the redaction:+# ``urlsplit(...).port`` raises on those, and the fail-open fallback returned+# the credential the error text was being scrubbed of.+_URL_IN_TEXT_RE = re.compile(r"([a-zA-Z][a-zA-Z0-9+.\-]*://)(?:[^/\s?#]*@)?(\S*)")def _strip_url_userinfo(text: str) -> str:"""Return *text* with username/password stripped from every embedded URL."""-- def _strip(match: re.Match[str]) -> str:- url = match.group(0)- try:- parts = urlsplit(url)- if parts.username is None and parts.password is None:- return url- host = parts.hostname or ""- if parts.port is not None: # raises ValueError on a malformed port- host = f"{host}:{parts.port}"- return urlunsplit((parts.scheme, host, parts.path, parts.query, parts.fragment))- except ValueError:- return url-- return _URL_IN_TEXT_RE.sub(_strip, text)+ return _URL_IN_TEXT_RE.sub(lambda match: match.group(1) + match.group(2), text)class ChannelManager:diff --git a/agent/tests/test_channel_config_meta.py b/agent/tests/test_channel_config_meta.pyindex 3e76b5db2..ed5e7f50b 100644--- a/agent/tests/test_channel_config_meta.py+++ b/agent/tests/test_channel_config_meta.py@@ -618,6 +618,7 @@ def test_proxy_url_userinfo_is_stripped_from_values() -> None:"webhook_url": "https://example.com/hook","note": "plain text","bad_proxy": "http://user:pw@host:notaport",+ "far_port": "https://user:pw@host:99999/hook","token": "abc",},)@@ -626,7 +627,10 @@ def test_proxy_url_userinfo_is_stripped_from_values() -> None:assert "pw" not in values["proxy"]assert values["webhook_url"] == "https://example.com/hook"assert values["note"] == "plain text"- assert values["bad_proxy"] == "http://user:pw@host:notaport"+ # A malformed port must not exempt the URL from redaction.+ assert values["bad_proxy"] == "http://host:notaport"+ # Nor may an out-of-range port (a plausible typo).+ assert values["far_port"] == "https://host:99999/hook"assert secrets["token"] == {"set": True, "masked": "****"}diff --git a/agent/tests/test_channel_manager_health.py b/agent/tests/test_channel_manager_health.pyindex f46e342eb..f3b85166c 100644--- a/agent/tests/test_channel_manager_health.py+++ b/agent/tests/test_channel_manager_health.py@@ -197,7 +197,19 @@ def test_record_error_scrubs_credentials_and_bounds_length(ValueError("bad url https://user:pass@host:notaport/x"),)error = manager.get_status()["fakea"]["error"]- assert "https://user:pass@host:notaport/x" in error # malformed URLs pass through+ # A malformed port must not exempt the URL from redaction.+ assert "user:pass" not in error+ assert "https://host:notaport/x" in error++ # An out-of-range port is the same fail-open path (a plausible typo).+ manager._record_error(+ "fakea",+ "start",+ ConnectionError("cannot reach https://bot:[email protected]:99999/api"),+ )+ error = manager.get_status()["fakea"]["error"]+ assert "sup3rsecret" not in error+ assert "https://chat.example.com:99999/api" in errormanager._record_error("fakea", "send", RuntimeError("x" * 500))error = manager.get_status()["fakea"]["error"]