feat: enable applications to redact headers from recovery handler

This commit is contained in:
Logan Garrett 2022-03-14 14:36:44 -07:00
parent 269f9add25
commit 998c7ca1b3
No known key found for this signature in database
GPG key ID: 79FFC2937BAAB215

View file

@ -78,6 +78,13 @@ func (r *bufferedReadCloser) Close() error {
return r.reader.Close()
}
// RemovedHeaders defines a list of HTTP headers that will be redacted from the
// request in the Recovery handler--if any logging or other output occurs, these
// headings will have value '<redacted>'.
var RemovedHeaders []string
const redacted = "<redacted>"
// PanicFunc defines a function to run after a panic, which allows you to set
// up custom logging, metrics, etc.
type PanicFunc func(ctx context.Context, err error, request string)
@ -105,6 +112,10 @@ func Recovery(onPanic PanicFunc) func(http.Handler) http.Handler {
r = r.WithContext(context.WithValue(r.Context(), bufContextKey, buf))
}
for _, v := range RemovedHeaders {
r.Header.Set(v, redacted)
}
// Recovering comes *after* the above so the buffer is not returned to
// the pool until after we print out its contents. This deferred func
// is used to recover from panics and deliberately left in-line.