-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredact.go
More file actions
41 lines (35 loc) · 836 Bytes
/
redact.go
File metadata and controls
41 lines (35 loc) · 836 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package logging
import (
"net/http"
"net/url"
"slices"
"strings"
)
const redacted = "[REDACTED]"
func RedactSecret(_ string) string {
return redacted
}
func RedactURLString(raw string) string {
parsed, err := url.Parse(raw)
if err != nil {
return redacted
}
parsed.User = nil
parsed.RawQuery = ""
parsed.Fragment = ""
return parsed.String()
}
func RedactHeaderMap(headers http.Header, allowlist ...string) http.Header {
allowed := make([]string, 0, len(allowlist))
for _, key := range allowlist {
allowed = append(allowed, strings.ToLower(http.CanonicalHeaderKey(key)))
}
sanitized := make(http.Header)
for key, values := range headers {
if !slices.Contains(allowed, strings.ToLower(http.CanonicalHeaderKey(key))) {
continue
}
sanitized[key] = append([]string(nil), values...)
}
return sanitized
}