Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(channels): token probe survives non-object JSON 200 bodies

shadowinlifecefc5ae3ac56
diff --git a/agent/src/channels/dingtalk_probe.py b/agent/src/channels/dingtalk_probe.py
index 87689dd23..fa0039a40 100644
--- a/agent/src/channels/dingtalk_probe.py
+++ b/agent/src/channels/dingtalk_probe.py
@@ -70,6 +70,7 @@ async def test_connection(
"ok": False,
"code": "invalid_credentials",
"detail": "missing credentials",
+ "sdk_available": sdk_available,
}
return await probe_token_endpoint(
diff --git a/agent/src/channels/qq_probe.py b/agent/src/channels/qq_probe.py
index 5e0f23e13..b06224623 100644
--- a/agent/src/channels/qq_probe.py
+++ b/agent/src/channels/qq_probe.py
@@ -48,6 +48,7 @@ async def test_connection(config: QQConfig, *, sdk_available: bool) -> dict[str,
"ok": False,
"code": "invalid_credentials",
"detail": "missing credentials",
+ "sdk_available": sdk_available,
}
return await probe_token_endpoint(
diff --git a/agent/src/channels/token_probe.py b/agent/src/channels/token_probe.py
index 66670088b..adc91ed0d 100644
--- a/agent/src/channels/token_probe.py
+++ b/agent/src/channels/token_probe.py
@@ -10,6 +10,31 @@ can be checked before the channel is started.
Secrets are scrubbed from every ``detail`` before it reaches a result
envelope, and a successfully fetched token is discarded: it is never
returned, logged, or cached.
+
+Adding another token-endpoint channel (checklist):
+
+1. ``<name>_probe.py``: endpoint URL, payload shape, ``token_key``; import
+ the adapter's config type under ``TYPE_CHECKING`` only, so a credential
+ check never pulls the channel's SDK.
+2. ``registry._INTERNAL``: add ``"<name>_probe"`` so channel discovery keeps
+ treating the module as internal rather than as an adapter.
+3. Adapter: ``supports_connection_test = True`` plus a thin
+ ``test_connection`` delegate that passes ``sdk_available``.
+4. ``config_meta.FIELD_HINTS``: hand-written field hints (optional — without
+ them the generic form derives fields from ``default_config()``).
+5. Frontend ``GUIDE_DEFS`` entry plus ``fields.<name>.*`` /
+ ``guides.<name>.*`` i18n keys in every locale (optional, guided setup).
+6. Tests mirroring ``test_qq_connection_test.py`` and one README sentence.
+
+Contract notes:
+
+- A 200 whose body is valid JSON but not an object (``null``, a list, a
+ string — middleboxes and proxied error pages produce these) classifies as
+ ``invalid_credentials``; the probe never raises on response shape.
+- ``detail`` echoes at most 200 characters of a rejection body, scrubbed of
+ the caller-declared secrets only.
+- Success means a truthy value under ``token_key``; the value's type is not
+ otherwise constrained (real endpoints return non-empty strings).
"""
from __future__ import annotations
@@ -89,9 +114,13 @@ async def probe_token_endpoint(
if resp.status_code == 200:
try:
- token = resp.json().get(token_key)
+ body = resp.json()
except ValueError:
- token = None
+ body = None
+ # A 200 with valid non-object JSON (null/list/string, e.g. from a
+ # middlebox) must classify, never raise: .get on a non-dict would
+ # surface as a bare 500 on the unguarded /test route.
+ token = body.get(token_key) if isinstance(body, dict) else None
if not token:
return {
"ok": False,
diff --git a/agent/tests/test_channels_config_api.py b/agent/tests/test_channels_config_api.py
index 3248f5944..4163e433b 100644
--- a/agent/tests/test_channels_config_api.py
+++ b/agent/tests/test_channels_config_api.py
@@ -724,6 +724,38 @@ def test_post_test_reports_invalid_credentials(tmp_path: Path, monkeypatch) -> N
assert path.read_bytes() == before
[email protected]("body", [b"null", b"[1, 2]", b'"oops"'])
+def test_post_test_non_object_json_200_returns_envelope_not_500(
+ tmp_path: Path, monkeypatch, body: bytes
+) -> None:
+ """A token endpoint answering 200 with non-object JSON must not 500.
+
+ Regression for the shared probe's ``.get`` on a non-dict body: the
+ ``/test`` route does not wrap ``test_connection`` in try/except, so an
+ ``AttributeError`` here surfaced as a bare 500 instead of an honest
+ ``invalid_credentials`` envelope.
+ """
+ client, path = _client(
+ tmp_path, monkeypatch, channels={"dingtalk": _dingtalk_section()}
+ )
+ before = path.read_bytes()
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(200, content=body)
+
+ requests = _inject_mock_transport(monkeypatch, handler)
+
+ response = client.post("/channels/dingtalk/test", json={})
+
+ assert response.status_code == 200
+ payload = response.json()
+ assert payload["ok"] is False
+ assert payload["code"] == "invalid_credentials"
+ assert len(requests) == 1
+ assert path.read_bytes() == before
+ assert STORED_SECRET not in response.text
+
+
def test_post_test_honors_pending_secret_clear(tmp_path: Path, monkeypatch) -> None:
client, path = _client(
tmp_path, monkeypatch, channels={"dingtalk": _dingtalk_section()}
diff --git a/agent/tests/test_qq_connection_test.py b/agent/tests/test_qq_connection_test.py
index 542bd4921..ac9a3b7c1 100644
--- a/agent/tests/test_qq_connection_test.py
+++ b/agent/tests/test_qq_connection_test.py
@@ -192,6 +192,33 @@ def test_http_200_error_body_reports_invalid_credentials(
assert SECRET not in json.dumps(result)
[email protected]("body", [b"null", b"[1, 2]", b'"oops"'])
+def test_http_200_non_object_json_body_reports_invalid_credentials(
+ tmp_path: Path,
+ monkeypatch: pytest.MonkeyPatch,
+ body: bytes,
+) -> None:
+ """A 200 with valid non-object JSON must classify, never raise.
+
+ Middleboxes and proxied error pages produce exactly such bodies; without
+ the isinstance guard in ``token_probe`` the ``.get`` raised
+ ``AttributeError``, which the unguarded ``/test`` route turned into a
+ bare 500.
+ """
+
+ def handler(request: httpx.Request) -> httpx.Response:
+ return httpx.Response(200, content=body)
+
+ _inject_mock_transport(monkeypatch, handler)
+
+ result = _run(_make_channel(tmp_path).test_connection())
+
+ assert result["ok"] is False
+ assert result["code"] == "invalid_credentials"
+ assert result["sdk_available"] is QQ_AVAILABLE
+ assert SECRET not in json.dumps(result)
+
+
@pytest.mark.parametrize(
("app_id", "secret"),
[("", ""), (APP_ID, ""), ("", SECRET)],
@@ -213,6 +240,8 @@ def test_missing_credentials_short_circuit_without_network(
assert result["ok"] is False
assert result["code"] == "invalid_credentials"
assert result["detail"] == "missing credentials"
+ # The short-circuit envelope is self-contained like every other branch.
+ assert result["sdk_available"] is QQ_AVAILABLE
# --------------------------------------------------------------------------- #