diff --git a/internal/logger/sanitize/sanitize.go b/internal/logger/sanitize/sanitize.go index 98c02ef6e..74783bca1 100644 --- a/internal/logger/sanitize/sanitize.go +++ b/internal/logger/sanitize/sanitize.go @@ -50,6 +50,14 @@ var SecretPatterns = []*regexp.Regexp{ regexp.MustCompile(`(?i)"(token|password|passwd|pwd|apikey|api_key|api-key|secret|client_secret|api_secret|authorization|auth|key|private_key|public_key|credentials|credential|access_token|refresh_token|bearer_token)"\s*:\s*"[^"]{1,}"`), } +// MarshalAndSanitize marshals value to JSON and sanitizes the result to redact secrets. +// If marshaling fails, it returns a sanitized empty string rather than surfacing a +// logging-only error — callers should use this only in best-effort logging contexts. +func MarshalAndSanitize(value any) string { + resultJSON, _ := json.Marshal(value) + return SanitizeString(string(resultJSON)) +} + // SanitizeString replaces potential secrets in a string with [REDACTED] func SanitizeString(message string) string { result := message diff --git a/internal/server/tool_registry.go b/internal/server/tool_registry.go index fe98c3b00..f6687703f 100644 --- a/internal/server/tool_registry.go +++ b/internal/server/tool_registry.go @@ -285,7 +285,7 @@ func (us *UnifiedServer) registerToolsFromBackend(serverID string) error { if err != nil { logger.LogError("client", "MCP tool call error, session=%s, tool=%s, error=%v", sessionID, toolNameCopy, err) } else { - logger.LogInfo("client", "MCP tool call response, session=%s, tool=%s, result=%s", sessionID, toolNameCopy, marshalAndSanitizeForLog(data)) + logger.LogInfo("client", "MCP tool call response, session=%s, tool=%s, result=%s", sessionID, toolNameCopy, sanitize.MarshalAndSanitize(data)) } return result, data, err @@ -349,13 +349,6 @@ func (us *UnifiedServer) callSysServer(toolName string) (interface{}, error) { return result, nil } -func marshalAndSanitizeForLog(value interface{}) string { - // Best-effort logging helper: if marshaling fails, we intentionally keep logging - // a sanitized empty string rather than surfacing an additional logging-only error. - resultJSON, _ := json.Marshal(value) - return sanitize.SanitizeString(string(resultJSON)) -} - func (us *UnifiedServer) callAndLogSysTool(sessionID, operationName, sysToolName string) (*sdk.CallToolResult, interface{}, error) { result, err := us.callSysServer(sysToolName) if err != nil { @@ -363,7 +356,7 @@ func (us *UnifiedServer) callAndLogSysTool(sessionID, operationName, sysToolName return mcp.NewErrorCallToolResult(err) } - logger.LogInfo("client", "MCP %s response, session=%s, result=%s", operationName, sessionID, marshalAndSanitizeForLog(result)) + logger.LogInfo("client", "MCP %s response, session=%s, result=%s", operationName, sessionID, sanitize.MarshalAndSanitize(result)) return nil, result, nil } diff --git a/internal/server/tool_registry_test.go b/internal/server/tool_registry_test.go index 6bb21f066..9e11589e6 100644 --- a/internal/server/tool_registry_test.go +++ b/internal/server/tool_registry_test.go @@ -8,6 +8,7 @@ import ( "testing" "github.com/github/gh-aw-mcpg/internal/config" + "github.com/github/gh-aw-mcpg/internal/logger/sanitize" sdk "github.com/modelcontextprotocol/go-sdk/mcp" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -496,11 +497,11 @@ func TestCallSysServer_UnknownTool(t *testing.T) { require.Error(err, "callSysServer with unknown tool should return an error") } -func TestMarshalAndSanitizeForLog_RedactsSecrets(t *testing.T) { +func TestSanitizeMarshalAndSanitize_RedactsSecrets(t *testing.T) { assert := assert.New(t) const secret = "ghp_1234567890123456789012345678901234567890" - sanitized := marshalAndSanitizeForLog(map[string]interface{}{ + sanitized := sanitize.MarshalAndSanitize(map[string]interface{}{ "token": secret, })