This commit is contained in:
@@ -5,22 +5,27 @@ import (
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"github.com/bytedance/sonic"
|
||||
)
|
||||
|
||||
var forbiddenFragments = []string{
|
||||
"password", "passwd", "credential", "operation_password", "verification_code", "captcha",
|
||||
"access_token", "refresh_token", "authorization", "cookie", "secret", "private_key", "public_key",
|
||||
"token", "access_token", "refresh_token", "id_token", "session_token", "sms_code", "api_key", "payment_key",
|
||||
"authorization", "cookie", "secret", "private_key", "public_key",
|
||||
"encoding_aes_key", "callback_token", "signature", "sign", "nonce", "media_id", "signed_url",
|
||||
"private_url", "qr_content", "id_card", "identity_number",
|
||||
}
|
||||
|
||||
var forbiddenTextPattern = regexp.MustCompile(`(?i)(bearer[[:space:]]+[a-z0-9._~+/=-]{8,}|-----BEGIN [A-Z ]*PRIVATE KEY-----|(?:access_token|refresh_token|id_token|session_token|authorization|cookie|secret|private_key|api_key|payment_key|signed_url|x-amz-signature|x-amz-credential)[[:space:]]*[=:][[:space:]]*[^&[:space:]]+)`)
|
||||
|
||||
// IsForbiddenField 判断字段是否禁止进入普通日志、审计或外部交互摘要。
|
||||
func IsForbiddenField(key string) bool {
|
||||
normalized := strings.ToLower(strings.NewReplacer("-", "_", ".", "_").Replace(key))
|
||||
if normalized == "credentials_configured" {
|
||||
normalized := normalizeFieldName(key)
|
||||
if normalized == "credentials_configured" || normalized == "token_present" {
|
||||
return false
|
||||
}
|
||||
for _, fragment := range forbiddenFragments {
|
||||
@@ -34,6 +39,21 @@ func IsForbiddenField(key string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func normalizeFieldName(key string) string {
|
||||
var normalized strings.Builder
|
||||
for index, char := range key {
|
||||
if unicode.IsUpper(char) && index > 0 {
|
||||
normalized.WriteByte('_')
|
||||
}
|
||||
if char == '-' || char == '.' {
|
||||
normalized.WriteByte('_')
|
||||
continue
|
||||
}
|
||||
normalized.WriteRune(unicode.ToLower(char))
|
||||
}
|
||||
return normalized.String()
|
||||
}
|
||||
|
||||
// MarshalSummary 递归删除禁止字段并返回 sonic 编码的安全 JSON。
|
||||
func MarshalSummary(value any) ([]byte, error) {
|
||||
if value == nil {
|
||||
@@ -60,15 +80,31 @@ func RemoveForbiddenFields(value any) {
|
||||
delete(typed, key)
|
||||
continue
|
||||
}
|
||||
if text, ok := item.(string); ok {
|
||||
typed[key] = SanitizeText(text)
|
||||
continue
|
||||
}
|
||||
RemoveForbiddenFields(item)
|
||||
}
|
||||
case []any:
|
||||
for _, item := range typed {
|
||||
for index, item := range typed {
|
||||
if text, ok := item.(string); ok {
|
||||
typed[index] = SanitizeText(text)
|
||||
continue
|
||||
}
|
||||
RemoveForbiddenFields(item)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SanitizeText 将疑似包含安全凭据的文本转换为不可逆摘要。
|
||||
func SanitizeText(value string) string {
|
||||
if !forbiddenTextPattern.MatchString(value) {
|
||||
return value
|
||||
}
|
||||
return TextSummary(value)
|
||||
}
|
||||
|
||||
// TextSummary 将不可信外部文本转换为不可逆大小和哈希摘要。
|
||||
func TextSummary(value string) string {
|
||||
if value == "" {
|
||||
|
||||
Reference in New Issue
Block a user