Skip to content

Commit fb8fa90

Browse files
committed
Better timing for event attributes removal/restore.
1 parent 06a4d27 commit fb8fa90

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

src/content/DocumentCSP.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ class DocumentCSP {
33
constructor(document) {
44
this.document = document;
55
this.builder = new CapsCSP();
6+
this.root = document.documentElement;
7+
}
8+
9+
removeEventAttributes() {
10+
console.debug("Removing event attributes"); // DEV_ONLY
11+
let {root} = this;
12+
this.rootAttrs = [...root.attributes].filter(a => a.name.toLowerCase().startsWith("on"));
13+
for (let a of this.rootAttrs) root.removeAttributeNode(a);
614
}
715

816
apply(capabilities, embedding = CSP.isEmbedType(this.document.contentType)) {
@@ -46,14 +54,19 @@ class DocumentCSP {
4654
debug(`Failsafe <meta> CSP inserted in %s: "%s"`, document.URL, header.value);
4755
meta.remove();
4856
if (!head) parent.remove();
49-
for (let a of rootAttrs) {
50-
root.setAttributeNodeNS(a);
51-
}
5257
} catch (e) {
5358
error(e, "Error inserting CSP %s in %s", document.URL, header && header.value);
5459
return false;
5560
}
5661
return true;
5762
}
5863

64+
restoreEventAttributes() {
65+
if (!this.rootAttrs) return;
66+
console.debug("Restoring event attributes"); // DEV_ONLY
67+
let {root, rootAttrs} = this;
68+
for (let a of rootAttrs) {
69+
root.setAttributeNodeNS(a);
70+
}
71+
}
5972
}

src/content/staticNS.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
'use strict';
33
let listenersMap = new Map();
44
let backlog = new Set();
5-
5+
let documentCSP = new DocumentCSP(document);
6+
documentCSP.removeEventAttributes();
67
let ns = {
78
debug: true, // DEV_ONLY
89
get embeddingDocument() {
@@ -94,6 +95,7 @@
9495
this.setup(policy);
9596
if (syncLoad && !localPolicy) {
9697
sessionStorage.setItem(localPolicyKey, JSON.stringify(policy));
98+
location.reload(false);
9799
return;
98100
}
99101
}
@@ -129,9 +131,9 @@
129131
} else {
130132
let perms = policy.permissions;
131133
this.capabilities = new Set(perms.capabilities);
132-
new DocumentCSP(document).apply(this.capabilities, this.embeddingDocument);
134+
documentCSP.apply(this.capabilities, this.embeddingDocument);
133135
}
134-
136+
documentCSP.restoreEventAttributes();
135137
this.canScript = this.allows("script");
136138
this.fire("capabilities");
137139
},

0 commit comments

Comments
 (0)