-
Notifications
You must be signed in to change notification settings - Fork 7
/
popup.js
443 lines (372 loc) · 13.8 KB
/
popup.js
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
'use strict';
var KEYWORDS = ['none', 'self', 'unsafe-inline', 'unsafe-eval'];
var MODE_SIMPLE = 1;
var MODE_ADVANCED = 2;
var STATE_ACTIVE = 1;
var STATE_NOTACTIVE = 0;
class Directive {
constructor(name, keywords) {
this.name = name;
this.value = '';
if (keywords === undefined) {
this.keywords = ['this'];
}
}
init_value(value) {
this.value = value;
}
static get_directive_names() {
let core_directive_names = [
'default-src', 'script-src', 'style-src', 'img-src',
'connect-src', 'child-src', 'font-src', 'form-action',
'frame-ancestors', 'frame-src', 'media-src', 'object-src',
'plugin-types', 'base-uri', 'sandbox', 'report-uri'
];
// https://www.w3.org/TR/CSP3/#directives-elsewhere
let ext_directive_names = [
'upgrade-insecure-requests'
];
return core_directive_names.concat(ext_directive_names);
}
support_eval() {
if (['script-src', 'style-src'].indexOf(this.name) !== -1) {
return true
}
return false;
}
support_inline() {
if (['script-src', 'style-src'].indexOf(this.name) !== -1) {
return true
}
return false;
}
only_bolean() {
if ('upgrade-insecure-requests' == this.name) {
return true
}
return false;
}
static make_directive(directive_name) {
let directive_names = Directive.get_directive_names();
for (let i=0; i<directive_names.length; i++) {
if (directive_names[i] == directive_name) {
return new Directive(directive_name);
}
}
return null;
}
}
class Policy {
constructor() {
this.directives = [];
}
init_value(policy) {
this.directives = [];
let tokens = policy.split(';');
// https://www.w3.org/TR/CSP2/#policy-parsing
for (let i=0; i<tokens.length; i++) {
let directive_value = '';
let chunks = tokens[i].trim().split(' ');
let directive_name = chunks[0];
if (chunks.length > 1) {
directive_value = chunks.slice(1).join(' ');
}
let directive = Directive.make_directive(directive_name);
if (directive) {
directive.init_value(directive_value);
this.directives.push(directive);
}
}
}
get_string_policy() {
let result = '';
for (let i=0; i<this.directives.length; i++) {
result += this.directives[i].name + ' ' + this.directives[i].value.trim() + '; ';
}
if (result) {
result = result.slice(0, result.length - 2);
}
return result;
}
init_from_simple_form() {
this.directives = [];
let elems = document.getElementsByClassName("directive_value");
for (let i=0; i<elems.length; i++) {
let directive = Directive.make_directive(elems[i].id);
let directive_value = elems[i].value.trim();
if (directive == null) {
continue;
}
for (let j=0; j<KEYWORDS.length; j++) {
let keyword_value = get_checked(directive.name + '-' + KEYWORDS[j]);
if (keyword_value) {
if (KEYWORDS[j] == 'none') {
directive_value = "'none'";
break;
}
// Clear keyword from directive value
directive_value = directive_value.replace("'" + KEYWORDS[j] + "'", '');
directive_value += " '" + KEYWORDS[j] + "'";
}
}
// TODO add support for empty values
if (directive_value || directive.only_bolean()) {
directive.init_value(directive_value);
this.directives.push(directive);
}
}
}
init_from_advanced_form() {
this.init_value(get_value("policy"));
}
}
class SimpleForm {
static reset() {
let directive_names = [];
let elems = document.getElementsByClassName("directive_value");
set_value('default-src', '');
set_checked('default-src-self', false);
for (let i=0; i<elems.length; i++) {
if (elems[i].id != 'default-src') {
directive_names.push(elems[i].id);
}
}
for (let i=0; i<directive_names.length; i++) {
SimpleForm.remove_directive(directive_names[i]);
}
let pinned_directives = ['script-src', 'style-src', 'img-src'];
for (let i=0; i<pinned_directives.length; i++) {
SimpleForm.add_directive(pinned_directives[i], false);
}
}
static remove_directive(directive_name) {
let simple_form = document.getElementById("simple_form");
simple_form.removeChild(document.getElementById('tr-' + directive_name));
}
static restore(csp) {
SimpleForm.reset();
let tmp_value = '';
for (let i=0; i<csp.directives.length; i++) {
let directive_value = csp.directives[i].value;
let directive_name = csp.directives[i].name;
if (!SimpleForm.is_directive_exists(directive_name)) {
SimpleForm.add_directive(directive_name);
}
for (let j=0; j<KEYWORDS.length; j++) {
if (directive_value.indexOf("'" + KEYWORDS[j] + "'") > -1
&& document.getElementById(directive_name + '-' + KEYWORDS[j])) {
directive_value = directive_value.replace("'" + KEYWORDS[j] +"'", '');
set_checked(directive_name + '-' + KEYWORDS[j], true);
if (KEYWORDS[j] == 'none') {
check_none_box(directive_name);
}
}
}
set_value(directive_name, directive_value.trim());
}
}
static is_directive_exists(directive_name) {
return document.getElementById(directive_name) != null;
}
static add_directive(directive_name, delete_link) {
var directive = new Directive(directive_name);
if (delete_link === undefined) {
var delete_link = true;
}
let simple_form = document.getElementById("simple_form");
let tr = document.getElementById("tr-default-src").cloneNode(true);
tr.id = 'tr-' + directive_name;
tr.querySelector(".help_link").href = "https://www.w3.org/TR/CSP2/#directive-" + directive_name;
tr.querySelector(".help_link").innerText = directive_name;
tr.querySelector(".directive_value").id = directive_name;
tr.querySelector(".directive_value").value = '';
if (!directive.only_bolean()) {
tr.querySelector(".directive_value").disabled = false;
let d = tr.querySelector("div.self");
d.style.display = 'inline';
d.querySelector("input").id = directive_name + '-self';
d.querySelector("input").checked = false;
d.querySelector("input").disabled = false;
d.querySelector("label").setAttribute("for", directive_name + '-self');
d = tr.querySelector("div.none");
d.style.display = 'inline';
d.querySelector("input").id = directive_name + '-none';
d.querySelector("input").checked = false;
d.querySelector("input").onchange = function() {check_none_box(directive_name);};
d.querySelector("label").setAttribute("for", directive_name + '-none');
} else {
tr.querySelector(".directive_value").disabled = true;
let d = tr.querySelector("div.none");
d.style.display = 'none';
d = tr.querySelector("div.self");
d.style.display = 'none';
}
if (delete_link) {
let d = tr.querySelector("div.delete-icon");
d.style.display = 'inline';
d.onclick = function() {SimpleForm.remove_directive(directive_name);return false;}
}
if (directive.support_eval()) {
let d = tr.querySelector("div.unsafe-eval");
d.style.display = 'inline';
d.querySelector("input").id = directive_name + '-unsafe-eval';
d.querySelector("input").checked = false;
d.querySelector("input").disabled = false;
d.querySelector("label").setAttribute("for", directive_name + '-unsafe-eval');
}
if (directive.support_inline()) {
let d = tr.querySelector("div.unsafe-inline");
d.style.display = 'inline';
d.querySelector("input").id = directive_name + '-unsafe-inline';
d.querySelector("input").checked = false;
d.querySelector("input").disabled = false;
d.querySelector("label").setAttribute("for", directive_name + '-unsafe-inline');
}
simple_form.appendChild(tr);
}
}
function save_policy() {
var csp = new Policy();
localStorage["target"] = get_value("target");
if (localStorage.getItem('mode') == MODE_ADVANCED) {
csp.init_from_advanced_form();
} else {
csp.init_from_simple_form();
}
localStorage['policy'] = csp.get_string_policy();
if (get_checked("state")) {
localStorage["state"] = STATE_ACTIVE;
} else {
localStorage["state"] = STATE_NOTACTIVE;
}
if (get_checked("report_only")) {
localStorage["report_only"] = STATE_ACTIVE;
} else {
localStorage["report_only"] = STATE_NOTACTIVE;
}
var status = document.getElementById("status");
var bg = chrome.extension.getBackgroundPage()
if (bg.reload()) {
status.innerHTML = "Policy has been saved.";
status.style.display = 'block';
} else {
status.innerHTML = "Policy has not been saved! Please, check URL pattern.";
status.style.display = 'block';
localStorage["state"] = STATE_NOTACTIVE;
document.getElementById("state").checked = false;
}
setTimeout(function() {
status.style.display = 'none';
}, 850);
}
function load_policy() {
if (localStorage.getItem("target")) {
set_value("target", localStorage.getItem("target"));
}
var csp = new Policy();
if (localStorage.getItem('policy')) {
csp.init_value(localStorage['policy']);
}
var mode = localStorage.getItem('mode') ? localStorage.getItem('mode') : MODE_ADVANCED;
if (mode == MODE_ADVANCED) {
set_value("policy", csp.get_string_policy());
} else {
SimpleForm.restore(csp);
}
if (localStorage["state"] == STATE_ACTIVE) {
set_checked("state", true);
}
if (localStorage["report_only"] == 1) {
set_checked("report_only", true);
}
}
function reset_policy() {
set_value("target", '');
set_value("policy", '');
set_checked("state", false);
set_checked("report_only", false);
SimpleForm.reset();
}
function toggle_view() {
var adv = document.getElementById("advanced");
var advanced_link = document.getElementById("advanced_link");
var simple_link = document.getElementById("simple_link");
var simple = document.getElementById("simple");
if (localStorage.getItem('mode') == MODE_SIMPLE) {
advanced_link.style.display = 'inline';
adv.style.display = 'none';
simple_link.style.display = 'none';
simple.style.display = 'block';
} else {
advanced_link.style.display = 'none';
adv.style.display = 'block';
simple_link.style.display = 'inline';
simple.style.display = 'none';
}
}
function switch2advanced() {
var csp = new Policy();
csp.init_from_simple_form();
set_value("policy", csp.get_string_policy());
localStorage['mode'] = MODE_ADVANCED;
toggle_view();
}
function switch2simple() {
var csp = new Policy();
csp.init_from_advanced_form();
SimpleForm.restore(csp);
localStorage['mode'] = MODE_SIMPLE;
toggle_view();
}
function get_value(id) {
return document.getElementById(id).value;
}
function set_value(id, value) {
document.getElementById(id).value = value;
}
function get_checked(id) {
if (document.getElementById(id)) {
return document.getElementById(id).checked;
} else {
return null;
}
}
function set_checked(id, value) {
document.getElementById(id).checked = value;
}
function set_disabled(id, value) {
if (value == undefined) {
var value = true;
}
if (document.getElementById(id)) {
document.getElementById(id).disabled = value;
} else {
return null;
}
}
function check_none_box(directive_name) {
let state = false;
if (get_checked(directive_name + '-none')) {
state = true;
}
set_disabled(directive_name, state);
set_disabled(directive_name + '-self', state);
set_disabled(directive_name + '-unsafe-inline', state);
set_disabled(directive_name + '-unsafe-eval', state);
}
function add_directive() {
let select = document.getElementById("select-directive");
let directive_name = select.options[select.selectedIndex].value;
if (!SimpleForm.is_directive_exists(directive_name)) {
SimpleForm.add_directive(directive_name);
}
return false;
}
document.addEventListener('DOMContentLoaded', load_policy);
document.querySelector('#save').addEventListener('click', save_policy);
document.querySelector('#reset').addEventListener('click', reset_policy);
document.querySelector('#advanced_link').addEventListener('click', switch2advanced);
document.querySelector('#simple_link').addEventListener('click', switch2simple);
document.querySelector('#add-directive').addEventListener('click', add_directive);
document.getElementById('default-src-none').addEventListener('change', function(){check_none_box('default-src')});
switch2simple();