Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(trading): keep the connector override allowlist on the vault credential path

Haozhe Wu19d4bee3568b
diff --git a/agent/src/trading/service.py b/agent/src/trading/service.py
index 027bceea3..b71792578 100644
--- a/agent/src/trading/service.py
+++ b/agent/src/trading/service.py
@@ -3,6 +3,7 @@
from __future__ import annotations
import math
+import sys
from typing import Any
from src.trading.profiles import list_profiles, profile_by_id
@@ -39,6 +40,39 @@ def _sdk_module(connector: str):
return importlib.import_module(path)
+#: Where each SDK connector declares which keys a per-call override may set.
+#: Two spellings exist (Longbridge calls its narrower set an *overlay*); a
+#: module declaring neither gets an EMPTY allowlist, so a connector added
+#: without one drops every override rather than passing them all through.
+_OVERRIDE_ALLOWLIST_ATTRS = ("_OVERRIDE_KEYS", "_OVERLAY_KEYS")
+
+
+def _allowed_override_keys(module: Any) -> frozenset[str]:
+ """Return the keys ``module`` permits a caller-supplied override to set.
+
+ Args:
+ module: A ``src.trading.connectors.<name>.sdk`` module.
+
+ Returns:
+ The connector's declared allowlist, or an empty set when it declares
+ none (fail closed).
+ """
+ # Some connectors (etoro, mt5) only re-export ``build_config`` into their
+ # ``sdk`` module, leaving the allowlist beside the definition. Follow the
+ # function to its defining module before giving up.
+ candidates = [module]
+ builder = getattr(module, "build_config", None)
+ defining = sys.modules.get(getattr(builder, "__module__", ""))
+ if defining is not None and defining is not module:
+ candidates.append(defining)
+ for candidate in candidates:
+ for attr in _OVERRIDE_ALLOWLIST_ATTRS:
+ keys = getattr(candidate, attr, None)
+ if keys:
+ return frozenset(str(key) for key in keys)
+ return frozenset()
+
+
def _sdk_config(
profile: TradingProfile,
module: Any,
@@ -90,10 +124,21 @@ def _sdk_config(
# Resolve only the config class through the connector's existing builder,
# then construct a fresh instance so credentials cannot be mixed with a
# different account's legacy JSON file.
- config_type = type(module.build_config(profile.config, options))
- payload = {**dict(profile.config), **credentials, **options}
+ #
+ # Per-call overrides must still pass the connector's own allowlist. Every
+ # SDK connector deliberately narrows what a caller may override — OKX and
+ # Binance both exclude ``readonly`` ("always true for this layer") and
+ # ``timeout``; Longbridge's overlay is ``profile``/``region`` only,
+ # precisely so a caller "cannot mix or bypass the shared resolver".
+ # ``build_config`` applies that filter; constructing from a raw merged
+ # mapping would not, and ``overrides`` is the one part of this payload that
+ # reaches us from an MCP tool argument or a REST body.
+ allowed = _allowed_override_keys(module)
+ clean = {key: value for key, value in options.items() if key in allowed and value not in (None, "")}
+ payload = {**dict(profile.config), **credentials, **clean}
if profile.connector == "longbridge":
payload["_credential_source"] = "keyring"
+ config_type = type(module.build_config(profile.config, clean))
return config_type.from_mapping(payload)
diff --git a/agent/tests/test_local_connections.py b/agent/tests/test_local_connections.py
index 4bd6ee844..901819be3 100644
--- a/agent/tests/test_local_connections.py
+++ b/agent/tests/test_local_connections.py
@@ -226,3 +226,86 @@ def test_mcp_connector_discovery_exposes_onboarding_contract_without_values():
"passphrase",
]
assert "credential_values" not in okx["onboarding"]
+
+
+# ---------------------------------------------------------------------------
+# Per-call overrides must pass the connector's own allowlist (#1250 follow-up)
+# ---------------------------------------------------------------------------
+
+
+def _vault_store(tmp_path, connection_id, profile_id):
+ credentials = CredentialStore(_MemoryCredentials())
+ store = ConnectionStore(tmp_path / "connections.json", credential_store=credentials)
+ store.create(connection_id, profile_id, "Test")
+ return store, credentials
+
+
+def test_connection_scoped_overrides_obey_the_connector_allowlist(tmp_path, monkeypatch):
+ """A vault-backed call must not widen what a caller may override.
+
+ Every SDK connector narrows overrides on purpose: OKX and Binance both
+ exclude ``readonly`` ("always true for this layer") and ``timeout``, and
+ Longbridge's overlay is ``profile``/``region`` only so a caller "cannot mix
+ or bypass the shared resolver". ``build_config`` enforces that; a config
+ built from a raw merged mapping would not — and ``overrides`` is the one
+ part of that payload that arrives from an MCP tool argument or REST body.
+ """
+ import src.trading.connections as conns
+ from src.trading import service
+ from src.trading.connectors.okx import sdk as okx_sdk
+ from src.trading.profiles import profile_by_id
+
+ store, credentials = _vault_store(tmp_path, "main-okx", "okx-live-sdk-readonly")
+ credentials.save("main-okx", {"api_key": "k", "api_secret": "s", "passphrase": "p"})
+ monkeypatch.setattr(conns, "ConnectionStore", lambda *a, **k: store)
+
+ profile = profile_by_id("okx-live-sdk-readonly")
+ out_of_allowlist = {"readonly": False, "timeout": 999.0}
+
+ # The connector's own builder drops them...
+ legacy = okx_sdk.build_config(profile.config, out_of_allowlist)
+ assert legacy.readonly is True
+ assert legacy.timeout == 15.0
+
+ # ...and so must the vault-backed path.
+ vaulted = service._sdk_config(
+ profile, okx_sdk, {"connection_id": "main-okx", **out_of_allowlist}
+ )
+ assert vaulted.readonly is True
+ assert vaulted.timeout == 15.0
+ # An allowlisted override still works.
+ assert (
+ service._sdk_config(
+ profile, okx_sdk, {"connection_id": "main-okx", "expected_uid": "uid-1"}
+ ).expected_uid
+ == "uid-1"
+ )
+ # And the vault credentials really were used, or the assertions are vacuous.
+ assert vaulted.api_key == "k"
+
+
+def test_every_sdk_connector_declares_an_override_allowlist():
+ """A connector added without one must fail closed, and be noticed here."""
+ import importlib
+ import pkgutil
+
+ from src.trading import connectors as connectors_pkg
+ from src.trading.service import _allowed_override_keys
+
+ missing = []
+ for info in pkgutil.iter_modules(connectors_pkg.__path__):
+ try:
+ module = importlib.import_module(
+ f"src.trading.connectors.{info.name}.sdk"
+ )
+ except ModuleNotFoundError:
+ continue # connector without an SDK surface
+ if not hasattr(module, "build_config"):
+ continue # not a per-call-config connector
+ if not _allowed_override_keys(module):
+ missing.append(info.name)
+ assert missing == [], (
+ f"SDK connectors without an override allowlist: {missing}. "
+ "_allowed_override_keys fails closed, so their per-call overrides are "
+ "silently dropped — declare _OVERRIDE_KEYS."
+ )