fix(mcp): redact credentials from URLs before logging

MCP server URLs can carry secrets in userinfo
(`https://user:token@host/sse`) or a query string (`?token=...`). A few
connect/validate paths logged the raw `cfg.url` / `request.url`, so those
secrets could land in log files that are often shared or aggregated.

Add a small `_redact_url()` helper that keeps only scheme/host/port/path
and use it at the four sites that log a server or request URL. Logging
only; no other behavior changes.
This commit is contained in:
xiaweiwei67-stack
2026-06-30 22:43:12 +08:00
committed by Xubin Ren
parent 1873e948c3
commit 780093d037
2 changed files with 32 additions and 4 deletions
+12
View File
@@ -1240,3 +1240,15 @@ async def test_connect_mcp_servers_enabled_tools_matches_sanitized_name(
await stack.aclose()
assert registry.tool_names == ["mcp_test_My_Tool"]
@pytest.mark.parametrize(
"url, expected",
[
("https://user:secret@host.example/sse", "https://host.example/sse"),
("https://host.example:8443/mcp?token=abc#frag", "https://host.example:8443/mcp"),
("https://host.example/sse", "https://host.example/sse"),
],
)
def test_redact_url_strips_credentials_and_query(url: str, expected: str) -> None:
assert mcp_mod._redact_url(url) == expected