feat(audit): sanitize stored upstream responses

This commit is contained in:
2026-09-21 11:10:01 +03:30
parent 22d67c7c36
commit 14df432175
5 changed files with 145 additions and 0 deletions

View File

@@ -0,0 +1,48 @@
package audit
import (
"encoding/json"
"strings"
)
func SanitizeResponse(raw []byte, maskedCard string) []byte {
if len(raw) == 0 {
return nil
}
var value any
if err := json.Unmarshal(raw, &value); err != nil {
return nil
}
if maskedCard == "" {
maskedCard = "[redacted]"
}
redactCardFields(value, maskedCard)
sanitized, err := json.Marshal(value)
if err != nil {
return nil
}
return sanitized
}
func redactCardFields(value any, maskedCard string) {
switch current := value.(type) {
case map[string]any:
for key, child := range current {
if strings.EqualFold(key, "card") {
current[key] = maskedCard
continue
}
redactCardFields(child, maskedCard)
}
case []any:
for _, child := range current {
redactCardFields(child, maskedCard)
}
}
}