-
Notifications
You must be signed in to change notification settings - Fork 45
/
parser_test.ts
162 lines (142 loc) · 6.36 KB
/
parser_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
/**
* @fileoverview Tests for CSP Parser.
* @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 {CspParser, TEST_ONLY} from './parser';
describe('Test parser', () => {
it('CspParser', () => {
const validCsp = // Test policy with different features from CSP2.
'default-src \'none\';' +
'script-src \'nonce-unsafefoobar\' \'unsafe-eval\' \'unsafe-hashes\' \'unsafe-inline\' \n' +
'https://example.com/foo.js foo.bar \'sha256-1DCfk1NYWuHMfoobarfoobar=\';' +
'script-src-elem \'self\' \'unsafe-inline\' https://apis.google.com https://www.googletagmanager.com https://www.google-analytics.com https://wchat.freshchat.com;' +
'object-src \'none\';' +
'img-src \'self\' https: data: blob:;' +
'style-src \'self\' \'unsafe-inline\' \'sha256-1DCfk1NYWuHMfoobarfoobar=\';' +
'style-src-elem \'self\' \'unsafe-inline\' https://fonts.googleapis.com https://fonts.gstatic.com;' +
'font-src *;' +
'child-src *.example.com:9090;' +
'upgrade-insecure-requests;\n' +
'report-uri /csp/test';
const parser = new (CspParser)(validCsp);
const parsedCsp = parser.csp;
// check directives
const directives = Object.keys(parsedCsp.directives);
const expectedDirectives = [
'default-src', 'script-src', 'script-src-elem', 'object-src', 'img-src',
'style-src', 'style-src-elem', 'font-src', 'child-src',
'upgrade-insecure-requests', 'report-uri'
];
expect(expectedDirectives)
.toEqual(jasmine.arrayWithExactContents(directives));
// check directive values
expect(['\'none\''])
.toEqual(jasmine.arrayWithExactContents(
parsedCsp.directives['default-src'] as string[]));
expect([
'\'nonce-unsafefoobar\'', '\'unsafe-eval\'', '\'unsafe-hashes\'',
'\'unsafe-inline\'', 'https://example.com/foo.js', 'foo.bar',
'\'sha256-1DCfk1NYWuHMfoobarfoobar=\''
])
.toEqual(jasmine.arrayWithExactContents(
parsedCsp.directives['script-src'] as string[]));
expect([
'\'self\'', '\'unsafe-inline\'', 'https://apis.google.com',
'https://www.googletagmanager.com', 'https://www.google-analytics.com',
'https://wchat.freshchat.com'
])
.toEqual(jasmine.arrayWithExactContents(
parsedCsp.directives['script-src-elem'] as string[]));
expect(['\'none\''])
.toEqual(jasmine.arrayWithExactContents(
parsedCsp.directives['object-src'] as string[]));
expect(['\'self\'', 'https:', 'data:', 'blob:'])
.toEqual(jasmine.arrayWithExactContents(
parsedCsp.directives['img-src'] as string[]));
expect([
'\'self\'', '\'unsafe-inline\'', '\'sha256-1DCfk1NYWuHMfoobarfoobar=\''
])
.toEqual(jasmine.arrayWithExactContents(
parsedCsp.directives['style-src'] as string[]));
expect([
'\'self\'', '\'unsafe-inline\'', 'https://fonts.googleapis.com',
'https://fonts.gstatic.com'
])
.toEqual(jasmine.arrayWithExactContents(
parsedCsp.directives['style-src-elem'] as string[]));
expect(['*']).toEqual(jasmine.arrayWithExactContents(
parsedCsp.directives['font-src'] as string[]));
expect(['*.example.com:9090'])
.toEqual(jasmine.arrayWithExactContents(
parsedCsp.directives['child-src'] as string[]));
expect([]).toEqual(jasmine.arrayWithExactContents(
parsedCsp.directives['upgrade-insecure-requests'] as string[]));
expect(['/csp/test'])
.toEqual(jasmine.arrayWithExactContents(
parsedCsp.directives['report-uri'] as string[]));
});
it('CspParserDuplicateDirectives', () => {
const validCsp = 'default-src \'none\';' +
'default-src foo.bar;' +
'object-src \'none\';' +
'OBJECT-src foo.bar;';
const parser = new (CspParser)(validCsp);
const parsedCsp = parser.csp;
// check directives
const directives = Object.keys(parsedCsp.directives);
const expectedDirectives = ['default-src', 'object-src'];
expect(expectedDirectives)
.toEqual(jasmine.arrayWithExactContents(directives));
// check directive values
expect(['\'none\''])
.toEqual(jasmine.arrayWithExactContents(
parsedCsp.directives['default-src'] as string[]));
expect(['\'none\''])
.toEqual(jasmine.arrayWithExactContents(
parsedCsp.directives['object-src'] as string[]));
});
it('CspParserMixedCaseKeywords', () => {
const validCsp = 'DEFAULT-src \'NONE\';' + // Keywords should be
// case insensetive.
'img-src \'sElf\' HTTPS: Example.com/CaseSensitive;';
const parser = new (CspParser)(validCsp);
const parsedCsp = parser.csp;
// check directives
const directives = Object.keys(parsedCsp.directives);
const expectedDirectives = ['default-src', 'img-src'];
expect(expectedDirectives)
.toEqual(jasmine.arrayWithExactContents(directives));
// check directive values
expect(['\'none\''])
.toEqual(jasmine.arrayWithExactContents(
parsedCsp.directives['default-src'] as string[]));
expect(['\'self\'', 'https:', 'Example.com/CaseSensitive'])
.toEqual(jasmine.arrayWithExactContents(
parsedCsp.directives['img-src'] as string[]));
});
it('NormalizeDirectiveValue', () => {
expect(TEST_ONLY.normalizeDirectiveValue('\'nOnE\'')).toBe('\'none\'');
expect(TEST_ONLY.normalizeDirectiveValue('\'nonce-aBcD\''))
.toBe('\'nonce-aBcD\'');
expect(TEST_ONLY.normalizeDirectiveValue('\'hash-XyZ==\''))
.toBe('\'hash-XyZ==\'');
expect(TEST_ONLY.normalizeDirectiveValue('HTTPS:')).toBe('https:');
expect(TEST_ONLY.normalizeDirectiveValue('example.com/TEST'))
.toBe('example.com/TEST');
});
});