Skip to content

Commit 6e80d3f

Browse files
committed
Let content script inject failsafe CSP in the DOM.
1 parent e82e961 commit 6e80d3f

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

src/content/DocumentCSP.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
3+
class DocumentCSP {
4+
constructor(document) {
5+
this.document = document;
6+
this.builder = new CapsCSP();
7+
}
8+
9+
apply(capabilities) {
10+
let csp = this.builder;
11+
let blocker = csp.buildFromCapabilities(capabilities);
12+
if (!blocker) return;
13+
14+
let document = this.document;
15+
let header = csp.asHeader(blocker);
16+
let meta = document.createElementNS("http://www.w3.org/1999/xhtml", "meta");
17+
meta.setAttribute("http-equiv", header.name);
18+
meta.setAttribute("content", header.value);
19+
let parent = document.head || document.documentElement;
20+
try {
21+
parent.insertBefore(meta, parent.firstChild);
22+
} catch (e) {
23+
error(e, "Error inserting CSP %s in the DOM", header && header.value);
24+
}
25+
}
26+
}

src/content/content.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,21 @@
6161

6262
if (!this.perms.DEFAULT || this.perms.tabInfo.unrestricted) {
6363
this.allows = () => true;
64+
this.capabilities = Object.assign(
65+
new Set(["script"]), { has() { return true; } });
66+
} else {
67+
let perms = this.perms.CURRENT || this.perms.DEFAULT;
68+
this.capabilities = new Set(perms.capabilities);
69+
new DocumentCSP(document).apply(this.capabilities);
6470
}
6571
ns.fire("perms");
6672
},
6773
perms: { DEFAULT: null, CURRENT: null, tabInfo: {}, MARKER: "" },
74+
6875
allows(cap) {
69-
let perms = this.perms.CURRENT;
70-
return perms && perms.capabilities.includes(cap);
76+
return this.capabilities && this.capabilities.has(cap);
7177
},
78+
7279
getWindowName() {
7380
return top !== window || !this.perms.MARKER ? window.name
7481
: window.name.split(this.perms.MARKER + ",").pop();

src/manifest.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@
6767
"js": [
6868
"lib/log.js",
6969
"lib/Messages.js",
70+
"lib/CSP.js",
71+
"common/CapsCSP.js",
72+
"content/DocumentCSP.js",
7073
"content/onScriptDisabled.js",
7174
"content/content.js",
7275
"content/webglHook.js",

0 commit comments

Comments
 (0)