AIStudio2API icon indicating copy to clipboard operation
AIStudio2API copied to clipboard

[Security/Bug] connectMCP lacks SSRF/IP validation and leaks http.Transport connections, while resolveMCPTools clobbers ServerSideInvocations for google_search

Open photobrand opened this issue 1 week ago • 1 comments

发生了什么 / What happened

Summary

internal/api/mcp_tools.go has three concrete bugs still present in v0.3.14 (ae59eba):

  1. Unvalidated SSRF with attacker-controlled headers (lines 163-190): Unlike internal/api/remote_media.go (which blocks loopback, private, link-local, and multicast IPs via checkRemoteMediaHost / checkRemoteMediaAddress), connectMCP accepts arbitrary server.URL and server.Headers from public API requests (/v1/responses, /v1/messages) and dials directly using a cloned http.DefaultTransport with no scheme/IP validation and no HTTP client timeout. When ADMIN_AUTH_ENABLED=false (default local mode), a caller can pass "server_url": "http://127.0.0.1:<port>/api/control/stop" (or http://169.254.169.254/...), causing AIStudio2API to issue loopback POST requests with no Origin/Sec-Fetch-Site headers (bypassing loopbackMiddleware + sameOriginMiddleware) and reflect connection/HTTP errors back in the 400 response.
  2. Leaked http.Transport idle connections (line 164): Every listMCPTools / callMCPTool clones a brand-new http.DefaultTransport and never calls base.CloseIdleConnections() when session.Close() runs.
  3. resolveMCPTools overwrites ServerSideInvocations (lines 157-159):
    request.Tools.MCPServers = servers
    request.Tools.ToolConfig.ServerSideInvocations = len(servers) > 0
    
    If a request enables both web_search (google_search) and an MCP server whose read_only / disabled_tools filter leaves len(servers) == 0, line 158 overwrites ServerSideInvocations from true to false, silently dropping google_search server-side tool invocations/results.

Suggested Fix

  1. Reuse the remote_media.go SSRF guard (http/https only, checkRemoteMediaHost, DialContext.Control / CheckRedirect, bounded timeout) when dialing MCP servers directly without EgressProxy, and call base.CloseIdleConnections() when closing the MCP session.
  2. In resolveMCPTools, preserve ServerSideInvocations when google_search (or GoogleSearch / FileSearch) is enabled.

版本 / Version

v0.3.14

photobrand avatar Nov 22 '26 15:11 photobrand

每人最多同时打开 3 个 Issue,这个 Issue 已自动关闭。请等已有的 Issue 处理完再提,或把内容补充到已有的 Issue 里。

Each user can have at most 3 open issues at a time, so this one was closed automatically. Please wait until your existing issues are resolved, or add this to one of them.

github-actions[bot] avatar Nov 22 '26 15:11 github-actions[bot]