From f9b02496c8233b547bdea018184f4ee66eb4dbd7 Mon Sep 17 00:00:00 2001 From: Xubin Ren <52506698+Re-bin@users.noreply.github.com> Date: Tue, 30 Jun 2026 17:13:24 +0800 Subject: [PATCH] fix(mcp): redact URL paths in logs --- nanobot/agent/tools/mcp.py | 6 ++++-- tests/tools/test_mcp_tool.py | 10 ++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/nanobot/agent/tools/mcp.py b/nanobot/agent/tools/mcp.py index 28219892..11fe1243 100644 --- a/nanobot/agent/tools/mcp.py +++ b/nanobot/agent/tools/mcp.py @@ -170,7 +170,8 @@ def _redact_url(url: str) -> str: """Strip credentials and query/fragment before logging an MCP URL. Server URLs may embed secrets (``https://user:token@host/sse`` or a - ``?token=`` query); only scheme, host, port, and path are safe to log. + ``?token=`` query). Some deployments also put opaque tokens in the path, so + log only the origin and a path placeholder. """ try: parts = urllib.parse.urlsplit(url) @@ -178,7 +179,8 @@ def _redact_url(url: str) -> str: netloc = f"[{hostname}]" if ":" in hostname else hostname if parts.port: netloc = f"{netloc}:{parts.port}" - return urllib.parse.urlunsplit((parts.scheme, netloc, parts.path, "", "")) + path = "/..." if parts.path and parts.path != "/" else parts.path + return urllib.parse.urlunsplit((parts.scheme, netloc, path, "", "")) except Exception: return "" diff --git a/tests/tools/test_mcp_tool.py b/tests/tools/test_mcp_tool.py index 81676f18..acbb673f 100644 --- a/tests/tools/test_mcp_tool.py +++ b/tests/tools/test_mcp_tool.py @@ -1245,10 +1245,12 @@ async def test_connect_mcp_servers_enabled_tools_matches_sanitized_name( @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://user:secret@[::1]:8443/sse?token=abc", "https://[::1]:8443/sse"), - ("https://host.example/sse", "https://host.example/sse"), + ("https://user:secret@host.example/sse", "https://host.example/..."), + ("https://host.example:8443/mcp?token=abc#frag", "https://host.example:8443/..."), + ("https://user:secret@[::1]:8443/sse?token=abc", "https://[::1]:8443/..."), + ("https://host.example/sse", "https://host.example/..."), + ("https://host.example", "https://host.example"), + ("https://host.example/", "https://host.example/"), ], ) def test_redact_url_strips_credentials_and_query(url: str, expected: str) -> None: