Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(channels): deny-by-default secret masking for credential-shaped keys

shadowinlifefd27a98061c2
diff --git a/agent/src/channels/config_meta.py b/agent/src/channels/config_meta.py
index 2455ca4fe..c2048f258 100644
--- a/agent/src/channels/config_meta.py
+++ b/agent/src/channels/config_meta.py
@@ -5,6 +5,12 @@ without per-channel frontend code. Hand-written :data:`FIELD_HINTS` supply
i18n-friendly metadata (labels are owned by the frontend via ``help_key``);
channels without hints fall back to their adapter's ``default_config()`` with
type inference and a fail-safe secret heuristic.
+
+Secret detection is the extended :data:`SECRET_KEY_RE` family plus per-channel
+hint flags, so credential-shaped keys such as Feishu's ``encrypt_key`` and
+Discord's ``proxy_username`` never cross the wire in ``values``. URL userinfo
+(``user:password@``) is stripped from non-secret values before they are
+returned.
"""
from __future__ import annotations
@@ -13,12 +19,16 @@ import logging
import re
from typing import Any
from typing import TypedDict
+from urllib.parse import urlsplit, urlunsplit
from src.channels.registry import load_channel_class
logger = logging.getLogger(__name__)
-SECRET_KEY_RE = re.compile(r"secret|token|password|api_key", re.IGNORECASE)
+SECRET_KEY_RE = re.compile(
+ r"secret|token|password|api_?key|encrypt_?key|signing_?key|credential|private|proxy_?username",
+ re.IGNORECASE,
+)
_HELP_KEY_PREFIX = "settings.channels.fields"
_EXCLUDED_KEYS = frozenset({"enabled"})
@@ -132,10 +142,27 @@ def channel_field_hints(name: str) -> list[FieldHint]:
def _mask(value: Any) -> dict[str, Any]:
"""Return the ``{set, masked}`` descriptor for one secret value."""
is_set = bool(value)
- return {
- "set": is_set,
- "masked": "****" + str(value)[-4:] if is_set else "",
- }
+ if not is_set:
+ return {"set": False, "masked": ""}
+ text = str(value)
+ return {"set": True, "masked": "****" if len(text) <= 8 else "****" + text[-4:]}
+
+
+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
def split_values_secrets(
@@ -152,8 +179,10 @@ def split_values_secrets(
section: Raw config section as loaded from disk.
Returns:
- ``(values, secrets)`` where ``values`` holds non-secret keys verbatim
- and each secret is ``{"set": bool, "masked": "****" + last4}``.
+ ``(values, secrets)`` where ``values`` holds non-secret keys with any
+ URL userinfo stripped, and each secret is
+ ``{"set": bool, "masked": "****"}`` (a suffix is disclosed only for
+ values longer than 8 characters).
"""
secret_hint_keys = {
hint["key"] for hint in channel_field_hints(name) if hint["secret"]
@@ -164,5 +193,5 @@ def split_values_secrets(
if SECRET_KEY_RE.search(key) or key in secret_hint_keys:
secrets[key] = _mask(value)
else:
- values[key] = value
+ values[key] = _strip_url_userinfo(value)
return values, secrets
diff --git a/agent/tests/test_channel_config_meta.py b/agent/tests/test_channel_config_meta.py
index 082a9eb38..558950422 100644
--- a/agent/tests/test_channel_config_meta.py
+++ b/agent/tests/test_channel_config_meta.py
@@ -16,15 +16,9 @@ from src.channels.config_meta import (
)
from src.channels.registry import discover_channel_names, load_channel_class
-# Helper modules that live beside the adapters but export no ``BaseChannel``:
-# ``targets`` is a scheduled-delivery registry, ``config_meta`` is this module.
-_NON_CHANNEL_MODULES = frozenset({"targets", "config_meta"})
-
-# Every discovered adapter that is a real channel. The sweep below locks this
-# surface at 16.
-ADAPTER_NAMES: list[str] = sorted(
- name for name in discover_channel_names() if name not in _NON_CHANNEL_MODULES
-)
+# Discovery already excludes the helper modules (``registry._INTERNAL``), so
+# this is the real channel surface. The sweep below locks it at 16.
+ADAPTER_NAMES: list[str] = sorted(discover_channel_names())
def _dummy_value(value: object) -> object:
@@ -166,11 +160,22 @@ def test_unknown_channel_masks_secret_keys_and_strips_them_from_values() -> None
)
assert values == {"name": "n"}
assert secrets == {
- "webhook_secret": {"set": True, "masked": "****2345"},
- "bot_token": {"set": True, "masked": "****x"},
+ "webhook_secret": {"set": True, "masked": "****"},
+ "bot_token": {"set": True, "masked": "****"},
}
+def test_short_secret_masking_boundary() -> None:
+ """Secrets of 8 characters or fewer disclose nothing; 9 disclose the last 4."""
+ for length in range(1, 9):
+ _, secrets = split_values_secrets(
+ "no_such_channel", {"token": "1234567890"[:length]}
+ )
+ assert secrets["token"] == {"set": True, "masked": "****"}, length
+ _, secrets = split_values_secrets("no_such_channel", {"token": "123456789"})
+ assert secrets["token"] == {"set": True, "masked": "****6789"}
+
+
def test_empty_secret_is_unset_and_unmasked() -> None:
"""An unset secret reports ``set: false`` and an empty mask."""
values, secrets = split_values_secrets("no_such_channel", {"api_key": ""})
@@ -186,6 +191,27 @@ def test_non_secret_values_pass_through_verbatim() -> None:
assert secrets == {}
+def test_proxy_url_userinfo_is_stripped_from_values() -> None:
+ """URL userinfo is stripped from non-secret values; other strings stay put."""
+ values, secrets = split_values_secrets(
+ "telegram",
+ {
+ "proxy": "http://user:[email protected]:8080",
+ "webhook_url": "https://example.com/hook",
+ "note": "plain text",
+ "bad_proxy": "http://user:pw@host:notaport",
+ "token": "abc",
+ },
+ )
+ assert values["proxy"] == "http://proxy.local:8080"
+ assert "user" not in values["proxy"]
+ 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"
+ assert secrets["token"] == {"set": True, "masked": "****"}
+
+
def test_hint_marked_secret_is_masked_for_known_channel() -> None:
"""A hint-declared secret is masked and never reaches values."""
values, secrets = split_values_secrets(
@@ -196,15 +222,50 @@ def test_hint_marked_secret_is_masked_for_known_channel() -> None:
def test_secret_key_regex_is_case_insensitive() -> None:
- assert SECRET_KEY_RE.search("API_KEY")
- assert SECRET_KEY_RE.search("WebhookSecret")
- assert SECRET_KEY_RE.search("bot_token")
- assert SECRET_KEY_RE.search("db_password")
- assert not SECRET_KEY_RE.search("allow_from")
+ """Credential-shaped keys match case-insensitively; identifiers do not."""
+ for key in (
+ "API_KEY",
+ "WebhookSecret",
+ "bot_token",
+ "db_password",
+ "encrypt_key",
+ "ENCRYPT_KEY",
+ "signing_key",
+ "private_key",
+ "credential",
+ "proxy_username",
+ "apikey",
+ ):
+ assert SECRET_KEY_RE.search(key), key
+ for key in ("allow_from", "client_id", "imap_username", "domain"):
+ assert not SECRET_KEY_RE.search(key), key
# --- (e) security sweep over every discovered adapter ---------------------- #
+_EXPECTED_SECRET_KEYS: dict[str, set[str]] = {
+ "dingtalk": {"client_secret"},
+ "discord": {"token", "proxy_password", "proxy_username"},
+ "email": {"imap_password", "smtp_password"},
+ "feishu": {"app_secret", "verification_token", "encrypt_key"},
+ "matrix": {"password", "access_token"},
+ "mochat": {"claw_token"},
+ "msteams": {"app_password"},
+ "napcat": {"access_token"},
+ "qq": {"secret"},
+ "signal": set(),
+ "slack": {"bot_token", "app_token", "user_token_read_only"},
+ "telegram": {"token", "webhook_secret_token"},
+ "wecom": {"secret"},
+ "weixin": {"token"},
+ "whatsapp": set(),
+ "websocket": {"token", "token_issue_secret"},
+}
+
+_IDENTIFIER_KEYS = frozenset(
+ {"client_id", "app_id", "bot_id", "phone_number", "imap_username", "smtp_username"}
+)
+
def test_discovered_adapter_surface_is_locked() -> None:
"""The sweep below covers the frozen set of 16 built-in adapters."""
@@ -227,6 +288,30 @@ def test_split_never_leaks_secret_keys_in_values(name: str) -> None:
assert not (set(values) & set(secrets))
[email protected]("name", ADAPTER_NAMES)
+def test_every_adapter_credential_key_lands_in_secrets(name: str) -> None:
+ """Every credential-shaped field lands in ``secrets``, never in ``values``."""
+ section = _section_for(name)
+ if section is None:
+ pytest.skip(f"{name} adapter is not loadable in this environment")
+ values, secrets = split_values_secrets(name, section)
+ expected = _EXPECTED_SECRET_KEYS[name]
+ assert expected <= set(secrets)
+ assert set(expected) & set(values) == set()
+
+
[email protected]("name", ADAPTER_NAMES)
+def test_identifier_fields_stay_visible(name: str) -> None:
+ """Identifier-shaped fields stay in ``values``, not ``secrets``."""
+ section = _section_for(name)
+ if section is None:
+ pytest.skip(f"{name} adapter is not loadable in this environment")
+ values, secrets = split_values_secrets(name, section)
+ for key in _IDENTIFIER_KEYS & set(section):
+ assert key in values, f"{name}.{key} must stay visible"
+ assert key not in secrets, f"{name}.{key} must not be masked"
+
+
# --- (f) unknown channel ---------------------------------------------------- #