From a68fa3d6bd1d1ed25d8b73ffacd432cbbe9a2352 Mon Sep 17 00:00:00 2001 From: Algis Dumbris Date: Mon, 31 Aug 2026 17:55:43 +0300 Subject: [PATCH 1/3] fix(server): record redacted mutation payloads and the target server in activity (#1146) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The activity records for the two mutating built-in tools carried only {operation, name}. Every payload field — env_json, headers_json, oauth_json, isolation_json, args_json, command, url, protocol, enabled, trust_mode, init_timeout, registry, id — was dropped, so the audit trail could say that a server was patched but never what changed. The read-only read_cache tool logged more than either mutator. On top of that all 18 emit sites passed targetServer="" and EmitActivityInternalToolCall omits an empty target_server, so the Activity Log Server column rendered "-" and `activity list --server ` missed every mutation. Record the FULL argument set instead of a hand-maintained allowlist (the allowlist is what rotted into this bug), redacted through the existing internal/oauth helpers, and thread the resolved server name through every emit site. update/patch additionally record the resolved config diff: patch semantics (deep merge, `null` means remove) make what was requested genuinely different from what changed. Redaction policy: redacted-but-PRESENT. oauth.RedactEnvValues / RedactStringHeaders / RedactURLQueryParams mask values with `•••• ( chars)`, so the operator sees THAT API_KEY changed and which credential it was, while ${keyring:…}/${env:…} references and ordinary config (protocol, LOG_LEVEL, image python:3.12) stay readable. The walk is structure-aware: marshalling to JSON and running the regex scrubber over it rewrites across the closing quote and yields invalid JSON. Each string leaf is capped at 512 bytes so one mutation carrying a large env map cannot bloat the store. config.RevealSecretHeaders is deliberately NOT honoured here — the activity store is persisted, CLI-exported and SSE-broadcast, a different durability class from the live `list` projection it governs. Also closes two pre-existing leaks found while wiring this up: config.FieldChange carries raw before/after VALUES, and both handleUpdateUpstream and handlePatchUpstream put configDiff.Modified straight into responseMap["changes"] and into a zap.Any audit line — so a patch echoed another server's env values, Authorization header and oauth.client_secret back to the calling agent, into main.log, into BBolt and over SSE. Both now render through the shared redactedConfigDiff. Field paths and the key set are preserved, which is what agents branch on (TestE2E_PatchPreservesOAuthConfig stays green); only values are masked. handleUpdateUpstream / handlePatchUpstream now return the resolved *config.ConfigDiff; both are unexported with a single caller each. --- internal/server/mcp.go | 170 ++++++---- internal/server/mcp_activity_args.go | 227 +++++++++++++ internal/server/mcp_activity_args_test.go | 390 ++++++++++++++++++++++ 3 files changed, 713 insertions(+), 74 deletions(-) create mode 100644 internal/server/mcp_activity_args.go create mode 100644 internal/server/mcp_activity_args_test.go diff --git a/internal/server/mcp.go b/internal/server/mcp.go index 7d4839823..69100252d 100644 --- a/internal/server/mcp.go +++ b/internal/server/mcp.go @@ -3260,30 +3260,33 @@ func (p *MCPProxyServer) handleUpstreamServers(ctx context.Context, request mcp. } requestID := mintCorrelationID("upstream_servers") + // Issue #1146: the resolved server name must reach every emit site, or the + // Activity Log Server column renders "-" and --server filtering misses the + // mutation entirely. Hoisted above the first emit so even a malformed + // request is attributable. + targetServer := activityTargetServer(request) + operation, err := request.RequireString("operation") if err != nil { - p.emitActivityInternalToolCall("upstream_servers", "", "", "", sessionID, requestID, "error", err.Error(), time.Since(startTime).Milliseconds(), nil, nil, nil, "") + p.emitActivityInternalToolCall("upstream_servers", targetServer, "", "", sessionID, requestID, "error", err.Error(), time.Since(startTime).Milliseconds(), activityArgsFromRequest(request), nil, nil, "") return mcp.NewToolResultError(fmt.Sprintf("Missing required parameter 'operation': %v", err)), nil } - // Build arguments map for activity logging (Spec 024) - args := map[string]interface{}{ - "operation": operation, - } - if name := request.GetString("name", ""); name != "" { - args["name"] = name - } + // Build arguments map for activity logging (Spec 024). Issue #1146: record + // the FULL redacted argument set, not just {operation, name} — see + // mcp_activity_args.go for the redaction policy. + args := activityArgsFromRequest(request) // Security checks if p.config.ReadOnlyMode { if operation != operationList { - p.emitActivityInternalToolCall("upstream_servers", "", "", "", sessionID, requestID, "error", "Operation not allowed in read-only mode", time.Since(startTime).Milliseconds(), args, nil, nil, "") + p.emitActivityInternalToolCall("upstream_servers", targetServer, "", "", sessionID, requestID, "error", "Operation not allowed in read-only mode", time.Since(startTime).Milliseconds(), args, nil, nil, "") return mcp.NewToolResultError("Operation not allowed in read-only mode"), nil } } if p.config.DisableManagement { - p.emitActivityInternalToolCall("upstream_servers", "", "", "", sessionID, requestID, "error", "Server management is disabled for security", time.Since(startTime).Milliseconds(), args, nil, nil, "") + p.emitActivityInternalToolCall("upstream_servers", targetServer, "", "", sessionID, requestID, "error", "Server management is disabled for security", time.Since(startTime).Milliseconds(), args, nil, nil, "") return mcp.NewToolResultError("Server management is disabled for security"), nil } @@ -3294,12 +3297,12 @@ func (p *MCPProxyServer) handleUpstreamServers(ctx context.Context, request mcp. // must honor the same AllowServerAdd gate — otherwise the "Let agents add // servers" setting is bypassable by registry reference (MCP-800 finding 1). if !p.config.AllowServerAdd { - p.emitActivityInternalToolCall("upstream_servers", "", "", "", sessionID, requestID, "error", "Adding servers is not allowed", time.Since(startTime).Milliseconds(), args, nil, nil, "") + p.emitActivityInternalToolCall("upstream_servers", targetServer, "", "", sessionID, requestID, "error", "Adding servers is not allowed", time.Since(startTime).Milliseconds(), args, nil, nil, "") return mcp.NewToolResultError("Adding servers is not allowed"), nil } case operationRemove: if !p.config.AllowServerRemove { - p.emitActivityInternalToolCall("upstream_servers", "", "", "", sessionID, requestID, "error", "Removing servers is not allowed", time.Since(startTime).Milliseconds(), args, nil, nil, "") + p.emitActivityInternalToolCall("upstream_servers", targetServer, "", "", sessionID, requestID, "error", "Removing servers is not allowed", time.Since(startTime).Milliseconds(), args, nil, nil, "") return mcp.NewToolResultError("Removing servers is not allowed"), nil } } @@ -3310,13 +3313,17 @@ func (p *MCPProxyServer) handleUpstreamServers(ctx context.Context, request mcp. // /api/v1/servers handlers, so the two can never drift (issues #877/#878). if authCtx := auth.AuthContextFromContext(ctx); !auth.AuthorizeServerOp(authCtx, operation) { errMsg := fmt.Sprintf("Agent tokens cannot perform '%s' operations on upstream servers", operation) - p.emitActivityInternalToolCall("upstream_servers", "", "", "", sessionID, requestID, "error", errMsg, time.Since(startTime).Milliseconds(), args, nil, nil, "") + p.emitActivityInternalToolCall("upstream_servers", targetServer, "", "", sessionID, requestID, "error", errMsg, time.Since(startTime).Milliseconds(), args, nil, nil, "") return mcp.NewToolResultError(errMsg), nil } // Execute operation and track result var result *mcp.CallToolResult var opErr error + // Issue #1146: update/patch resolve a before/after diff that the raw + // request cannot express (deep merge, `null` means remove). Capture it so + // the activity record says what actually CHANGED, not just what was asked. + var configDiff *config.ConfigDiff switch operation { case operationList: @@ -3326,9 +3333,9 @@ func (p *MCPProxyServer) handleUpstreamServers(ctx context.Context, request mcp. case operationRemove: result, opErr = p.handleRemoveUpstream(ctx, request) case "update": - result, opErr = p.handleUpdateUpstream(ctx, request) + result, configDiff, opErr = p.handleUpdateUpstream(ctx, request) case "patch": - result, opErr = p.handlePatchUpstream(ctx, request) + result, configDiff, opErr = p.handlePatchUpstream(ctx, request) case "tail_log": result, opErr = p.handleTailLog(ctx, request) case "enable": @@ -3342,7 +3349,7 @@ func (p *MCPProxyServer) handleUpstreamServers(ctx context.Context, request mcp. case "add_from_registry": result, opErr = p.handleAddServerFromRegistry(ctx, request) default: - p.emitActivityInternalToolCall("upstream_servers", "", "", "", sessionID, requestID, "error", fmt.Sprintf("Unknown operation: %s", operation), time.Since(startTime).Milliseconds(), args, nil, nil, "") + p.emitActivityInternalToolCall("upstream_servers", targetServer, "", "", sessionID, requestID, "error", fmt.Sprintf("Unknown operation: %s", operation), time.Since(startTime).Milliseconds(), args, nil, nil, "") return mcp.NewToolResultError(fmt.Sprintf("Unknown operation: %s", operation)), nil } @@ -3354,18 +3361,30 @@ func (p *MCPProxyServer) handleUpstreamServers(ctx context.Context, request mcp. } } + // Issue #1146: record the resolved diff alongside the raw request, and + // attribute add_from_registry rows to the name the registry resolved. + if redacted := redactedConfigDiff(configDiff); redacted != nil { + if args == nil { + args = make(map[string]interface{}, 1) + } + args["config_diff"] = redacted + } + if targetServer == "" && operation == "add_from_registry" { + targetServer = serverNameFromRegistryResult(responseText) + } + // Spec 024: Emit activity event based on result with args and response if opErr != nil { - p.emitActivityInternalToolCall("upstream_servers", "", "", "", sessionID, requestID, "error", opErr.Error(), time.Since(startTime).Milliseconds(), args, nil, nil, "") + p.emitActivityInternalToolCall("upstream_servers", targetServer, "", "", sessionID, requestID, "error", opErr.Error(), time.Since(startTime).Milliseconds(), args, nil, nil, "") } else if result != nil && result.IsError { // Extract error message from result if available errMsg := "operation failed" if responseText != "" { errMsg = responseText } - p.emitActivityInternalToolCall("upstream_servers", "", "", "", sessionID, requestID, "error", errMsg, time.Since(startTime).Milliseconds(), args, nil, nil, "") + p.emitActivityInternalToolCall("upstream_servers", targetServer, "", "", sessionID, requestID, "error", errMsg, time.Since(startTime).Milliseconds(), args, nil, nil, "") } else { - p.emitActivityInternalToolCall("upstream_servers", "", "", "", sessionID, requestID, "success", "", time.Since(startTime).Milliseconds(), args, responseText, nil, "") + p.emitActivityInternalToolCall("upstream_servers", targetServer, "", "", sessionID, requestID, "success", "", time.Since(startTime).Milliseconds(), args, responseText, nil, "") } return result, opErr @@ -3384,35 +3403,36 @@ func (p *MCPProxyServer) handleQuarantineSecurity(ctx context.Context, request m } requestID := mintCorrelationID("quarantine_security") + // Issue #1146: see handleUpstreamServers — the resolved server name must + // reach every emit site, including the pre-gate denials. + targetServer := activityTargetServer(request) + operation, err := request.RequireString("operation") if err != nil { - p.emitActivityInternalToolCall("quarantine_security", "", "", "", sessionID, requestID, "error", err.Error(), time.Since(startTime).Milliseconds(), nil, nil, nil, "") + p.emitActivityInternalToolCall("quarantine_security", targetServer, "", "", sessionID, requestID, "error", err.Error(), time.Since(startTime).Milliseconds(), activityArgsFromRequest(request), nil, nil, "") return mcp.NewToolResultError(fmt.Sprintf("Missing required parameter 'operation': %v", err)), nil } - // Build arguments map for activity logging (Spec 024) - args := map[string]interface{}{ - "operation": operation, - } - if name := request.GetString("name", ""); name != "" { - args["name"] = name - } + // Build arguments map for activity logging (Spec 024). Issue #1146: the full + // redacted argument set — this also picks up prompt_name, which the old + // hand-rolled builder never recorded. + args := activityArgsFromRequest(request) // Spec 028: Agent tokens cannot perform quarantine operations if authCtx := auth.AuthContextFromContext(ctx); authCtx != nil && !authCtx.IsAdmin() { errMsg := "Agent tokens cannot perform quarantine security operations" - p.emitActivityInternalToolCall("quarantine_security", "", "", "", sessionID, requestID, "error", errMsg, time.Since(startTime).Milliseconds(), args, nil, nil, "") + p.emitActivityInternalToolCall("quarantine_security", targetServer, "", "", sessionID, requestID, "error", errMsg, time.Since(startTime).Milliseconds(), args, nil, nil, "") return mcp.NewToolResultError(errMsg), nil } // Security checks if p.config.ReadOnlyMode { - p.emitActivityInternalToolCall("quarantine_security", "", "", "", sessionID, requestID, "error", "Quarantine operations not allowed in read-only mode", time.Since(startTime).Milliseconds(), args, nil, nil, "") + p.emitActivityInternalToolCall("quarantine_security", targetServer, "", "", sessionID, requestID, "error", "Quarantine operations not allowed in read-only mode", time.Since(startTime).Milliseconds(), args, nil, nil, "") return mcp.NewToolResultError("Quarantine operations not allowed in read-only mode"), nil } if p.config.DisableManagement { - p.emitActivityInternalToolCall("quarantine_security", "", "", "", sessionID, requestID, "error", "Server management is disabled for security", time.Since(startTime).Milliseconds(), args, nil, nil, "") + p.emitActivityInternalToolCall("quarantine_security", targetServer, "", "", sessionID, requestID, "error", "Server management is disabled for security", time.Since(startTime).Milliseconds(), args, nil, nil, "") return mcp.NewToolResultError("Server management is disabled for security"), nil } @@ -3420,10 +3440,6 @@ func (p *MCPProxyServer) handleQuarantineSecurity(ctx context.Context, request m var result *mcp.CallToolResult var opErr error - if toolNameArg := request.GetString("tool_name", ""); toolNameArg != "" { - args["tool_name"] = toolNameArg - } - switch operation { case "list_quarantined": result, opErr = p.handleListQuarantinedUpstreams(ctx) @@ -3458,7 +3474,7 @@ func (p *MCPProxyServer) handleQuarantineSecurity(ctx context.Context, request m case "approve_all_prompts": result, opErr = p.handleApproveAllPromptsByServer(request) default: - p.emitActivityInternalToolCall("quarantine_security", "", "", "", sessionID, requestID, "error", fmt.Sprintf("Unknown quarantine operation: %s", operation), time.Since(startTime).Milliseconds(), args, nil, nil, "") + p.emitActivityInternalToolCall("quarantine_security", targetServer, "", "", sessionID, requestID, "error", fmt.Sprintf("Unknown quarantine operation: %s", operation), time.Since(startTime).Milliseconds(), args, nil, nil, "") return mcp.NewToolResultError(fmt.Sprintf("Unknown quarantine operation: %s", operation)), nil } @@ -3472,16 +3488,16 @@ func (p *MCPProxyServer) handleQuarantineSecurity(ctx context.Context, request m // Spec 024: Emit activity event based on result with args and response if opErr != nil { - p.emitActivityInternalToolCall("quarantine_security", "", "", "", sessionID, requestID, "error", opErr.Error(), time.Since(startTime).Milliseconds(), args, nil, nil, "") + p.emitActivityInternalToolCall("quarantine_security", targetServer, "", "", sessionID, requestID, "error", opErr.Error(), time.Since(startTime).Milliseconds(), args, nil, nil, "") } else if result != nil && result.IsError { // Extract error message from result if available errMsg := "operation failed" if responseText != "" { errMsg = responseText } - p.emitActivityInternalToolCall("quarantine_security", "", "", "", sessionID, requestID, "error", errMsg, time.Since(startTime).Milliseconds(), args, nil, nil, "") + p.emitActivityInternalToolCall("quarantine_security", targetServer, "", "", sessionID, requestID, "error", errMsg, time.Since(startTime).Milliseconds(), args, nil, nil, "") } else { - p.emitActivityInternalToolCall("quarantine_security", "", "", "", sessionID, requestID, "success", "", time.Since(startTime).Milliseconds(), args, responseText, nil, "") + p.emitActivityInternalToolCall("quarantine_security", targetServer, "", "", sessionID, requestID, "success", "", time.Since(startTime).Milliseconds(), args, responseText, nil, "") } return result, opErr @@ -4855,16 +4871,20 @@ func (p *MCPProxyServer) handleRemoveUpstream(_ context.Context, request mcp.Cal return mcp.NewToolResultText(string(jsonResult)), nil } -func (p *MCPProxyServer) handleUpdateUpstream(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { +// handleUpdateUpstream returns the resolved config diff alongside the result so +// handleUpstreamServers can record what actually changed in the activity log +// (issue #1146). The diff is returned RAW — every rendering seam below and in +// the caller passes it through redactedConfigDiff first. +func (p *MCPProxyServer) handleUpdateUpstream(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, *config.ConfigDiff, error) { name, err := request.RequireString("name") if err != nil { - return mcp.NewToolResultError("Missing required parameter 'name'"), nil + return mcp.NewToolResultError("Missing required parameter 'name'"), nil, nil } // Find server by name first servers, err := p.storage.ListUpstreams() if err != nil { - return mcp.NewToolResultError(fmt.Sprintf("Failed to list upstreams: %v", err)), nil + return mcp.NewToolResultError(fmt.Sprintf("Failed to list upstreams: %v", err)), nil, nil } var serverID string @@ -4878,32 +4898,36 @@ func (p *MCPProxyServer) handleUpdateUpstream(ctx context.Context, request mcp.C } if serverID == "" { - return mcp.NewToolResultError(fmt.Sprintf("Server '%s' not found", name)), nil + return mcp.NewToolResultError(fmt.Sprintf("Server '%s' not found", name)), nil, nil } // Build patch config from request parameters patch, mergeOpts, err := p.buildPatchConfigFromRequest(request, existingServer) if err != nil { - return mcp.NewToolResultError(err.Error()), nil + return mcp.NewToolResultError(err.Error()), nil, nil } // Use smart merge to preserve existing config fields (Fix for #239, #240) mergedServer, configDiff, err := config.MergeServerConfig(existingServer, patch, mergeOpts) if err != nil { - return mcp.NewToolResultError(fmt.Sprintf("Failed to merge config: %v", err)), nil + return mcp.NewToolResultError(fmt.Sprintf("Failed to merge config: %v", err)), nil, nil } - // Log the config diff for audit trail (FR-006) - if configDiff != nil && !configDiff.IsEmpty() { + // Log the config diff for audit trail (FR-006). Issue #1146: config.FieldChange + // carries raw before/after VALUES, so logging Modified verbatim wrote env + // values, Authorization headers and oauth.client_secret to main.log in the + // clear. Render through the shared redactor instead. + redactedDiff := redactedConfigDiff(configDiff) + if redactedDiff != nil { p.logger.Info("Server config updated via MCP tool", zap.String("server", name), - zap.Any("modified", configDiff.Modified), + zap.Any("modified", redactedDiff["modified"]), zap.Strings("removed", configDiff.Removed)) } // Update in storage if err := p.storage.UpdateUpstream(serverID, mergedServer); err != nil { - return mcp.NewToolResultError(fmt.Sprintf("Failed to update upstream: %v", err)), nil + return mcp.NewToolResultError(fmt.Sprintf("Failed to update upstream: %v", err)), nil, nil } // Update in upstream manager with connection monitoring @@ -4942,12 +4966,10 @@ func (p *MCPProxyServer) handleUpdateUpstream(ctx context.Context, request mcp.C "connection_message": connectionMessage, } - // Include diff in response for LLM transparency (T4.3) - if configDiff != nil && !configDiff.IsEmpty() { - responseMap["changes"] = map[string]interface{}{ - "modified": configDiff.Modified, - "removed": configDiff.Removed, - } + // Include diff in response for LLM transparency (T4.3). Issue #1146: values + // are masked — field paths, which is what agents branch on, are preserved. + if redactedDiff != nil { + responseMap["changes"] = redactedDiff } if isolationManager := p.getIsolationManager(); isolationManager != nil { @@ -4958,22 +4980,24 @@ func (p *MCPProxyServer) handleUpdateUpstream(ctx context.Context, request mcp.C jsonResult, err := json.Marshal(responseMap) if err != nil { - return mcp.NewToolResultError(fmt.Sprintf("Failed to serialize result: %v", err)), nil + return mcp.NewToolResultError(fmt.Sprintf("Failed to serialize result: %v", err)), nil, nil } - return mcp.NewToolResultText(string(jsonResult)), nil + return mcp.NewToolResultText(string(jsonResult)), configDiff, nil } -func (p *MCPProxyServer) handlePatchUpstream(_ context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { +// handlePatchUpstream returns the resolved config diff alongside the result; see +// handleUpdateUpstream for why (issue #1146). +func (p *MCPProxyServer) handlePatchUpstream(_ context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, *config.ConfigDiff, error) { name, err := request.RequireString("name") if err != nil { - return mcp.NewToolResultError("Missing required parameter 'name'"), nil + return mcp.NewToolResultError("Missing required parameter 'name'"), nil, nil } // Find server by name first servers, err := p.storage.ListUpstreams() if err != nil { - return mcp.NewToolResultError(fmt.Sprintf("Failed to list upstreams: %v", err)), nil + return mcp.NewToolResultError(fmt.Sprintf("Failed to list upstreams: %v", err)), nil, nil } var serverID string @@ -4987,32 +5011,33 @@ func (p *MCPProxyServer) handlePatchUpstream(_ context.Context, request mcp.Call } if serverID == "" { - return mcp.NewToolResultError(fmt.Sprintf("Server '%s' not found", name)), nil + return mcp.NewToolResultError(fmt.Sprintf("Server '%s' not found", name)), nil, nil } // Build patch config from request parameters patch, mergeOpts, err := p.buildPatchConfigFromRequest(request, existingServer) if err != nil { - return mcp.NewToolResultError(err.Error()), nil + return mcp.NewToolResultError(err.Error()), nil, nil } // Use smart merge to preserve existing config fields (Fix for #239, #240) mergedServer, configDiff, err := config.MergeServerConfig(existingServer, patch, mergeOpts) if err != nil { - return mcp.NewToolResultError(fmt.Sprintf("Failed to merge config: %v", err)), nil + return mcp.NewToolResultError(fmt.Sprintf("Failed to merge config: %v", err)), nil, nil } - // Log the config diff for audit trail (FR-006) - if configDiff != nil && !configDiff.IsEmpty() { + // Log the config diff for audit trail (FR-006), values masked (issue #1146). + redactedDiff := redactedConfigDiff(configDiff) + if redactedDiff != nil { p.logger.Info("Server config patched via MCP tool", zap.String("server", name), - zap.Any("modified", configDiff.Modified), + zap.Any("modified", redactedDiff["modified"]), zap.Strings("removed", configDiff.Removed)) } // Update in storage if err := p.storage.UpdateUpstream(serverID, mergedServer); err != nil { - return mcp.NewToolResultError(fmt.Sprintf("Failed to update upstream: %v", err)), nil + return mcp.NewToolResultError(fmt.Sprintf("Failed to update upstream: %v", err)), nil, nil } // Update in upstream manager @@ -5040,12 +5065,9 @@ func (p *MCPProxyServer) handlePatchUpstream(_ context.Context, request mcp.Call "enabled": mergedServer.Enabled, } - // Include diff in response for LLM transparency (T4.3) - if configDiff != nil && !configDiff.IsEmpty() { - responseMap["changes"] = map[string]interface{}{ - "modified": configDiff.Modified, - "removed": configDiff.Removed, - } + // Include diff in response for LLM transparency (T4.3), values masked. + if redactedDiff != nil { + responseMap["changes"] = redactedDiff } if isolationManager := p.getIsolationManager(); isolationManager != nil { @@ -5056,10 +5078,10 @@ func (p *MCPProxyServer) handlePatchUpstream(_ context.Context, request mcp.Call jsonResult, err := json.Marshal(responseMap) if err != nil { - return mcp.NewToolResultError(fmt.Sprintf("Failed to serialize result: %v", err)), nil + return mcp.NewToolResultError(fmt.Sprintf("Failed to serialize result: %v", err)), nil, nil } - return mcp.NewToolResultText(string(jsonResult)), nil + return mcp.NewToolResultText(string(jsonResult)), configDiff, nil } // buildPatchConfigFromRequest constructs a partial ServerConfig from request parameters diff --git a/internal/server/mcp_activity_args.go b/internal/server/mcp_activity_args.go new file mode 100644 index 000000000..47b453da2 --- /dev/null +++ b/internal/server/mcp_activity_args.go @@ -0,0 +1,227 @@ +package server + +import ( + "bytes" + "encoding/json" + "strings" + + "github.com/mark3labs/mcp-go/mcp" + + "github.com/smart-mcp-proxy/mcpproxy-go/internal/config" + "github.com/smart-mcp-proxy/mcpproxy-go/internal/oauth" +) + +// Issue #1146: the activity records for the two MUTATING built-in tools +// (upstream_servers, quarantine_security) used to carry only {operation, name}. +// Every payload field — the env vars added, the URL changed, the isolation +// image swapped — was dropped, so the audit trail could say that something was +// patched but never what. This file records the FULL argument set instead, with +// redaction, plus the resolved before/after config diff. +// +// Redaction policy, and why it is unconditional: +// +// The activity store is a different durability class from the live +// upstream_servers `list` projection. Rows are persisted in BBolt, exported by +// `mcpproxy activity list`, and broadcast over SSE to every Web UI viewer, so +// `config.RevealSecretHeaders` — an opt-in flag for a live read surface — is +// deliberately NOT honoured here. Secrets are always masked. +// +// The goal is redacted-but-PRESENT: the operator must see THAT `API_KEY` +// changed, and recognise which credential it was, without the value ever +// landing in the store. oauth.MaskValue renders `•••• ( chars)`, +// matching the tray / Web UI convention, and passes ${keyring:…} / ${env:…} +// references through unchanged because those are labels, not secrets. +// +// The whole request is captured rather than an allowlist of known field names: +// an allowlist is exactly what rotted into this bug, and a future tool +// parameter must be logged by default rather than silently dropped. +const ( + // activityArgValueLimit caps each recorded string leaf so one mutation + // carrying a large env map or arg list cannot bloat the activity store. + // Mirrors activityErrorMessageLimit. + activityArgValueLimit = 512 + + // jsonParamSuffix marks the tool parameters whose value is a JSON *string* + // (env_json, headers_json, isolation_json, oauth_json, args_json). They are + // parsed before redaction so the record shows WHICH keys changed instead of + // an opaque blob. + jsonParamSuffix = "_json" +) + +// activityArgsFromRequest builds the redacted activity-argument map from the +// raw MCP request. Returns nil for an empty request so callers keep emitting +// `null` arguments exactly as before. +func activityArgsFromRequest(request mcp.CallToolRequest) map[string]interface{} { + raw := request.GetArguments() + if len(raw) == 0 { + return nil + } + + args := make(map[string]interface{}, len(raw)) + for key, value := range raw { + // The *_json parameters carry a serialized object; parse it so the + // walker can mask per key. On failure (including the literal "null" + // removal marker) fall back to scrubbing the raw string — a rejected + // mutation is still audit-relevant, so never drop it. + if s, ok := value.(string); ok && strings.HasSuffix(key, jsonParamSuffix) { + var parsed interface{} + if err := json.Unmarshal([]byte(s), &parsed); err == nil && parsed != nil { + args[key] = redactActivityValue(strings.TrimSuffix(key, jsonParamSuffix), parsed) + continue + } + } + args[key] = redactActivityValue(key, value) + } + return args +} + +// activityTargetServer resolves the server a mutation targets, so the activity +// row renders a Server column and `activity list --server ` matches it. +// runtime.EmitActivityInternalToolCall omits target_server when empty, which is +// why every site used to render "-" (issue #1146). +func activityTargetServer(request mcp.CallToolRequest) string { + return request.GetString("name", "") +} + +// serverNameFromRegistryResult lifts the persisted server name out of the +// add_from_registry success payload. That operation resolves the name from the +// registry entry, so it is knowable only after the fact. Returns "" for error +// payloads and for anything that is not the expected shape. +func serverNameFromRegistryResult(responseText string) string { + if responseText == "" { + return "" + } + var payload struct { + Success bool `json:"success"` + Server struct { + Name string `json:"name"` + } `json:"server"` + } + if err := json.Unmarshal([]byte(responseText), &payload); err != nil { + return "" + } + if !payload.Success { + return "" + } + return payload.Server.Name +} + +// redactedConfigDiff renders a *config.ConfigDiff with every field PATH +// verbatim — that is the audit signal — and every before/after VALUE masked. +// +// config.FieldChange carries raw From/To values, and MergeServerConfig +// populates Modified["env"], ["headers"] and ["oauth"] with the whole +// before/after maps in the clear (including OAuthConfig.ClientSecret). This one +// rendering is shared by the tool response, the audit log line and the activity +// record so the three can never drift. Returns nil for a nil or empty diff. +func redactedConfigDiff(diff *config.ConfigDiff) map[string]interface{} { + if diff == nil || diff.IsEmpty() { + return nil + } + + modified := make(map[string]interface{}, len(diff.Modified)) + for field, change := range diff.Modified { + modified[field] = map[string]interface{}{ + "path": change.Path, + "from": redactActivityValue(field, normalizeForRedaction(change.From)), + "to": redactActivityValue(field, normalizeForRedaction(change.To)), + } + } + + out := map[string]interface{}{ + "modified": modified, + "removed": diff.Removed, + } + if len(diff.Added) > 0 { + out["added"] = diff.Added + } + return out +} + +// normalizeForRedaction turns an arbitrary typed config value into generic JSON +// (maps / slices / json.Number / string / bool) so the walker below can inspect +// it key by key. json.Number keeps integers exact instead of degrading them to +// float64 and rendering as 1.048576e+06. On failure the value is returned +// unchanged; the walker's default branch then passes it through. +func normalizeForRedaction(v interface{}) interface{} { + if v == nil { + return nil + } + encoded, err := json.Marshal(v) + if err != nil { + return v + } + dec := json.NewDecoder(bytes.NewReader(encoded)) + dec.UseNumber() + var out interface{} + if err := dec.Decode(&out); err != nil { + return v + } + return out +} + +// redactActivityValue walks a generic JSON value, masking leaves according to +// the key that encloses them. `key` is the enclosing field name (for a slice, +// the name of the slice itself). +func redactActivityValue(key string, v interface{}) interface{} { + switch typed := v.(type) { + case map[string]interface{}: + out := make(map[string]interface{}, len(typed)) + isHeaders := isActivityHeadersKey(key) + for k, val := range typed { + if isHeaders { + if s, ok := val.(string); ok { + // HTTP header semantics: mask by header name. + out[k] = capActivityValue(oauth.RedactStringHeaders(map[string]string{k: s})[k]) + continue + } + } + out[k] = redactActivityValue(k, val) + } + return out + case []interface{}: + out := make([]interface{}, len(typed)) + for i, val := range typed { + out[i] = redactActivityValue(key, val) + } + return out + case string: + return redactActivityString(key, typed) + default: + // Bools, json.Number and nil carry no secrets and stay verbatim. + return v + } +} + +// redactActivityString masks one string leaf. The default policy is the env-var +// rule (oauth.RedactEnvValues over a single pair), which is the package's single +// source of truth for "does this key name look like it holds a secret": it masks +// sensitive-looking keys with MaskValue, passes ${keyring:…}/${env:…} references +// through, URL-redacts connection strings, and runs RedactSensitiveData over +// everything else as defence in depth. Over-masking a harmless field is the safe +// direction; ordinary configuration (protocol, command, LOG_LEVEL, an image +// named python:3.12) stays readable. +func redactActivityString(key, s string) string { + switch { + case isActivityHeadersKey(key): + return capActivityValue(oauth.RedactStringHeaders(map[string]string{key: s})[key]) + case strings.EqualFold(key, "url"): + return capActivityValue(oauth.RedactURLQueryParams(s)) + default: + return capActivityValue(oauth.RedactEnvValues(map[string]string{key: s})[key]) + } +} + +// isActivityHeadersKey reports whether a map should be masked with HTTP-header +// semantics. Covers both the `headers` config field and the `headers_json` tool +// parameter (the suffix is trimmed before the walk). +func isActivityHeadersKey(key string) bool { + return strings.EqualFold(key, "headers") +} + +func capActivityValue(s string) string { + if len(s) <= activityArgValueLimit { + return s + } + return s[:safeTruncateBytes(s, activityArgValueLimit)] + activityErrorMessageEllipsis +} diff --git a/internal/server/mcp_activity_args_test.go b/internal/server/mcp_activity_args_test.go new file mode 100644 index 000000000..c3322ff81 --- /dev/null +++ b/internal/server/mcp_activity_args_test.go @@ -0,0 +1,390 @@ +package server + +import ( + "context" + "encoding/json" + "go/ast" + "go/parser" + "go/token" + "os" + "path/filepath" + "strings" + "testing" + + "github.com/mark3labs/mcp-go/mcp" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + + "github.com/smart-mcp-proxy/mcpproxy-go/internal/config" +) + +// Issue #1146: the activity log used to record only {operation, name} for +// upstream_servers / quarantine_security, dropping every mutation payload +// field. The whole point of an audit trail is to say WHAT changed. + +const ( + testEnvSecret = "sk-live-abcdef1234567890" + testBearerSecret = "ghp_realtokenAAAAAAAAAAAAAAAAAAAAAA" + testClientSecret = "cs-supersecret-9876543210" + testURLTokenValue = "leakmeleakmeleakme" +) + +func upstreamPatchRequest() mcp.CallToolRequest { + return mcp.CallToolRequest{Params: mcp.CallToolParams{ + Name: "upstream_servers", + Arguments: map[string]interface{}{ + "operation": "patch", + "name": "github", + "url": "https://example.test/mcp?token=" + testURLTokenValue, + "protocol": "streamable-http", + "command": "npx", + "enabled": true, + "trust_mode": "manual", + "init_timeout": "30s", + "args_json": `["-y","@modelcontextprotocol/server-github"]`, + "env_json": `{"API_KEY":"` + testEnvSecret + `","LOG_LEVEL":"debug"}`, + "headers_json": `{"Authorization":"Bearer ` + testBearerSecret + `","Accept":"application/json"}`, + "isolation_json": `{"enabled":true,"image":"python:3.12","memory_limit":"1g"}`, + "oauth_json": `{"client_id":"public-client","client_secret":"` + testClientSecret + `","scopes":["read"]}`, + }, + }} +} + +// (a) Every supplied parameter must survive into the activity arguments. +func TestActivityArgs_UpstreamPatch_RecordsMutatedFieldNames(t *testing.T) { + req := upstreamPatchRequest() + args := activityArgsFromRequest(req) + require.NotNil(t, args) + + for key := range req.GetArguments() { + assert.Containsf(t, args, key, + "parameter %q was dropped from the activity record (issue #1146)", key) + } +} + +// (b) A secret-bearing value must be PRESENT but REDACTED — never absent, never +// in the clear. The operator has to see THAT API_KEY changed. +func TestActivityArgs_SecretsArePresentButRedacted(t *testing.T) { + args := activityArgsFromRequest(upstreamPatchRequest()) + + env, ok := args["env_json"].(map[string]interface{}) + require.Truef(t, ok, "env_json must be recorded as a parsed map, got %T", args["env_json"]) + + apiKey, ok := env["API_KEY"].(string) + require.True(t, ok, "the API_KEY name must survive so the operator sees which var changed") + assert.NotEmpty(t, apiKey) + assert.NotEqual(t, testEnvSecret, apiKey, "the raw secret must not be recorded") + assert.True(t, strings.HasPrefix(apiKey, "••••"), "expected a masked value, got %q", apiKey) + + assert.Equal(t, "debug", env["LOG_LEVEL"], "ordinary env values must stay readable") +} + +// (b') A single net that catches any field the walker forgets, present or +// future: no raw secret may survive anywhere in the recorded arguments. +func TestActivityArgs_NoSecretSurvivesAnywhere(t *testing.T) { + args := activityArgsFromRequest(upstreamPatchRequest()) + encoded, err := json.Marshal(args) + require.NoError(t, err) + rendered := string(encoded) + + for _, secret := range []string{testEnvSecret, testBearerSecret, testClientSecret, testURLTokenValue} { + assert.NotContainsf(t, rendered, secret, + "a raw secret leaked into the activity arguments: %s", rendered) + } +} + +// (b”) Header names and URL structure stay readable; only the credential is +// masked. +func TestActivityArgs_HeadersAndURLRedacted(t *testing.T) { + args := activityArgsFromRequest(upstreamPatchRequest()) + + headers, ok := args["headers_json"].(map[string]interface{}) + require.Truef(t, ok, "headers_json must be recorded as a parsed map, got %T", args["headers_json"]) + authz, ok := headers["Authorization"].(string) + require.True(t, ok, "the Authorization header NAME must survive") + assert.NotContains(t, authz, testBearerSecret) + assert.Equal(t, "application/json", headers["Accept"], "ordinary headers stay readable") + + rawURL, ok := args["url"].(string) + require.True(t, ok) + assert.Contains(t, rawURL, "https://example.test/mcp", "scheme/host/path must stay readable") + assert.NotContains(t, rawURL, testURLTokenValue) +} + +// (b”') ${keyring:...} references are labels, not secrets — masking them would +// destroy the very information the operator needs. +func TestActivityArgs_KeyringReferencePassesThrough(t *testing.T) { + req := mcp.CallToolRequest{Params: mcp.CallToolParams{ + Name: "upstream_servers", + Arguments: map[string]interface{}{ + "operation": "patch", + "name": "github", + "env_json": `{"API_KEY":"${keyring:gh}"}`, + }, + }} + args := activityArgsFromRequest(req) + env, ok := args["env_json"].(map[string]interface{}) + require.True(t, ok) + assert.Equal(t, "${keyring:gh}", env["API_KEY"]) +} + +// (b””) A malformed *_json parameter must still be recorded (scrubbed and +// capped), not silently dropped — a rejected mutation is audit-relevant too. +func TestActivityArgs_MalformedJSONParamIsScrubbedNotDropped(t *testing.T) { + req := mcp.CallToolRequest{Params: mcp.CallToolParams{ + Name: "upstream_servers", + Arguments: map[string]interface{}{ + "operation": "patch", + "name": "github", + "env_json": `{not json`, + }, + }} + args := activityArgsFromRequest(req) + require.Contains(t, args, "env_json") + assert.Equal(t, `{not json`, args["env_json"]) +} + +// Over-masking is the safe direction, but the log must stay readable: ordinary +// configuration fields must not be turned into bullets. +func TestActivityArgs_OrdinaryFieldsStayReadable(t *testing.T) { + args := activityArgsFromRequest(upstreamPatchRequest()) + + assert.Equal(t, "patch", args["operation"]) + assert.Equal(t, "github", args["name"]) + assert.Equal(t, "npx", args["command"]) + assert.Equal(t, "streamable-http", args["protocol"]) + assert.Equal(t, true, args["enabled"]) + + isolation, ok := args["isolation_json"].(map[string]interface{}) + require.True(t, ok) + assert.Equal(t, "python:3.12", isolation["image"]) + assert.Equal(t, true, isolation["enabled"]) + + argsList, ok := args["args_json"].([]interface{}) + require.True(t, ok) + assert.Equal(t, []interface{}{"-y", "@modelcontextprotocol/server-github"}, argsList) +} + +// A key literally named api_key pins the structure-aware walk: the naive +// marshal → RedactSensitiveData → unmarshal shortcut rewrites across the JSON +// closing quote and yields invalid JSON. +func TestActivityArgs_LowercaseSecretKeyStaysStructured(t *testing.T) { + req := mcp.CallToolRequest{Params: mcp.CallToolParams{ + Name: "upstream_servers", + Arguments: map[string]interface{}{ + "operation": "patch", + "name": "github", + "env_json": `{"api_key":"` + testEnvSecret + `"}`, + }, + }} + args := activityArgsFromRequest(req) + env, ok := args["env_json"].(map[string]interface{}) + require.Truef(t, ok, "env_json must stay a structured map, got %T", args["env_json"]) + require.Contains(t, env, "api_key") + assert.NotContains(t, env["api_key"], testEnvSecret) +} + +// A very long value must be capped so one mutation cannot bloat the store. +func TestActivityArgs_LongValuesAreCapped(t *testing.T) { + long := strings.Repeat("é", activityArgValueLimit) + req := mcp.CallToolRequest{Params: mcp.CallToolParams{ + Name: "upstream_servers", + Arguments: map[string]interface{}{ + "operation": "patch", + "name": "github", + "command": long, + }, + }} + args := activityArgsFromRequest(req) + got, ok := args["command"].(string) + require.True(t, ok) + assert.LessOrEqual(t, len(got), activityArgValueLimit+len(activityErrorMessageEllipsis)) + assert.True(t, len(got) < len(long)) +} + +// (c) The resolved server name must reach the activity record so the Server +// column renders and `--server` filtering matches. +func TestActivityTargetServer_ResolvedFromNameParam(t *testing.T) { + tests := []struct { + name string + args map[string]interface{} + want string + }{ + {"upstream patch", map[string]interface{}{"operation": "patch", "name": "github"}, "github"}, + {"quarantine approve", map[string]interface{}{"operation": "approve_tool", "name": "github", "tool_name": "x"}, "github"}, + {"list has no target", map[string]interface{}{"operation": "list"}, ""}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + req := mcp.CallToolRequest{Params: mcp.CallToolParams{Arguments: tc.args}} + assert.Equal(t, tc.want, activityTargetServer(req)) + }) + } +} + +// (c”) add_from_registry resolves its server name only in the response, so +// lift it from there rather than leaving the row unattributed. +func TestActivityTargetServer_AddFromRegistryLiftsResolvedName(t *testing.T) { + assert.Equal(t, "pulse-weather", + serverNameFromRegistryResult(`{"success":true,"server":{"name":"pulse-weather"}}`)) + assert.Empty(t, serverNameFromRegistryResult(`{"success":false,"message":"nope"}`)) + assert.Empty(t, serverNameFromRegistryResult("not json at all")) + assert.Empty(t, serverNameFromRegistryResult("")) +} + +// (c') Guard against a new emit site reintroducing the "-" Server column: no +// call inside these two handlers may hardcode the targetServer argument. +func TestActivityEmitsNeverHardcodeEmptyTargetServer(t *testing.T) { + const targetServerArgIndex = 1 + guardedFuncs := map[string]bool{ + "handleUpstreamServers": true, + "handleQuarantineSecurity": true, + } + + entries, err := os.ReadDir(".") + require.NoError(t, err) + + fset := token.NewFileSet() + var offenders []string + + for _, entry := range entries { + name := entry.Name() + if entry.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") { + continue + } + file, err := parser.ParseFile(fset, filepath.Join(".", name), nil, 0) + require.NoErrorf(t, err, "parsing %s", name) + + for _, decl := range file.Decls { + fn, ok := decl.(*ast.FuncDecl) + if !ok || !guardedFuncs[fn.Name.Name] { + continue + } + ast.Inspect(fn, func(node ast.Node) bool { + call, ok := node.(*ast.CallExpr) + if !ok { + return true + } + sel, ok := call.Fun.(*ast.SelectorExpr) + if !ok || sel.Sel.Name != "emitActivityInternalToolCall" { + return true + } + if len(call.Args) <= targetServerArgIndex { + return true + } + if lit, ok := call.Args[targetServerArgIndex].(*ast.BasicLit); ok && lit.Kind == token.STRING { + offenders = append(offenders, + fset.Position(lit.Pos()).String()+" (in "+fn.Name.Name+")") + } + return true + }) + } + } + + require.Emptyf(t, offenders, + "these emit sites hardcode the target server, so the Activity Log Server "+ + "column renders '-' and --server filtering misses the row (issue "+ + "#1146); pass the resolved server name instead:\n%s", + strings.Join(offenders, "\n")) +} + +// (diff) The resolved before/after diff must keep field PATHS verbatim (that is +// the audit signal) while masking every value. +func TestRedactedConfigDiff_KeepsPathsMasksValues(t *testing.T) { + diff := config.NewConfigDiff() + diff.Modified["env"] = config.FieldChange{ + Path: "env", + From: map[string]string{"API_KEY": "old-" + testEnvSecret}, + To: map[string]string{"API_KEY": testEnvSecret}, + } + diff.Modified["oauth"] = config.FieldChange{ + Path: "oauth", + From: nil, + To: &config.OAuthConfig{ClientID: "public-client", ClientSecret: testClientSecret}, + } + diff.Modified["url"] = config.FieldChange{ + Path: "url", + From: "https://old.test/mcp", + To: "https://new.test/mcp", + } + diff.Removed = []string{"env.OLD_KEY"} + + redacted := redactedConfigDiff(diff) + require.NotNil(t, redacted) + + modified, ok := redacted["modified"].(map[string]interface{}) + require.True(t, ok) + assert.Contains(t, modified, "env") + assert.Contains(t, modified, "oauth") + assert.Contains(t, modified, "url") + + encoded, err := json.Marshal(redacted) + require.NoError(t, err) + rendered := string(encoded) + assert.Contains(t, rendered, "env.OLD_KEY", "removed field paths are key names, keep them") + assert.Contains(t, rendered, "https://new.test/mcp", "ordinary values stay readable") + assert.NotContains(t, rendered, testEnvSecret) + assert.NotContains(t, rendered, testClientSecret) + + assert.Nil(t, redactedConfigDiff(nil)) + assert.Nil(t, redactedConfigDiff(config.NewConfigDiff())) +} + +// Numbers must render as numbers, not as 1.048576e+06. +func TestRedactedConfigDiff_NumbersRenderSanely(t *testing.T) { + diff := config.NewConfigDiff() + diff.Modified["isolation.memory_bytes"] = config.FieldChange{ + Path: "isolation.memory_bytes", + From: 0, + To: 1048576, + } + encoded, err := json.Marshal(redactedConfigDiff(diff)) + require.NoError(t, err) + assert.Contains(t, string(encoded), "1048576") + assert.NotContains(t, string(encoded), "e+06") +} + +// (regression, Finding A) The patch response echoed the whole before/after +// config diff in the clear — env values, Authorization headers and +// oauth.client_secret — straight back to the calling agent AND into the +// persisted activity `response` field. +func TestPatchResponse_DoesNotEchoSecrets(t *testing.T) { + proxy, _ := newStoredScriptProxy(t) + + seed := &config.ServerConfig{ + Name: "secretful", + URL: "https://old.test/mcp", + Protocol: "streamable-http", + Enabled: false, + Env: map[string]string{"API_KEY": "old-" + testEnvSecret}, + Headers: map[string]string{"Authorization": "Bearer " + testBearerSecret}, + } + _, err := proxy.storage.AddUpstream(seed) + require.NoError(t, err) + + req := mcp.CallToolRequest{Params: mcp.CallToolParams{ + Name: "upstream_servers", + Arguments: map[string]interface{}{ + "operation": "patch", + "name": "secretful", + "env_json": `{"API_KEY":"` + testEnvSecret + `"}`, + }, + }} + + result, _, err := proxy.handlePatchUpstream(context.Background(), req) + require.NoError(t, err) + require.NotNil(t, result) + text := resultText(t, result) + + assert.NotContains(t, text, testEnvSecret, "the patch response echoed a secret in the clear") + assert.NotContains(t, text, "old-"+testEnvSecret, "the patch response echoed the previous secret") + assert.NotContains(t, text, testBearerSecret) + + var payload map[string]interface{} + require.NoError(t, json.Unmarshal([]byte(text), &payload)) + changes, ok := payload["changes"].(map[string]interface{}) + require.Truef(t, ok, "the diff must still be reported, got %s", text) + modified, ok := changes["modified"].(map[string]interface{}) + require.True(t, ok) + assert.Contains(t, modified, "env", "the operator must still see WHICH field changed") +} From ffc35b85586e35ab4733fa8b108a5333d0ac3d2f Mon Sep 17 00:00:00 2001 From: Algis Dumbris Date: Mon, 31 Aug 2026 19:24:27 +0300 Subject: [PATCH 2/3] fix(server): mask argv secrets and stop caller-forged attribution in activity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Cross-model review of #1146 found four ways the "record the full mutation payload" change could leak or be spoofed, plus one unannounced contract change. All five are addressed here. 1. Command-line ARGUMENT VALUES were recorded verbatim. argv has no key to judge by — the slice branch recursed with the parent key ("args"), so every element fell through redactActivityString unmasked. `uvx mcp-foo --api-key sk-live-…` therefore persisted the credential in BBolt, in the SSE payload and in `mcpproxy activity list`, on the exact path #1146 set out to seal. redactActivityArgv now masks by two rules: the flag names the credential (`--api-key VALUE` / `--api-key=VALUE`, delegated to the same env-var rule via the new oauth.IsSensitiveKeyName), and the value is recognisable on its own (internal/security's detector, reused rather than re-invented). Package names, subcommands, paths and ports still round-trip verbatim. 2. targetServer was taken from the unvalidated agent-supplied `name` and hoisted above the dispatch, so operations that never receive the request (list, list_quarantined) attributed their row to a server they never touched. activityAttributedOps is now an allowlist — unknown and unparsed operations default to unattributed — pinned to both operation switches in BOTH directions by an AST test, so a new mutating operation cannot silently lose its Server column either. 3. Populating targetServer let management chatter into /api/v1/activity/summary's top_servers/top_tools, which previously skipped those rows only by the accident of an empty ServerName. Those lists answer "which upstreams is this proxy talking to", so the management built-ins are now excluded explicitly (storage.IsManagementBuiltin — the same population CountsAsCall already names). The rows stay in the totals. 4. Capturing the whole request captured caller-supplied `_auth_*` keys, which internal/runtime copies onto ActivityRecord.UserID/UserEmail. Neither handler calls injectAuthMetadata, so those can only be client-supplied: a caller-controlled identity stamp on the audit row for a privileged mutation. Stripped with security.StripInternalArgs. 5. The masked changes.modified[*].from/.to is agent-observable. Non-secret values already round-trip truthfully (now pinned by a test); the upstream_servers description says so. Goldens regenerated and the delta enumerated on all three surfaces. --- internal/httpapi/activity.go | 17 +- .../activity_summary_management_test.go | 96 ++++++ internal/oauth/logging.go | 10 + internal/server/mcp.go | 29 +- internal/server/mcp_activity_args.go | 163 ++++++++- internal/server/mcp_activity_args_test.go | 325 +++++++++++++++++- internal/server/mcp_menu_surface_test.go | 29 ++ .../code_execution_mode.json | 2 +- .../toolslist_goldens/default_server.json | 2 +- .../retrieve_tools_mode.json | 2 +- internal/server/toolslist_snapshot_test.go | 11 +- internal/storage/activity_call_population.go | 16 + 12 files changed, 678 insertions(+), 24 deletions(-) create mode 100644 internal/httpapi/activity_summary_management_test.go diff --git a/internal/httpapi/activity.go b/internal/httpapi/activity.go index 944f60f0d..c1fe6d359 100644 --- a/internal/httpapi/activity.go +++ b/internal/httpapi/activity.go @@ -747,12 +747,25 @@ func (s *Server) handleActivitySummary(w http.ResponseWriter, r *http.Request) { otherCount++ } - // Count by server + // Count by server / by tool — UPSTREAM traffic only. + // + // Issue #1146 gave the management built-ins a target_server so the + // Activity Log could render a Server column and --server could filter + // on it. These two lists answer a different question ("which upstreams + // is this proxy talking to"), and they used to skip those rows only by + // the accident of an empty ServerName. Excluding them explicitly keeps + // a burst of config edits from reading as traffic to the server being + // configured, and keeps "github:upstream_servers" — a built-in no + // upstream owns — out of the top-tools list. The rows stay in + // totalCount: they are real activity, just not upstream traffic. + if storage.IsManagementBuiltin(a) { + continue + } + if a.ServerName != "" { serverCounts[a.ServerName]++ } - // Count by tool (server:tool) if a.ServerName != "" && a.ToolName != "" { key := a.ServerName + ":" + a.ToolName toolCounts[key]++ diff --git a/internal/httpapi/activity_summary_management_test.go b/internal/httpapi/activity_summary_management_test.go new file mode 100644 index 000000000..c4f7a3006 --- /dev/null +++ b/internal/httpapi/activity_summary_management_test.go @@ -0,0 +1,96 @@ +package httpapi + +import ( + "encoding/json" + "fmt" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.uber.org/zap" + + "github.com/smart-mcp-proxy/mcpproxy-go/internal/contracts" + "github.com/smart-mcp-proxy/mcpproxy-go/internal/storage" +) + +// Cross-model review of #1146, finding 3. Giving the management built-ins a +// target_server fixed the Activity Log's Server column — and, as a side effect, +// let their rows into /api/v1/activity/summary's per-server aggregation, which +// previously skipped them only by the accident of an empty ServerName. +// +// top_servers / top_tools answer "which upstreams is this proxy talking to". +// upstream_servers and quarantine_security are mcpproxy's own housekeeping — +// the population storage.CountsAsCall already excludes by name — so a burst of +// config edits must not read as traffic to the server being configured, and +// "github:upstream_servers" is not a tool github owns. +func TestActivitySummary_ManagementChatterIsNotUpstreamTraffic(t *testing.T) { + const ( + realCalls = 3 + managementRows = 10 + managementTool = "upstream_servers" + quarantineTool = "quarantine_security" + quarantineCount = 4 + ) + now := time.Now().UTC() + + var activities []*storage.ActivityRecord + for i := 0; i < realCalls; i++ { + activities = append(activities, &storage.ActivityRecord{ + ID: fmt.Sprintf("call-%d", i), Type: storage.ActivityTypeToolCall, + ServerName: "github", ToolName: "search", + Status: storage.ActivityStatusSuccess, Timestamp: now.Add(-time.Duration(i) * time.Minute), + }) + } + for i := 0; i < managementRows; i++ { + activities = append(activities, &storage.ActivityRecord{ + ID: fmt.Sprintf("mgmt-%d", i), Type: storage.ActivityTypeInternalToolCall, + ServerName: "github", ToolName: managementTool, + Status: storage.ActivityStatusSuccess, Timestamp: now.Add(-time.Duration(i) * time.Minute), + }) + } + for i := 0; i < quarantineCount; i++ { + activities = append(activities, &storage.ActivityRecord{ + ID: fmt.Sprintf("quar-%d", i), Type: storage.ActivityTypeInternalToolCall, + ServerName: "github", ToolName: quarantineTool, + Status: storage.ActivityStatusSuccess, Timestamp: now.Add(-time.Duration(i) * time.Minute), + }) + } + + srv := NewServer(&summaryScopeController{activities: activities}, zap.NewNop().Sugar(), nil) + + req := httptest.NewRequest(http.MethodGet, "/api/v1/activity/summary?period=24h", nil) + req.Header.Set("X-API-Key", "test-key") + w := httptest.NewRecorder() + srv.ServeHTTP(w, req) + require.Equal(t, http.StatusOK, w.Code) + + var resp struct { + Success bool `json:"success"` + Data contracts.ActivitySummaryResponse `json:"data"` + } + require.NoError(t, json.NewDecoder(w.Body).Decode(&resp)) + require.True(t, resp.Success) + + // Every row is still in the log and still in the denominator. + assert.Equal(t, realCalls+managementRows+quarantineCount, resp.Data.TotalCount, + "the management rows are real activity and must stay in the total") + + require.Len(t, resp.Data.TopServers, 1) + assert.Equal(t, "github", resp.Data.TopServers[0].Name) + assert.Equal(t, realCalls, resp.Data.TopServers[0].Count, + "config edits to a server are not traffic to it") + + for _, tool := range resp.Data.TopTools { + assert.NotEqual(t, managementTool, tool.Tool, + "upstream_servers is mcpproxy's built-in, not a tool the upstream owns") + assert.NotEqual(t, quarantineTool, tool.Tool, + "quarantine_security is mcpproxy's built-in, not a tool the upstream owns") + } + require.Len(t, resp.Data.TopTools, 1) + assert.Equal(t, "github", resp.Data.TopTools[0].Server) + assert.Equal(t, "search", resp.Data.TopTools[0].Tool) + assert.Equal(t, realCalls, resp.Data.TopTools[0].Count) +} diff --git a/internal/oauth/logging.go b/internal/oauth/logging.go index 58253f607..87740814a 100644 --- a/internal/oauth/logging.go +++ b/internal/oauth/logging.go @@ -191,6 +191,16 @@ var sensitiveEnvMarkers = []string{ "DSN", "CONNECTION_STRING", "CONN_STR", } +// IsSensitiveKeyName reports whether a NAME looks like it holds a secret, using +// the same case-insensitive marker match RedactEnvValues applies to env-var +// keys. Exported for callers that have to decide masking from a name they hold +// separately from the value — e.g. a command-line flag naming the argv token +// that follows it (issue #1146), where there is no map to hand to +// RedactEnvValues. +func IsSensitiveKeyName(name string) bool { + return isSensitiveEnvKey(name) +} + // isSensitiveEnvKey reports whether an env var name looks like it holds a // secret, based on a case-insensitive substring match against // sensitiveEnvMarkers. diff --git a/internal/server/mcp.go b/internal/server/mcp.go index 69100252d..485a4eb15 100644 --- a/internal/server/mcp.go +++ b/internal/server/mcp.go @@ -1124,7 +1124,7 @@ func (p *MCPProxyServer) buildManagementTools() []mcpserver.ServerTool { var tools []mcpserver.ServerTool { upstreamServersTool := mcp.NewTool("upstream_servers", - mcp.WithDescription("Manage upstream MCP servers - add, remove, update, and list servers. Includes Docker isolation configuration and connection status monitoring. SECURITY: Newly added servers are automatically quarantined to prevent Tool Poisoning Attacks (TPAs). Use 'quarantine_security' tool to review and manage quarantined servers. NOTE: Unquarantining servers is only available through manual config editing or system tray UI for security.\n\nDocker Isolation: Use 'isolation_json' parameter to configure per-server Docker images, CPU/memory limits, and network isolation. Example: {\"enabled\": true, \"image\": \"node:20\", \"network_mode\": \"bridge\"}.\n\nSMART PATCHING (update/patch): Uses deep merge - only specify fields you want to change. Omitted fields are PRESERVED, not removed. Examples:\n- Enable server: {\"operation\": \"patch\", \"name\": \"my-server\", \"enabled\": true} - only enabled changes\n- Enable isolation: {\"operation\": \"patch\", \"name\": \"my-server\", \"isolation_json\": \"{\\\"enabled\\\": true}\"} - enables isolation with defaults\n- Update image: {\"operation\": \"patch\", \"name\": \"my-server\", \"isolation_json\": \"{\\\"image\\\": \\\"python:3.12\\\"}\"} - other isolation fields preserved\n- Add env var: env_json merges with existing vars\n- Replace args: args_json replaces entirely (arrays not merged)\n- Remove field: use 'null' (e.g., isolation_json: \"null\" removes isolation)"), + mcp.WithDescription("Manage upstream MCP servers - add, remove, update, and list servers. Includes Docker isolation configuration and connection status monitoring. SECURITY: Newly added servers are automatically quarantined to prevent Tool Poisoning Attacks (TPAs). Use 'quarantine_security' tool to review and manage quarantined servers. NOTE: Unquarantining servers is only available through manual config editing or system tray UI for security.\n\nDocker Isolation: Use 'isolation_json' parameter to configure per-server Docker images, CPU/memory limits, and network isolation. Example: {\"enabled\": true, \"image\": \"node:20\", \"network_mode\": \"bridge\"}.\n\nSMART PATCHING (update/patch): Uses deep merge - only specify fields you want to change. Omitted fields are PRESERVED, not removed. Examples:\n- Enable server: {\"operation\": \"patch\", \"name\": \"my-server\", \"enabled\": true} - only enabled changes\n- Enable isolation: {\"operation\": \"patch\", \"name\": \"my-server\", \"isolation_json\": \"{\\\"enabled\\\": true}\"} - enables isolation with defaults\n- Update image: {\"operation\": \"patch\", \"name\": \"my-server\", \"isolation_json\": \"{\\\"image\\\": \\\"python:3.12\\\"}\"} - other isolation fields preserved\n- Add env var: env_json merges with existing vars\n- Replace args: args_json replaces entirely (arrays not merged)\n- Remove field: use 'null' (e.g., isolation_json: \"null\" removes isolation)\n\nREDACTION (update/patch): the returned 'changes' diff keeps every field PATH exact, but MASKS values under secret-bearing keys (env vars, headers, oauth secrets, credential-shaped argv tokens) in both this response and the activity log. Non-secret values round-trip unchanged; do not read a masked value back as what was stored."), mcp.WithTitleAnnotation("Upstream Servers"), mcp.WithDestructiveHintAnnotation(true), mcp.WithReadOnlyHintAnnotation(false), @@ -3260,18 +3260,21 @@ func (p *MCPProxyServer) handleUpstreamServers(ctx context.Context, request mcp. } requestID := mintCorrelationID("upstream_servers") - // Issue #1146: the resolved server name must reach every emit site, or the - // Activity Log Server column renders "-" and --server filtering misses the - // mutation entirely. Hoisted above the first emit so even a malformed - // request is attributable. - targetServer := activityTargetServer(request) - operation, err := request.RequireString("operation") if err != nil { - p.emitActivityInternalToolCall("upstream_servers", targetServer, "", "", sessionID, requestID, "error", err.Error(), time.Since(startTime).Milliseconds(), activityArgsFromRequest(request), nil, nil, "") + // No operation resolved, so nothing acted on the caller's `name`: + // activityTargetServer leaves the row unattributed rather than letting + // a malformed request stamp itself onto a server. + p.emitActivityInternalToolCall("upstream_servers", activityTargetServer(request, ""), "", "", sessionID, requestID, "error", err.Error(), time.Since(startTime).Milliseconds(), activityArgsFromRequest(request), nil, nil, "") return mcp.NewToolResultError(fmt.Sprintf("Missing required parameter 'operation': %v", err)), nil } + // Issue #1146: the resolved server name must reach every emit site, or the + // Activity Log Server column renders "-" and --server filtering misses the + // mutation entirely. Resolved from the operation as well as the parameter, + // so only operations that actually act on `name` are attributed to it. + targetServer := activityTargetServer(request, operation) + // Build arguments map for activity logging (Spec 024). Issue #1146: record // the FULL redacted argument set, not just {operation, name} — see // mcp_activity_args.go for the redaction policy. @@ -3403,16 +3406,16 @@ func (p *MCPProxyServer) handleQuarantineSecurity(ctx context.Context, request m } requestID := mintCorrelationID("quarantine_security") - // Issue #1146: see handleUpstreamServers — the resolved server name must - // reach every emit site, including the pre-gate denials. - targetServer := activityTargetServer(request) - operation, err := request.RequireString("operation") if err != nil { - p.emitActivityInternalToolCall("quarantine_security", targetServer, "", "", sessionID, requestID, "error", err.Error(), time.Since(startTime).Milliseconds(), activityArgsFromRequest(request), nil, nil, "") + p.emitActivityInternalToolCall("quarantine_security", activityTargetServer(request, ""), "", "", sessionID, requestID, "error", err.Error(), time.Since(startTime).Milliseconds(), activityArgsFromRequest(request), nil, nil, "") return mcp.NewToolResultError(fmt.Sprintf("Missing required parameter 'operation': %v", err)), nil } + // Issue #1146: see handleUpstreamServers — the resolved server name must + // reach every emit site, including the pre-gate denials. + targetServer := activityTargetServer(request, operation) + // Build arguments map for activity logging (Spec 024). Issue #1146: the full // redacted argument set — this also picks up prompt_name, which the old // hand-rolled builder never recorded. diff --git a/internal/server/mcp_activity_args.go b/internal/server/mcp_activity_args.go index 47b453da2..3d8b52687 100644 --- a/internal/server/mcp_activity_args.go +++ b/internal/server/mcp_activity_args.go @@ -4,11 +4,13 @@ import ( "bytes" "encoding/json" "strings" + "sync" "github.com/mark3labs/mcp-go/mcp" "github.com/smart-mcp-proxy/mcpproxy-go/internal/config" "github.com/smart-mcp-proxy/mcpproxy-go/internal/oauth" + "github.com/smart-mcp-proxy/mcpproxy-go/internal/security" ) // Issue #1146: the activity records for the two MUTATING built-in tools @@ -52,7 +54,16 @@ const ( // raw MCP request. Returns nil for an empty request so callers keep emitting // `null` arguments exactly as before. func activityArgsFromRequest(request mcp.CallToolRequest) map[string]interface{} { - raw := request.GetArguments() + // Drop MCPProxy's own `_auth_*` plumbing BEFORE anything else. Neither of + // the two handlers this file serves calls injectAuthMetadata, so a key + // under that prefix here can only have been sent by the CALLER — and + // internal/runtime copies `_auth_user_id` / `_auth_user_email` straight + // onto ActivityRecord.UserID/UserEmail. Capturing the whole request would + // therefore hand an agent a forged identity stamp on the audit row for a + // privileged mutation. The old {operation,name} allowlist dropped these by + // accident; dropping them on purpose is what keeps "record everything" + // safe. + raw := security.StripInternalArgs(request.GetArguments()) if len(raw) == 0 { return nil } @@ -75,11 +86,63 @@ func activityArgsFromRequest(request mcp.CallToolRequest) map[string]interface{} return args } +// activityAttributedOps are the operations of the two management built-ins that +// are handed the request — the only ones that can have acted on the caller's +// `name`. An operation absent from this set (an inventory read, an unknown +// string, a request that never named an operation at all) dispatched nothing +// against `name`, so its activity row stays unattributed rather than letting an +// agent stamp activity onto a server it never touched. +// +// The set is an ALLOWLIST so an unknown operation defaults to safe, and +// TestActivityAttributedOps_MatchTheOperationSwitches pins it to the two switch +// statements in BOTH directions: a new mutating operation that forgets to +// register here fails the test (the issue-#1146 "-" Server column would be +// back), and so does an inventory operation that wrongly appears. +var activityAttributedOps = map[string]bool{ + // upstream_servers + operationAdd: true, + operationRemove: true, + "update": true, + "patch": true, + "tail_log": true, + "enable": true, + "disable": true, + "restart": true, + "refresh": true, + "add_from_registry": true, + + // quarantine_security + "inspect_quarantined": true, + "quarantine_server": true, + "quarantine": true, + "inspect_tools": true, + "scan_server": true, + "get_scan_report": true, + "approve_tool": true, + "approve_all_tools": true, + "block_tool": true, + "block_all_tools": true, + "enable_tool": true, + "disable_tool": true, + "inspect_prompts": true, + "approve_prompt": true, + "approve_all_prompts": true, +} + // activityTargetServer resolves the server a mutation targets, so the activity // row renders a Server column and `activity list --server ` matches it. // runtime.EmitActivityInternalToolCall omits target_server when empty, which is // why every site used to render "-" (issue #1146). -func activityTargetServer(request mcp.CallToolRequest) string { +// +// `name` is agent-supplied and never validated here, so it is only trusted for +// an operation that actually acts on it: an unresolved operation and the +// inventory reads stay unattributed. Without that gate an agent could attribute +// its management chatter — and, through /api/v1/activity/summary, per-server +// totals — to any server it liked by calling `list` with a `name`. +func activityTargetServer(request mcp.CallToolRequest, operation string) string { + if !activityAttributedOps[operation] { + return "" + } return request.GetString("name", "") } @@ -180,6 +243,9 @@ func redactActivityValue(key string, v interface{}) interface{} { } return out case []interface{}: + if isActivityArgvKey(key) { + return redactActivityArgv(typed) + } out := make([]interface{}, len(typed)) for i, val := range typed { out[i] = redactActivityValue(key, val) @@ -212,6 +278,99 @@ func redactActivityString(key, s string) string { } } +// isActivityArgvKey reports whether a slice is a command-line argument vector. +// Covers the `args` config field (config.MergeServerConfig's diff) and the +// `args_json` tool parameter (the suffix is trimmed before the walk). +func isActivityArgvKey(key string) bool { + return strings.EqualFold(key, "args") +} + +// redactActivityArgv masks a command-line argument vector. +// +// Issue #1146, review round: argv was the one leak the key-driven walker could +// not see. An argv token has no enclosing key — the slice branch recursed with +// the PARENT key ("args"), so every element reached redactActivityString("args", +// elem) and fell through unmasked. Passing a credential as an argv token +// (`uvx mcp-foo --api-key sk-live-…`) is one of the commonest MCP server config +// shapes, so the record persisted in BBolt, the SSE payload and +// `mcpproxy activity list` all carried it in the clear. +// +// Two rules, because argv secrets announce themselves in two ways: +// +// - The FLAG names the credential — `--api-key VALUE` and `--api-key=VALUE`. +// Delegated to redactActivityString with the de-dashed flag as the key, so +// the same single source of truth the env map uses decides, and the flag +// itself stays readable (it is the audit signal: WHICH credential moved). +// - The VALUE is recognisably a credential on its own — an AWS key, a GitHub +// token, a PEM block, a high-entropy blob — wherever it sits. That is what +// internal/security's detector is for, so it is reused rather than +// re-invented as another regex here. +// +// Everything else round-trips verbatim: package names, subcommands, paths and +// ports are what make the record useful. +func redactActivityArgv(argv []interface{}) []interface{} { + out := make([]interface{}, len(argv)) + maskNext := false + + for i, item := range argv { + s, ok := item.(string) + if !ok { + // A non-string element carries no flag semantics; walk it with no + // enclosing key and reset the pairing. + out[i] = redactActivityValue("", item) + maskNext = false + continue + } + + if maskNext { + out[i] = capActivityValue(oauth.MaskValue(s)) + maskNext = false + continue + } + + if flag, value, inline := strings.Cut(s, "="); inline && isArgvFlag(flag) { + out[i] = capActivityValue(flag + "=" + redactActivityString(argvFlagKey(flag), value)) + continue + } + + out[i] = capActivityValue(maskDetectedSecrets(s)) + maskNext = isArgvFlag(s) && oauth.IsSensitiveKeyName(argvFlagKey(s)) + } + + return out +} + +// isArgvFlag reports whether a token is an option rather than a positional +// argument. A bare "-" or "--" is a convention, not a flag. +func isArgvFlag(token string) bool { + return strings.HasPrefix(token, "-") && strings.Trim(token, "-") != "" +} + +// argvFlagKey turns `--api-key` into the key name the env/header redactors +// match on. +func argvFlagKey(flag string) string { + return strings.TrimLeft(flag, "-") +} + +// activitySecretMasker is the value-shaped secret masker used for argv tokens, +// which have no key to be judged by. +// +// It is built from the DEFAULT detection config rather than the running one on +// purpose: masking here is a property of the audit STORE, and an operator who +// turned sensitive-data detection off for the call path must not thereby cause +// credentials to be written to that store in the clear. (Detector.MaskText +// ignores the `enabled` flag for the same reason.) +var activitySecretMasker = sync.OnceValue(func() *security.Detector { + return security.NewDetector(config.DefaultSensitiveDataDetectionConfig()) +}) + +// maskDetectedSecrets masks any credential the detector recognises inside one +// argv token, leaving text it does not recognise untouched. +func maskDetectedSecrets(s string) string { + masked, _ := activitySecretMasker().MaskText(s) + return masked +} + // isActivityHeadersKey reports whether a map should be masked with HTTP-header // semantics. Covers both the `headers` config field and the `headers_json` tool // parameter (the suffix is trimmed before the walk). diff --git a/internal/server/mcp_activity_args_test.go b/internal/server/mcp_activity_args_test.go index c3322ff81..ccd0a43df 100644 --- a/internal/server/mcp_activity_args_test.go +++ b/internal/server/mcp_activity_args_test.go @@ -8,6 +8,7 @@ import ( "go/token" "os" "path/filepath" + "strconv" "strings" "testing" @@ -217,7 +218,8 @@ func TestActivityTargetServer_ResolvedFromNameParam(t *testing.T) { for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { req := mcp.CallToolRequest{Params: mcp.CallToolParams{Arguments: tc.args}} - assert.Equal(t, tc.want, activityTargetServer(req)) + operation, _ := tc.args["operation"].(string) + assert.Equal(t, tc.want, activityTargetServer(req, operation)) }) } } @@ -388,3 +390,324 @@ func TestPatchResponse_DoesNotEchoSecrets(t *testing.T) { require.True(t, ok) assert.Contains(t, modified, "env", "the operator must still see WHICH field changed") } + +// (5) MINOR/contract — masking the `changes` diff is agent-observable: an agent +// that reads modified[*].from/.to back to confirm what it wrote must still get +// the TRUTH for anything that is not a secret. Only secret-bearing values may +// mask. (That the description now says so is pinned by +// assertUpstreamServersDelta in mcp_menu_surface_test.go.) +func TestPatchResponse_NonSecretValuesRoundTripTruthfully(t *testing.T) { + proxy, _ := newStoredScriptProxy(t) + + seed := &config.ServerConfig{ + Name: "plain", + URL: "https://old.test/mcp", + Protocol: "streamable-http", + Enabled: false, + } + _, err := proxy.storage.AddUpstream(seed) + require.NoError(t, err) + + req := mcp.CallToolRequest{Params: mcp.CallToolParams{ + Name: "upstream_servers", + Arguments: map[string]interface{}{ + "operation": "patch", + "name": "plain", + "url": "https://new.test/mcp", + "enabled": true, + }, + }} + + result, _, err := proxy.handlePatchUpstream(context.Background(), req) + require.NoError(t, err) + require.NotNil(t, result) + + var payload map[string]interface{} + require.NoError(t, json.Unmarshal([]byte(resultText(t, result)), &payload)) + changes, ok := payload["changes"].(map[string]interface{}) + require.Truef(t, ok, "the diff must be reported, got %v", payload) + modified, ok := changes["modified"].(map[string]interface{}) + require.True(t, ok) + + urlChange, ok := modified["url"].(map[string]interface{}) + require.Truef(t, ok, "url must appear in the diff, got %v", modified) + assert.Equal(t, "https://old.test/mcp", urlChange["from"], + "a non-secret value must round-trip exactly — an agent verifies what it wrote from this") + assert.Equal(t, "https://new.test/mcp", urlChange["to"]) + + if enabledChange, ok := modified["enabled"].(map[string]interface{}); ok { + assert.Equal(t, false, enabledChange["from"]) + assert.Equal(t, true, enabledChange["to"]) + } +} + +// --------------------------------------------------------------------------- +// Cross-model review of #1146 (round 1). Findings 1-4, each pinned here. +// --------------------------------------------------------------------------- + +const ( + testArgvSecret = "sk-live-1234567890abcdef" + testArgvInlineSecret = "ghp_inlineAAAAAAAAAAAAAAAAAAAAAAAAAA" +) + +// (1) MAJOR — a credential passed as an argv TOKEN has no key to be judged by, +// so the env-var rule could not see it and recorded it verbatim. Passing a +// secret as `--api-key ` is one of the commonest MCP server config +// shapes, so this was a live leak on the exact path #1146 set out to seal. +func TestActivityArgs_ArgvSecretsAreMasked(t *testing.T) { + req := mcp.CallToolRequest{Params: mcp.CallToolParams{ + Name: "upstream_servers", + Arguments: map[string]interface{}{ + "operation": "add", + "name": "x", + "command": "uvx", + "args_json": `["mcp-foo","--api-key","` + testArgvSecret + `","--token=` + testArgvInlineSecret + `"]`, + }, + }} + + argv, ok := activityArgsFromRequest(req)["args_json"].([]interface{}) + require.Truef(t, ok, "args_json must stay a list, got %T", activityArgsFromRequest(req)["args_json"]) + require.Len(t, argv, 4) + + assert.Equal(t, "mcp-foo", argv[0], "a package name is not a secret and must stay readable") + assert.Equal(t, "--api-key", argv[1], "the flag NAMES the credential — keep it, that is the audit signal") + assert.NotContains(t, argv[2], testArgvSecret, "the argv credential was recorded in the clear") + assert.Contains(t, argv[2], "••••", "the credential must be present but masked, not dropped") + assert.NotContains(t, argv[3], testArgvInlineSecret, "the inline --flag=secret form leaked") + assert.Contains(t, argv[3], "--token=", "the inline flag name must survive the mask") +} + +// (1') …and the same token reaching the record through the resolved config +// diff (`args` is a FieldChange whose From/To are []string). +func TestRedactedConfigDiff_ArgvSecretsAreMasked(t *testing.T) { + diff := config.NewConfigDiff() + diff.Modified["args"] = config.FieldChange{ + Path: "args", + From: []string{"mcp-foo"}, + To: []string{"mcp-foo", "--api-key", testArgvSecret}, + } + + encoded, err := json.Marshal(redactedConfigDiff(diff)) + require.NoError(t, err) + assert.NotContains(t, string(encoded), testArgvSecret, + "the config diff recorded an argv credential in the clear") + assert.Contains(t, string(encoded), "--api-key", + "the flag name must survive so the operator knows WHICH credential moved") +} + +// (1”) Over-masking would make the record useless. Ordinary argv tokens — +// package names, subcommands, paths, ports — must round-trip verbatim. +func TestActivityArgs_OrdinaryArgvStaysReadable(t *testing.T) { + req := mcp.CallToolRequest{Params: mcp.CallToolParams{ + Name: "upstream_servers", + Arguments: map[string]interface{}{ + "operation": "add", + "name": "x", + "args_json": `["-y","@modelcontextprotocol/server-github","--port","8080","--transport=stdio","/srv/data"]`, + }, + }} + argv, ok := activityArgsFromRequest(req)["args_json"].([]interface{}) + require.True(t, ok) + assert.Equal(t, + []interface{}{"-y", "@modelcontextprotocol/server-github", "--port", "8080", "--transport=stdio", "/srv/data"}, + argv) +} + +// (4) MINOR/security — `_auth_*` are keys MCPProxy injects into a call for its +// own use, and internal/runtime copies `_auth_user_id` / `_auth_user_email` +// straight onto ActivityRecord.UserID/UserEmail. Neither of these two handlers +// calls injectAuthMetadata, so anything under that prefix here can ONLY have +// come from the caller: capturing the whole request handed an agent a +// caller-controlled identity stamp on the audit row for a privileged mutation. +// The old {operation,name} allowlist dropped them by accident; drop them on +// purpose. +func TestActivityArgs_DropsCallerSuppliedAuthMetadata(t *testing.T) { + req := mcp.CallToolRequest{Params: mcp.CallToolParams{ + Name: "upstream_servers", + Arguments: map[string]interface{}{ + "operation": "remove", + "name": "github", + "_auth_user_id": "01HIMPERSONATED", + "_auth_user_email": "victim@example.test", + "_auth_auth_type": "oauth", + }, + }} + args := activityArgsFromRequest(req) + require.NotNil(t, args) + + for key := range args { + assert.NotContainsf(t, key, "_auth_", + "caller-supplied %q became a forged identity stamp on the audit row", key) + } + assert.Equal(t, "remove", args["operation"], "the real parameters must still be recorded") + assert.Equal(t, "github", args["name"]) +} + +// (2) MINOR — `name` is agent-supplied and unvalidated. The inventory +// operations do not take the request at all, so attributing their activity row +// to whatever `name` the caller passed lets an agent stamp a row onto a server +// it never touched (and, through /api/v1/activity/summary, onto that server's +// traffic totals). +func TestActivityTargetServer_UnattributedForInventoryOperations(t *testing.T) { + tests := []struct { + name string + operation string + want string + }{ + {"upstream list ignores name", "list", ""}, + {"quarantine list ignores name", "list_quarantined", ""}, + {"unparsed operation", "", ""}, + {"unknown operation", "definitely-not-an-operation", ""}, + {"patch acts on name", "patch", "github"}, + {"remove acts on name", "remove", "github"}, + {"approve_tool acts on name", "approve_tool", "github"}, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + req := mcp.CallToolRequest{Params: mcp.CallToolParams{ + Arguments: map[string]interface{}{"operation": tc.operation, "name": "github"}, + }} + assert.Equal(t, tc.want, activityTargetServer(req, tc.operation)) + }) + } +} + +// (2') Rot guard, both directions: activityAttributedOps must be EXACTLY the +// operations whose switch arm is handed the request. A new mutating operation +// that forgets to register is unattributed — the issue-#1146 "-" Server column, +// back again; an inventory operation that wrongly appears is the misattribution +// this finding was about. +func TestActivityAttributedOps_MatchTheOperationSwitches(t *testing.T) { + consts := parseServerStringConsts(t) + + fset := token.NewFileSet() + file, err := parser.ParseFile(fset, filepath.Join(".", "mcp.go"), nil, 0) + require.NoError(t, err) + + guarded := map[string]bool{"handleUpstreamServers": true, "handleQuarantineSecurity": true} + withRequest := map[string]bool{} + withoutRequest := map[string]bool{} + + for _, decl := range file.Decls { + fn, ok := decl.(*ast.FuncDecl) + if !ok || !guarded[fn.Name.Name] { + continue + } + ast.Inspect(fn, func(node ast.Node) bool { + clause, isClause := node.(*ast.CaseClause) + if !isClause || len(clause.List) == 0 || !caseBodyCallsHandler(clause) { + return true + } + target := withoutRequest + if caseBodyPassesRequest(clause) { + target = withRequest + } + for _, expr := range clause.List { + if op, resolved := resolveCaseString(expr, consts); resolved { + target[op] = true + } + } + return true + }) + } + + require.NotEmpty(t, withRequest, "the AST walk found no operation arms — the guard would be vacuous") + require.NotEmpty(t, withoutRequest, "the AST walk found no inventory arms — the guard would be vacuous") + + assert.Equal(t, sortedKeys(withRequest), sortedKeys(activityAttributedOps), + "activityAttributedOps must list exactly the operations whose handler receives the request") + + for op := range withoutRequest { + assert.Falsef(t, activityAttributedOps[op], + "operation %q never receives the request, so the caller's `name` was not acted on", op) + } +} + +// parseServerStringConsts collects the package's `const x = "literal"` values +// so a case clause written as an identifier (operationList) can be resolved. +func parseServerStringConsts(t *testing.T) map[string]string { + t.Helper() + out := map[string]string{} + entries, err := os.ReadDir(".") + require.NoError(t, err) + fset := token.NewFileSet() + for _, entry := range entries { + name := entry.Name() + if entry.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") { + continue + } + file, err := parser.ParseFile(fset, filepath.Join(".", name), nil, 0) + require.NoErrorf(t, err, "parsing %s", name) + for _, decl := range file.Decls { + gd, ok := decl.(*ast.GenDecl) + if !ok || gd.Tok != token.CONST { + continue + } + for _, spec := range gd.Specs { + vs, ok := spec.(*ast.ValueSpec) + if !ok || len(vs.Names) != 1 || len(vs.Values) != 1 { + continue + } + lit, ok := vs.Values[0].(*ast.BasicLit) + if !ok || lit.Kind != token.STRING { + continue + } + unquoted, err := strconv.Unquote(lit.Value) + if err == nil { + out[vs.Names[0].Name] = unquoted + } + } + } + } + require.NotEmpty(t, out) + return out +} + +func resolveCaseString(expr ast.Expr, consts map[string]string) (string, bool) { + switch e := expr.(type) { + case *ast.BasicLit: + if e.Kind != token.STRING { + return "", false + } + unquoted, err := strconv.Unquote(e.Value) + return unquoted, err == nil + case *ast.Ident: + v, ok := consts[e.Name] + return v, ok + } + return "", false +} + +// caseBodyCallsHandler reports whether the clause dispatches to a p.handleX +// method — i.e. it is one of the operation switch's arms rather than an +// unrelated switch (status strings, etc.). +func caseBodyCallsHandler(clause *ast.CaseClause) bool { + found := false + for _, stmt := range clause.Body { + ast.Inspect(stmt, func(node ast.Node) bool { + call, ok := node.(*ast.CallExpr) + if !ok { + return true + } + if sel, ok := call.Fun.(*ast.SelectorExpr); ok && strings.HasPrefix(sel.Sel.Name, "handle") { + found = true + } + return true + }) + } + return found +} + +func caseBodyPassesRequest(clause *ast.CaseClause) bool { + found := false + for _, stmt := range clause.Body { + ast.Inspect(stmt, func(node ast.Node) bool { + ident, ok := node.(*ast.Ident) + if ok && ident.Name == "request" { + found = true + } + return true + }) + } + return found +} diff --git a/internal/server/mcp_menu_surface_test.go b/internal/server/mcp_menu_surface_test.go index 88ddd4518..f2ab4c926 100644 --- a/internal/server/mcp_menu_surface_test.go +++ b/internal/server/mcp_menu_surface_test.go @@ -158,6 +158,8 @@ func TestMenuSurface_ExactDeltaFromPreFeature(t *testing.T) { assertCodeExecutionDelta(t, surface, preM, curM) case name == "quarantine_security": assertQuarantineSecurityDelta(t, surface, preM, curM) + case name == "upstream_servers": + assertUpstreamServersDelta(t, surface, preM, curM) default: assert.Equal(t, preM, curM, "surface %s: tool %q must be byte-identical to the pre-feature snapshot (SC-003)", surface, name) @@ -281,6 +283,33 @@ func schemaWithout(schema map[string]interface{}, drop ...string) map[string]int return out } +// upstreamServersRedactionMarker is the verbatim marker the upstream_servers +// description must carry. Issue #1146 made update/patch mask secret-bearing +// values in the `changes` diff they return — an agent that reads the diff back +// to verify what it wrote observes that, so the surface has to say so. The +// literal is pinned here so the sentence cannot be silently dropped or reworded +// into something an agent would miss. +const upstreamServersRedactionMarker = "REDACTION (update/patch):" + +// assertUpstreamServersDelta: upstream_servers may change by EXACTLY its +// description, which gains the redaction note above. No parameter may be added, +// removed or altered — this is a documentation change over an existing +// behaviour, not a new capability. +func assertUpstreamServersDelta(t *testing.T, surface string, preM, curM map[string]interface{}) { + t.Helper() + + assert.Equal(t, schemaWithout(preM, "description"), schemaWithout(curM, "description"), + "surface %s: only upstream_servers' description may move (issue #1146)", surface) + + assert.Contains(t, curM["description"], upstreamServersRedactionMarker, + "surface %s: upstream_servers must document that update/patch mask secret values in the diff", surface) + + preDesc, _ := preM["description"].(string) + curDesc, _ := curM["description"].(string) + assert.True(t, strings.HasPrefix(curDesc, preDesc), + "surface %s: the redaction note is APPENDED — no pre-feature prose may be rewritten", surface) +} + // assertQuarantineSecurityDelta: quarantine_security may grow the two scan // operations and the prose that documents them, and NOTHING else. In // particular it takes no new parameter — both operations reuse `name` — so the diff --git a/internal/server/testdata/toolslist_goldens/code_execution_mode.json b/internal/server/testdata/toolslist_goldens/code_execution_mode.json index 29e080fd2..72f84e5f2 100644 --- a/internal/server/testdata/toolslist_goldens/code_execution_mode.json +++ b/internal/server/testdata/toolslist_goldens/code_execution_mode.json @@ -199,7 +199,7 @@ "idempotentHint": false, "openWorldHint": false }, - "description": "Manage upstream MCP servers - add, remove, update, and list servers. Includes Docker isolation configuration and connection status monitoring. SECURITY: Newly added servers are automatically quarantined to prevent Tool Poisoning Attacks (TPAs). Use 'quarantine_security' tool to review and manage quarantined servers. NOTE: Unquarantining servers is only available through manual config editing or system tray UI for security.\n\nDocker Isolation: Use 'isolation_json' parameter to configure per-server Docker images, CPU/memory limits, and network isolation. Example: {\"enabled\": true, \"image\": \"node:20\", \"network_mode\": \"bridge\"}.\n\nSMART PATCHING (update/patch): Uses deep merge - only specify fields you want to change. Omitted fields are PRESERVED, not removed. Examples:\n- Enable server: {\"operation\": \"patch\", \"name\": \"my-server\", \"enabled\": true} - only enabled changes\n- Enable isolation: {\"operation\": \"patch\", \"name\": \"my-server\", \"isolation_json\": \"{\\\"enabled\\\": true}\"} - enables isolation with defaults\n- Update image: {\"operation\": \"patch\", \"name\": \"my-server\", \"isolation_json\": \"{\\\"image\\\": \\\"python:3.12\\\"}\"} - other isolation fields preserved\n- Add env var: env_json merges with existing vars\n- Replace args: args_json replaces entirely (arrays not merged)\n- Remove field: use 'null' (e.g., isolation_json: \"null\" removes isolation)", + "description": "Manage upstream MCP servers - add, remove, update, and list servers. Includes Docker isolation configuration and connection status monitoring. SECURITY: Newly added servers are automatically quarantined to prevent Tool Poisoning Attacks (TPAs). Use 'quarantine_security' tool to review and manage quarantined servers. NOTE: Unquarantining servers is only available through manual config editing or system tray UI for security.\n\nDocker Isolation: Use 'isolation_json' parameter to configure per-server Docker images, CPU/memory limits, and network isolation. Example: {\"enabled\": true, \"image\": \"node:20\", \"network_mode\": \"bridge\"}.\n\nSMART PATCHING (update/patch): Uses deep merge - only specify fields you want to change. Omitted fields are PRESERVED, not removed. Examples:\n- Enable server: {\"operation\": \"patch\", \"name\": \"my-server\", \"enabled\": true} - only enabled changes\n- Enable isolation: {\"operation\": \"patch\", \"name\": \"my-server\", \"isolation_json\": \"{\\\"enabled\\\": true}\"} - enables isolation with defaults\n- Update image: {\"operation\": \"patch\", \"name\": \"my-server\", \"isolation_json\": \"{\\\"image\\\": \\\"python:3.12\\\"}\"} - other isolation fields preserved\n- Add env var: env_json merges with existing vars\n- Replace args: args_json replaces entirely (arrays not merged)\n- Remove field: use 'null' (e.g., isolation_json: \"null\" removes isolation)\n\nREDACTION (update/patch): the returned 'changes' diff keeps every field PATH exact, but MASKS values under secret-bearing keys (env vars, headers, oauth secrets, credential-shaped argv tokens) in both this response and the activity log. Non-secret values round-trip unchanged; do not read a masked value back as what was stored.", "inputSchema": { "properties": { "args_json": { diff --git a/internal/server/testdata/toolslist_goldens/default_server.json b/internal/server/testdata/toolslist_goldens/default_server.json index ef30fb994..e9cb06fdb 100644 --- a/internal/server/testdata/toolslist_goldens/default_server.json +++ b/internal/server/testdata/toolslist_goldens/default_server.json @@ -394,7 +394,7 @@ "idempotentHint": false, "openWorldHint": false }, - "description": "Manage upstream MCP servers - add, remove, update, and list servers. Includes Docker isolation configuration and connection status monitoring. SECURITY: Newly added servers are automatically quarantined to prevent Tool Poisoning Attacks (TPAs). Use 'quarantine_security' tool to review and manage quarantined servers. NOTE: Unquarantining servers is only available through manual config editing or system tray UI for security.\n\nDocker Isolation: Use 'isolation_json' parameter to configure per-server Docker images, CPU/memory limits, and network isolation. Example: {\"enabled\": true, \"image\": \"node:20\", \"network_mode\": \"bridge\"}.\n\nSMART PATCHING (update/patch): Uses deep merge - only specify fields you want to change. Omitted fields are PRESERVED, not removed. Examples:\n- Enable server: {\"operation\": \"patch\", \"name\": \"my-server\", \"enabled\": true} - only enabled changes\n- Enable isolation: {\"operation\": \"patch\", \"name\": \"my-server\", \"isolation_json\": \"{\\\"enabled\\\": true}\"} - enables isolation with defaults\n- Update image: {\"operation\": \"patch\", \"name\": \"my-server\", \"isolation_json\": \"{\\\"image\\\": \\\"python:3.12\\\"}\"} - other isolation fields preserved\n- Add env var: env_json merges with existing vars\n- Replace args: args_json replaces entirely (arrays not merged)\n- Remove field: use 'null' (e.g., isolation_json: \"null\" removes isolation)", + "description": "Manage upstream MCP servers - add, remove, update, and list servers. Includes Docker isolation configuration and connection status monitoring. SECURITY: Newly added servers are automatically quarantined to prevent Tool Poisoning Attacks (TPAs). Use 'quarantine_security' tool to review and manage quarantined servers. NOTE: Unquarantining servers is only available through manual config editing or system tray UI for security.\n\nDocker Isolation: Use 'isolation_json' parameter to configure per-server Docker images, CPU/memory limits, and network isolation. Example: {\"enabled\": true, \"image\": \"node:20\", \"network_mode\": \"bridge\"}.\n\nSMART PATCHING (update/patch): Uses deep merge - only specify fields you want to change. Omitted fields are PRESERVED, not removed. Examples:\n- Enable server: {\"operation\": \"patch\", \"name\": \"my-server\", \"enabled\": true} - only enabled changes\n- Enable isolation: {\"operation\": \"patch\", \"name\": \"my-server\", \"isolation_json\": \"{\\\"enabled\\\": true}\"} - enables isolation with defaults\n- Update image: {\"operation\": \"patch\", \"name\": \"my-server\", \"isolation_json\": \"{\\\"image\\\": \\\"python:3.12\\\"}\"} - other isolation fields preserved\n- Add env var: env_json merges with existing vars\n- Replace args: args_json replaces entirely (arrays not merged)\n- Remove field: use 'null' (e.g., isolation_json: \"null\" removes isolation)\n\nREDACTION (update/patch): the returned 'changes' diff keeps every field PATH exact, but MASKS values under secret-bearing keys (env vars, headers, oauth secrets, credential-shaped argv tokens) in both this response and the activity log. Non-secret values round-trip unchanged; do not read a masked value back as what was stored.", "inputSchema": { "properties": { "args_json": { diff --git a/internal/server/testdata/toolslist_goldens/retrieve_tools_mode.json b/internal/server/testdata/toolslist_goldens/retrieve_tools_mode.json index 685224857..3ad94fafa 100644 --- a/internal/server/testdata/toolslist_goldens/retrieve_tools_mode.json +++ b/internal/server/testdata/toolslist_goldens/retrieve_tools_mode.json @@ -415,7 +415,7 @@ "idempotentHint": false, "openWorldHint": false }, - "description": "Manage upstream MCP servers - add, remove, update, and list servers. Includes Docker isolation configuration and connection status monitoring. SECURITY: Newly added servers are automatically quarantined to prevent Tool Poisoning Attacks (TPAs). Use 'quarantine_security' tool to review and manage quarantined servers. NOTE: Unquarantining servers is only available through manual config editing or system tray UI for security.\n\nDocker Isolation: Use 'isolation_json' parameter to configure per-server Docker images, CPU/memory limits, and network isolation. Example: {\"enabled\": true, \"image\": \"node:20\", \"network_mode\": \"bridge\"}.\n\nSMART PATCHING (update/patch): Uses deep merge - only specify fields you want to change. Omitted fields are PRESERVED, not removed. Examples:\n- Enable server: {\"operation\": \"patch\", \"name\": \"my-server\", \"enabled\": true} - only enabled changes\n- Enable isolation: {\"operation\": \"patch\", \"name\": \"my-server\", \"isolation_json\": \"{\\\"enabled\\\": true}\"} - enables isolation with defaults\n- Update image: {\"operation\": \"patch\", \"name\": \"my-server\", \"isolation_json\": \"{\\\"image\\\": \\\"python:3.12\\\"}\"} - other isolation fields preserved\n- Add env var: env_json merges with existing vars\n- Replace args: args_json replaces entirely (arrays not merged)\n- Remove field: use 'null' (e.g., isolation_json: \"null\" removes isolation)", + "description": "Manage upstream MCP servers - add, remove, update, and list servers. Includes Docker isolation configuration and connection status monitoring. SECURITY: Newly added servers are automatically quarantined to prevent Tool Poisoning Attacks (TPAs). Use 'quarantine_security' tool to review and manage quarantined servers. NOTE: Unquarantining servers is only available through manual config editing or system tray UI for security.\n\nDocker Isolation: Use 'isolation_json' parameter to configure per-server Docker images, CPU/memory limits, and network isolation. Example: {\"enabled\": true, \"image\": \"node:20\", \"network_mode\": \"bridge\"}.\n\nSMART PATCHING (update/patch): Uses deep merge - only specify fields you want to change. Omitted fields are PRESERVED, not removed. Examples:\n- Enable server: {\"operation\": \"patch\", \"name\": \"my-server\", \"enabled\": true} - only enabled changes\n- Enable isolation: {\"operation\": \"patch\", \"name\": \"my-server\", \"isolation_json\": \"{\\\"enabled\\\": true}\"} - enables isolation with defaults\n- Update image: {\"operation\": \"patch\", \"name\": \"my-server\", \"isolation_json\": \"{\\\"image\\\": \\\"python:3.12\\\"}\"} - other isolation fields preserved\n- Add env var: env_json merges with existing vars\n- Replace args: args_json replaces entirely (arrays not merged)\n- Remove field: use 'null' (e.g., isolation_json: \"null\" removes isolation)\n\nREDACTION (update/patch): the returned 'changes' diff keeps every field PATH exact, but MASKS values under secret-bearing keys (env vars, headers, oauth secrets, credential-shaped argv tokens) in both this response and the activity log. Non-secret values round-trip unchanged; do not read a masked value back as what was stored.", "inputSchema": { "properties": { "args_json": { diff --git a/internal/server/toolslist_snapshot_test.go b/internal/server/toolslist_snapshot_test.go index b9a2e2baa..6ced627c7 100644 --- a/internal/server/toolslist_snapshot_test.go +++ b/internal/server/toolslist_snapshot_test.go @@ -185,10 +185,15 @@ func TestToolsListSnapshot_MatchesMergeBaseGoldens(t *testing.T) { // - describe_tool — spec 099 (FR-014), retrieve_tools surfaces only. // - quarantine_security — scan_server / get_scan_report operations, on every // surface that registers the tool (all three). +// - upstream_servers — issue #1146: update/patch now mask secret-bearing +// values in the `changes` diff they return, which an agent reading the diff +// back can observe, so the description says so. DESCRIPTION ONLY — no +// parameter moves, which assertUpstreamServersDelta in +// mcp_menu_surface_test.go pins field by field. var toolsListAllowedDelta = map[string][]string{ - "default_server": {"describe_tool", "quarantine_security"}, - "retrieve_tools_mode": {"describe_tool", "quarantine_security"}, - "code_execution_mode": {"quarantine_security"}, + "default_server": {"describe_tool", "quarantine_security", "upstream_servers"}, + "retrieve_tools_mode": {"describe_tool", "quarantine_security", "upstream_servers"}, + "code_execution_mode": {"quarantine_security", "upstream_servers"}, } // TestToolsListSnapshot_DeltaIsEnumerated is the FR-014 gate: the goldens diff --git a/internal/storage/activity_call_population.go b/internal/storage/activity_call_population.go index ed477b078..3d3113a75 100644 --- a/internal/storage/activity_call_population.go +++ b/internal/storage/activity_call_population.go @@ -34,6 +34,22 @@ var glanceManagementBuiltins = map[string]bool{ "quarantine_security": true, } +// IsManagementBuiltin reports whether a record is one of mcpproxy's own +// management built-ins (upstream_servers / quarantine_security) rather than +// something a user asked an upstream for. +// +// It is the same population CountsAsCall excludes above, exported because the +// per-server aggregation needs it too: issue #1146 gave these rows a +// target_server so the Activity Log could render a Server column and --server +// could filter on it, and without this predicate a burst of config edits would +// then read as traffic TO the server being configured — and "github: +// upstream_servers" would show up as one of github's busiest tools. +func IsManagementBuiltin(rec *ActivityRecord) bool { + return rec != nil && + rec.Type == ActivityTypeInternalToolCall && + glanceManagementBuiltins[rec.ToolName] +} + // CountsAsCall reports whether a record is one of the calls the user made, and // whether that call is a failure. // From 5d590c2669c17e8263ffbeeb4ad3ed358f108a4a Mon Sep 17 00:00:00 2001 From: Algis Dumbris Date: Mon, 31 Aug 2026 19:58:05 +0300 Subject: [PATCH 3/3] fix(server): close the remaining activity-redaction leaks (round 3) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Key-name matching was still the only rule on several activity paths, so a credential whose NAME gave nothing away reached BBolt, the SSE payload and `mcpproxy activity list` in the clear. - argv: the inline `--flag=VALUE` form consulted only the flag-name rule, so `--endpoint=ghp_…` leaked while the identical `--endpoint ghp_…` masked. redactActivityString now ends in the value-shaped detector for EVERY leaf, which keeps the two spellings in step by construction. - headers: oauth's header matcher only recognised whole delimiter-separated segments, so `X-AuthToken` / `X-SecretValue` matched nothing. It now also splits CamelCase (X-Author-ID / X-Monkey-ID stay readable), and header values additionally go through the detector — neither rule alone is sufficient. - `upstream_servers add` emitted a second, unredacted config_change row built from the resolved values; it goes through the same redaction now. - audit surfaces mask with the new oauth.AuditMaskValue (a fixed marker) rather than MaskValue's `•••• ( chars)`: persisted, streamed and exported rows must not carry a credential's exact length and trailing bytes. The interactive/read surfaces keep MaskValue — the write path's unmask contract depends on its exact rendering. - a `*_json` argument that does not parse now fails CLOSED. Heuristic string scrubbing is regex-shaped and missed `{"API_KEY" "sk-live-…"}` entirely; the row records that the parameter was sent and its size, not its bytes. oauth gains *With variants of the three redactors so the mask can vary without forking the "which name is sensitive" rules. --- internal/oauth/logging.go | 104 +++++++- internal/oauth/logging_camelcase_test.go | 91 +++++++ internal/server/mcp.go | 12 +- internal/server/mcp_activity_args.go | 148 +++++++++-- .../server/mcp_activity_args_round3_test.go | 240 ++++++++++++++++++ internal/server/mcp_activity_args_test.go | 11 +- 6 files changed, 574 insertions(+), 32 deletions(-) create mode 100644 internal/oauth/logging_camelcase_test.go create mode 100644 internal/server/mcp_activity_args_round3_test.go diff --git a/internal/oauth/logging.go b/internal/oauth/logging.go index 87740814a..6c22b622f 100644 --- a/internal/oauth/logging.go +++ b/internal/oauth/logging.go @@ -47,6 +47,28 @@ var sensitiveHeaderSegments = map[string]bool{ var headerNameSegmentPattern = regexp.MustCompile(`[a-z0-9]+`) +// headerNameCamelSegmentPattern splits a CamelCase header name on its case +// transitions. Issue #1146 (review round 3): the delimiter-based pass above +// only ever sees whole `-`/`_`-separated segments, so `X-AuthToken` collapsed +// to the single segment "authtoken", matched nothing, and a credential under a +// custom header name was recorded and logged in the clear. Splitting on case +// transitions as well recovers "Auth" and "Token" without reopening the +// substring false positives the segment rule exists to prevent: "Author" and +// "Monkey" are still single segments that equal no marker. +// +// Only mixed-case words are extracted here — an all-caps run like +// `X-CUSTOM-TOKEN` has no case transition to split on and is already covered by +// the delimiter pass. +var headerNameCamelSegmentPattern = regexp.MustCompile(`[A-Z][a-z0-9]*`) + +// IsSensitiveHeaderName reports whether an HTTP header NAME looks like it +// carries a credential. Exported for callers that must decide masking from a +// name they hold apart from the value (the activity/audit redactor, issue +// #1146) so the rule stays defined in exactly one place. +func IsSensitiveHeaderName(name string) bool { + return isSensitiveHeaderKey(name) +} + func isSensitiveHeaderKey(name string) bool { if sensitiveHeaders[strings.ToLower(name)] { return true @@ -58,6 +80,12 @@ func isSensitiveHeaderKey(name string) bool { } } + for _, segment := range headerNameCamelSegmentPattern.FindAllString(name, -1) { + if sensitiveHeaderSegments[strings.ToLower(segment)] { + return true + } + } + return false } @@ -159,13 +187,28 @@ func RedactHeaders(headers http.Header) map[string]string { // "Convert to secret" affordance work on the UI side because the user // can confirm a recognisable suffix before approving. func RedactStringHeaders(headers map[string]string) map[string]string { + return RedactStringHeadersWith(headers, MaskValue) +} + +// RedactStringHeadersWith is RedactStringHeaders with a caller-chosen mask. +// +// Issue #1146 (review round 3): MaskValue's `•••• ( chars)` rendering +// is a deliberate trade for the INTERACTIVE surfaces — it lets an operator +// recognise which token is configured, and the write path (UnmaskHeaders) +// depends on being able to recognise it when a client echoes it back. On the +// AUDIT surfaces that trade is wrong: those rows are persisted, streamed and +// exported, so the length and trailing bytes become a durable fingerprint of +// every credential. Those callers pass AuditMaskValue instead. Splitting the +// mask out — rather than forking the "which name is sensitive" rules — keeps +// one definition of sensitivity behind both renderings. +func RedactStringHeadersWith(headers map[string]string, mask func(string) string) map[string]string { if headers == nil { return nil } redacted := make(map[string]string, len(headers)) for key, value := range headers { if isSensitiveHeaderKey(key) { - redacted[key] = MaskValue(value) + redacted[key] = mask(value) } else { redacted[key] = RedactSensitiveData(value) } @@ -228,12 +271,18 @@ func isSensitiveEnvKey(name string) bool { // with a RedactSensitiveData pass over the value as a defence-in-depth fallback // for embedded secrets (it leaves ordinary values like `debug` untouched). func RedactEnvValues(env map[string]string) map[string]string { + return RedactEnvValuesWith(env, MaskValue) +} + +// RedactEnvValuesWith is RedactEnvValues with a caller-chosen mask. See +// RedactStringHeadersWith for why the audit surfaces need a different one. +func RedactEnvValuesWith(env map[string]string, mask func(string) string) map[string]string { if env == nil { return nil } redacted := make(map[string]string, len(env)) for key, value := range env { - redacted[key] = maskedEnvValue(key, value) + redacted[key] = maskedEnvValueWith(key, value, mask) } return redacted } @@ -249,11 +298,15 @@ func RedactEnvValues(env map[string]string) map[string]string { // embedded userinfo password and any credential query params are masked while // scheme/host/db stay readable. func maskedEnvValue(key, value string) string { + return maskedEnvValueWith(key, value, MaskValue) +} + +func maskedEnvValueWith(key, value string, mask func(string) string) string { if isSensitiveEnvKey(key) { - return MaskValue(value) + return mask(value) } if strings.Contains(value, "://") { - return RedactURLQueryParams(value) + return RedactURLQueryParamsWith(value, mask) } return RedactSensitiveData(value) } @@ -312,6 +365,12 @@ func isSensitiveQueryParam(name string) bool { // are labels, not secrets. A URL with no query, or no sensitive params, is // returned unchanged. On parse failure it falls back to the regex RedactURL. func RedactURLQueryParams(rawURL string) string { + return RedactURLQueryParamsWith(rawURL, MaskValue) +} + +// RedactURLQueryParamsWith is RedactURLQueryParams with a caller-chosen mask. +// See RedactStringHeadersWith for why the audit surfaces need a different one. +func RedactURLQueryParamsWith(rawURL string, mask func(string) string) string { if rawURL == "" { return rawURL } @@ -328,7 +387,7 @@ func RedactURLQueryParams(rawURL string) string { // below. if u.User != nil { if pw, hasPW := u.User.Password(); hasPW && !isConfigReference(pw) { - u.User = url.UserPassword(u.User.Username(), MaskValue(pw)) + u.User = url.UserPassword(u.User.Username(), mask(pw)) changed = true } } @@ -361,7 +420,7 @@ func RedactURLQueryParams(rawURL string) string { if isConfigReference(decVal) { continue } - parts[i] = key + "=" + url.QueryEscape(MaskValue(decVal)) + parts[i] = key + "=" + url.QueryEscape(mask(decVal)) queryChanged = true } if queryChanged { @@ -392,6 +451,39 @@ func isConfigReference(v string) bool { return configRefPattern.MatchString(v) } +// IsConfigReference is the exported form of isConfigReference, for callers +// outside this package that must decide whether a value is a secret-store +// LABEL rather than a secret (issue #1146: the activity redactor runs a +// value-shaped detector over everything the name rules pass through, and a +// reference must not be mangled by it). +func IsConfigReference(v string) bool { + return isConfigReference(v) +} + +// AuditMaskValue is the mask for surfaces that PERSIST and EXPORT what they +// redact — the activity store, its SSE payloads and `mcpproxy activity list` +// (issue #1146, review round 3). +// +// Unlike MaskValue it carries no length and no trailing bytes. On an +// interactive surface those help an operator recognise which token is +// configured and are re-read within seconds; written into an audit row they +// become a durable fingerprint of the credential — a correlation handle across +// records and a materially smaller search space for a low-entropy secret. The +// audit surfaces have no need for the affordance, so they do not pay for it. +// +// ${keyring:…} / ${env:…} references still pass through unchanged: they are +// labels pointing at the secret store, and masking them would erase exactly the +// information the audit row exists to carry. +func AuditMaskValue(v string) string { + if v == "" { + return "(empty)" + } + if isConfigReference(v) { + return v + } + return "••••" +} + // MaskValue renders a string secret as `•••• ( chars)` for // human display. Returns "(empty)" for empty input, a 4-bullet preview // for values up to 4 characters (where revealing the last two would diff --git a/internal/oauth/logging_camelcase_test.go b/internal/oauth/logging_camelcase_test.go new file mode 100644 index 000000000..3d25d84de --- /dev/null +++ b/internal/oauth/logging_camelcase_test.go @@ -0,0 +1,91 @@ +package oauth + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +// Issue #1146, review round 3. The header-name matcher recognised a marker only +// as a WHOLE delimiter-separated segment, so a CamelCase custom header — +// `X-AuthToken`, `X-SecretValue` — matched nothing and its (opaque, unshaped) +// value passed through RedactSensitiveData untouched. Splitting on case +// transitions as well as delimiters closes that without reopening the +// substring false positives the segment rule was introduced to prevent. +func TestIsSensitiveHeaderName_CamelCaseCustomHeaders(t *testing.T) { + sensitive := []string{ + "X-AuthToken", + "X-SecretValue", + "XApiKeyHeader", + "X-SessionCookie", + "MyCredentialHeader", + // Still covered by the original delimiter-separated rule. + "X-CUSTOM-TOKEN", + "x-custom-token", + "Authorization", + } + for _, name := range sensitive { + assert.Truef(t, IsSensitiveHeaderName(name), "%s must be treated as credential-bearing", name) + } + + // The false positives the segment rule exists to prevent must stay clear: + // "Author" contains "auth", "Monkey" contains "key", "Passage" contains + // "pass". + benign := []string{ + "X-Author-ID", + "X-Monkey-ID", + "X-AuthorName", + "X-MonkeyId", + "X-PassageId", + "Content-Type", + "Accept", + } + for _, name := range benign { + assert.Falsef(t, IsSensitiveHeaderName(name), "%s must stay readable", name) + } +} + +// The audit/activity surfaces need a mask that carries no length and no +// trailing bytes, while the interactive surfaces keep MaskValue's recognisable +// rendering. Both must still pass ${keyring:…}/${env:…} references through, +// because those are labels rather than secrets. +func TestAuditMaskValue(t *testing.T) { + assert.Equal(t, "••••", AuditMaskValue("sk-live-abcdef1234567890")) + assert.Equal(t, "••••", AuditMaskValue("ab")) + assert.Equal(t, "(empty)", AuditMaskValue("")) + assert.Equal(t, "${keyring:gh}", AuditMaskValue("${keyring:gh}")) + assert.Equal(t, "${env:GITHUB_TOKEN}", AuditMaskValue("${env:GITHUB_TOKEN}")) + + assert.NotContains(t, AuditMaskValue("sk-live-abcdef1234567890"), "chars") + assert.NotContains(t, AuditMaskValue("sk-live-abcdef1234567890"), "90") +} + +// The *With variants let a caller choose the mask without duplicating the +// "which name is sensitive" rules; passing MaskValue must reproduce the +// existing exported behaviour exactly. +func TestRedactWithVariants_HonourTheSuppliedMask(t *testing.T) { + headers := map[string]string{"Authorization": "Bearer abcdefgh", "Accept": "application/json"} + assert.Equal(t, RedactStringHeaders(headers), RedactStringHeadersWith(headers, MaskValue)) + assert.Equal(t, "••••", RedactStringHeadersWith(headers, AuditMaskValue)["Authorization"]) + assert.Equal(t, "application/json", RedactStringHeadersWith(headers, AuditMaskValue)["Accept"]) + + env := map[string]string{"API_KEY": "sk-live-abcdef1234567890", "LOG_LEVEL": "debug"} + assert.Equal(t, RedactEnvValues(env), RedactEnvValuesWith(env, MaskValue)) + assert.Equal(t, "••••", RedactEnvValuesWith(env, AuditMaskValue)["API_KEY"]) + assert.Equal(t, "debug", RedactEnvValuesWith(env, AuditMaskValue)["LOG_LEVEL"]) + + const rawURL = "https://user:hunter2pass@host.test/mcp?token=leakmeleakmeleakme&page=2" + assert.Equal(t, RedactURLQueryParams(rawURL), RedactURLQueryParamsWith(rawURL, MaskValue)) + audited := RedactURLQueryParamsWith(rawURL, AuditMaskValue) + assert.NotContains(t, audited, "leakmeleakmeleakme") + assert.NotContains(t, audited, "hunter2pass") + assert.NotContains(t, audited, "chars") + assert.Contains(t, audited, "page=2") +} + +func TestIsConfigReference_Exported(t *testing.T) { + assert.True(t, IsConfigReference("${keyring:gh}")) + assert.True(t, IsConfigReference("${env:GITHUB_TOKEN}")) + assert.False(t, IsConfigReference("${env:GITHUB_TOKEN}garbage")) + assert.False(t, IsConfigReference("plain")) +} diff --git a/internal/server/mcp.go b/internal/server/mcp.go index 485a4eb15..f313db082 100644 --- a/internal/server/mcp.go +++ b/internal/server/mcp.go @@ -4734,7 +4734,15 @@ func (p *MCPProxyServer) handleAddUpstream(ctx context.Context, request mcp.Call } p.mainServer.OnUpstreamServerChange() - // Spec 024: Emit config change activity for server addition + // Spec 024: Emit config change activity for server addition. + // + // Issue #1146 (review round 3): this is a SECOND activity row for the + // same `add`, built from the resolved values rather than the request + // map, so it does not pass through activityArgsFromRequest. It has the + // same durability class — BBolt, SSE, `mcpproxy activity list` — and + // `url` routinely carries a credential in its query string, so it gets + // the same redaction. Masking one row and publishing the other is not a + // redaction policy. newValues := map[string]interface{}{ "protocol": protocol, "enabled": enabled, @@ -4746,7 +4754,7 @@ func (p *MCPProxyServer) handleAddUpstream(ctx context.Context, request mcp.Call if command != "" { newValues["command"] = command } - p.mainServer.runtime.EmitActivityConfigChange("server_added", name, "mcp", nil, nil, newValues) + p.mainServer.runtime.EmitActivityConfigChange("server_added", name, "mcp", nil, nil, redactActivityConfigValues(newValues)) } // Wait briefly for supervisor to reconcile and connect (if enabled) diff --git a/internal/server/mcp_activity_args.go b/internal/server/mcp_activity_args.go index 3d8b52687..926f256de 100644 --- a/internal/server/mcp_activity_args.go +++ b/internal/server/mcp_activity_args.go @@ -3,6 +3,7 @@ package server import ( "bytes" "encoding/json" + "fmt" "strings" "sync" @@ -29,10 +30,33 @@ import ( // deliberately NOT honoured here. Secrets are always masked. // // The goal is redacted-but-PRESENT: the operator must see THAT `API_KEY` -// changed, and recognise which credential it was, without the value ever -// landing in the store. oauth.MaskValue renders `•••• ( chars)`, -// matching the tray / Web UI convention, and passes ${keyring:…} / ${env:…} -// references through unchanged because those are labels, not secrets. +// changed, and which FIELD it was, without the value ever landing in the +// store. ${keyring:…} / ${env:…} references pass through unchanged because +// those are labels, not secrets. +// +// Why this file masks with oauth.AuditMaskValue and not oauth.MaskValue +// (issue #1146, review round 3): +// +// MaskValue renders `•••• ( chars)`. On the live read surfaces — +// the tray, the Web UI server form — that is a deliberate trade: it lets an +// operator recognise WHICH token is configured, and the write path +// (oauth.UnmaskHeaders / UnmaskEnvValues) recognises the exact rendering when +// a client echoes it back, so an edit of a neighbouring field cannot overwrite +// the real secret with its own mask. Those surfaces keep it. +// +// The activity store has neither property and a much longer life. Its rows +// are persisted in BBolt, streamed over SSE, exported by `mcpproxy activity +// list` and pasted into bug reports; nothing ever writes back through them. +// There the exact byte length and the trailing two bytes are not an +// affordance, they are a durable fingerprint — a handle for correlating one +// credential across records, and a materially smaller search space for a +// low-entropy one. So the audit surfaces use a fixed marker that carries +// neither. +// +// The value-shaped detector (security.MaskText) keeps its own four-rune +// prefix — `AKIA…****`, `ghp_…****`. That prefix is the credential TYPE, not +// its content: it is what tells the operator which vendor's key to rotate, +// and it carries no length. It is deliberately left as is. // // The whole request is captured rather than an allowlist of known field names: // an allowlist is exactly what rotted into this bug, and a future tool @@ -48,6 +72,15 @@ const ( // parsed before redaction so the record shows WHICH keys changed instead of // an opaque blob. jsonParamSuffix = "_json" + + // activityJSONNullMarker is how the literal `null` removal marker is + // recorded once it has been parsed. + activityJSONNullMarker = "null" + + // activityUnparsableJSONFormat is the fail-closed placeholder for a + // `*_json` argument that does not parse. It carries the byte count so the + // row still says something happened, and none of the bytes. + activityUnparsableJSONFormat = "" ) // activityArgsFromRequest builds the redacted activity-argument map from the @@ -71,21 +104,44 @@ func activityArgsFromRequest(request mcp.CallToolRequest) map[string]interface{} args := make(map[string]interface{}, len(raw)) for key, value := range raw { // The *_json parameters carry a serialized object; parse it so the - // walker can mask per key. On failure (including the literal "null" - // removal marker) fall back to scrubbing the raw string — a rejected - // mutation is still audit-relevant, so never drop it. + // walker can mask per key. if s, ok := value.(string); ok && strings.HasSuffix(key, jsonParamSuffix) { - var parsed interface{} - if err := json.Unmarshal([]byte(s), &parsed); err == nil && parsed != nil { - args[key] = redactActivityValue(strings.TrimSuffix(key, jsonParamSuffix), parsed) - continue - } + args[key] = redactActivityJSONParam(strings.TrimSuffix(key, jsonParamSuffix), s) + continue } args[key] = redactActivityValue(key, value) } return args } +// redactActivityJSONParam records one `*_json` tool parameter. +// +// Parsing is what makes per-key masking possible, so a payload that does not +// parse fails CLOSED (issue #1146, review round 3). The previous fallback — +// heuristic string scrubbing of the raw text — is regex-shaped and keyed on +// `=` / `: `, so a payload that is merely malformed +// JSON slips straight past it: `{"API_KEY" "sk-live-…"}` has no separator to +// match and was stored verbatim. The mutation is rejected by the handler in +// that case, but the activity row outlives the rejection. +// +// The row still records THAT the parameter was sent, and its size, because a +// rejected mutation is audit-relevant; only the bytes we could not understand +// are withheld. +func redactActivityJSONParam(key, raw string) interface{} { + var parsed interface{} + err := json.Unmarshal([]byte(raw), &parsed) + switch { + case err == nil && parsed != nil: + return redactActivityValue(key, parsed) + case err == nil: + // The literal `null` removal marker: valid JSON, carries nothing, and + // its presence is the audit signal ("this field was cleared"). + return activityJSONNullMarker + default: + return fmt.Sprintf(activityUnparsableJSONFormat, len(raw)) + } +} + // activityAttributedOps are the operations of the two management built-ins that // are handed the request — the only ones that can have acted on the caller's // `name`. An operation absent from this set (an inventory read, an unknown @@ -235,7 +291,7 @@ func redactActivityValue(key string, v interface{}) interface{} { if isHeaders { if s, ok := val.(string); ok { // HTTP header semantics: mask by header name. - out[k] = capActivityValue(oauth.RedactStringHeaders(map[string]string{k: s})[k]) + out[k] = redactActivityHeaderValue(k, s) continue } } @@ -260,22 +316,67 @@ func redactActivityValue(key string, v interface{}) interface{} { } // redactActivityString masks one string leaf. The default policy is the env-var -// rule (oauth.RedactEnvValues over a single pair), which is the package's single -// source of truth for "does this key name look like it holds a secret": it masks -// sensitive-looking keys with MaskValue, passes ${keyring:…}/${env:…} references +// rule (oauth.RedactEnvValuesWith over a single pair), which is the package's +// single source of truth for "does this key name look like it holds a secret": +// it masks sensitive-looking keys, passes ${keyring:…}/${env:…} references // through, URL-redacts connection strings, and runs RedactSensitiveData over // everything else as defence in depth. Over-masking a harmless field is the safe // direction; ordinary configuration (protocol, command, LOG_LEVEL, an image // named python:3.12) stays readable. +// +// Issue #1146, review round 3 — the lesson from the argv leak, applied to EVERY +// leaf: a key name is never the only rule. `--endpoint=ghp_…`, a credential +// under a header name no matcher recognises, a token pasted into a field nobody +// thought of — each is invisible to name matching and obvious to the +// value-shaped detector. So the name rule decides first (it is what keeps the +// record readable and says WHICH credential moved), and whatever it passes +// through is then offered to the detector. A value the name rule already masked +// is inert by the time the detector sees it. +// +// The mask is oauth.AuditMaskValue, not oauth.MaskValue — see the file header. func redactActivityString(key, s string) string { switch { case isActivityHeadersKey(key): - return capActivityValue(oauth.RedactStringHeaders(map[string]string{key: s})[key]) + return redactActivityHeaderValue(key, s) case strings.EqualFold(key, "url"): - return capActivityValue(oauth.RedactURLQueryParams(s)) + return capActivityValue(maskDetectedSecrets(oauth.RedactURLQueryParamsWith(s, oauth.AuditMaskValue))) default: - return capActivityValue(oauth.RedactEnvValues(map[string]string{key: s})[key]) + named := oauth.RedactEnvValuesWith(map[string]string{key: s}, oauth.AuditMaskValue)[key] + return capActivityValue(maskDetectedSecrets(named)) + } +} + +// redactActivityHeaderValue masks one HTTP header leaf, judged by the header +// NAME first and then by the value's own shape. +// +// Both rules are needed and neither is sufficient. oauth's name matcher cannot +// enumerate every custom credential header (issue #1146 round 3 widened it to +// split CamelCase, which is what recovers X-AuthToken / X-SecretValue, but +// `X-Zyx` holding a bearer token is still unknowable from the name); the +// detector cannot recognise an opaque value with no credential shape. Running +// both is the only combination that closes each other's gap. +func redactActivityHeaderValue(name, value string) string { + masked := oauth.RedactStringHeadersWith(map[string]string{name: value}, oauth.AuditMaskValue)[name] + return capActivityValue(maskDetectedSecrets(masked)) +} + +// redactActivityConfigValues redacts a flat before/after value map for a +// runtime config_change activity row. +// +// Issue #1146, review round 3: `upstream_servers add` emits TWO activity rows — +// the internal-tool row built by activityArgsFromRequest, and a separate +// config_change row built straight from the resolved request values. The second +// one was unredacted, so `add` with `https://host/mcp?token=…` masked the +// credential in one row and published it in the other. +func redactActivityConfigValues(values map[string]interface{}) map[string]interface{} { + if values == nil { + return nil } + out := make(map[string]interface{}, len(values)) + for key, value := range values { + out[key] = redactActivityValue(key, normalizeForRedaction(value)) + } + return out } // isActivityArgvKey reports whether a slice is a command-line argument vector. @@ -306,6 +407,13 @@ func isActivityArgvKey(key string) bool { // internal/security's detector is for, so it is reused rather than // re-invented as another regex here. // +// BOTH rules run on BOTH spellings. Round 3 found the inline form consulting +// only the first: `--endpoint=ghp_…` names nothing sensitive, so nothing ever +// looked at the value and the token was stored in the clear, while the +// identical `--endpoint ghp_…` was masked. redactActivityString now ends in the +// detector for every leaf, which is what keeps the two spellings in step by +// construction rather than by two call sites remembering to agree. +// // Everything else round-trips verbatim: package names, subcommands, paths and // ports are what make the record useful. func redactActivityArgv(argv []interface{}) []interface{} { @@ -323,7 +431,7 @@ func redactActivityArgv(argv []interface{}) []interface{} { } if maskNext { - out[i] = capActivityValue(oauth.MaskValue(s)) + out[i] = capActivityValue(oauth.AuditMaskValue(s)) maskNext = false continue } diff --git a/internal/server/mcp_activity_args_round3_test.go b/internal/server/mcp_activity_args_round3_test.go new file mode 100644 index 000000000..e1fff8d5c --- /dev/null +++ b/internal/server/mcp_activity_args_round3_test.go @@ -0,0 +1,240 @@ +package server + +import ( + "encoding/json" + "strings" + "testing" + + "github.com/mark3labs/mcp-go/mcp" + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" +) + +// Issue #1146, review round 3. Every case here is a place where the record was +// decided by a KEY NAME alone — the flag before an argv value, the header name, +// the config field — and a credential whose name gave nothing away walked +// straight into BBolt, the SSE payload and `mcpproxy activity list`. + +// argvCredentialShapes are credentials the value-shaped detector recognises on +// sight. The FLAG names are deliberately innocuous: none of them carries a +// marker from oauth.sensitiveEnvMarkers, so the key-name rule sees nothing. +var argvCredentialShapes = []struct { + name string + flag string + secret string +}{ + {"github token", "--endpoint", "ghp_1234567890abcdefghij1234567890abcdef"}, + {"aws access key id", "--endpoint", "AKIAIOSFODNN7EXAMPLE"}, + {"slack bot token", "--session", "xoxb-123456789012-1234567890123-AbCdEfGh"}, + {"openai project key", "--cookie", "sk-proj-abcdefghijklmnopqrstuvwxyz0123456789"}, +} + +// (1) MAJOR — redactActivityArgv had two masking rules and only the +// space-separated one consulted the value-shaped detector. `--endpoint=ghp_…` +// was routed through the key-name rule alone, matched nothing, and was stored +// in the clear. Both argv spellings must reach the same verdict. +func TestActivityArgs_ArgvInlineFlagValuesConsultTheValueDetector(t *testing.T) { + for _, shape := range argvCredentialShapes { + t.Run(shape.name, func(t *testing.T) { + inline := activityArgvFor(t, `["srv","`+shape.flag+`=`+shape.secret+`"]`) + require.Len(t, inline, 2) + assert.NotContainsf(t, inline[1], shape.secret, + "inline %s=VALUE recorded the credential in the clear", shape.flag) + assert.Contains(t, inline[1], shape.flag+"=", + "the flag name is the audit signal and must survive the mask") + + spaced := activityArgvFor(t, `["srv","`+shape.flag+`","`+shape.secret+`"]`) + require.Len(t, spaced, 3) + assert.NotContainsf(t, spaced[2], shape.secret, + "space-separated %s VALUE recorded the credential in the clear", shape.flag) + }) + } +} + +// (1') A positional credential — no flag at all — must be caught too. +func TestActivityArgs_PositionalArgvCredentialIsMasked(t *testing.T) { + argv := activityArgvFor(t, `["srv","AKIAIOSFODNN7EXAMPLE"]`) + require.Len(t, argv, 2) + assert.NotContains(t, argv[1], "AKIAIOSFODNN7EXAMPLE") +} + +func activityArgvFor(t *testing.T, argsJSON string) []interface{} { + t.Helper() + req := mcp.CallToolRequest{Params: mcp.CallToolParams{ + Name: "upstream_servers", + Arguments: map[string]interface{}{ + "operation": "add", + "name": "x", + "command": "npx", + "args_json": argsJSON, + }, + }} + argv, ok := activityArgsFromRequest(req)["args_json"].([]interface{}) + require.Truef(t, ok, "args_json must stay a list, got %T", activityArgsFromRequest(req)["args_json"]) + return argv +} + +// (2) Custom credential headers. oauth's header matcher recognises only whole +// delimiter-separated segments, so `X-AuthToken` / `X-SecretValue` fell through +// and an opaque value — one no value-shaped detector can recognise either — +// was recorded verbatim. +func TestActivityArgs_CustomCredentialHeaderNamesAreMasked(t *testing.T) { + const ( + authTokenSecret = "hdr-secret-98765" + secretValue = "hdr-value-13579" + ) + req := mcp.CallToolRequest{Params: mcp.CallToolParams{ + Name: "upstream_servers", + Arguments: map[string]interface{}{ + "operation": "patch", + "name": "github", + "headers_json": `{"X-AuthToken":"` + authTokenSecret + `","X-SecretValue":"` + secretValue + `","X-Session-Id":"` + testEnvSecret + `"}`, + }, + }} + + headers, ok := activityArgsFromRequest(req)["headers_json"].(map[string]interface{}) + require.Truef(t, ok, "headers_json must stay a map, got %T", activityArgsFromRequest(req)["headers_json"]) + + require.Contains(t, headers, "X-AuthToken", "the header NAME is the audit signal and must survive") + assert.NotContains(t, headers["X-AuthToken"], authTokenSecret, "X-AuthToken value recorded in the clear") + assert.NotContains(t, headers["X-SecretValue"], secretValue, "X-SecretValue value recorded in the clear") + assert.NotContains(t, headers["X-Session-Id"], testEnvSecret, "X-Session-Id value recorded in the clear") +} + +// (2') …and the same widening must not start masking ordinary headers, which +// is what makes the record worth reading. +func TestActivityArgs_OrdinaryHeadersStayReadable(t *testing.T) { + req := mcp.CallToolRequest{Params: mcp.CallToolParams{ + Name: "upstream_servers", + Arguments: map[string]interface{}{ + "operation": "patch", + "name": "github", + "headers_json": `{"X-Author-ID":"42","X-Monkey-ID":"capuchin","Accept":"application/json"}`, + }, + }} + headers, ok := activityArgsFromRequest(req)["headers_json"].(map[string]interface{}) + require.True(t, ok) + assert.Equal(t, "42", headers["X-Author-ID"]) + assert.Equal(t, "capuchin", headers["X-Monkey-ID"]) + assert.Equal(t, "application/json", headers["Accept"]) +} + +// (3) The `add` operation emitted a SECOND activity row (config_change) built +// from the raw request values, so a URL credential survived there even though +// the internal-tool row masked it. +func TestRedactActivityConfigValues_MasksURLCredential(t *testing.T) { + values := redactActivityConfigValues(map[string]interface{}{ + "protocol": "streamable-http", + "enabled": true, + "url": "https://host.test/mcp?token=" + testURLTokenValue, + "command": "npx", + }) + + encoded, err := json.Marshal(values) + require.NoError(t, err) + assert.NotContains(t, string(encoded), testURLTokenValue, + "the config_change row recorded the URL credential in the clear") + assert.Contains(t, values["url"], "https://host.test/mcp", "the endpoint must stay readable") + assert.Equal(t, "npx", values["command"]) + assert.Equal(t, true, values["enabled"]) +} + +// (4) The mask itself must not be a fingerprint. `••••90 (22 chars)` persisted +// across BBolt, SSE and the CLI hands a reader the exact length and the last +// two bytes of every credential; on the audit surfaces the mask carries no +// length and no trailing bytes. +func TestActivityArgs_MaskCarriesNoLengthOrTail(t *testing.T) { + req := mcp.CallToolRequest{Params: mcp.CallToolParams{ + Name: "upstream_servers", + Arguments: map[string]interface{}{ + "operation": "patch", + "name": "github", + "env_json": `{"API_KEY":"` + testEnvSecret + `"}`, + "headers_json": `{"Authorization":"` + testBearerSecret + `"}`, + "url": "https://host.test/mcp?token=" + testURLTokenValue, + }, + }} + args := activityArgsFromRequest(req) + + env, ok := args["env_json"].(map[string]interface{}) + require.True(t, ok) + masked, ok := env["API_KEY"].(string) + require.True(t, ok) + assert.NotContains(t, masked, "chars", "the mask must not carry the credential length") + assert.NotContains(t, masked, testEnvSecret[len(testEnvSecret)-2:], + "the mask must not carry the credential's trailing bytes") + + headers, ok := args["headers_json"].(map[string]interface{}) + require.True(t, ok) + authz, ok := headers["Authorization"].(string) + require.True(t, ok) + assert.NotContains(t, authz, "chars") + assert.NotContains(t, authz, testBearerSecret[len(testBearerSecret)-2:]) + + rawURL, ok := args["url"].(string) + require.True(t, ok) + assert.NotContains(t, rawURL, "chars") + assert.NotContains(t, rawURL, testURLTokenValue[len(testURLTokenValue)-2:]) +} + +// (5) A *_json argument that does not parse used to fall back to heuristic +// string scrubbing — which is regex-shaped and misses `{"API_KEY" "sk-live-…"}` +// entirely. A payload we could not understand must fail CLOSED. +func TestActivityArgs_UnparsableJSONFailsClosed(t *testing.T) { + req := mcp.CallToolRequest{Params: mcp.CallToolParams{ + Name: "upstream_servers", + Arguments: map[string]interface{}{ + "operation": "patch", + "name": "github", + "env_json": `{"API_KEY" "` + testEnvSecret + `"}`, + }, + }} + args := activityArgsFromRequest(req) + + require.Contains(t, args, "env_json", "a rejected mutation is still audit-relevant — record THAT it happened") + recorded, ok := args["env_json"].(string) + require.True(t, ok) + assert.NotContains(t, recorded, testEnvSecret, "unparsable JSON was stored in the clear") + assert.Contains(t, recorded, "unparsable", "the placeholder must say why the payload is missing") +} + +// (5') The literal `null` removal marker parses fine and carries nothing; it +// must stay legible rather than being replaced by the fail-closed placeholder. +func TestActivityArgs_NullJSONRemovalMarkerSurvives(t *testing.T) { + req := mcp.CallToolRequest{Params: mcp.CallToolParams{ + Name: "upstream_servers", + Arguments: map[string]interface{}{ + "operation": "patch", + "name": "github", + "env_json": `null`, + }, + }} + args := activityArgsFromRequest(req) + recorded, ok := args["env_json"].(string) + require.True(t, ok) + assert.Equal(t, "null", strings.TrimSpace(recorded)) +} + +// A credential-bearing connection string is masked whole once the value-shaped +// detector is in the chain, rather than kept structurally readable with only +// its password masked. That is a deliberate trade: the FIELD NAME is the audit +// signal, over-masking is the safe direction, and the detector's four-rune +// prefix still says which kind of URL it was. Pinned so it is a choice rather +// than a surprise. An ordinary URL with no embedded credential stays readable. +func TestActivityArgs_ConnectionStringsMaskWholeButPlainURLsStayReadable(t *testing.T) { + req := mcp.CallToolRequest{Params: mcp.CallToolParams{ + Name: "upstream_servers", + Arguments: map[string]interface{}{ + "operation": "patch", + "name": "github", + "env_json": `{"DATABASE_URL":"postgres://u:p4ssw0rd@db.test/app","SERVICE_URL":"https://host.test/health"}`, + }, + }} + env, ok := activityArgsFromRequest(req)["env_json"].(map[string]interface{}) + require.True(t, ok) + + require.Contains(t, env, "DATABASE_URL", "the field name is the audit signal and must survive") + assert.NotContains(t, env["DATABASE_URL"], "p4ssw0rd") + assert.Equal(t, "https://host.test/health", env["SERVICE_URL"], + "a URL with no credential in it must stay readable") +} diff --git a/internal/server/mcp_activity_args_test.go b/internal/server/mcp_activity_args_test.go index ccd0a43df..f2002b321 100644 --- a/internal/server/mcp_activity_args_test.go +++ b/internal/server/mcp_activity_args_test.go @@ -129,9 +129,12 @@ func TestActivityArgs_KeyringReferencePassesThrough(t *testing.T) { assert.Equal(t, "${keyring:gh}", env["API_KEY"]) } -// (b””) A malformed *_json parameter must still be recorded (scrubbed and -// capped), not silently dropped — a rejected mutation is audit-relevant too. -func TestActivityArgs_MalformedJSONParamIsScrubbedNotDropped(t *testing.T) { +// (b””) A malformed *_json parameter must still be RECORDED — a rejected +// mutation is audit-relevant too — but not recorded verbatim: review round 3 +// showed the old heuristic string scrubbing is regex-shaped and misses a +// payload that is merely malformed, so the placeholder reports that the +// parameter was sent, and its size, and withholds the bytes. +func TestActivityArgs_MalformedJSONParamIsRecordedAsPlaceholder(t *testing.T) { req := mcp.CallToolRequest{Params: mcp.CallToolParams{ Name: "upstream_servers", Arguments: map[string]interface{}{ @@ -142,7 +145,7 @@ func TestActivityArgs_MalformedJSONParamIsScrubbedNotDropped(t *testing.T) { }} args := activityArgsFromRequest(req) require.Contains(t, args, "env_json") - assert.Equal(t, `{not json`, args["env_json"]) + assert.Equal(t, "", args["env_json"]) } // Over-masking is the safe direction, but the log must stay readable: ordinary