| 1 | package config |
| 2 | |
| 3 | import ( |
| 4 | "net/url" |
| 5 | "strings" |
| 6 | ) |
| 7 | |
| 8 | // RedactMCPConfigValue hides a credential-looking env or header value. |
| 9 | func RedactMCPConfigValue(key, value string) string { |
| 10 | if looksSensitiveMCPKey(key) || looksSensitiveMCPValue(value) { |
| 11 | return "<redacted>" |
| 12 | } |
| 13 | return value |
| 14 | } |
| 15 | |
| 16 | func looksSensitiveMCPKey(key string) bool { |
| 17 | lower := strings.ToLower(strings.TrimSpace(key)) |
| 18 | for _, needle := range []string{"auth", "token", "secret", "credential", "api_key", "api-key", "apikey", "cookie"} { |
| 19 | if strings.Contains(lower, needle) { |
| 20 | return true |
| 21 | } |
| 22 | } |
| 23 | return false |
| 24 | } |
| 25 | |
| 26 | func looksSensitiveMCPQueryKey(key string) bool { |
| 27 | return strings.EqualFold(strings.TrimSpace(key), "key") || looksSensitiveMCPKey(key) |
| 28 | } |
| 29 | |
| 30 | func looksSensitiveMCPValue(value string) bool { |
| 31 | lower := strings.ToLower(value) |
| 32 | for _, needle := range []string{"access_token", "id_token", "refresh_token", "api_key", "api-key", "apikey", "bearer "} { |
| 33 | if strings.Contains(lower, needle) { |
| 34 | return true |
| 35 | } |
| 36 | } |
| 37 | return false |
| 38 | } |
| 39 | |
| 40 | // RedactMCPURL hides credential query values in an MCP endpoint. |
| 41 | func RedactMCPURL(raw string) string { |
| 42 | trimmed := strings.TrimSpace(raw) |
| 43 | if trimmed == "" { |
| 44 | return raw |
| 45 | } |
| 46 | u, err := url.Parse(trimmed) |
| 47 | if err != nil || u == nil { |
| 48 | if looksSensitiveMCPValue(raw) { |
| 49 | return "<redacted>" |
| 50 | } |
| 51 | return raw |
| 52 | } |
| 53 | q := u.Query() |
| 54 | changed := false |
| 55 | for key := range q { |
| 56 | if looksSensitiveMCPQueryKey(key) { |
| 57 | q.Set(key, "<redacted>") |
| 58 | changed = true |
| 59 | } |
| 60 | } |
| 61 | if !changed { |
| 62 | return raw |
| 63 | } |
| 64 | u.RawQuery = q.Encode() |
| 65 | return u.String() |
| 66 | } |
| 67 |