2017-11-08 17:46:41 +01:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
(function(){
|
|
|
|
"use strict";
|
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
let scope;
|
2017-11-08 17:46:41 +01:00
|
|
|
if ((typeof exports) !== "undefined"){
|
|
|
|
scope = exports;
|
|
|
|
}
|
|
|
|
else {
|
2019-03-12 22:24:23 +01:00
|
|
|
scope = require.register("./persistentRndStorage", {});
|
2017-11-08 17:46:41 +01:00
|
|
|
}
|
2018-07-28 14:13:14 +02:00
|
|
|
|
|
|
|
|
2017-11-08 17:46:41 +01:00
|
|
|
const settings = require("./settings");
|
|
|
|
const logging = require("./logging");
|
|
|
|
|
|
|
|
scope.persistentRnd = Object.create(null);
|
2018-07-28 14:13:14 +02:00
|
|
|
scope.persistentIncognitoRnd = Object.create(null);
|
2019-11-28 01:26:35 +01:00
|
|
|
let clearTimeout;
|
2017-11-08 17:46:41 +01:00
|
|
|
scope.init = function init(){
|
|
|
|
logging.message("initializing persistent rng storage");
|
|
|
|
|
|
|
|
logging.notice("build persistent storage");
|
|
|
|
|
|
|
|
if (settings.storePersistentRnd){
|
|
|
|
try {
|
|
|
|
let storedData = JSON.parse(settings.persistentRndStorage);
|
2019-11-28 01:26:35 +01:00
|
|
|
for (let domain in storedData){
|
|
|
|
const value = storedData[domain];
|
2017-11-08 17:46:41 +01:00
|
|
|
if (
|
|
|
|
Array.isArray(value) &&
|
|
|
|
value.length === 128 &&
|
|
|
|
value.every(function(value){
|
|
|
|
return typeof value === "number" && value >= 0 && value < 256;
|
|
|
|
})
|
|
|
|
){
|
|
|
|
scope.persistentRnd[domain] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-28 01:26:35 +01:00
|
|
|
catch (error){
|
2017-11-08 17:46:41 +01:00
|
|
|
// JSON is not valid -> ignore it
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
settings.persistentRndStorage = "";
|
|
|
|
settings.lastPersistentRndClearing = Date.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
registerTimeout();
|
|
|
|
|
|
|
|
logging.notice("register settings change event listener");
|
|
|
|
settings.on(["persistentRndClearIntervalValue", "persistentRndClearIntervalUnit"], function(){
|
|
|
|
window.clearTimeout(clearTimeout);
|
|
|
|
registerTimeout();
|
|
|
|
});
|
|
|
|
settings.on("storePersistentRnd", function({newValue}){
|
|
|
|
settings.persistentRndStorage = newValue? JSON.stringify(scope.persistentRnd): "";
|
|
|
|
});
|
2019-06-14 10:36:49 +02:00
|
|
|
|
2019-06-25 08:21:34 +02:00
|
|
|
if (browser.contextualIdentities && browser.contextualIdentities.onRemoved){
|
|
|
|
logging.message("register contextual identities removal");
|
|
|
|
browser.contextualIdentities.onRemoved.addListener(function(details){
|
|
|
|
logging.message("Contextual identity", details.contextualIdentity.cookieStoreId, "removed.");
|
|
|
|
clearContainerData(details.contextualIdentity.cookieStoreId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
logging.error(
|
|
|
|
"Old Firefox does not support browser.contextualIdentities.onRemoved"
|
|
|
|
);
|
|
|
|
}
|
2017-11-08 17:46:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const getInterval = function(){
|
2019-11-28 01:26:35 +01:00
|
|
|
const units = {
|
2017-11-08 17:46:41 +01:00
|
|
|
seconds: 1000,
|
|
|
|
minutes: 60 * 1000,
|
|
|
|
hours: 60 * 60 * 1000,
|
|
|
|
days: 24 * 60 * 60 * 1000,
|
|
|
|
weeks: 7 * 24 * 60 * 60 * 1000,
|
|
|
|
months: 30 * 24 * 60 * 60 * 1000,
|
|
|
|
years: 365 * 24 * 60 * 60 * 1000,
|
|
|
|
};
|
|
|
|
return function getInterval(){
|
|
|
|
return settings.persistentRndClearIntervalValue * units[settings.persistentRndClearIntervalUnit] || 0;
|
|
|
|
};
|
|
|
|
}();
|
2018-07-28 14:13:14 +02:00
|
|
|
|
2017-11-08 17:46:41 +01:00
|
|
|
function registerTimeout(){
|
2019-11-28 01:26:35 +01:00
|
|
|
const interval = getInterval();
|
2017-11-08 17:46:41 +01:00
|
|
|
if (interval > 0){
|
2021-06-12 00:50:05 +02:00
|
|
|
const timeout = settings.lastPersistentRndClearing + interval - Date.now();
|
2017-11-08 17:46:41 +01:00
|
|
|
logging.message("registering persistent rng data clearing timeout. Clearing in ", timeout, "ms");
|
2018-02-16 18:10:18 +01:00
|
|
|
if (timeout > 1073741824){
|
|
|
|
// window.setTimeout can only handle delays up to 32 bit.
|
2019-11-28 01:26:35 +01:00
|
|
|
// Therefore we repeat the registering after 2^30 = 1073741824 seconds
|
2018-02-16 18:10:18 +01:00
|
|
|
clearTimeout = window.setTimeout(registerTimeout, 1073741824);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
clearTimeout = window.setTimeout(clear, timeout);
|
|
|
|
}
|
2017-11-08 17:46:41 +01:00
|
|
|
}
|
|
|
|
}
|
2019-12-28 23:23:55 +01:00
|
|
|
async function broadcast(data){
|
|
|
|
const tabs = await browser.tabs.query({});
|
|
|
|
tabs.forEach(function(tab){
|
|
|
|
browser.tabs.sendMessage(tab.id, data);
|
2017-11-08 17:46:41 +01:00
|
|
|
});
|
|
|
|
}
|
2018-07-28 14:13:14 +02:00
|
|
|
function clearIncognito(){
|
|
|
|
scope.persistentIncognitoRnd = Object.create(null);
|
|
|
|
settings.persistentIncognitoRndStorage = JSON.stringify(scope.persistentIncognitoRnd);
|
|
|
|
}
|
2021-02-21 11:49:29 +01:00
|
|
|
function clear(force = false){
|
2017-11-08 17:46:41 +01:00
|
|
|
logging.verbose("domain rnd cleared");
|
|
|
|
scope.persistentRnd = Object.create(null);
|
|
|
|
settings.persistentRndStorage = JSON.stringify(scope.persistentRnd);
|
|
|
|
settings.lastPersistentRndClearing = Date.now();
|
2018-07-28 14:13:14 +02:00
|
|
|
clearIncognito();
|
2017-11-08 17:46:41 +01:00
|
|
|
registerTimeout();
|
2021-02-21 11:49:29 +01:00
|
|
|
broadcast({"canvasBlocker-clear-domain-rnd": force? "force": true});
|
2017-11-08 17:46:41 +01:00
|
|
|
}
|
2018-07-28 14:13:14 +02:00
|
|
|
function setDomainData(domain, incognito, rnd){
|
|
|
|
logging.verbose("got new domain rnd for ", domain, " (incognito:", incognito, "):", rnd);
|
|
|
|
if (incognito){
|
|
|
|
scope.persistentIncognitoRnd[domain] = rnd;
|
|
|
|
settings.persistentIncognitoRndStorage = JSON.stringify(scope.persistentIncognitoRnd);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
scope.persistentRnd[domain] = rnd;
|
|
|
|
settings.persistentRndStorage = JSON.stringify(scope.persistentRnd);
|
|
|
|
}
|
|
|
|
broadcast({"canvasBlocker-set-domain-rnd": {domain, incognito, rnd}});
|
2017-11-08 17:46:41 +01:00
|
|
|
}
|
2019-06-14 10:36:49 +02:00
|
|
|
function clearDomainData(domain){
|
|
|
|
logging.verbose("clear domain rnd for ", domain);
|
|
|
|
delete scope.persistentIncognitoRnd[domain];
|
|
|
|
settings.persistentIncognitoRndStorage = JSON.stringify(scope.persistentIncognitoRnd);
|
|
|
|
delete scope.persistentRnd[domain];
|
|
|
|
settings.persistentRndStorage = JSON.stringify(scope.persistentRnd);
|
|
|
|
}
|
|
|
|
|
|
|
|
function clearContainerData(cookieStoreId){
|
|
|
|
logging.verbose("clear container rnd for ", cookieStoreId);
|
|
|
|
Object.keys(scope.persistentRnd).forEach(function(domain){
|
|
|
|
if (domain.startsWith(cookieStoreId + "@")){
|
|
|
|
delete scope.persistentRnd[domain];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
settings.persistentRndStorage = JSON.stringify(scope.persistentRnd);
|
|
|
|
|
|
|
|
Object.keys(scope.persistentIncognitoRnd).forEach(function(domain){
|
|
|
|
if (domain.startsWith(cookieStoreId + "@")){
|
|
|
|
delete scope.persistentIncognitoRnd[domain];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
settings.persistentIncognitoRndStorage = JSON.stringify(scope.persistentIncognitoRnd);
|
|
|
|
}
|
|
|
|
|
2017-11-08 17:46:41 +01:00
|
|
|
scope.clear = clear;
|
|
|
|
scope.setDomainData = setDomainData;
|
2019-06-14 10:36:49 +02:00
|
|
|
scope.clearDomainData = clearDomainData;
|
|
|
|
scope.clearContainerData = clearContainerData;
|
2023-05-29 21:39:02 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
browser.windows.onRemoved.addListener(async function(){
|
|
|
|
const windows = await browser.windows.getAll();
|
|
|
|
if (windows.every(function(window){
|
|
|
|
return !window.incognito;
|
|
|
|
})){
|
|
|
|
clearIncognito();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch (error){
|
|
|
|
logging.error("Unable to register windows.onRemoved listener", error);
|
|
|
|
}
|
2017-11-08 17:46:41 +01:00
|
|
|
}());
|