FILE / ScuroNeko/Laniakea

tgapi/log_redaction.go

Исходный файл и его история в репозитории.
FILE v1.1.0
Files
Laniakea/tgapi/log_redaction.go
T
ScuroNeko f03a081ed6
Golang lint / lint (pull_request) Successful in 1m20s
Golang lint / lint (push) Successful in 4m8s
(new): rich message support
(fix): runtime reliability
(tests): regression coverage
(doc): v1.1 release notes
2026-08-12 16:34:44 +03:00

58 lines
1.2 KiB
Go

package tgapi
import (
"encoding/json"
"fmt"
"strings"
)
const redactedLogValue = "<REDACTED>"
var sensitiveLogFields = map[string]struct{}{
"callback_data": {},
"credentials": {},
"data": {},
"invoice_payload": {},
"payload": {},
"provider_data": {},
"provider_token": {},
"secret": {},
"secret_token": {},
"token": {},
"web_app_query_id": {},
}
func redactRequestLog(data []byte) string {
var value any
if err := json.Unmarshal(data, &value); err != nil {
return fmt.Sprintf("<invalid JSON omitted: %d bytes>", len(data))
}
redactLogValue(value)
redacted, err := json.Marshal(value)
if err != nil {
return fmt.Sprintf("<unavailable JSON omitted: %d bytes>", len(data))
}
return string(redacted)
}
func redactLogValue(value any) {
switch value := value.(type) {
case map[string]any:
for key, item := range value {
if _, sensitive := sensitiveLogFields[strings.ToLower(key)]; sensitive {
value[key] = redactedLogValue
continue
}
redactLogValue(item)
}
case []any:
for _, item := range value {
redactLogValue(item)
}
}
}
func responseLogSummary(method string, size int) string {
return fmt.Sprintf("method=%s bytes=%d body=omitted", method, size)
}