126 lines
4.0 KiB
Go
126 lines
4.0 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" -- anywhere in a string. It is unanchored so
|
|
// a token embedded in prose is still caught, both in JSON string values and
|
|
// in free text.
|
|
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
|
|
}
|
|
|
|
// RedactString replaces a free-text value that has an unambiguous secret's
|
|
// shape, for fields that carry prose rather than a JSON document. Key-based
|
|
// redaction does not apply: there is no key.
|
|
func RedactString(value string) string {
|
|
if sensitiveValueShape(value) {
|
|
return redactedValue
|
|
}
|
|
return value
|
|
}
|
|
|
|
// pemPrivateKeyRe matches a PEM private-key block, header to footer.
|
|
var pemPrivateKeyRe = regexp.MustCompile(`-----BEGIN [A-Z ]*PRIVATE KEY-----[\s\S]*?-----END [A-Z ]*PRIVATE KEY-----`)
|
|
|
|
// RedactText replaces each secret-shaped span inside free text, keeping the
|
|
// rest, for long text such as tool output where blanking the whole value would
|
|
// hide everything around the secret.
|
|
func RedactText(value string) string {
|
|
if len(value) < 20 {
|
|
return value
|
|
}
|
|
value = jwtTokenRe.ReplaceAllString(value, redactedValue)
|
|
return pemPrivateKeyRe.ReplaceAllString(value, redactedValue)
|
|
}
|
|
|
|
func redactValue(value any) any {
|
|
switch typed := value.(type) {
|
|
case map[string]any:
|
|
for key, child := range typed {
|
|
if sensitiveKey(key) && !tokenCount(key, child) {
|
|
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 contains an unambiguous secret
|
|
// even under a non-sensitive key: a compact JWT anywhere in the string, 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-----")
|
|
}
|
|
|
|
// tokenCount reports a model usage count such as total_tokens: a number under
|
|
// a "tokens" key is a count, while a numeric value under other sensitive keys
|
|
// (an OTP, a PIN) is still a secret.
|
|
func tokenCount(key string, value any) bool {
|
|
_, number := value.(json.Number)
|
|
return number && strings.Contains(strings.ToLower(key), "tokens")
|
|
}
|
|
|
|
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
|
|
}
|