-
Notifications
You must be signed in to change notification settings - Fork 1
/
referrerpolicy.go
71 lines (62 loc) · 1.72 KB
/
referrerpolicy.go
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
package bassinet
import (
"fmt"
"net/http"
"strings"
)
const (
// PolicyNoReferrer no-referrer policy
PolicyNoReferrer = iota
// PolicyNoReferrerWhenDowngrade no-referrer-when-downgrade policy
PolicyNoReferrerWhenDowngrade
// PolicySameOrigin same-origin policy
PolicySameOrigin
// PolicyOrigin origin policy
PolicyOrigin
// PolicyStrictOrigin strict-origin policy
PolicyStrictOrigin
// PolicyOriginWhenCrossOrigin origin-when-cross-origin policy
PolicyOriginWhenCrossOrigin
// PolicyStrictOriginWhenCrossOrigin strict-origin-when-cross-origin policy
PolicyStrictOriginWhenCrossOrigin
// PolicyUnsafeURL unsafe-url policy
PolicyUnsafeURL
)
// ReferrerPolicy sets the Referrer-Policy HTTP header to let authors control how browsers set the Referer header.
func ReferrerPolicy(policies []int) (Middleware, error) {
permittedPolicies := map[int]string{
0: "no-referrer",
1: "no-referrer-when-downgrade",
2: "same-origin",
3: "origin",
4: "strict-origin",
5: "origin-when-cross-origin",
6: "strict-origin-when-cross-origin",
7: "unsafe-url",
}
policies = unique(append(policies, PolicyNoReferrer))
headerValue := []string{}
for _, p := range policies {
policy, ok := permittedPolicies[p]
headerValue = append(headerValue, policy)
if !ok {
return nil, fmt.Errorf("Referrer-Policy does not support %d", p)
}
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Referrer-Policy", strings.Join(headerValue, ","))
next.ServeHTTP(w, r)
})
}, nil
}
func unique(ns []int) (r []int) {
seen := make(map[int]bool)
for _, n := range ns {
if _, ok := seen[n]; !ok {
r = append(r, n)
}
seen[n] = true
}
return r
}