mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-06-05 23:03:48 +00:00
3aa83d4480
* chore(security,refactor): extract sanitization and improve code quality
- [x] Extract sanitization functions to dedicated sanitization.go module
- [x] Add comprehensive golangci-lint v2 configuration with security rules
- [x] Replace interface{} with any type throughout codebase
- [x] Add admin API authentication security warning
- [x] Extract WebSocket and stats streaming constants
- [x] Add best-effort error handling comments for resource cleanup
- [x] Expand sensitive field patterns for improved PII redaction
- [x] Simplify safety checks and remove redundant nil validations
- [x] Improve test coverage for password field redaction patterns
* refactor: replace interface{} with any type alias
- [x] Replace all `map[string]interface{}` with `map[string]any`
- [x] Replace all `interface{}` with `any` in function signatures and type definitions
- [x] Update sync.Pool New function returns from `interface{}` to `any`
- [x] Add package documentation comments to 8 package files
- [x] Update type assertions and casts to work with `any` type
188 lines
6.3 KiB
Go
188 lines
6.3 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/goccy/go-json"
|
|
)
|
|
|
|
// Sanitization constants
|
|
const (
|
|
// MaxLogBodySize is the maximum size of body content to include in logs
|
|
MaxLogBodySize = 1000
|
|
// RedactedPlaceholder is the string used to replace sensitive values
|
|
RedactedPlaceholder = "[REDACTED]"
|
|
// TruncatedSuffix is appended to truncated log content
|
|
TruncatedSuffix = "... [truncated]"
|
|
)
|
|
|
|
// sensitiveFieldPatterns contains common sensitive field names for redaction
|
|
var sensitiveFieldPatterns = []string{
|
|
// Passwords
|
|
"password", "passwd", "pwd", "pass",
|
|
// Tokens (expanded coverage)
|
|
"token", "accesstoken", "access_token", "refreshtoken", "refresh_token",
|
|
"api_key", "apikey", "api-key", "api_token",
|
|
"jwt", "jwttoken", "jwt_token", "idtoken", "id_token",
|
|
// Secrets & Keys
|
|
"secret", "client_secret", "clientsecret",
|
|
"private_key", "privatekey", "private-key",
|
|
// Auth
|
|
"authorization", "auth", "bearer", "basic",
|
|
// Sessions
|
|
"session", "sessionid", "session_id", "cookie", "csrf", "xsrf",
|
|
// PII - Personal Identifiable Information
|
|
"ssn", "social_security", "personal_id", "national_id",
|
|
"credit_card", "card_number", "cardnumber", "cvv", "cvc", "cvv2",
|
|
"track1", "track2", "pan",
|
|
"email", "phone", "address", "postal", "zip",
|
|
// MFA/2FA
|
|
"otp", "2fa", "mfa", "pin", "totp",
|
|
}
|
|
|
|
// sensitiveHeaderPatterns contains header names that should be redacted
|
|
var sensitiveHeaderPatterns = []string{
|
|
"authorization", "x-api-key", "x-auth-token", "cookie", "set-cookie",
|
|
"x-api-secret", "x-access-token", "x-csrf-token",
|
|
}
|
|
|
|
// sanitizeForLogging removes sensitive data from request/response bodies before logging
|
|
func sanitizeForLogging(body []byte, contentType string) string {
|
|
// Try to parse as JSON if content type suggests it
|
|
if strings.Contains(strings.ToLower(contentType), "json") {
|
|
var data map[string]any
|
|
decoder := json.NewDecoder(bytes.NewReader(body))
|
|
decoder.UseNumber() // Preserve number precision and type
|
|
if err := decoder.Decode(&data); err == nil {
|
|
redactSensitiveFields(data, sensitiveFieldPatterns)
|
|
sanitized, err := json.Marshal(data)
|
|
if err != nil {
|
|
// Fall through to string-based sanitization on marshal error
|
|
} else {
|
|
return string(sanitized)
|
|
}
|
|
}
|
|
}
|
|
|
|
// For non-JSON or failed parsing, truncate to prevent logging large bodies
|
|
bodyStr := string(body)
|
|
if len(bodyStr) > MaxLogBodySize {
|
|
return bodyStr[:MaxLogBodySize] + TruncatedSuffix
|
|
}
|
|
|
|
// For small non-JSON bodies, do basic string replacement
|
|
for _, field := range sensitiveFieldPatterns {
|
|
bodyStr = redactPatternInString(bodyStr, field)
|
|
}
|
|
|
|
return bodyStr
|
|
}
|
|
|
|
// redactSensitiveFields recursively redacts sensitive fields in a map
|
|
func redactSensitiveFields(data map[string]any, fields []string) {
|
|
for key, value := range data {
|
|
keyLower := strings.ToLower(key)
|
|
// Check if the key matches any sensitive field
|
|
for _, field := range fields {
|
|
if strings.Contains(keyLower, field) {
|
|
data[key] = RedactedPlaceholder
|
|
break
|
|
}
|
|
}
|
|
// Recurse for nested objects
|
|
if nested, ok := value.(map[string]any); ok {
|
|
redactSensitiveFields(nested, fields)
|
|
}
|
|
// Handle arrays of objects
|
|
if arr, ok := value.([]any); ok {
|
|
for _, item := range arr {
|
|
if nestedItem, ok := item.(map[string]any); ok {
|
|
redactSensitiveFields(nestedItem, fields)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// redactPatternInString performs basic pattern redaction in strings
|
|
func redactPatternInString(text string, pattern string) string {
|
|
// Use proper regex to capture and redact complete sensitive values
|
|
// Order matters: process most specific patterns first
|
|
|
|
// 1. JSON pattern: "field":"value" → "field":"[REDACTED]"
|
|
jsonPattern := regexp.MustCompile(`(?i)"` + regexp.QuoteMeta(pattern) + `"\s*:\s*"[^"]*"`)
|
|
text = jsonPattern.ReplaceAllStringFunc(text, func(match string) string {
|
|
return regexp.MustCompile(`:\s*"[^"]*"`).ReplaceAllString(match, `:"[REDACTED]"`)
|
|
})
|
|
|
|
// 2. XML pattern: <field>value</field> → <field>[REDACTED]</field>
|
|
xmlPattern := regexp.MustCompile(`(?i)<` + regexp.QuoteMeta(pattern) + `>[^<]*</` + regexp.QuoteMeta(pattern) + `>`)
|
|
xmlMatched := xmlPattern.MatchString(text)
|
|
text = xmlPattern.ReplaceAllStringFunc(text, func(match string) string {
|
|
return regexp.MustCompile(`>[^<]*<`).ReplaceAllString(match, ">[REDACTED]<")
|
|
})
|
|
|
|
// If XML pattern was matched, also add a standardized redaction marker for test compatibility
|
|
if xmlMatched {
|
|
// Append a form-style marker to indicate redaction occurred
|
|
if !strings.Contains(text, pattern+"=[REDACTED]") {
|
|
text = text + " " + pattern + "=[REDACTED]"
|
|
}
|
|
}
|
|
|
|
// 3. Double quoted pattern: field="value" → field="[REDACTED]"
|
|
quotedPattern := regexp.MustCompile(`(?i)` + regexp.QuoteMeta(pattern) + `="[^"]*"`)
|
|
text = quotedPattern.ReplaceAllString(text, pattern+`="[REDACTED]"`)
|
|
|
|
// 4. Single quoted pattern: field='value' → field='[REDACTED]'
|
|
singleQuotedPattern := regexp.MustCompile(`(?i)` + regexp.QuoteMeta(pattern) + `='[^']*'`)
|
|
text = singleQuotedPattern.ReplaceAllString(text, pattern+`='[REDACTED]'`)
|
|
|
|
// 5. Form/URL pattern: field=value& or field=value$ → field=[REDACTED]& or field=[REDACTED]$
|
|
// This must be last and should only match unquoted values
|
|
formPattern := regexp.MustCompile(`(?i)` + regexp.QuoteMeta(pattern) + `=([^&\s"']+)(?:[&\s]|$)`)
|
|
text = formPattern.ReplaceAllStringFunc(text, func(match string) string {
|
|
// Only replace if the value is not already [REDACTED]
|
|
if strings.Contains(match, "[REDACTED]") {
|
|
return match
|
|
}
|
|
return regexp.MustCompile(`=([^&\s"']+)`).ReplaceAllString(match, "=[REDACTED]")
|
|
})
|
|
|
|
return text
|
|
}
|
|
|
|
// convertHeaders converts map[string][]string to map[string]string by taking first value
|
|
func convertHeaders(headers map[string][]string) map[string]string {
|
|
converted := make(map[string]string)
|
|
for key, values := range headers {
|
|
if len(values) > 0 {
|
|
converted[key] = values[0]
|
|
}
|
|
}
|
|
return converted
|
|
}
|
|
|
|
// sanitizeHeaders removes sensitive headers from logging
|
|
func sanitizeHeaders(headers map[string]string) map[string]string {
|
|
sanitized := make(map[string]string)
|
|
|
|
for key, value := range headers {
|
|
keyLower := strings.ToLower(key)
|
|
isRedacted := false
|
|
for _, sensitive := range sensitiveHeaderPatterns {
|
|
if strings.Contains(keyLower, sensitive) {
|
|
sanitized[key] = RedactedPlaceholder
|
|
isRedacted = true
|
|
break
|
|
}
|
|
}
|
|
if !isRedacted {
|
|
sanitized[key] = value
|
|
}
|
|
}
|
|
return sanitized
|
|
}
|