-
Notifications
You must be signed in to change notification settings - Fork 46
/
csp_test.ts
319 lines (258 loc) · 11.8 KB
/
csp_test.ts
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
/**
* @fileoverview Tests for CSP Defintions.
* @author [email protected] (Lukas Weichselbaum)
*
* @license
* Copyright 2016 Google Inc. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import 'jasmine';
import {Directive, isDirective, isHash, isKeyword, isNonce, isUrlScheme, Keyword, Version} from './csp';
import {Finding, Severity, Type} from './finding';
import {CspParser} from './parser';
describe('Test Csp', () => {
it('ConvertToString', () => {
const testCsp = 'default-src \'none\'; ' +
'script-src \'nonce-unsafefoobar\' \'unsafe-eval\' \'unsafe-inline\' ' +
'https://example.com/foo.js foo.bar; ' +
'img-src \'self\' https: data: blob:; ';
const parsed = (new CspParser(testCsp)).csp;
expect(parsed.convertToString()).toBe(testCsp);
});
it('GetEffectiveCspVersion1', () => {
const testCsp =
'default-src \'unsafe-inline\' \'strict-dynamic\' \'nonce-123\' ' +
'\'sha256-foobar\' \'self\'; report-to foo.bar; worker-src *; manifest-src *; script-src-attr \'self\'; script-src-elem \'self\'; style-src-attr \'self\'; style-src-elem \'self\';';
const parsed = (new CspParser(testCsp)).csp;
const findings: Finding[] = [];
const effectiveCsp = parsed.getEffectiveCsp(Version.CSP1, findings);
expect(findings.length)
.toBe(0); // No warnings about unsafe-inline being ignored in CSP 1.
expect(effectiveCsp.directives[Directive.DEFAULT_SRC]).toEqual([
'\'unsafe-inline\'', '\'self\''
]);
expect(effectiveCsp.directives.hasOwnProperty(Directive.REPORT_TO))
.toBeFalse();
expect(effectiveCsp.directives.hasOwnProperty(Directive.WORKER_SRC))
.toBeFalse();
expect(effectiveCsp.directives.hasOwnProperty(Directive.MANIFEST_SRC))
.toBeFalse();
});
it('GetEffectiveCspVersion2', () => {
const testCsp =
'default-src \'unsafe-inline\' \'strict-dynamic\' \'nonce-123\' ' +
'\'sha256-foobar\' \'self\'; report-to foo.bar; worker-src *; manifest-src *; script-src-attr \'self\'; script-src-elem \'self\'; style-src-attr \'self\'; style-src-elem \'self\';';
const parsed = (new CspParser(testCsp)).csp;
const findings: Finding[] = [];
const effectiveCsp = parsed.getEffectiveCsp(Version.CSP2, findings);
// Ignored messages are surfaced properly.
expect(findings.length).toBe(1); // Only nonces cause unsafe-inline to be
// ignored in CSP2, not strict-dynamic.
expect(findings[0].value).toBe('\'unsafe-inline\'');
expect(findings[0].type).toBe(Type.IGNORED);
expect(findings[0].severity).toBe(Severity.NONE);
expect(findings[0].directive).toBe(Directive.DEFAULT_SRC);
expect(effectiveCsp.directives[Directive.DEFAULT_SRC]).toEqual([
'\'nonce-123\'', '\'sha256-foobar\'', '\'self\''
]);
expect(effectiveCsp.directives.hasOwnProperty(Directive.REPORT_TO))
.toBeFalse();
expect(effectiveCsp.directives.hasOwnProperty(Directive.WORKER_SRC))
.toBeFalse();
expect(effectiveCsp.directives.hasOwnProperty(Directive.MANIFEST_SRC))
.toBeFalse();
expect(effectiveCsp.directives.hasOwnProperty(Directive.SCRIPT_SRC_ATTR))
.toBeFalse();
expect(effectiveCsp.directives.hasOwnProperty(Directive.SCRIPT_SRC_ELEM))
.toBeFalse();
expect(effectiveCsp.directives.hasOwnProperty(Directive.STYLE_SRC_ATTR))
.toBeFalse();
expect(effectiveCsp.directives.hasOwnProperty(Directive.STYLE_SRC_ELEM))
.toBeFalse();
});
it('GetEffectiveCspVersion3', () => {
const testCsp =
'default-src \'unsafe-inline\' \'strict-dynamic\' \'nonce-123\' ' +
'\'sha256-foobar\' \'self\'; report-to foo.bar; worker-src *; manifest-src *; script-src-attr \'self\'; script-src-elem \'self\'; style-src-attr \'self\'; style-src-elem \'self\';';
const parsed = (new CspParser(testCsp)).csp;
const findings: Finding[] = [];
const effectiveCsp = parsed.getEffectiveCsp(Version.CSP3, findings);
// Ignored messages are only on the default-src: Ignoring self and
// unsafe-inline because of strict-dynamic and the unsafe-inline because of
// the nonce.
expect(findings.length).toBe(3);
expect(findings.every(f => f.type === Type.IGNORED)).toBeTrue();
expect(findings.every(f => f.severity === Severity.NONE)).toBeTrue();
expect(findings.every(f => f.directive === Directive.DEFAULT_SRC))
.toBeTrue();
expect(effectiveCsp.directives[Directive.DEFAULT_SRC]).toEqual([
'\'strict-dynamic\'', '\'nonce-123\'', '\'sha256-foobar\''
]);
expect(effectiveCsp.directives[Directive.REPORT_TO]).toEqual(['foo.bar']);
expect(effectiveCsp.directives[Directive.WORKER_SRC]).toEqual(['*']);
expect(effectiveCsp.directives[Directive.MANIFEST_SRC]).toEqual(['*']);
expect(effectiveCsp.directives[Directive.SCRIPT_SRC_ATTR]).toEqual([
'\'self\''
]);
expect(effectiveCsp.directives[Directive.SCRIPT_SRC_ELEM]).toEqual([
'\'self\''
]);
expect(effectiveCsp.directives[Directive.STYLE_SRC_ATTR]).toEqual([
'\'self\''
]);
expect(effectiveCsp.directives[Directive.STYLE_SRC_ELEM]).toEqual([
'\'self\''
]);
});
it('GetEffectiveDirective', () => {
const testCsp = 'default-src https:; script-src foo.bar';
const parsed = (new CspParser(testCsp)).csp;
const script = parsed.getEffectiveDirective(Directive.SCRIPT_SRC);
expect(script).toBe(Directive.SCRIPT_SRC);
const style = parsed.getEffectiveDirective(Directive.STYLE_SRC);
expect(style).toBe(Directive.DEFAULT_SRC);
});
it('GetEffectiveDirectives', () => {
const testCsp = 'default-src https:; script-src foo.bar';
const parsed = (new CspParser(testCsp)).csp;
const directives = parsed.getEffectiveDirectives(
[Directive.SCRIPT_SRC, Directive.STYLE_SRC]);
expect(directives).toEqual([Directive.SCRIPT_SRC, Directive.DEFAULT_SRC]);
});
it('GetEffectiveDirectives', () => {
const testCsp = 'default-src https:; style-src-elem foo.bar';
const parsed = (new CspParser(testCsp)).csp;
const directives = parsed.getEffectiveDirectives([
Directive.STYLE_SRC, Directive.STYLE_SRC_ATTR, Directive.STYLE_SRC_ELEM
]);
// Both style-src and style-src-attr default to default-src.
expect(directives).toEqual([
Directive.DEFAULT_SRC, Directive.STYLE_SRC_ELEM
]);
});
it('GetEffectiveDirectives', () => {
const testCsp =
'default-src https:; script-src foo.bar; script-src-elem bar.baz; style-src foo.bar; style-src-elem bar.baz';
const parsed = (new CspParser(testCsp)).csp;
const directives = parsed.getEffectiveDirectives([
Directive.SCRIPT_SRC, Directive.STYLE_SRC, Directive.SCRIPT_SRC_ATTR,
Directive.SCRIPT_SRC_ELEM, Directive.STYLE_SRC_ATTR,
Directive.STYLE_SRC_ELEM
]);
expect(directives).toEqual([
Directive.SCRIPT_SRC, // Both script-src and script-src-attr
Directive.STYLE_SRC, // Both style-src and style-src-attr
Directive.SCRIPT_SRC_ELEM,
Directive.STYLE_SRC_ELEM,
]);
});
it('PolicyHasScriptNoncesScriptSrcWithNonce', () => {
const testCsp = 'default-src https:; script-src \'nonce-test123\'';
const parsed = (new CspParser(testCsp)).csp;
expect(parsed.policyHasScriptNonces()).toBeTrue();
});
it('PolicyHasScriptNoncesNoNonce', () => {
const testCsp =
'default-src https: \'nonce-ignored\'; script-src nonce-invalid';
const parsed = (new CspParser(testCsp)).csp;
expect(parsed.policyHasScriptNonces()).toBeFalse();
});
it('PolicyHasScriptHashesScriptSrcWithHash', () => {
const testCsp = 'default-src https:; script-src \'sha256-asdfASDF\'';
const parsed = (new CspParser(testCsp)).csp;
expect(parsed.policyHasScriptHashes()).toBeTrue();
});
it('PolicyHasScriptHashesNoHash', () => {
const testCsp =
'default-src https: \'nonce-ignored\'; script-src sha256-invalid';
const parsed = (new CspParser(testCsp)).csp;
expect(parsed.policyHasScriptHashes()).toBeFalse();
});
it('PolicyHasStrictDynamicScriptSrcWithStrictDynamic', () => {
const testCsp = 'default-src https:; script-src \'strict-dynamic\'';
const parsed = (new CspParser(testCsp)).csp;
expect(parsed.policyHasStrictDynamic()).toBeTrue();
});
it('PolicyHasStrictDynamicDefaultSrcWithStrictDynamic', () => {
const testCsp = 'default-src https \'strict-dynamic\'';
const parsed = (new CspParser(testCsp)).csp;
expect(parsed.policyHasStrictDynamic()).toBeTrue();
});
it('PolicyHasStrictDynamicNoStrictDynamic', () => {
const testCsp = 'default-src \'strict-dynamic\'; script-src foo.bar';
const parsed = (new CspParser(testCsp)).csp;
expect(parsed.policyHasStrictDynamic()).toBeFalse();
});
it('IsDirective', () => {
const directives = Object.keys(Directive).map(
(name) => Directive[name as keyof typeof Directive]);
expect(directives.every(isDirective)).toBeTrue();
expect(isDirective('invalid-src')).toBeFalse();
});
it('IsKeyword', () => {
const keywords = Object.keys(Keyword).map(
(name) => (Keyword[name as keyof typeof Keyword]));
expect(keywords.every(isKeyword)).toBeTrue();
expect(isKeyword('invalid')).toBeFalse();
});
it('IsUrlScheme', () => {
expect(isUrlScheme('http:')).toBeTrue();
expect(isUrlScheme('https:')).toBeTrue();
expect(isUrlScheme('data:')).toBeTrue();
expect(isUrlScheme('blob:')).toBeTrue();
expect(isUrlScheme('b+l.o-b:')).toBeTrue();
expect(isUrlScheme('filesystem:')).toBeTrue();
expect(isUrlScheme('invalid')).toBeFalse();
expect(isUrlScheme('ht_tp:')).toBeFalse();
});
it('IsNonce', () => {
expect(isNonce('\'nonce-asdfASDF=\'')).toBeTrue();
expect(isNonce('\'sha256-asdfASDF=\'')).toBeFalse();
expect(isNonce('\'asdfASDF=\'')).toBeFalse();
expect(isNonce('example.com')).toBeFalse();
});
it('IsStrictNonce', () => {
expect(isNonce('\'nonce-asdfASDF=\'', true)).toBeTrue();
expect(isNonce('\'nonce-as+df/A0234SDF==\'', true)).toBeTrue();
expect(isNonce('\'nonce-as_dfASDF=\'', true)).toBeTrue();
expect(isNonce('\'nonce-asdfASDF===\'', true)).toBeFalse();
expect(isNonce('\'sha256-asdfASDF=\'', true)).toBeFalse();
});
it('IsHash', () => {
expect(isHash('\'sha256-asdfASDF=\'')).toBeTrue();
expect(isHash('\'sha777-asdfASDF=\'')).toBeFalse();
expect(isHash('\'asdfASDF=\'')).toBeFalse();
expect(isHash('example.com')).toBeFalse();
});
it('IsStrictHash', () => {
expect(isHash('\'sha256-asdfASDF=\'', true)).toBeTrue();
expect(isHash('\'sha256-as+d/f/ASD0+4F==\'', true)).toBeTrue();
expect(isHash('\'sha256-asdfASDF===\'', true)).toBeFalse();
expect(isHash('\'sha256-asd_fASDF=\'', true)).toBeFalse();
expect(isHash('\'sha777-asdfASDF=\'', true)).toBeFalse();
expect(isHash('\'asdfASDF=\'', true)).toBeFalse();
expect(isHash('example.com', true)).toBeFalse();
});
it('ParseNavigateTo', () => {
const testCsp = 'navigate-to \'self\'; script-src \'nonce-foo\'';
const parsed = (new CspParser(testCsp)).csp;
expect(parsed.policyHasStrictDynamic()).toBeFalse();
expect(parsed.policyHasScriptNonces()).toBeTrue();
});
it('ParseWebRtc', () => {
const testCsp = 'web-rtc \'allow\'; script-src \'nonce-foo\'';
const parsed = (new CspParser(testCsp)).csp;
expect(parsed.policyHasStrictDynamic()).toBeFalse();
expect(parsed.policyHasScriptNonces()).toBeTrue();
});
});