Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions internal/logger/sanitize/sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 2 additions & 9 deletions internal/server/tool_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -349,21 +349,14 @@ 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 {
logger.LogError("client", "MCP %s call failed, session=%s, error=%v", operationName, sessionID, err)
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
}

Expand Down
5 changes: 3 additions & 2 deletions internal/server/tool_registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
})

Expand Down
Loading