-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.spec.ts
138 lines (103 loc) · 4.79 KB
/
index.spec.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
import * as CSP from '../src/index';
test('Using builder without directives outputs empty string', () => {
const csp = new CSP.Builder();
expect(csp.stringify()).toEqual('');
});
test('Correctly outputs a single directive', () => {
const csp = new CSP.Builder();
const directive = new CSP.ConnectSource().addValue(CSP.PredefinedSource.Self);
csp.addDirective(directive);
expect(csp.stringify()).toEqual("connect-src 'self';");
});
test('Using same directive twice overrides the previous', () => {
const csp = new CSP.Builder();
const directive1 = new CSP.ConnectSource().addValue(CSP.PredefinedSource.Self);
const directive2 = new CSP.ConnectSource().addValue(CSP.PredefinedSource.None);
csp.addDirective(directive1).addDirective(directive2);
expect(csp.stringify()).toEqual("connect-src 'none';");
});
test('Correctly spaces between 2 different directives', () => {
const csp = new CSP.Builder();
const directive1 = new CSP.ConnectSource().addValue(CSP.PredefinedSource.Self);
const directive2 = new CSP.ScriptSource().addValue(CSP.PredefinedSource.Self);
csp.addDirective(directive1).addDirective(directive2);
expect(csp.stringify()).toEqual("connect-src 'self'; script-src 'self';");
});
test('Throws when directive is beyond requested CSP level', () => {
const csp = new CSP.Builder(2);
const directive = new CSP.PrefetchSource().addValue(CSP.PredefinedSource.Self);
expect(() => csp.addDirective(directive)).toThrowError(/not supported/);
});
test('Toggle directives work as well', () => {
const csp = new CSP.Builder();
const directive = new CSP.BlockAllMixedContent().toggle(true);
csp.addDirective(directive);
expect(csp.stringify()).toEqual('block-all-mixed-content;');
});
test('Report directives work as well', () => {
const csp = new CSP.Builder();
const directive = new CSP.ReportUri().setValue('/csp');
csp.addDirective(directive);
expect(csp.stringify()).toEqual('report-uri /csp;');
});
test('Adding multiple values to a multi value directive', () => {
const csp = new CSP.Builder();
const directive = new CSP.MediaSource().addValue([CSP.PredefinedSource.Self, 'www.google.com']);
csp.addDirective(directive);
expect(csp.stringify()).toEqual("media-src 'self' www.google.com;");
});
test('Verify full usage using my own CSP', () => {
const csp = new CSP.Builder();
const analyticsDomain = 'www.google-analytics.com';
const ownDomain = 'www.giladpeleg.com';
const reportUri = 'https://giladpeleg.report-uri.com/r/d/csp/enforce';
const extensiveSourceDirective = [
CSP.PredefinedSource.Self,
CSP.SchemaSource.Data,
analyticsDomain,
ownDomain,
];
const regularSourceDirective = [CSP.PredefinedSource.Self, analyticsDomain, ownDomain];
const localSourceDirective = [CSP.PredefinedSource.Self, ownDomain];
csp.addDirective(new CSP.DefaultSource().addValue(regularSourceDirective))
.addDirective(new CSP.FontSource().addValue(extensiveSourceDirective))
.addDirective(new CSP.ImageSource().addValue(extensiveSourceDirective))
.addDirective(new CSP.MediaSource().addValue(localSourceDirective))
.addDirective(new CSP.ObjectSource().addValue([CSP.PredefinedSource.None]))
.addDirective(new CSP.FontSource().addValue(extensiveSourceDirective))
.addDirective(
new CSP.PrefetchSource().addValue([
CSP.PredefinedSource.Self,
analyticsDomain,
ownDomain,
])
)
.addDirective(
new CSP.ScriptSource().addValue([
CSP.PredefinedSource.Self,
CSP.PredefinedSource.UnsafeInline,
analyticsDomain,
ownDomain,
])
)
.addDirective(
new CSP.StyleSource().addValue([
CSP.PredefinedSource.Self,
CSP.PredefinedSource.UnsafeInline,
ownDomain,
])
)
.addDirective(new CSP.WorkerSource().addValue(localSourceDirective))
.addDirective(new CSP.ReportUri().setValue(reportUri));
const expected = `default-src 'self' www.google-analytics.com www.giladpeleg.com;
font-src 'self' data: www.google-analytics.com www.giladpeleg.com;
img-src 'self' data: www.google-analytics.com www.giladpeleg.com;
media-src 'self' www.giladpeleg.com;
object-src 'none';
prefetch-src 'self' www.google-analytics.com www.giladpeleg.com;
script-src 'self' 'unsafe-inline' www.google-analytics.com www.giladpeleg.com;
style-src 'self' 'unsafe-inline' www.giladpeleg.com;
worker-src 'self' www.giladpeleg.com;
report-uri https://giladpeleg.report-uri.com/r/d/csp/enforce;`;
expect(csp.stringify()).toMatch(expected.replace(/\s+/g, ' '));
});