File: header.go

package info (click to toggle)
golang-github-henvic-httpretty 0.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 352 kB
  • sloc: sh: 61; makefile: 3
file content (114 lines) | stat: -rw-r--r-- 2,559 bytes parent folder | download | duplicates (3)
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// Package header can be used to sanitize HTTP request and response headers.
package header

import (
	"fmt"
	"net/http"
	"strings"
)

// Sanitize list of headers.
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ can be consulted for header syntax.
func Sanitize(sanitizers map[string]SanitizeHeaderFunc, headers http.Header) http.Header {
	var redacted = http.Header{}

	for k, values := range headers {
		if s, ok := sanitizers[http.CanonicalHeaderKey(k)]; ok {
			redacted[k] = sanitize(s, values)
			continue
		}

		redacted[k] = values
	}

	return redacted
}

func sanitize(s SanitizeHeaderFunc, values []string) []string {
	var redacted = []string{}

	for _, v := range values {
		redacted = append(redacted, s(v))
	}

	return redacted
}

// DefaultSanitizers contains a list of sanitizers to be used for common headers.
var DefaultSanitizers = map[string]SanitizeHeaderFunc{
	"Authorization":       AuthorizationSanitizer,
	"Set-Cookie":          SetCookieSanitizer,
	"Cookie":              CookieSanitizer,
	"Proxy-Authorization": AuthorizationSanitizer,
}

// SanitizeHeaderFunc implements sanitization for a header value.
type SanitizeHeaderFunc func(string) string

// AuthorizationSanitizer is used to sanitize Authorization and Proxy-Authorization headers.
func AuthorizationSanitizer(unsafe string) string {
	if unsafe == "" {
		return ""
	}

	directives := strings.SplitN(unsafe, " ", 2)

	l := 0

	if len(directives) > 1 {
		l = len(directives[1])
	}

	if l == 0 {
		return directives[0]
	}

	return directives[0] + " " + redact(l)
}

// SetCookieSanitizer is used to sanitize Set-Cookie header.
func SetCookieSanitizer(unsafe string) string {
	directives := strings.SplitN(unsafe, ";", 2)

	cookie := strings.SplitN(directives[0], "=", 2)

	l := 0

	if len(cookie) > 1 {
		l = len(cookie[1])
	}

	if len(directives) == 2 {
		return fmt.Sprintf("%s=%s; %s", cookie[0], redact(l), strings.TrimPrefix(directives[1], " "))
	}

	return fmt.Sprintf("%s=%s", cookie[0], redact(l))
}

// CookieSanitizer is used to sanitize Cookie header.
func CookieSanitizer(unsafe string) string {
	cookies := strings.Split(unsafe, ";")

	var list []string

	for _, unsafeCookie := range cookies {
		cookie := strings.SplitN(unsafeCookie, "=", 2)
		l := 0

		if len(cookie) > 1 {
			l = len(cookie[1])
		}

		list = append(list, fmt.Sprintf("%s=%s", cookie[0], redact(l)))
	}

	return strings.Join(list, "; ")
}

func redact(count int) string {
	if count == 0 {
		return ""
	}

	return "████████████████████"
}