(new): rich message support
(fix): runtime reliability (tests): regression coverage (doc): v1.1 release notes
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user