[nscl] Switch early content script configuration to use /nscl/service/DocStartInjection.js.

This commit is contained in:
hackademix 2021-04-26 13:06:29 +02:00
parent c293ebc250
commit cc06e86a1d
5 changed files with 52 additions and 58 deletions

View File

@ -533,10 +533,12 @@ var RequestGuard = (() => {
} catch (e) {
error(e, "Error in onHeadersReceived", request);
}
promises = promises.filter(p => p instanceof Promise);
if (promises.length > 0) {
return Promise.all(promises).then(() => result);
}
return result;
},
onResponseStarted(request) {
@ -553,6 +555,7 @@ var RequestGuard = (() => {
TabStatus.record(request, "noscriptFrame", scriptBlocked);
let pending = pendingRequests.get(requestId);
if (pending) {
pending.scriptBlocked = scriptBlocked;
if (!(pending.headersProcessed &&
(scriptBlocked || ns.requestCan(request, "script"))
@ -617,38 +620,28 @@ var RequestGuard = (() => {
return ABORT;
}
async function onNavCommitted(details) {
debug("onNavCommitted", details);
function injectPolicyScript(details) {
let {url, tabId, frameId} = details;
try {
let policy = ns.computeChildPolicy({url}, {tab: {id: tabId}, frameId});
policy.navigationURL = url;
let debugStatement = ns.local.debug ? 'console.debug("domPolicy", domPolicy);' : '';
let ret = await browser.tabs.executeScript(details.tabId, {
code:
`{
let domPolicy = ${JSON.stringify(policy)};
if (this.ns) {
ns.domPolicy = domPolicy;
if (ns.setup) {
if (ns.syncSetup) ns.syncSetup(domPolicy);
else if (!ns.pendingSyncFetchPolicy) {
ns.setup(domPolicy);
}
} ;
} else {
ns = {domPolicy}
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);
}
${debugStatement}
}
ns;`,
runAt: "document_start",
frameId,
});
debug("onNavCommitted return: ", ret);
} catch(e) {
console.error(e);
}
} ;
} else {
window.ns = {domPolicy}
}
${debugStatement}`;
}
const RequestGuard = {
@ -702,11 +695,10 @@ var RequestGuard = (() => {
wr.onBeforeRequest.addListener(onViolationReport,
{urls: [csp.reportURI], types: ["csp_report"]}, ["blocking", "requestBody"]);
}
browser.webNavigation.onCommitted.addListener(onNavCommitted);
DocStartInjection.register(injectPolicyScript);
TabStatus.probe();
},
stop() {
browser.webNavigation.onCommitted.removeListener(onNavCommitted);
let wr = browser.webRequest;
for (let [name, listener] of Object.entries(listeners)) {
if (typeof listener === "function") {
@ -719,6 +711,7 @@ var RequestGuard = (() => {
if (listeners.onHeadersReceived.resetCSP) {
wr.onHeadersReceived.removeListener(listeners.onHeadersReceived.resetCSP);
}
DocStartInjection.unregister(injectPolicyScript);
Messages.removeHandler(messageHandler);
}
};

View File

@ -42,6 +42,15 @@
//, document.domain, document.baseURI, window.isSecureContext // DEV_ONLY
);
if (this.domPolicy) {
debug("Injected policy found!");
try {
this.setup(this.domPolicy);
return;
} catch(e) {
error(e);
}
}
if (/^(ftp|file):/.test(url)) { // ftp: or file: - no CSP headers yet
if (this.syncFetchPolicy) {
@ -52,15 +61,6 @@
return;
}
} else {
if (this.domPolicy) {
debug("File policy set in webNavigation found!");
try {
this.setup(this.domPolicy);
return;
} catch(e) {
error(e);
}
}
// CSP headers have been already provided by webRequest, we are not in a hurry...
if (/^(javascript|about):/.test(url)) {
url = document.readyState === "loading"
@ -118,6 +118,6 @@
return this.capabilities && this.capabilities.has(cap);
},
};
this.ns = this.ns ? Object.assign(ns, this.ns) : ns;
debug("StaticNS", JSON.stringify(this.ns)); // DEV_ONLY
window.ns = window.ns ? Object.assign(ns, window.ns) : ns;
debug("StaticNS", window.domPolicy, Date.now(), JSON.stringify(window.ns)); // DEV_ONLY
}

View File

@ -1,6 +1,6 @@
"use strict";
(this.ns || (this.ns = {})).syncFetchPolicy = function() {
(window.ns || (window.ns = {})).syncFetchPolicy = function() {
let url = document.URL;
@ -218,7 +218,7 @@
}
};
if (this.ns.pendingSyncFetchPolicy) {
this.ns.pendingSyncFetchPolicy = false;
this.ns.syncFetchPolicy();
if (ns.pendingSyncFetchPolicy) {
ns.pendingSyncFetchPolicy = false;
ns.syncFetchPolicy();
}

View File

@ -36,6 +36,7 @@
"scripts": [
"/nscl/lib/browser-polyfill.js",
"/nscl/lib/punycode.js",
"/nscl/lib/sha256.js",
"/nscl/common/UA.js",
"/nscl/common/uuid.js",
"/nscl/common/SyncMessage.js",
@ -53,6 +54,7 @@
"/nscl/common/SyntaxChecker.js",
"/nscl/common/Storage.js",
"/nscl/common/include.js",
"/nscl/service/DocStartInjection.js",
"/nscl/service/LastListener.js",
"/nscl/service/TabCache.js",
"ui/Prompts.js",
@ -75,15 +77,6 @@
"/content/content.css"
]
},
{
"run_at": "document_start",
"matches": ["file://*/*", "ftp://*/*"],
"all_frames": true,
"js": [
"lib/DocumentFreezer.js",
"content/syncFetchPolicy.js"
]
},
{
"run_at": "document_start",
"matches": ["<all_urls>"],
@ -114,13 +107,21 @@
"content/content.js"
]
},
{
"run_at": "document_start",
"matches": ["ftp://*/*"],
"js": [
"content/ftp.js"
]
},
{
"run_at": "document_start",
"matches": ["file://*/*", "ftp://*/*"],
"all_frames": true,
"js": [
"lib/DocumentFreezer.js",
"content/syncFetchPolicy.js"
]
}
],

@ -1 +1 @@
Subproject commit b6506b0275720233a500fbdb1cdb6548815405eb
Subproject commit c89b6b972aafbdb6bb1e520d7ecb4eea67d727d1