fix(tools): redact query secrets in send_message errors

This commit is contained in:
WAXLYY 2026-04-07 01:02:56 +03:00 committed by Teknium
parent f3ae2491a3
commit c1818b7e9e
3 changed files with 111 additions and 32 deletions

View file

@ -314,6 +314,29 @@ class TestSendDingtalk:
assert "error" in result
assert "DingTalk send failed" in result["error"]
def test_http_error_redacts_access_token_in_exception_text(self):
token = "supersecret-access-token-123456789"
resp = self._make_httpx_resp(status_code=401)
resp.raise_for_status = MagicMock(
side_effect=Exception(
f"POST https://oapi.dingtalk.com/robot/send?access_token={token} returned 401"
)
)
client_ctx, _ = self._make_httpx_client(resp)
with patch("httpx.AsyncClient", return_value=client_ctx):
result = asyncio.run(
_send_dingtalk(
{"webhook_url": f"https://oapi.dingtalk.com/robot/send?access_token={token}"},
"ch",
"hi",
)
)
assert "error" in result
assert token not in result["error"]
assert "access_token=***" in result["error"]
def test_missing_config(self):
with patch.dict(os.environ, {"DINGTALK_WEBHOOK_URL": ""}, clear=False):
result = asyncio.run(_send_dingtalk({}, "ch", "hi"))

View file

@ -276,6 +276,33 @@ class TestSendMessageTool:
thread_id=None,
)
def test_top_level_send_failure_redacts_query_token(self):
config, _telegram_cfg = _make_config()
leaked = "very-secret-query-token-123456"
def _raise_and_close(coro):
coro.close()
raise RuntimeError(
f"transport error: https://api.example.com/send?access_token={leaked}"
)
with patch("gateway.config.load_gateway_config", return_value=config), \
patch("tools.interrupt.is_interrupted", return_value=False), \
patch("model_tools._run_async", side_effect=_raise_and_close):
result = json.loads(
send_message_tool(
{
"action": "send",
"target": "telegram:-1001",
"message": "hello",
}
)
)
assert "error" in result
assert leaked not in result["error"]
assert "access_token=***" in result["error"]
class TestSendTelegramMediaDelivery:
def test_sends_text_then_photo_for_media_tag(self, tmp_path, monkeypatch):