Skip to content

Commit 08bd914

Browse files
committed
Initial support for container tabs
1 parent 4cfdebd commit 08bd914

File tree

14 files changed

+258
-50
lines changed

14 files changed

+258
-50
lines changed

.gitmodules

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
[submodule "nscl"]
22
path = src/nscl
33
url = ../nscl.git
4+
branch = container-tabs

src/bg/LifeCycle.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,17 @@ var LifeCycle = (() => {
269269
if (changed) {
270270
await ns.savePolicy();
271271
}
272+
if (ns.contextStore) {
273+
changed = false;
274+
for (let k of Object.keys(ns.contextStore.policies)){
275+
for (let p of ns.contextStore.policies[k].getPresets(presetNames)) {
276+
if (callback(p)) changed = true;
277+
}
278+
}
279+
if (changed) {
280+
await ns.saveContextStore();
281+
}
282+
}
272283
};
273284

274285
let configureNewCap = async (cap, presetNames, capsFilter) => {

src/bg/RequestGuard.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,6 @@ var RequestGuard = (() => {
399399
normalizeRequest(request);
400400
try {
401401
let redirected = initPendingRequest(request);
402-
let {policy} = ns
403402
let {type} = request;
404403
if (type in policyTypesMap) {
405404
let previous = recent.find(request);
@@ -411,8 +410,9 @@ var RequestGuard = (() => {
411410
recent.add(previous);
412411

413412
let policyType = policyTypesMap[type];
414-
let {url, originUrl, documentUrl, tabId} = request;
413+
let {url, originUrl, documentUrl, tabId, cookieStoreId} = request;
415414
let isFetch = "fetch" === policyType;
415+
let policy = ns.getPolicy(cookieStoreId);
416416

417417
if ((isFetch || "frame" === policyType) &&
418418
(((isFetch && (!originUrl ||
@@ -515,12 +515,12 @@ var RequestGuard = (() => {
515515
let headersModified = false;
516516

517517
pending.headersProcessed = true;
518-
let {url, documentUrl, tabId, responseHeaders, type} = request;
518+
let {url, documentUrl, tabId, cookieStoreId, responseHeaders, type} = request;
519519
let isMainFrame = type === "main_frame";
520520
try {
521521
let capabilities;
522522
if (ns.isEnforced(tabId)) {
523-
let policy = ns.policy;
523+
let policy = ns.getPolicy(cookieStoreId);
524524
let perms = policy.get(url, documentUrl).perms;
525525
if (isMainFrame) {
526526
if (policy.autoAllowTop && perms === policy.DEFAULT) {
@@ -640,9 +640,11 @@ var RequestGuard = (() => {
640640
return ABORT;
641641
}
642642

643-
function injectPolicyScript(details) {
643+
async function injectPolicyScript(details) {
644+
console.debug("INJECT", details);
644645
let {url, tabId, frameId} = details;
645-
let policy = ns.computeChildPolicy({url}, {tab: {id: tabId}, frameId});
646+
let tab = await browser.tabs.get(tabId);
647+
let policy = ns.computeChildPolicy({url}, {tab, frameId});
646648
policy.navigationURL = url;
647649
let debugStatement = ns.local.debug ? `
648650
let mark = Date.now() + ":" + Math.random();

src/bg/Settings.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ var Settings = {
9898
async update(settings) {
9999
let {
100100
policy,
101+
contextStore,
101102
xssUserChoices,
102103
tabId,
103104
unrestrictedTab,
@@ -144,8 +145,10 @@ var Settings = {
144145
}
145146

146147
if (settings.sync === null) {
148+
debug("resetting options", settings);
147149
// user is resetting options
148150
policy = this.createDefaultDryPolicy();
151+
contextStore = new ContextStore().dry();
149152

150153
// overriden defaults when user manually resets options
151154

@@ -170,6 +173,13 @@ var Settings = {
170173
await ns.savePolicy();
171174
}
172175

176+
if (contextStore) {
177+
let newContextStore = new ContextStore(contextStore);
178+
debug("set contextstore", newContextStore);
179+
ns.contextStore = newContextStore
180+
await ns.saveContextStore();
181+
}
182+
173183
if (typeof unrestrictedTab === "boolean") {
174184
ns.unrestrictedTabs[unrestrictedTab ? "add" : "delete"](tabId);
175185
}
@@ -213,6 +223,7 @@ var Settings = {
213223
export() {
214224
return JSON.stringify({
215225
policy: ns.policy.dry(),
226+
contextStore: ns.contextStore.dry(),
216227
local: ns.local,
217228
sync: ns.sync,
218229
xssUserChoices: XSS.getUserChoices(),

src/bg/main.js

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,24 @@
6363
}
6464
}
6565

66+
if (!ns.contextStore) { // it could have been already retrieved by LifeCycle
67+
let contextStoreData = (await Storage.get("sync", "contextStore")).contextStore;
68+
if (contextStoreData) {
69+
ns.contextStore = new ContextStore(contextStoreData);
70+
await ns.contextStore.updateContainers();
71+
} else {
72+
log("No container data found. Initializing new empty policies.")
73+
identities = await browser.contextualIdentities.query({});
74+
policies = new Object();
75+
identities.forEach(({cookieStoreId}) => {
76+
policies[cookieStoreId] = new Policy();
77+
})
78+
ns.contextStore = new ContextStore(({policies}));
79+
await ns.contextStore.updateContainers();
80+
await ns.saveContextStore();
81+
}
82+
}
83+
6684
let {isTorBrowser} = ns.local;
6785
Sites.onionSecure = isTorBrowser;
6886

@@ -160,10 +178,12 @@
160178
tabId = -1
161179
}) {
162180
let policy = ns.policy.dry(true);
181+
let contextStore = ns.contextStore.dry(true);
163182
let seen = tabId !== -1 ? await ns.collectSeen(tabId) : null;
164183
let xssUserChoices = await XSS.getUserChoices();
165184
await Messages.send("settings", {
166185
policy,
186+
contextStore,
167187
seen,
168188
xssUserChoices,
169189
local: ns.local,
@@ -221,6 +241,7 @@
221241
var ns = {
222242
running: false,
223243
policy: null,
244+
contextStore: null,
224245
local: null,
225246
sync: null,
226247
initializing: null,
@@ -233,9 +254,31 @@
233254
return !this.isEnforced(request.tabId) || this.policy.can(request.url, capability, request.documentURL);
234255
},
235256

257+
getPolicy(cookieStoreId){
258+
// debug("get policy", cookieStoreId, ns.policy, ns.contextStore);
259+
if (ns.contextStore.policies.hasOwnProperty(cookieStoreId)) {
260+
let currentPolicy = ns.contextStore.policies[cookieStoreId];
261+
debug("has cookiestore", cookieStoreId, currentPolicy);
262+
if (currentPolicy) return currentPolicy;
263+
}
264+
debug("default cookiestore", cookieStoreId);
265+
return ns.policy;
266+
},
267+
236268
computeChildPolicy({url, contextUrl}, sender) {
237269
let {tab, frameId} = sender;
238-
let policy = ns.policy;
270+
let cookieStoreId = (tab)? tab.cookieStoreId : "default";
271+
var policy = ns.policy;
272+
debug("computing policy", cookieStoreId, ns.contextStore);
273+
if (
274+
cookieStoreId &&
275+
!cookieStoreId.toLowerCase().includes("default") && //exclude firefox-default
276+
ns.contextStore &&
277+
ns.contextStore.policies.hasOwnProperty(cookieStoreId)
278+
) {
279+
policy = ns.contextStore.policies[cookieStoreId];
280+
}
281+
debug("chose policy", policy);
239282
let {isTorBrowser} = ns.local;
240283
if (!policy) {
241284
console.log("Policy is null, initializing: %o, sending fallback.", ns.initializing);
@@ -314,11 +357,21 @@
314357
await Storage.set("sync", {
315358
policy: this.policy.dry()
316359
});
317-
await browser.webRequest.handlerBehaviorChanged()
360+
await browser.webRequest.handlerBehaviorChanged();
318361
}
319362
return this.policy;
320363
},
321364

365+
async saveContextStore() {
366+
if (this.contextStore) {
367+
await Storage.set("sync", {
368+
contextStore: this.contextStore.dry()
369+
});
370+
await browser.webRequest.handlerBehaviorChanged();
371+
}
372+
return this.contextStore;
373+
},
374+
322375

323376

324377
async save(obj) {

src/manifest.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@
2828
"webNavigation",
2929
"webRequest",
3030
"webRequestBlocking",
31-
"<all_urls>"
31+
"<all_urls>",
32+
"contextualIdentities"
3233
],
3334

3435
"background": {
@@ -50,6 +51,7 @@
5051
"/nscl/common/Sites.js",
5152
"/nscl/common/Permissions.js",
5253
"/nscl/common/Policy.js",
54+
"/nscl/common/ContextStore.js",
5355
"/nscl/common/locale.js",
5456
"/nscl/common/SyntaxChecker.js",
5557
"/nscl/common/Storage.js",

src/nscl

src/ui/options.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,12 @@ fieldset:disabled {
9494
.hide, div.debug {
9595
display: none;
9696
}
97+
#context-store {
98+
display: block;
99+
margin-top: .5em;
100+
min-height: 20em;
101+
width: 90%;
102+
}
97103

98104
body.debug div.debug {
99105
display: initial;
@@ -112,6 +118,14 @@ body.debug div.debug {
112118
font-weight: bold;
113119
}
114120

121+
#context-store-error {
122+
background: red;
123+
color: #ff8;
124+
padding: 0;
125+
margin: 0;
126+
font-weight: bold;
127+
}
128+
115129
input, button {
116130
font-size: 1em;
117131
}

src/ui/options.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ <h3 class="flextabs__tab"><button class="flextabs__toggle">__MSG_SectionGeneral_
6868

6969
<h3 class="flextabs__tab"><button class="flextabs__toggle enforcement_required">__MSG_SectionSitePermissions__</button></h3>
7070
<div class="flextabs__content">
71+
<label for="select-container">Choose a container:</label>
72+
<select name="select-container" id="select-container">
73+
<option value="default">Default</option>
74+
</select>
7175
<section class="sect-sites">
7276
<form id="form-newsite" class="browser-style" >
7377
<label id="newsite-label" for="newsite" accesskey="__MSG_WebAddress_accesskey__">__MSG_WebAddress__</label><input name="newsite" id="newsite" type="text" placeholder="[https://]noscript.net"
@@ -147,6 +151,11 @@ <h3 class="flextabs__tab"><button class="flextabs__toggle">__MSG_SectionAdvanced
147151
<div id="policy-error"></div>
148152
<textarea id="policy" class="browser-style">
149153
</textarea>
154+
<label for="contextStore">ContextStore:</label>
155+
<div id="context-store-error"></div>
156+
<textarea id="context-store" class="browser-style">
157+
</textarea>
158+
</div>
150159
</div>
151160
</section>
152161
</div>

0 commit comments

Comments
 (0)