-
Notifications
You must be signed in to change notification settings - Fork 0
/
owasp.go
61 lines (52 loc) · 1.72 KB
/
owasp.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
package owasp
import (
"flag"
"net/http"
"strings"
"github.com/ViBiOh/httputils/v2/pkg/tools"
)
// Config of package
type Config struct {
csp *string
hsts *bool
frameOptions *string
}
// App of package
type App struct {
csp string
hsts bool
frameOptions string
}
// Flags adds flags for configuring package
func Flags(fs *flag.FlagSet, prefix string) Config {
return Config{
csp: tools.NewFlag(prefix, "owasp").Name("Csp").Default("default-src 'self'; base-uri 'self'").Label("Content-Security-Policy").ToString(fs),
hsts: tools.NewFlag(prefix, "owasp").Name("Hsts").Default(true).Label("Indicate Strict Transport Security").ToBool(fs),
frameOptions: tools.NewFlag(prefix, "owasp").Name("FrameOptions").Default("deny").Label("X-Frame-Options").ToString(fs),
}
}
// New creates new App from Config
func New(config Config) *App {
return &App{
csp: strings.TrimSpace(*config.csp),
hsts: *config.hsts,
frameOptions: strings.TrimSpace(*config.frameOptions),
}
}
// Handler for net/http package allowing owasp header
func (a App) Handler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Security-Policy", a.csp)
w.Header().Set("Referrer-Policy", "strict-origin-when-cross-origin")
w.Header().Set("X-Frame-Options", a.frameOptions)
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Header().Set("X-Xss-Protection", "1; mode=block")
w.Header().Set("X-Permitted-Cross-Domain-Policies", "none")
if a.hsts {
w.Header().Set("Strict-Transport-Security", "max-age=10886400")
}
if next != nil {
next.ServeHTTP(&middleware{w, tools.IsRoot(r), false}, r)
}
})
}