FILE / ScuroNeko/Laniakea
tgapi/log_redaction.go
Исходный файл и его история в репозитории.
(fix): runtime reliability (tests): regression coverage (doc): v1.1 release notes
58 lines
1.2 KiB
Go
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)
|
|
}
|