Skip to content

Commit cc06e86

Browse files
committed
[nscl] Switch early content script configuration to use /nscl/service/DocStartInjection.js.
1 parent c293ebc commit cc06e86

5 files changed

Lines changed: 52 additions & 58 deletions

File tree

src/bg/RequestGuard.js

Lines changed: 25 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -533,10 +533,12 @@ var RequestGuard = (() => {
533533
} catch (e) {
534534
error(e, "Error in onHeadersReceived", request);
535535
}
536+
536537
promises = promises.filter(p => p instanceof Promise);
537538
if (promises.length > 0) {
538539
return Promise.all(promises).then(() => result);
539540
}
541+
540542
return result;
541543
},
542544
onResponseStarted(request) {
@@ -553,6 +555,7 @@ var RequestGuard = (() => {
553555
TabStatus.record(request, "noscriptFrame", scriptBlocked);
554556
let pending = pendingRequests.get(requestId);
555557
if (pending) {
558+
556559
pending.scriptBlocked = scriptBlocked;
557560
if (!(pending.headersProcessed &&
558561
(scriptBlocked || ns.requestCan(request, "script"))
@@ -617,38 +620,28 @@ var RequestGuard = (() => {
617620
return ABORT;
618621
}
619622

620-
async function onNavCommitted(details) {
621-
debug("onNavCommitted", details);
623+
function injectPolicyScript(details) {
622624
let {url, tabId, frameId} = details;
623-
try {
624-
let policy = ns.computeChildPolicy({url}, {tab: {id: tabId}, frameId});
625-
policy.navigationURL = url;
626-
let debugStatement = ns.local.debug ? 'console.debug("domPolicy", domPolicy);' : '';
627-
let ret = await browser.tabs.executeScript(details.tabId, {
628-
code:
629-
`{
630-
let domPolicy = ${JSON.stringify(policy)};
631-
if (this.ns) {
632-
ns.domPolicy = domPolicy;
633-
if (ns.setup) {
634-
if (ns.syncSetup) ns.syncSetup(domPolicy);
635-
else if (!ns.pendingSyncFetchPolicy) {
636-
ns.setup(domPolicy);
637-
}
638-
} ;
639-
} else {
640-
ns = {domPolicy}
625+
let policy = ns.computeChildPolicy({url}, {tab: {id: tabId}, frameId});
626+
policy.navigationURL = url;
627+
let debugStatement = ns.local.debug ? `console.debug("domPolicy", domPolicy, mark);` : '';
628+
return `
629+
let mark = Date.now() + ":" + Math.random();
630+
console.log("domPolicy", document.readyState, mark);
631+
let domPolicy = ${JSON.stringify(policy)};
632+
let {ns} = window;
633+
if (ns) {
634+
ns.domPolicy = domPolicy;
635+
if (ns.setup) {
636+
if (ns.syncSetup) ns.syncSetup(domPolicy);
637+
else if (!ns.pendingSyncFetchPolicy) {
638+
ns.setup(domPolicy);
641639
}
642-
${debugStatement}
643-
}
644-
ns;`,
645-
runAt: "document_start",
646-
frameId,
647-
});
648-
debug("onNavCommitted return: ", ret);
649-
} catch(e) {
650-
console.error(e);
651-
}
640+
} ;
641+
} else {
642+
window.ns = {domPolicy}
643+
}
644+
${debugStatement}`;
652645
}
653646

654647
const RequestGuard = {
@@ -702,11 +695,10 @@ var RequestGuard = (() => {
702695
wr.onBeforeRequest.addListener(onViolationReport,
703696
{urls: [csp.reportURI], types: ["csp_report"]}, ["blocking", "requestBody"]);
704697
}
705-
browser.webNavigation.onCommitted.addListener(onNavCommitted);
698+
DocStartInjection.register(injectPolicyScript);
706699
TabStatus.probe();
707700
},
708701
stop() {
709-
browser.webNavigation.onCommitted.removeListener(onNavCommitted);
710702
let wr = browser.webRequest;
711703
for (let [name, listener] of Object.entries(listeners)) {
712704
if (typeof listener === "function") {
@@ -719,6 +711,7 @@ var RequestGuard = (() => {
719711
if (listeners.onHeadersReceived.resetCSP) {
720712
wr.onHeadersReceived.removeListener(listeners.onHeadersReceived.resetCSP);
721713
}
714+
DocStartInjection.unregister(injectPolicyScript);
722715
Messages.removeHandler(messageHandler);
723716
}
724717
};

src/content/staticNS.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@
4242
//, document.domain, document.baseURI, window.isSecureContext // DEV_ONLY
4343
);
4444

45+
if (this.domPolicy) {
46+
debug("Injected policy found!");
47+
try {
48+
this.setup(this.domPolicy);
49+
return;
50+
} catch(e) {
51+
error(e);
52+
}
53+
}
4554

4655
if (/^(ftp|file):/.test(url)) { // ftp: or file: - no CSP headers yet
4756
if (this.syncFetchPolicy) {
@@ -52,15 +61,6 @@
5261
return;
5362
}
5463
} else {
55-
if (this.domPolicy) {
56-
debug("File policy set in webNavigation found!");
57-
try {
58-
this.setup(this.domPolicy);
59-
return;
60-
} catch(e) {
61-
error(e);
62-
}
63-
}
6464
// CSP headers have been already provided by webRequest, we are not in a hurry...
6565
if (/^(javascript|about):/.test(url)) {
6666
url = document.readyState === "loading"
@@ -118,6 +118,6 @@
118118
return this.capabilities && this.capabilities.has(cap);
119119
},
120120
};
121-
this.ns = this.ns ? Object.assign(ns, this.ns) : ns;
122-
debug("StaticNS", JSON.stringify(this.ns)); // DEV_ONLY
121+
window.ns = window.ns ? Object.assign(ns, window.ns) : ns;
122+
debug("StaticNS", window.domPolicy, Date.now(), JSON.stringify(window.ns)); // DEV_ONLY
123123
}

src/content/syncFetchPolicy.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use strict";
22

3-
(this.ns || (this.ns = {})).syncFetchPolicy = function() {
3+
(window.ns || (window.ns = {})).syncFetchPolicy = function() {
44

55
let url = document.URL;
66

@@ -218,7 +218,7 @@
218218
}
219219
};
220220

221-
if (this.ns.pendingSyncFetchPolicy) {
222-
this.ns.pendingSyncFetchPolicy = false;
223-
this.ns.syncFetchPolicy();
221+
if (ns.pendingSyncFetchPolicy) {
222+
ns.pendingSyncFetchPolicy = false;
223+
ns.syncFetchPolicy();
224224
}

src/manifest.json

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"scripts": [
3737
"/nscl/lib/browser-polyfill.js",
3838
"/nscl/lib/punycode.js",
39+
"/nscl/lib/sha256.js",
3940
"/nscl/common/UA.js",
4041
"/nscl/common/uuid.js",
4142
"/nscl/common/SyncMessage.js",
@@ -53,6 +54,7 @@
5354
"/nscl/common/SyntaxChecker.js",
5455
"/nscl/common/Storage.js",
5556
"/nscl/common/include.js",
57+
"/nscl/service/DocStartInjection.js",
5658
"/nscl/service/LastListener.js",
5759
"/nscl/service/TabCache.js",
5860
"ui/Prompts.js",
@@ -75,15 +77,6 @@
7577
"/content/content.css"
7678
]
7779
},
78-
{
79-
"run_at": "document_start",
80-
"matches": ["file://*/*", "ftp://*/*"],
81-
"all_frames": true,
82-
"js": [
83-
"lib/DocumentFreezer.js",
84-
"content/syncFetchPolicy.js"
85-
]
86-
},
8780
{
8881
"run_at": "document_start",
8982
"matches": ["<all_urls>"],
@@ -114,13 +107,21 @@
114107
"content/content.js"
115108
]
116109
},
117-
118110
{
119111
"run_at": "document_start",
120112
"matches": ["ftp://*/*"],
121113
"js": [
122114
"content/ftp.js"
123115
]
116+
},
117+
{
118+
"run_at": "document_start",
119+
"matches": ["file://*/*", "ftp://*/*"],
120+
"all_frames": true,
121+
"js": [
122+
"lib/DocumentFreezer.js",
123+
"content/syncFetchPolicy.js"
124+
]
124125
}
125126
],
126127

src/nscl

0 commit comments

Comments
 (0)