Files
SimpleRemoter/server/go/mcp/audit.go
T
yuanyuanxianganddeepseek-flash 1076e7614a Feature(Go): per-device locks, a global call gate and tool-call audit
Add the three cross-cutting guards the Go MCP endpoint needs before any
tool talks to a client. Each tools/call now takes one of MaxConcurrency
global slots and, for single-host tools, that host's device lock; both are
non-blocking, so contention returns -32003 instead of queueing. Panicking
handlers are converted to -32000 rather than dropping the connection.

Write one mcp.tool.call audit record per named call, including rejected
ones: actor/role/source, tool, device, ok/code, duration, a size-only
result summary and a truncated argument digest. Commands keep only their
first word plus length, and free-form or credential fields (text, message,
body, headers, password, token) are replaced by their size, so secrets do
not reach the audit file.

Co-Authored-By: deepseek-flash
2026-11-12 21:05:37 +01:00

124 lines
3.3 KiB
Go

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("<unparsed %d bytes>", 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] + "…"
}