Skip to content

Commit 1e36986

Browse files
committed
Work-arounds for edge cases in synchronous page loads bypassing webRequest (thanks skriptimaahinen).
1 parent d4c2ab2 commit 1e36986

3 files changed

Lines changed: 20 additions & 27 deletions

File tree

src/content/DocumentCSP.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
'use strict';
2-
32
class DocumentCSP {
43
constructor(document) {
54
this.document = document;
@@ -33,6 +32,9 @@ class DocumentCSP {
3332
meta.setAttribute("http-equiv", header.name);
3433
meta.setAttribute("content", header.value);
3534
let root = document.documentElement;
35+
let rootAttrs = [...root.attributes].filter(a => a.name.toLowerCase().startsWith("on"));
36+
for (let a of rootAttrs) root.removeAttributeNode(a);
37+
3638
let {head} = document;
3739
let parent = head ||
3840
(root instanceof HTMLElement
@@ -44,6 +46,9 @@ class DocumentCSP {
4446
debug(`Failsafe <meta> CSP inserted in %s: "%s"`, document.URL, header.value);
4547
meta.remove();
4648
if (!head) parent.remove();
49+
for (let a of rootAttrs) {
50+
root.setAttributeNodeNS(a);
51+
}
4752
} catch (e) {
4853
error(e, "Error inserting CSP %s in %s", document.URL, header && header.value);
4954
return false;

src/content/staticNS.js

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,9 @@
6666
}
6767

6868
let originalState = document.readyState;
69-
let blockedScripts = [];
69+
let syncLoad = UA.isMozilla && /^(?:ftp|file):/.test(url);
7070
let localPolicyKey, localPolicy;
71-
if (UA.isMozilla && /^(?:ftp|file):/.test(url)) {
72-
71+
if (syncLoad) {
7372
localPolicyKey = `ns.policy.${url}|${browser.runtime.getURL("")}`;
7473
let localPolicy = sessionStorage.getItem(localPolicyKey);
7574
sessionStorage.removeItem(localPolicyKey);
@@ -81,39 +80,27 @@
8180
} catch(e) {
8281
error(e, "Could not setup local policy", localPolicy);
8382
}
83+
} else {
84+
addEventListener("beforescriptexecute", e => {
85+
console.log("Blocking early script", e.target);
86+
e.preventDefault();
87+
});
88+
stop();
8489
}
8590
}
8691

87-
let policy = null;
88-
8992
let setup = policy => {
9093
debug("Fetched %o, readyState %s", policy, document.readyState); // DEV_ONLY
9194
this.setup(policy);
92-
if (this.canScript && blockedScripts.length && originalState === "loading") {
93-
log("Running suspended scripts which are permitted by %s policy.", url);
94-
// something went wrong, e.g. with session restore.
95-
if (url.startsWith("file:") && !localPolicy) {
96-
stop();
97-
sessionStorage.setItem(localPolicyKey, JSON.stringify(policy));
98-
location.reload(false);
99-
return;
100-
}
101-
for (let s of blockedScripts) {
102-
// reinsert the script:
103-
// just s.cloneNode(true) doesn't work, the script wouldn't run,
104-
// let's clone it the hard way...
105-
try {
106-
s.replaceWith(document.createRange().createContextualFragment(s.outerHTML));
107-
} catch (e) {
108-
error(e);
109-
}
110-
}
95+
if (syncLoad && !localPolicy) {
96+
sessionStorage.setItem(localPolicyKey, JSON.stringify(policy));
97+
return;
11198
}
11299
}
113100

114101
for (;;) {
115102
try {
116-
policy = browser.runtime.sendSyncMessage(
103+
browser.runtime.sendSyncMessage(
117104
{id: "fetchPolicy", url, contextUrl: url},
118105
setup);
119106
break;

src/lib/SyncMessage.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@
221221
// or when other extensions manipulate the DOM early) we additionally
222222
// suspend on beforescriptexecute events
223223

224+
let startTime = Date.now(); // DEV_ONLY
224225
let suspendURL = url + "&suspend=true";
225226
let suspended = 0;
226227
let suspendedId = 0;
@@ -236,7 +237,7 @@
236237
console.error(e);
237238
}
238239
suspended--;
239-
console.debug("sendSyncMessage resume #%s/%s", id, suspended);
240+
console.debug("sendSyncMessage resume #%s/%s - %sms", id, suspended, Date.now() - startTime); // DEV_ONLY
240241
};
241242

242243

0 commit comments

Comments
 (0)