-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecure.go
More file actions
51 lines (46 loc) · 1.45 KB
/
secure.go
File metadata and controls
51 lines (46 loc) · 1.45 KB
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
package api
import (
"net/http"
"strconv"
)
// SecureConfig configures the Secure headers middleware.
type SecureConfig struct {
ContentTypeNosniff bool // default: true → X-Content-Type-Options: nosniff
FrameDeny bool // default: true → X-Frame-Options: DENY
HSTSMaxAge int // default: 0 (disabled). If >0: Strict-Transport-Security
XSSProtection string // default: "1; mode=block"
ReferrerPolicy string // default: "strict-origin-when-cross-origin"
}
// Secure returns middleware that sets security response headers.
// With no arguments, it uses sensible defaults.
func Secure(cfg ...SecureConfig) Middleware {
c := SecureConfig{
ContentTypeNosniff: true,
FrameDeny: true,
XSSProtection: "1; mode=block",
ReferrerPolicy: "strict-origin-when-cross-origin",
}
if len(cfg) > 0 {
c = cfg[0]
}
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if c.ContentTypeNosniff {
w.Header().Set("X-Content-Type-Options", "nosniff")
}
if c.FrameDeny {
w.Header().Set("X-Frame-Options", "DENY")
}
if c.HSTSMaxAge > 0 {
w.Header().Set("Strict-Transport-Security", "max-age="+strconv.Itoa(c.HSTSMaxAge))
}
if c.XSSProtection != "" {
w.Header().Set("X-XSS-Protection", c.XSSProtection)
}
if c.ReferrerPolicy != "" {
w.Header().Set("Referrer-Policy", c.ReferrerPolicy)
}
next.ServeHTTP(w, r)
})
}
}