mirror of
https://github.com/caddyserver/caddy.git
synced 2026-05-18 21:49:14 -04:00
* admin: Redact sensitive request headers in API logs * Fix govulncheck and typed atomic lint failures * Sync Go module metadata after dependency downgrade
55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package internal
|
|
|
|
import (
|
|
"net/http"
|
|
"strings"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
)
|
|
|
|
// LoggableHTTPHeader makes an HTTP header loggable with zap.Object().
|
|
// Headers with potentially sensitive information (Cookie, Set-Cookie,
|
|
// Authorization, and Proxy-Authorization) are logged with empty values.
|
|
type LoggableHTTPHeader struct {
|
|
http.Header
|
|
|
|
ShouldLogCredentials bool
|
|
}
|
|
|
|
// MarshalLogObject satisfies the zapcore.ObjectMarshaler interface.
|
|
func (h LoggableHTTPHeader) MarshalLogObject(enc zapcore.ObjectEncoder) error {
|
|
if h.Header == nil {
|
|
return nil
|
|
}
|
|
for key, val := range h.Header {
|
|
if !h.ShouldLogCredentials {
|
|
switch strings.ToLower(key) {
|
|
case "cookie", "set-cookie", "authorization", "proxy-authorization":
|
|
val = []string{"REDACTED"} // see #5669. I still think ▒▒▒▒ would be cool.
|
|
}
|
|
}
|
|
enc.AddArray(key, LoggableStringArray(val))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// LoggableStringArray makes a slice of strings marshalable for logging.
|
|
type LoggableStringArray []string
|
|
|
|
// MarshalLogArray satisfies the zapcore.ArrayMarshaler interface.
|
|
func (sa LoggableStringArray) MarshalLogArray(enc zapcore.ArrayEncoder) error {
|
|
if sa == nil {
|
|
return nil
|
|
}
|
|
for _, s := range sa {
|
|
enc.AppendString(s)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Interface guards
|
|
var (
|
|
_ zapcore.ObjectMarshaler = (*LoggableHTTPHeader)(nil)
|
|
_ zapcore.ArrayMarshaler = (*LoggableStringArray)(nil)
|
|
)
|