mirror of
https://github.com/Fishwaldo/huma.git
synced 2025-03-15 19:31:27 +00:00
feat: enable applications to redact headers from recovery handler
This commit is contained in:
parent
269f9add25
commit
998c7ca1b3
1 changed files with 11 additions and 0 deletions
|
@ -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.
|
||||
|
|
Loading…
Add table
Reference in a new issue