package mcp import ( "encoding/json" "fmt" "strings" "time" "unicode/utf8" ) // auditArgsMaxBytes bounds the argument digest written to the audit file. The // full payload never goes there: commands can contain passwords and request // bodies can be large. const auditArgsMaxBytes = 512 // auditCall writes one mcp.tool.call record per tools/call, matching the Win32 // server's fields (design §4.4). actor/role/source distinguish it from browser // sessions, which audit as the logged-in username. func (s *Server) auditCall(tool, deviceID string, args json.RawMessage, result any, rerr *rpcError, elapsed time.Duration) { fields := map[string]any{ "actor": "mcp", "role": "admin", "source": "mcp", "tool": tool, "device": "-", "ok": rerr == nil, "code": 0, "ms": elapsed.Milliseconds(), "args": summarizeArgs(args), } if deviceID != "" { fields["device"] = deviceID } if rerr != nil { fields["code"] = rerr.Code fields["result"] = rerr.Message } else { fields["result"] = resultSize(result) } s.log.Audit("mcp.tool.call", fields) } // redactedArgKeys name top-level arguments whose value is user content or a // credential. They are replaced by their size so the audit trail records that // something was sent, without copying it: terminal commands, chat text, HTTP // bodies and headers routinely carry passwords. var redactedArgKeys = map[string]bool{ "text": true, "message": true, "body": true, "headers": true, "password": true, "token": true, } // summarizeArgs renders a compact, truncated argument digest. A "command" // value is reduced to its first word and length so terminal passwords never // reach the audit file; other sensitive values are replaced by their size. The // full payload stays out of the audit file entirely. func summarizeArgs(raw json.RawMessage) string { if len(raw) == 0 || string(raw) == "null" { return "{}" } var obj map[string]any if err := json.Unmarshal(raw, &obj); err != nil { return fmt.Sprintf("", len(raw)) } for key, value := range obj { if key == "command" { if command, ok := value.(string); ok { obj[key] = commandDigest(command) } continue } if redactedArgKeys[key] { obj[key] = redactedDigest(value) } } b, err := json.Marshal(obj) if err != nil { return "{}" } return truncateUTF8(string(b), auditArgsMaxBytes) } // redactedDigest replaces a sensitive value with its serialized size. The // marker avoids angle brackets so encoding/json does not escape it. func redactedDigest(value any) string { b, err := json.Marshal(value) if err != nil { return "redacted" } return fmt.Sprintf("redacted(%d bytes)", len(b)) } func commandDigest(command string) string { first := command if i := strings.IndexAny(command, " \t"); i >= 0 { first = command[:i] } return fmt.Sprintf("%s (len=%d)", first, len(command)) } func resultSize(result any) string { b, err := json.Marshal(result) if err != nil { return "?" } return fmt.Sprintf("%d bytes", len(b)) } // truncateUTF8 cuts s to at most max bytes, including the ellipsis, without // splitting a rune. func truncateUTF8(s string, max int) string { if len(s) <= max { return s } cut := max - len("…") for cut > 0 && !utf8.RuneStart(s[cut]) { cut-- } return s[:cut] + "…" }