-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcsp.go
273 lines (228 loc) · 5.61 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package csp
import (
"fmt"
"sort"
"strings"
)
const (
DispositionEnforce = "enforce"
DispositionReport = "report"
ContentSecurityPolicy = "Content-Security-Policy"
ContentSecurityPolicyReportOnly = "Content-Security-Policy-Report-Only"
)
type Policy struct {
Disposition string
Directives Directives
}
type Directives []Directive
func (d Directives) String() string {
o := make([]string, 0)
for _, v := range d {
o = append(o, v.String())
}
return strings.Join(o, "; ")
}
type Directive struct {
Name string
Value []string
}
func (d Directive) String() string {
return d.Name + " " + strings.Join(d.Value, " ")
}
func (d Directive) Valid() error {
if err := IsValidDirectiveName(d.Name); err != nil {
return err
}
for _, v := range d.Value {
if strings.Contains(v, ",") {
return ErrCommaInDirectiveValue
}
if !validValueChars(v) {
return ErrInvalidValueChars
}
}
return nil
}
var (
ErrDuplicateDirective = fmt.Errorf("duplicate directive")
ErrDirectiveNameUnknown = fmt.Errorf("unknown directive name")
ErrDirectiveNameDeprecated = fmt.Errorf("deprecated directive name")
ErrCommaInDirectiveValue = fmt.Errorf("directive value contains comma")
ErrInvalidValueChars = fmt.Errorf("invalid characters in value")
)
func ParseDirectives(serializedPolicy string) (Directives, error) {
d := make(Directives, 0)
// For each token returned by strictly splitting serialized CSP
// on the U+003B SEMICOLON character (;):
tokens := strings.Split(serializedPolicy, ";")
for _, t := range tokens {
// Strip leading and trailing whitespace from token.
t = strings.TrimSpace(t)
// If token is an empty string, skip the remaining substeps
// and continue to the next item.
if len(t) == 0 {
continue
}
// Let directive name be the result of collecting
// a sequence of characters from token which are not space characters.
x := strings.SplitN(t, " ", 2)
// The name is a non-empty string
if len(x) == 0 || len(x[0]) == 0 {
continue
}
name := x[0]
// Check directive name
if err := IsValidDirectiveName(name); err != nil {
return nil, err
}
// If the set of directives already contains a directive
// whose name is a case insensitive match for directive name,
// ignore this instance of the directive and continue to the next token.
// The user agent SHOULD notify developers that a directive was ignored.
for _, dx := range d {
if strings.ToLower(dx.Name) == strings.ToLower(name) {
return nil, &ParseError{
Err: ErrDuplicateDirective,
Custom: fmt.Sprintf("directive '%v' is a duplicate", name),
}
}
}
// The value is a set of non-empty strings. The value set MAY be empty.
values := make([]string, 0)
if len(x) > 1 {
for _, v := range strings.Split(x[1], " ") {
if len(v) > 0 {
if strings.Contains(v, ",") {
return nil, ErrCommaInDirectiveValue
}
if !validValueChars(v) {
return nil, ErrInvalidValueChars
}
values = append(values, v)
}
}
}
// Add directive to directive set.
d = append(d, Directive{
Name: name,
Value: values,
})
}
return d, nil
}
func (d *Directives) AddDirective(v Directive) error {
if err := v.Valid(); err != nil {
return err
}
// add values to existing directive if already exists
added := false
for i := 0; i < len(*d); i++ {
if (*d)[i].Name == v.Name {
added = true
var valmap = map[string]struct{}{}
for _, val := range v.Value {
valmap[val] = struct{}{}
}
for _, val := range (*d)[i].Value {
valmap[val] = struct{}{}
}
var sorted sort.StringSlice
for val := range valmap {
sorted = append(sorted, val)
}
sorted.Sort()
(*d)[i].Value = sorted
}
}
// ... or add new directive
if !added {
var sorted = sort.StringSlice(v.Value)
sorted.Sort()
*d = append(*d, v)
}
return nil
}
func (d *Directives) RemoveDirectiveByName(name string) {
x := make(Directives, 0)
for _, v := range *d {
if v.Name != name {
x = append(x, v)
}
}
*d = x
}
func IsValidDirectiveName(name string) error {
// Verify name
// see also https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
// see also https://www.w3.org/TR/CSP3/#csp-directives
switch strings.ToLower(name) {
case "child-src":
case "connect-src":
case "default-src":
case "font-src":
case "frame-src":
case "img-src":
case "manifest-src":
case "media-src":
case "prefetch-src":
case "object-src":
case "script-src":
case "script-src-elem":
case "script-src-attr":
case "style-src":
case "style-src-elem":
case "style-src-attr":
case "worker-src":
case "base-uri":
case "plugin-types":
case "sandbox":
case "disown-opener":
case "form-action":
case "frame-ancestors":
case "navigate-to":
case "report-uri":
case "report-to":
case "upgrade-insecure-requests":
case "block-all-mixed-content":
case "require-sri-for":
// ok
case "reflected-xss":
// ok, deprecated from CSP 2
case "referrer":
// ok, deprecated, use Referrer-Policy header
case "policy-uri":
return &ParseError{
Err: ErrDirectiveNameDeprecated,
Custom: "policy-uri has been removed and is not supported",
}
default:
return &ParseError{
Err: ErrDirectiveNameUnknown,
Custom: fmt.Sprintf("directive name '%v' is unknown", name),
}
}
return nil
}
func validValueChars(str string) bool {
for _, r := range str {
if validValueChar(r) == -1 {
return false
}
}
return true
}
func validValueChar(r rune) rune {
if r == 0x09 {
return r
}
if r >= 0x20 && r <= 0x2b {
return r
}
if r >= 0x2d && r <= 0x3a {
return r
}
if r >= 0x3c && r <= 0x7e {
return r
}
return -1
}