-
-
Notifications
You must be signed in to change notification settings - Fork 105
Expand file tree
/
Copy pathRequestGuard.js
More file actions
713 lines (694 loc) · 25.1 KB
/
RequestGuard.js
File metadata and controls
713 lines (694 loc) · 25.1 KB
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
var RequestGuard = (() => {
'use strict';
const VERSION_LABEL = `NoScript ${browser.runtime.getManifest().version}`;
browser.browserAction.setTitle({title: VERSION_LABEL});
const CSP_REPORT_URI = "https://noscript-csp.invalid/__NoScript_Probe__/";
const CSP_MARKER = "noscript-marker";
let csp = new ReportingCSP(CSP_MARKER, CSP_REPORT_URI);
const policyTypesMap = {
main_frame: "",
sub_frame: "frame",
script: "script",
xslt: "script",
xbl: "script",
font: "font",
object: "object",
object_subrequest: "fetch",
xmlhttprequest: "fetch",
ping: "ping",
beacon: "ping",
media: "media",
other: "",
};
Object.assign(policyTypesMap, {"webgl": "webgl"}); // fake types
const TabStatus = {
map: new Map(),
_originsCache: new Map(),
types: ["script", "object", "media", "frame", "font"],
newRecords() {
return {
allowed: {},
blocked: {},
noscriptFrames: {},
origins: new Set(),
}
},
hasOrigin(tabId, origin) {
let records = this.map.get(tabId);
return records && records.origins.has(origin);
},
findTabsByOrigin(origin) {
let tabIds = this._originsCache.get(origin);
if (!tabIds) {
tabIds = [];
for(let [tabId, {origins}] of [...this.map]) {
if (origins.has(origin)) tabIds.push(tabId);
}
this._originsCache.set(origin, tabIds);
}
return tabIds;
},
initTab(tabId, records = this.newRecords()) {
if (tabId < 0) return;
this.map.set(tabId, records);
return records;
},
_record(request, what, optValue) {
let {tabId, frameId, type, url, documentUrl} = request;
let policyType = policyTypesMap[type] || type;
let requestKey = Policy.requestKey(url, policyType, documentUrl);
let map = this.map;
let records = map.has(tabId) ? map.get(tabId) : this.initTab(tabId);
if (what === "noscriptFrame" && type !== "object") {
let nsf = records.noscriptFrames;
nsf[frameId] = optValue;
what = optValue ? "blocked" : "allowed";
if (frameId === 0) {
request.type = type = "main_frame";
Content.reportTo(request, optValue, type);
}
}
if (type.endsWith("frame")) {
records.origins.add(Sites.origin(url));
TabStatus._originsCache.clear();
}
let collection = records[what];
if (collection) {
if (type in collection) {
if (!collection[type].includes(requestKey)) {
collection[type].push(requestKey);
}
} else {
collection[type] = [requestKey];
}
}
return records;
},
record(request, what, optValue) {
let {tabId} = request;
if (tabId < 0) return;
let records = this._record(request, what, optValue);
if (records) {
this.updateTab(request.tabId);
}
},
_pendingTabs: new Set(),
updateTab(tabId) {
if (tabId < 0) return;
if (this._pendingTabs.size === 0) {
window.setTimeout(() => { // clamp UI updates
for (let tabId of this._pendingTabs) {
this._updateTabNow(tabId);
}
this._pendingTabs.clear();
}, 200);
}
this._pendingTabs.add(tabId);
},
_updateTabNow(tabId) {
this._pendingTabs.delete(tabId);
let records = this.map.get(tabId) || this.initTab(tabId);
let {allowed, blocked, noscriptFrames} = records;
let topAllowed = !(noscriptFrames && noscriptFrames[0]);
let numAllowed = 0, numBlocked = 0, sum = 0;
let report = this.types.map(t => {
let a = allowed[t] && allowed[t].length || 0, b = blocked[t] && blocked[t].length || 0, s = a + b;
numAllowed+= a, numBlocked += b, sum += s;
return s && `<${t === "sub_frame" ? "frame" : t}>: ${b}/${s}`;
}).filter(s => s).join("\n");
let enforced = ns.isEnforced(tabId);
let icon = enforced ?
(topAllowed ? (numBlocked ? "part" : "yes")
: (numAllowed ? "sub" : "no")) // not topAllowed
: "global"; // not enforced
let showBadge = ns.local.showCountBadge && numBlocked > 0;
let browserAction = browser.browserAction;
if (!browserAction.setIcon) { // Fennec
browserAction.setTitle({tabId, title: `NoScript (${numBlocked})`});
return;
}
browserAction.setIcon({tabId, path: {64: `/img/ui-${icon}64.png`}});
browserAction.setBadgeText({tabId, text: showBadge ? numBlocked.toString() : ""});
browserAction.setBadgeBackgroundColor({tabId, color: [128, 0, 0, 160]});
browserAction.setTitle({tabId,
title: UA.mobile ? "NoScript" : `${VERSION_LABEL} \n${enforced ?
_("BlockedItems", [numBlocked, numAllowed + numBlocked]) + ` \n${report}`
: _("NotEnforced")}`
});
},
async probe(tabId) {
if (tabId === undefined) {
(await browser.tabs.query({})).forEach(tab => TabStatus.probe(tab.id));
} else {
try {
TabStatus.recordAll(tabId, await ns.collectSeen(tabId));
} catch (e) {
error(e);
}
}
},
recordAll(tabId, seen) {
if (seen) {
let records = TabStatus.map.get(tabId);
if (records) {
records.allowed = {};
records.blocked = {};
}
for (let thing of seen) {
let {request, allowed} = thing;
request.tabId = tabId;
debug(`Recording`, request);
TabStatus._record(request, allowed ? "allowed" : "blocked");
if (request.key === "noscript-probe" && request.type === "main_frame" ) {
request.frameId = 0;
TabStatus._record(request, "noscriptFrame", !allowed);
}
}
this._updateTabNow(tabId);
}
},
async onActivatedTab(info) {
let {tabId} = info;
let seen = await ns.collectSeen(tabId);
TabStatus.recordAll(tabId, seen);
},
onRemovedTab(tabId) {
TabStatus.map.delete(tabId);
TabStatus._originsCache.clear();
TabStatus._pendingTabs.delete(tabId);
},
}
browser.tabs.onActivated.addListener(TabStatus.onActivatedTab);
browser.tabs.onRemoved.addListener(TabStatus.onRemovedTab);
let messageHandler = {
async pageshow(message, sender) {
TabStatus.recordAll(sender.tab.id, message.seen);
return true;
},
violation({url, type}, sender) {
let tabId = sender.tab.id;
let {frameId} = sender;
let r = {
url, type, tabId, frameId, documentUrl: sender.url
};
Content.reportTo(r, false, policyTypesMap[type]);
debug("Violation", type, url, tabId, frameId);
if (type === "script" && url === sender.url) {
TabStatus.record(r, "noscriptFrame", true);
} else {
TabStatus.record(r, "blocked");
}
},
async blockedObjects(message, sender) {
let {url, documentUrl, policyType} = message;
let TAG = `<${policyType.toUpperCase()}>`;
let origin = Sites.origin(url);
let {siteKey} = Sites.parse(url);
let options;
if (siteKey === origin) {
origin = new URL(url).protocol;
}
options = [
{label: _("allowLocal", siteKey), checked: true},
{label: _("allowLocal", origin)},
{label: _("CollapseBlockedObjects")},
];
let t = u => `${TAG}@${u}`;
let ret = await Prompts.prompt({
title: _("BlockedObjects"),
message: _("allowLocal", TAG),
options});
debug(`Prompt returned`, ret, sender);
if (ret.button !== 0) return;
if (ret.option === 2) {
return {collapse: "all"};
}
let key = [siteKey, origin][ret.option || 0];
if (!key) return;
let {siteMatch, contextMatch, perms} = ns.policy.get(key, documentUrl);
let {capabilities} = perms;
if (!capabilities.has(policyType)) {
let temp = sender.tab.incognito; // we don't want to store in PBM
perms = new Permissions(new Set(capabilities), temp);
perms.capabilities.add(policyType);
/* TODO: handle contextual permissions
if (documentUrl) {
let context = new URL(documentUrl).origin;
let contextualSites = new Sites([context, perms]);
perms = new Permissions(new Set(capabilities), false, contextualSites);
}
*/
ns.policy.set(key, perms);
await ns.savePolicy();
}
return {enable: key};
},
}
const Content = {
async reportTo(request, allowed, policyType) {
let {requestId, tabId, frameId, type, url, documentUrl, originUrl} = request;
let pending = pendingRequests.get(requestId); // null if from a CSP report
let initialUrl = pending ? pending.initialUrl : request.url;
request = {
key: Policy.requestKey(url, type, documentUrl || "", /^(media|object|frame)$/.test(type)),
type, url, documentUrl, originUrl
};
if (tabId < 0) {
if (type === "script" && url.startsWith("https://") && documentUrl && documentUrl.startsWith("https://")) {
// service worker / importScripts()?
let payload = {request, allowed, policyType, serviceWorker: Sites.origin(documentUrl)};
let recipient = {frameId: 0};
for (let tabId of TabStatus.findTabsByOrigin(payload.serviceWorker)) {
recipient.tabId = tabId;
try {
Messages.send("seen", payload, recipient);
} catch (e) {
// likely a privileged tab where our content script couldn't run
}
}
}
return;
}
if (pending) request.initialUrl = pending.initialUrl;
if (type !== "sub_frame") { // we couldn't deliver it to frameId, since it's generally not loaded yet
try {
await Messages.send("seen",
{request, allowed, policyType, ownFrame: true},
{tabId, frameId}
);
} catch (e) {
debug(`Couldn't deliver "seen" message for ${type}@${url} ${allowed ? "A" : "F" } to document ${documentUrl} (${frameId}/${tabId})`, e);
}
}
if (frameId === 0) return;
try {
await Messages.send("seen",
{request, allowed, policyType},
{tabId, frameId: 0}
);
} catch (e) {
debug(`Couldn't deliver "seen" message to top frame containing ${documentUrl} (${frameId}/${tabId}`, e);
}
}
};
const pendingRequests = new Map();
function initPendingRequest(request) {
let {requestId, url} = request;
let redirected = pendingRequests.get(requestId);
let initialUrl = redirected ? redirected.initialUrl : url;
pendingRequests.set(requestId, {
initialUrl, url, redirected,
onCompleted: new Set(),
});
return redirected;
}
let normalizeRequest = UA.isMozilla ? () => {} : request => {
if ("initiator" in request && !("originUrl" in request)) {
request.originUrl = request.initiator;
if (request.type !== "main_frame" && !("documentUrl" in request)) {
request.documentUrl = request.initiator;
}
}
};
function intersectCapabilities(perms, request) {
let {frameId, frameAncestors, tabId} = request;
if (frameId !== 0 && ns.sync.cascadeRestrictions) {
let topUrl = frameAncestors && frameAncestors.length
&& frameAncestors[frameAncestors.length - 1].url;
if (!topUrl) {
let tab = TabCache.get(tabId);
if (tab) topUrl = tab.url;
}
if (topUrl) {
return ns.policy.cascadeRestrictions(perms, topUrl).capabilities;
}
}
return perms.capabilities;
}
const ABORT = {cancel: true}, ALLOW = {};
const recent = {
MAX_AGE: 500,
_pendingGC: 0,
_byUrl: new Map(),
find(request, last = this._byUrl.get(request.url)) {
if (!last) return null;
for (let j = last.length; j-- > 0;) {
let other = last[j];
if (request.timeStamp - other.timeStamp > this.MAX_AGE) {
last.splice(0, ++j);
if (last.length === 0) this._byUrl.delete(other.url);
break;
}
if (request.url && other.type === request.type && other.documentUrl === request.documentUrl
&& other.tabId === request.tabId && other.frameId === request.frameId) {
return other;
}
}
return null;
},
add(request) {
let last = this._byUrl.get(request.url);
if (!last) {
last = [request];
this._byUrl.set(request.url, last);
} else {
last.push(request);
}
this._gc();
return;
},
_gc(now) {
if (!now && this._pendingGC) return;
debug("Recent requests garbage collection.");
let request = {timeStamp: Date.now()};
for (let last of this._byUrl.values()) {
this.find(request, last);
}
this._pendingGC = this._byUrl.size ?
setTimeout(() => this._gc(true), 1000)
: 0;
}
};
const listeners = {
onBeforeRequest(request) {
normalizeRequest(request);
try {
let redirected = initPendingRequest(request);
let {policy} = ns
let {type} = request;
if (type in policyTypesMap) {
let previous = recent.find(request);
if (previous) {
debug("Rapid fire request", previous);
return previous.return;
}
(previous = request).return = ALLOW;
recent.add(previous);
let policyType = policyTypesMap[type];
let {url, originUrl, documentUrl, tabId} = request;
let isFetch = "fetch" === policyType;
if ((isFetch || "frame" === policyType) &&
(((isFetch && (!originUrl ||
browser.runtime.onSyncMessage &&
url.includes(browser.runtime.onSyncMessage.ENDPOINT_PREFIX)
) || url === originUrl) && originUrl === documentUrl
// some extensions make them both undefined,
// see https://github.com/eight04/image-picka/issues/150
) ||
Sites.isInternal(originUrl))
) {
// livemark request or similar browser-internal, always allow;
return ALLOW;
}
if (/^(?:data|blob):/.test(url)) {
request._dataUrl = url;
request.url = url = documentUrl || originUrl;
}
let allowed = Sites.isInternal(url);
if (!allowed) {
if (tabId < 0 && documentUrl && documentUrl.startsWith("https://")) {
let origin = Sites.origin(documentUrl);
allowed = [...ns.unrestrictedTabs]
.some(tabId => TabStatus.hasOrigin(tabId, origin));
} else {
allowed = !ns.isEnforced(tabId);
}
if (!allowed) {
let capabilities = intersectCapabilities(
policy.get(url, documentUrl).perms,
request);
allowed = !policyType || capabilities.has(policyType);
if (allowed && request._dataUrl && type.endsWith("frame")) {
let blocker = csp.buildFromCapabilities(capabilities);
if (blocker) {
let redirectUrl = CSP.patchDataURI(request._dataUrl, blocker);
if (redirectUrl !== request._dataUrl) {
return previous.return = {redirectUrl};
}
}
}
}
}
if (type !== "main_frame") {
Content.reportTo(request, allowed, policyType);
}
if (!allowed) {
debug(`Blocking ${policyType}`, request);
TabStatus.record(request, "blocked");
return previous.return = ABORT;
}
}
} catch (e) {
error(e);
}
return ALLOW;
},
onHeadersReceived(request) {
// called for main_frame, sub_frame and object
// check for duplicate calls
let pending = pendingRequests.get(request.requestId);
if (pending) {
if (pending.headersProcessed) {
if (!request.fromCache) {
debug("Headers already processed, skipping ", request);
return ALLOW;
}
debug("Reprocessing headers for cached request ", request);
} else {
debug("onHeadersReceived", request);
}
} else {
debug("[WARNING] no pending information for ", request);
initPendingRequest(request);
pending = pendingRequests.get(request.requestId);
}
if (request.fromCache && listeners.onHeadersReceived.resetCSP && !pending.resetCachedCSP) {
debug("Resetting CSP Headers");
pending.resetCachedCSP = true;
let {responseHeaders} = request;
let headersCount = responseHeaders.length;
let purged = false;
responseHeaders.forEach((h, index) => {
if (csp.isMine(h)) {
responseHeaders.splice(index, 1);
}
});
if (headersCount > responseHeaders.length) {
debug("Resetting cached NoScript CSP header(s)", request);
return {responseHeaders};
}
}
normalizeRequest(request);
let result = ALLOW;
let headersModified = false;
pending.headersProcessed = true;
let {url, documentUrl, tabId, responseHeaders, type} = request;
let isMainFrame = type === "main_frame";
try {
let capabilities;
if (ns.isEnforced(tabId)) {
let policy = ns.policy;
let perms = policy.get(url, documentUrl).perms;
if (isMainFrame) {
if (policy.autoAllowTop && perms === policy.DEFAULT) {
policy.set(Sites.optimalKey(url), perms = policy.TRUSTED.tempTwin);
}
capabilities = perms.capabilities;
} else {
capabilities = intersectCapabilities(perms, request);
}
} // else unrestricted, either globally or per-tab
if (isMainFrame && !TabStatus.map.has(tabId)) {
debug("No TabStatus data yet for noscriptFrame", tabId);
TabStatus.record(request, "noscriptFrame",
capabilities && !capabilities.has("script"));
}
let header = csp.patchHeaders(responseHeaders, capabilities);
/*
// Uncomment me to disable networking-level CSP for debugging purposes
header = null;
*/
if (header) {
pending.cspHeader = header;
debug(`CSP blocker on %s:`, url, header.value);
headersModified = true;
}
if (headersModified) {
result = {responseHeaders};
debug("Headers changed ", request);
}
} catch (e) {
error(e, "Error in onHeadersReceived", request);
}
return result;
},
onResponseStarted(request) {
normalizeRequest(request);
debug("onResponseStarted", request);
let {requestId, url, tabId, frameId, type} = request;
if (type === "main_frame") {
TabStatus.initTab(tabId);
}
let scriptBlocked = request.responseHeaders.some(
h => csp.isMine(h) && csp.blocks(h.value, "script")
);
debug("%s scriptBlocked=%s setting noscriptFrame on ", url, scriptBlocked, tabId, frameId);
TabStatus.record(request, "noscriptFrame", scriptBlocked);
let pending = pendingRequests.get(requestId);
if (pending) {
pending.scriptBlocked = scriptBlocked;
if (!(pending.headersProcessed &&
(scriptBlocked || ns.requestCan(request, "script"))
)) {
debug("[WARNING] onHeadersReceived %s %o", frameId, tabId,
pending.headersProcessed ? "has been overridden on": "could not process",
request);
}
}
},
onCompleted(request) {
let {requestId} = request;
if (pendingRequests.has(requestId)) {
let r = pendingRequests.get(requestId);
pendingRequests.delete(requestId);
for (let callback of r.onCompleted) {
try {
callback(request, r);
} catch (e) {
error(e);
}
}
}
},
onErrorOccurred(request) {
pendingRequests.delete(request.requestId);
}
};
function fakeRequestFromCSP(report, request) {
let type = report["violated-directive"].split("-", 1)[0]; // e.g. script-src 'none' => script
if (type === "frame") type = "sub_frame";
let url = report['blocked-uri'];
if (!url || url === 'self') url = request.documentUrl;
return Object.assign({}, request, {
url,
type,
});
}
let utf8Decoder = new TextDecoder("UTF-8");
function onViolationReport(request) {
try {
let text = utf8Decoder.decode(request.requestBody.raw[0].bytes);
if (text.includes(`"inline"`)) return ABORT;
let report = JSON.parse(text)["csp-report"];
let originalPolicy = report["original-policy"]
debug("CSP report", report);
let blockedURI = report['blocked-uri'];
if (blockedURI && blockedURI !== 'self') {
let r = fakeRequestFromCSP(report, request);
if (!/:/.test(r.url)) r.url = request.documentUrl;
Content.reportTo(r, false, policyTypesMap[r.type]);
TabStatus.record(r, "blocked");
} else if (report["violated-directive"].startsWith("script-src") && (originalPolicy.includes("script-src 'none'"))) {
let r = fakeRequestFromCSP(report, request);
Content.reportTo(r, false, "script"); // NEW
TabStatus.record(r, "noscriptFrame", true);
}
} catch(e) {
error(e);
}
return ABORT;
}
function injectPolicyScript(details) {
let {url, tabId, frameId} = details;
let policy = ns.computeChildPolicy({url}, {tab: {id: tabId}, frameId});
policy.navigationURL = url;
let debugStatement = ns.local.debug ? `console.debug("domPolicy", domPolicy, mark);` : '';
return `
let mark = Date.now() + ":" + Math.random();
console.log("domPolicy", document.readyState, mark);
let domPolicy = ${JSON.stringify(policy)};
let {ns} = window;
if (ns) {
ns.domPolicy = domPolicy;
if (ns.setup) {
if (ns.syncSetup) ns.syncSetup(domPolicy);
else if (!ns.pendingSyncFetchPolicy) {
ns.setup(domPolicy);
}
} ;
} else {
window.ns = {domPolicy}
}
${debugStatement}`;
}
const RequestGuard = {
async start() {
Messages.addHandler(messageHandler);
let wr = browser.webRequest;
let listen = (what, ...args) => wr[what].addListener(listeners[what], ...args);
let allUrls = ["<all_urls>"];
let docTypes = ["main_frame", "sub_frame", "object"];
let filterDocs = {urls: allUrls, types: docTypes};
let filterAll = {urls: allUrls};
listen("onBeforeRequest", filterAll, ["blocking"]);
let mergingCSP = "getBrowserInfo" in browser.runtime;
if (mergingCSP) {
let {vendor, version} = await browser.runtime.getBrowserInfo();
mergingCSP = vendor === "Mozilla" && parseInt(version) >= 77;
}
if (mergingCSP) {
// In Gecko>=77 (https://bugzilla.mozilla.org/show_bug.cgi?id=1462989)
// we need to cleanup our own cached headers in a dedicated listener :(
// see also https://trac.torproject.org/projects/tor/ticket/34305
wr.onHeadersReceived.addListener(
listeners.onHeadersReceived.resetCSP = request => {
return listeners.onHeadersReceived(request);
}, filterDocs, ["blocking", "responseHeaders"]);
}
listen("onHeadersReceived", filterDocs, ["blocking", "responseHeaders"]);
// Still, other extensions may accidentally delete our CSP header
// if called before us, hence we try our best reinjecting it in the end
(listeners.onHeadersReceivedLast =
new LastListener(wr.onHeadersReceived, request => {
let {requestId, responseHeaders} = request;
let pending = pendingRequests.get(request.requestId);
if (pending && pending.headersProcessed) {
let {cspHeader} = pending;
if (cspHeader) {
responseHeaders.push(cspHeader);
return {responseHeaders};
}
} else {
debug("[WARNING] onHeadersReceived not called (yet?)", request);
}
return ALLOW;
}, filterDocs, ["blocking", "responseHeaders"])).install();
listen("onResponseStarted", filterDocs, ["responseHeaders"]);
listen("onCompleted", filterAll);
listen("onErrorOccurred", filterAll);
if (csp.reportURI) {
wr.onBeforeRequest.addListener(onViolationReport,
{urls: [csp.reportURI], types: ["csp_report"]}, ["blocking", "requestBody"]);
}
DocStartInjection.register(injectPolicyScript);
TabStatus.probe();
},
stop() {
let wr = browser.webRequest;
for (let [name, listener] of Object.entries(listeners)) {
if (typeof listener === "function") {
wr[name].removeListener(listener);
} else if (listener instanceof LastListener) {
listener.uninstall();
}
}
wr.onBeforeRequest.removeListener(onViolationReport);
if (listeners.onHeadersReceived.resetCSP) {
wr.onHeadersReceived.removeListener(listeners.onHeadersReceived.resetCSP);
}
DocStartInjection.unregister(injectPolicyScript);
Messages.removeHandler(messageHandler);
}
};
return RequestGuard;
})();