-
Notifications
You must be signed in to change notification settings - Fork 4
/
csp.go
144 lines (134 loc) · 4.07 KB
/
csp.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package csp
import (
"fmt"
"github.com/urfave/negroni"
"net/http"
"strings"
)
// Helpful constants for CSP values
const (
Self = "'self'"
None = "'none'"
Any = "*"
CSPHeader = "Content-Security-Policy"
DefaultSrc = "default-src"
ScriptSrc = "script-src"
ConnectSrc = "connect-src"
ImgSrc = "img-src"
FontSrc = "font-src"
StyleSrc = "style-src"
ReportURI = "report-uri"
)
// Config is Content Security Policy Configuration. If you do not define a
// policy string it will not be included in the policy output
type Config struct {
WebSocket bool // enable dynamic websocket support in CSP
Default string // default-src CSP policy
Script string // script-src CSP policy
Connect string // connect-src CSP policy
Img string // img-src CSP policy
Style string // style-src CSP policy
Font string // font-src CSP policy
ReportURI string // report-uri CSP violation reports URI
IgnorePrefix []string // URL prefixes not to apply CSP too
}
// StarterConfig is a reasonable default set of policies.
//
// Content-Security-Policy: default-src 'none'; script-src 'self'; connect-src 'self'; img-src 'self'; style: 'self';
func StarterConfig() Config {
return Config{
Default: None,
Script: Self,
Connect: Self,
Img: Self,
Style: Self,
}
}
// CSP is a http middleware that configures CSP in the response header of an http request
type CSP struct {
*Config
handler http.HandlerFunc
}
// New returns a new instance of CSP Middleware
func New(config Config) *CSP {
instance := &CSP{Config: &config}
instance.handler = instance.handlerFunc()
return instance
}
// NegroniHandlerFunc returns a function with the negroni middleware interface
func (csp *CSP) NegroniHandlerFunc() negroni.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
csp.handler(rw, r)
if next != nil {
next(rw, r)
}
}
}
// Middleware returns a function with the http.Handler interface and provides
// github.com/justinas/alice integration
func (csp *CSP) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
csp.handler(rw, r)
if next != nil {
next.ServeHTTP(rw, r)
}
})
}
// HandlerFunc returns a function the http.HandlerFunc interface
func (csp *CSP) HandlerFunc() http.HandlerFunc {
return csp.handler
}
// handlerFunc is the http.HandlerFunc interface
func (csp *CSP) handlerFunc() http.HandlerFunc {
// Do as much work during construction as possible
var defaultPolicy, scriptPolicy, connectPolicy, imgPolicy, stylePolicy, fontPolicy, reportPolicy, baseConnectPolicy string
if csp.Default != "" {
defaultPolicy = fmt.Sprintf("%s %s;", DefaultSrc, csp.Default)
}
if csp.Script != "" {
scriptPolicy = fmt.Sprintf(" %s %s;", ScriptSrc, csp.Script)
}
if csp.Connect != "" {
baseConnectPolicy = fmt.Sprintf(" %s %s", ConnectSrc, csp.Connect)
}
if csp.Img != "" {
imgPolicy = fmt.Sprintf(" %s %s;", ImgSrc, csp.Img)
}
if csp.Style != "" {
stylePolicy = fmt.Sprintf(" %s %s;", StyleSrc, csp.Style)
}
if csp.Font != "" {
fontPolicy = fmt.Sprintf(" %s %s;", FontSrc, csp.Font)
}
if csp.ReportURI != "" {
reportPolicy = fmt.Sprintf(" %s %s;", ReportURI, csp.ReportURI)
}
if csp.WebSocket && len(csp.Connect) == 0 {
baseConnectPolicy = " " + ConnectSrc
}
preConnectPolicy := defaultPolicy + scriptPolicy
postConnectPolicy := imgPolicy + stylePolicy + fontPolicy + reportPolicy
return func(rw http.ResponseWriter, r *http.Request) {
for _, prefix := range csp.IgnorePrefix {
// exclude specified paths from CSP protection
if strings.HasPrefix(r.URL.Path, prefix) {
return
}
}
connectPolicy = baseConnectPolicy
if csp.WebSocket {
proto := "ws"
if r.TLS != nil {
proto = "wss"
}
connectPolicy = fmt.Sprintf("%s %s://%s", connectPolicy, proto, r.Host)
}
if len(connectPolicy) > 0 {
connectPolicy += ";"
}
policy := fmt.Sprintf("%s%s%s", preConnectPolicy, connectPolicy, postConnectPolicy)
if policy != "" {
rw.Header().Add(CSPHeader, policy)
}
}
}