-
Notifications
You must be signed in to change notification settings - Fork 3
/
content-security-policy_test.go
325 lines (277 loc) · 8.01 KB
/
content-security-policy_test.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package helmet
import (
"fmt"
"strings"
"testing"
)
func TestContentSecurityPolicy_New(t *testing.T) {
t.Parallel()
t.Run("Nil", func(t *testing.T) {
t.Parallel()
csp := NewContentSecurityPolicy(nil)
if csp.policies == nil {
t.Errorf("Policies should not be nil\n")
}
if len(csp.policies) != 0 {
t.Errorf("There should be zero policies/sources\n")
}
if csp.cache != "" {
t.Errorf("Cache should not be set\tActual: %s\n", csp.cache)
}
})
testCases := []struct {
name string
policies map[CSPDirective][]CSPSource
}{
{
name: "Single Directive",
policies: map[CSPDirective][]CSPSource{
DirectiveDefaultSrc: {SourceNone},
},
},
{
name: "Multiple Directives",
policies: map[CSPDirective][]CSPSource{
DirectiveDefaultSrc: {SourceNone},
DirectiveScriptSrc: {SourceSelf, SourceUnsafeInline},
},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
csp := NewContentSecurityPolicy(tc.policies)
if len(csp.policies) != len(tc.policies) {
t.Errorf("Length doesn't match\tExpected: %d\tActual: %d\n", len(tc.policies), len(csp.policies))
}
for policy, sources := range csp.policies {
expectedSources, ok := tc.policies[policy]
if !ok {
t.Errorf("Missing policy\tExpected: %s\n", policy)
}
for i, source := range sources {
if source != expectedSources[i] {
t.Errorf("Missing source\tExpected: %s\n", source)
}
}
}
if csp.cache != "" {
t.Errorf("Cache should not be set\tCache: %s\n", csp.cache)
}
})
}
}
func TestContentSecurityPolicy_Empty(t *testing.T) {
t.Parallel()
csp := EmptyContentSecurityPolicy()
if csp.policies == nil {
t.Errorf("Policies should not be nil\n")
}
if len(csp.policies) != 0 {
t.Errorf("There should be zero policies/sources\n")
}
if csp.cache != "" {
t.Errorf("Cache should not be set\tActual: %s\n", csp.cache)
}
}
func TestCSP_Add(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
directive CSPDirective
sources []CSPSource
expectedOk bool
}{
{name: "Empty", directive: "", expectedOk: false},
{name: "Default Directive", directive: DirectiveDefaultSrc, sources: []CSPSource{SourceNone}, expectedOk: true},
{name: "No Sources", directive: DirectiveDefaultSrc, sources: []CSPSource{}, expectedOk: true},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
csp := EmptyContentSecurityPolicy()
csp.Add(tc.directive, tc.sources...)
// make sure directive is now in CSP policies
if _, ok := csp.policies[tc.directive]; ok != tc.expectedOk {
t.Errorf("Directive is missing\tDirective: %s\n", tc.directive)
}
// next part requires there to be sources
if tc.expectedOk == false {
return
}
// make sure the sources were added correctly
for i, source := range csp.policies[tc.directive] {
if source != tc.sources[i] {
t.Errorf("Index: %d\tExpected: %s\tActual: %s\n", i, tc.sources[i], source)
}
}
})
}
}
func TestCSP_Create(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
directive CSPDirective
expectedOk bool
}{
{name: "Empty", directive: "", expectedOk: false},
{name: "Default Directive", directive: DirectiveDefaultSrc, expectedOk: true},
{name: "Random Directive", directive: "test", expectedOk: true},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
csp := EmptyContentSecurityPolicy()
csp.create(tc.directive)
// make sure directive is now in CSP policies
if _, ok := csp.policies[tc.directive]; ok != tc.expectedOk {
t.Errorf("Expected: %t\tActual: %t\n", tc.expectedOk, ok)
}
})
}
}
func TestCSP_Remove(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
csp *ContentSecurityPolicy
directives []CSPDirective
}{
{name: "Empty", csp: EmptyContentSecurityPolicy(), directives: []CSPDirective{}},
{
name: "Single Directive",
csp: NewContentSecurityPolicy(map[CSPDirective][]CSPSource{
DirectiveDefaultSrc: {SourceNone},
}),
directives: []CSPDirective{DirectiveDefaultSrc},
},
{
name: "Multiple Directives",
csp: NewContentSecurityPolicy(map[CSPDirective][]CSPSource{
DirectiveDefaultSrc: {SourceNone},
DirectiveScriptSrc: {SourceSelf, SourceUnsafeInline},
}),
directives: []CSPDirective{DirectiveDefaultSrc, DirectiveScriptSrc},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
// make sure directives are in CSP
for _, directive := range tc.directives {
if _, ok := tc.csp.policies[directive]; !ok {
t.Errorf("Directive is missing\tDirective: %s\n", directive)
}
}
tc.csp.Remove(tc.directives...)
// make sure directives are NOT in CSP
for _, directive := range tc.directives {
if _, ok := tc.csp.policies[directive]; ok {
t.Errorf("Directive should be removed\tDirective: %s\n", directive)
}
}
})
}
}
func TestCSP_String(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
csp *ContentSecurityPolicy
expectedPolicies []string
}{
{name: "Empty", csp: EmptyContentSecurityPolicy(), expectedPolicies: []string{}},
{name: "Nil", csp: NewContentSecurityPolicy(nil), expectedPolicies: []string{}},
{
name: "Single Directive",
csp: NewContentSecurityPolicy(map[CSPDirective][]CSPSource{
DirectiveDefaultSrc: {SourceNone},
}),
expectedPolicies: []string{fmt.Sprintf("%s %s", DirectiveDefaultSrc, SourceNone)},
},
{
name: "Single Directive, No Sources",
csp: NewContentSecurityPolicy(map[CSPDirective][]CSPSource{
DirectiveUpgradeInsecureRequests: {},
}),
expectedPolicies: []string{fmt.Sprintf("%s", DirectiveUpgradeInsecureRequests)},
},
{
name: "Multiple Directives",
csp: NewContentSecurityPolicy(map[CSPDirective][]CSPSource{
DirectiveDefaultSrc: {SourceNone},
DirectiveUpgradeInsecureRequests: {},
}),
expectedPolicies: []string{
fmt.Sprintf("%s %s", DirectiveDefaultSrc, SourceNone),
fmt.Sprintf("%s", DirectiveUpgradeInsecureRequests),
},
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
// check that the CSP contains all policies
str := tc.csp.String()
for _, policy := range tc.expectedPolicies {
if !strings.Contains(str, policy) {
t.Errorf("CSP doesn't contain policy\tExpected: %s\tActual: %s\n", policy, str)
}
}
// check for the correct amount of semicolons
semicolonCount := strings.Count(str, ";")
expectedSemicolonCount := len(tc.expectedPolicies) - 1
if expectedSemicolonCount < 0 {
expectedSemicolonCount = 0
}
if semicolonCount != expectedSemicolonCount {
t.Errorf("Incorrect amount of semicolons\tExpected: %d\tActual: %d\n", expectedSemicolonCount, semicolonCount)
}
// check that cache is set
if len(tc.expectedPolicies) > 0 && len(tc.csp.cache) == 0 {
t.Errorf("CSP String() cache is not set!\tActual: %s\n", tc.csp.cache)
}
// utilize said cache
str = tc.csp.String()
for _, policy := range tc.expectedPolicies {
if !strings.Contains(str, policy) {
t.Errorf("CSP doesn't contain policy\tExpected: %s\tActual: %s\n", policy, str)
}
}
})
}
}
func TestCSP_Empty(t *testing.T) {
t.Parallel()
testCases := []struct {
name string
csp *ContentSecurityPolicy
expectedEmpty bool
}{
{name: "Empty", csp: EmptyContentSecurityPolicy(), expectedEmpty: true},
{name: "Nil", csp: NewContentSecurityPolicy(nil), expectedEmpty: true},
{
name: "Single Directive",
csp: NewContentSecurityPolicy(map[CSPDirective][]CSPSource{
DirectiveDefaultSrc: {SourceNone},
}),
expectedEmpty: false,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
exists := tc.csp.Empty()
if exists != tc.expectedEmpty {
t.Errorf("Expected: %t\tActual: %t\n", tc.expectedEmpty, exists)
}
})
}
}