Skip to content

Commit 5d92d1b

Browse files
committed
Anticipate onScriptDisabled surrogates to first script-src 'none' CSP violation.
1 parent 67d4a97 commit 5d92d1b

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

src/content/content.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,10 @@ window.addEventListener("pageshow", notifyPage);
8787
let violations = new Set();
8888
window.addEventListener("securitypolicyviolation", e => {
8989
if (!e.isTrusted) return;
90-
let type = e.violatedDirective.split("-", 1)[0]; // e.g. script-src 'none' => script
90+
let {violatedDirective} = e;
91+
if (violatedDirective === `script-src 'none'`) onScriptDisabled();
92+
93+
let type = violatedDirective.split("-", 1)[0]; // e.g. script-src 'none' => script
9194
let url = e.blockedURI;
9295
if (!(url && url.includes(":"))) {
9396
url = document.URL;
@@ -120,8 +123,7 @@ ns.on("capabilities", () => {
120123
})();
121124
}
122125

123-
if (document.readyState !== "loading") onScriptDisabled();
124-
window.addEventListener("DOMContentLoaded", onScriptDisabled);
126+
onScriptDisabled();
125127
}
126128

127129
notifyPage();

src/content/onScriptDisabled.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
function onScriptDisabled() {
2+
if (document.readyState === "loading") {
3+
window.addEventListener("DOMContentLoaded", e => onScriptDisabled());
4+
return;
5+
}
6+
onScriptDisabled = () => {};
27
let refresh = false;
38
for (let noscript of document.querySelectorAll("noscript")) {
49
// force show NOSCRIPT elements content

0 commit comments

Comments
 (0)