mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-20 07:00:58 +08:00
fix(mcphttp): cap JSON-RPC response bodies
Streamable HTTP decoded the whole response with no byte limit, so a remote MCP server could force an unbounded allocation.
This commit is contained in:
@@ -49,6 +49,7 @@ All notable changes to this project are documented in this file. The format is b
|
||||
|
||||
### Fixed
|
||||
|
||||
- **Remote MCP response bound**: Streamable HTTP JSON-RPC bodies and SSE data payloads are refused above 8 MiB, so a remote MCP server cannot force an unbounded decode.
|
||||
- **LLM response capture bound**: each recorded provider attempt keeps at most 1 MiB, and a call keeps at most 8 attempts, so a large or retrying stream cannot grow the recorder without bound. The live stream is unchanged.
|
||||
- **Intercept audit ring**: `preToolUse` records allow only after intercept and the target gate decide, so a blocked call no longer leaves a leftover allow row in `Guard.Audit()`.
|
||||
- **DSN password redaction**: `config.Redact` now masks libpq `password=` keywords and `?password=` query values as well as URL userinfo, so a ping-failure log cannot print the database password.
|
||||
|
||||
+27
-3
@@ -31,6 +31,10 @@ import (
|
||||
const protocolVersion = "2025-11-25"
|
||||
const legacySSEProtocolVersion = "2024-11-05"
|
||||
|
||||
// mcpMaxRPCBytes is the largest JSON-RPC response (JSON body or SSE data
|
||||
// payload) the client will buffer. A remote MCP server is untrusted.
|
||||
const mcpMaxRPCBytes = 8 << 20
|
||||
|
||||
// ToolName mirrors mcp.ToolName so remote tools share the mcp__server__tool scheme.
|
||||
func ToolName(server, name string) string { return "mcp__" + server + "__" + name }
|
||||
|
||||
@@ -352,10 +356,14 @@ func (c *Client) wrap(rt remoteTool) actool.CoreTool {
|
||||
if err := json.Unmarshal(raw, &res); err != nil {
|
||||
return actool.Errorf("Error: bad MCP response: " + err.Error()), nil
|
||||
}
|
||||
var text string
|
||||
var b strings.Builder
|
||||
for _, blk := range res.Content {
|
||||
text += blk.Text
|
||||
b.WriteString(blk.Text)
|
||||
if b.Len() > mcpMaxRPCBytes {
|
||||
break
|
||||
}
|
||||
}
|
||||
text := b.String()
|
||||
// Consistent with built-in/custom tools: oversized output goes through Capture —
|
||||
// truncated at the session's MaxOutputChars (default 30000), and when
|
||||
// ToolOutputDir is configured the full output spills to disk leaving only the
|
||||
@@ -510,8 +518,21 @@ func (c *Client) roundTrip(ctx context.Context, body rpcRequest, expectResp bool
|
||||
if strings.HasPrefix(resp.Header.Get("Content-Type"), "text/event-stream") {
|
||||
return parseSSE(resp.Body, body.ID)
|
||||
}
|
||||
return decodeRPC(resp.Body)
|
||||
}
|
||||
|
||||
// decodeRPC reads one JSON-RPC response, refusing a body larger than
|
||||
// mcpMaxRPCBytes so a remote server cannot force an unbounded allocation.
|
||||
func decodeRPC(r io.Reader) (*rpcResponse, error) {
|
||||
data, err := io.ReadAll(io.LimitReader(r, mcpMaxRPCBytes+1))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("mcp: read json response: %w", err)
|
||||
}
|
||||
if len(data) > mcpMaxRPCBytes {
|
||||
return nil, fmt.Errorf("mcp: json response exceeds %d bytes", mcpMaxRPCBytes)
|
||||
}
|
||||
var out rpcResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
|
||||
if err := json.Unmarshal(data, &out); err != nil {
|
||||
return nil, fmt.Errorf("mcp: decode json response: %w", err)
|
||||
}
|
||||
return &out, nil
|
||||
@@ -602,6 +623,9 @@ func parseSSE(r io.Reader, wantID int) (*rpcResponse, error) {
|
||||
}
|
||||
if strings.HasPrefix(line, "data:") {
|
||||
dataBuf.WriteString(strings.TrimSpace(line[len("data:"):]))
|
||||
if dataBuf.Len() > mcpMaxRPCBytes {
|
||||
return nil, fmt.Errorf("mcp: sse payload exceeds %d bytes", mcpMaxRPCBytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
if resp, ok := flush(); ok {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -233,3 +234,28 @@ func TestStreamableClientVersionRejectionForms(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeRPCCapsBody(t *testing.T) {
|
||||
ok := `{"jsonrpc":"2.0","id":1,"result":{}}`
|
||||
got, err := decodeRPC(strings.NewReader(ok))
|
||||
if err != nil || got == nil || got.ID != 1 {
|
||||
t.Fatalf("small body: got=%+v err=%v", got, err)
|
||||
}
|
||||
if _, err := decodeRPC(strings.NewReader(strings.Repeat("x", mcpMaxRPCBytes+1))); err == nil {
|
||||
t.Fatal("oversize body must fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseSSECapsPayload(t *testing.T) {
|
||||
chunk := strings.Repeat("a", 64*1024)
|
||||
var b strings.Builder
|
||||
for b.Len() <= mcpMaxRPCBytes {
|
||||
b.WriteString("data: ")
|
||||
b.WriteString(chunk)
|
||||
b.WriteByte('\n')
|
||||
}
|
||||
b.WriteByte('\n')
|
||||
if _, err := parseSSE(strings.NewReader(b.String()), 1); err == nil {
|
||||
t.Fatal("oversize SSE payload must fail")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user