Files
archie/internal/webhookguard/redact.go
T

108 lines
3.7 KiB
Go

package webhookguard
import (
"encoding/json"
"fmt"
"regexp"
"strings"
)
// redactedValue replaces a value whose key (or shape) looks sensitive.
const redactedValue = "[redacted]"
// SensitiveKeyMarkers are key substrings whose values are redacted.
var SensitiveKeyMarkers = []string{
"token", "secret", "password", "passwd", "pwd", "passphrase",
"api_key", "apikey", "api_secret", "client_secret", "private_key", "secret_key",
"signing_key", "authorization", "bearer", "credential", "cookie", "session",
"access_token", "refresh_token", "id_token", "bot_token", "webhook_secret",
"x_api_key", "x_auth_token", "jwt", "oauth", "csrf", "otp", "signature",
}
// jwtTokenRe matches a compact JWT (header.payload.signature) -- the header's
// base64url always begins "eyJ" -- which is an unambiguous secret token even
// when a sender puts it under a key the name heuristic misses.
var jwtTokenRe = regexp.MustCompile(`^eyJ[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+$`)
// RedactPayload replaces values under sensitive keys, or with secret-shaped
// values, with a marker and returns the re-encoded JSON. Best-effort.
func RedactPayload(payload []byte) ([]byte, error) {
var value any
decoder := json.NewDecoder(strings.NewReader(string(payload)))
decoder.UseNumber()
if err := decoder.Decode(&value); err != nil {
return nil, fmt.Errorf("redact payload: decode: %w", err)
}
redacted := redactValue(value)
encoded, err := json.Marshal(redacted)
if err != nil {
return nil, fmt.Errorf("redact payload: encode: %w", err)
}
return encoded, nil
}
func redactValue(value any) any {
switch typed := value.(type) {
case map[string]any:
for key, child := range typed {
if sensitiveKey(key) {
typed[key] = redactedValue
continue
}
typed[key] = redactValue(child)
}
return typed
case []any:
for i, child := range typed {
typed[i] = redactValue(child)
}
return typed
case string:
if sensitiveValueShape(typed) {
return redactedValue
}
return typed
default:
return value
}
}
// sensitiveValueShape reports whether a string is an unambiguous secret even
// under a non-sensitive key: a compact JWT or a PEM private-key block. These
// shapes are never a meaningful field-mapping value, so redacting them does
// not hurt the schema-by-example use case.
func sensitiveValueShape(s string) bool {
if len(s) < 20 {
return false
}
if jwtTokenRe.MatchString(s) {
return true
}
return strings.Contains(s, "-----BEGIN") && strings.Contains(s, "PRIVATE KEY-----")
}
func sensitiveKey(key string) bool {
normalized := strings.NewReplacer("-", "_", " ", "_").Replace(strings.ToLower(key))
for _, marker := range SensitiveKeyMarkers {
if strings.Contains(normalized, marker) {
return true
}
}
return false
}
// embeddedSecretRe matches secrets written inside a longer value, such as a
// token in a shell command: credential-carrying headers and flags, JWTs, PEM
// private keys, and token formats whose prefix marks them as credentials.
var embeddedSecretRe = regexp.MustCompile(`(?i)(?:(?:authorization|x-api-key|x-auth-token|private-token)\s*:\s*(?:bearer\s+|basic\s+|token\s+)?|bearer\s+|--?(?:token|password|api-key)[=\s]+)['"]?[^\s'"]+` +
`|eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+` +
`|-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?(?:-----END [A-Z ]*PRIVATE KEY-----|$)` +
`|\b(?:gh[pousr]_[A-Za-z0-9]{20,}|github_pat_[A-Za-z0-9_]{20,}|glpat-[A-Za-z0-9_-]{20,}|sk-[A-Za-z0-9_-]{20,}|xox[abprs]-[A-Za-z0-9-]{10,}|AKIA[0-9A-Z]{16})` +
`|://[^/\s:@]+:[^/\s@]+@`)
// ScrubSecrets masks every secret-shaped run inside s and leaves the rest,
// so a command stays readable without the credential it carried.
func ScrubSecrets(s string) string {
return embeddedSecretRe.ReplaceAllString(s, redactedValue)
}