Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions common/ContextStore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* NoScript Commons Library
* Reusable building blocks for cross-browser security/privacy WebExtensions.
* Copyright (C) 2020-2021 Giorgio Maone <https://maone.net>
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <https://www.gnu.org/licenses/>.
*/

var ContextStore = (() => {
'use strict';

class ContextStore {

constructor(contextStoreData) {
this.enabled = Boolean(contextStoreData && contextStoreData.enabled);
this.policies = ({});
if (contextStoreData && contextStoreData.policies) {
for (const [cookieStoreId, policy] of Object.entries(contextStoreData.policies)) {
this.policies[cookieStoreId] = new Policy(policy);
}
}
}

static hydrate(dry, contextObj) {
let newPolicies = new Object();
for (const [cookieStoreId, policy] of Object.entries(dry.policies)) {
newPolicies[cookieStoreId] = new Policy(contextObj.policies[cookieStoreId]);
}
var newContextStore = ({
enabled: dry.enabled,
policies: newPolicies,
});
newContextStore = contextObj ? Object.assign(contextObj, newContextStore)
: new ContextStore(newContextStore);
return newContextStore;
}

dry(includeTemp = false) {
var policies = Object.assign({}, this.policies);
for (const [cookieStoreId, policy] of Object.entries(policies)) {
policies[cookieStoreId] = policy.dry(includeTemp);
}
return ({
enabled: this.enabled,
policies,
});
}

revokeTemp() {
ContextStore.hydrate(this.dry(), this);
return this;
}

setAll(params = {}) {
for (const [key, value] of Object.entries(params)) {
for (const [cookieStoreId, policy] of Object.entries(this.policies)) {
policy[key] = value;
}
}
}

get snapshot() {
return JSON.stringify(this.dry(true));
}

equals(other) {
this.snapshot === other.snapshot;
}

updatePresets(policy) {
Object.entries(this.policies).forEach(([cookieStoreId, containerPolicy]) => {
containerPolicy.DEFAULT.capabilities = new Set(policy.DEFAULT.capabilities);
containerPolicy.TRUSTED.capabilities = new Set(policy.TRUSTED.capabilities);
containerPolicy.UNTRUSTED.capabilities = new Set(policy.UNTRUSTED.capabilities);
});
}

async updateContainers(defaultPolicy = null) {
var identities = this.enabled && browser.contextualIdentities && await browser.contextualIdentities.query({});
if (!identities) return;
identities.forEach(({cookieStoreId}) => {
if (!this.policies.hasOwnProperty(cookieStoreId)) {
if (!defaultPolicy) {
defaultPolicy = new Policy().dry();
} else if (typeof defaultPolicy.dry == 'function') {
defaultPolicy = defaultPolicy.dry();
}
this.policies[cookieStoreId] = new Policy(defaultPolicy);
}
})
}
}

return ContextStore;
})();
4 changes: 2 additions & 2 deletions service/DocStartInjection.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ var DocStartInjection = (() => {

async function begin(request) {
let scripts = new Set();
let {tabId, frameId, url, type, documentId, documentLifecycle, frameType} = request;
let {tabId, frameId, cookieStoreId, url, type, documentId, documentLifecycle, frameType} = request;

if (tabId < 0 || !/^(?:(?:https?|ftp|data|blob|file):|about:blank$)/.test(url)) return;

Expand All @@ -59,7 +59,7 @@ var DocStartInjection = (() => {
await Promise.allSettled([...scriptBuilders].map(async buildScript => {
let script;
try {
script = await buildScript({tabId, frameId, url, type});
script = await buildScript({tabId, frameId, cookieStoreId, url, type});
if (!script) return;
if (mv3Callbacks) {
if (typeof script !== "object") {
Expand Down