From 9b186317681bc69893ce0c0ace2c8dd2e68b5c9d Mon Sep 17 00:00:00 2001 From: kkapsner Date: Sat, 28 Jul 2018 14:13:14 +0200 Subject: [PATCH] separate persistent random numbers for incognito windows --- lib/frame.js | 12 +++++++----- lib/main.js | 4 +++- lib/persistentRndStorage.js | 36 +++++++++++++++++++++++++++++------- lib/randomSupplies.js | 18 ++++++++++++++---- lib/settingDefinitions.js | 5 +++++ releaseNotes.txt | 4 +++- 6 files changed, 61 insertions(+), 18 deletions(-) diff --git a/lib/frame.js b/lib/frame.js index a097fbd..56390e3 100644 --- a/lib/frame.js +++ b/lib/frame.js @@ -56,12 +56,14 @@ notice("my tab id is", data.tabId); tabId = data.tabId; } - if (data.hasOwnProperty("persistentRnd")){ - notice("got persistent random data", data.persistentRnd); + const persistentRndName = "persistent" + (browser.extension.inIncognitoContext? "Incognito": "") + "Rnd"; + if (data.hasOwnProperty(persistentRndName)){ + const persistentRndValue = data[persistentRndName]; + notice("got persistent random data", persistentRndValue); const {persistent: persistentRnd} = require("./randomSupplies.js"); - Object.keys(data.persistentRnd).forEach(function(domain){ - verbose("random data for", domain, data.persistentRnd[domain]); - persistentRnd.setDomainRnd(domain, data.persistentRnd[domain]); + Object.keys(persistentRndValue).forEach(function(domain){ + verbose("random data for", domain, persistentRndValue[domain]); + persistentRnd.setDomainRnd(domain, persistentRndValue[domain]); }); } }); diff --git a/lib/main.js b/lib/main.js index 56de16d..69f9ce1 100644 --- a/lib/main.js +++ b/lib/main.js @@ -28,6 +28,7 @@ if (data["canvasBlocker-new-domain-rnd"]){ persistentRndStorage.setDomainData( data["canvasBlocker-new-domain-rnd"].domain, + data["canvasBlocker-new-domain-rnd"].incognito, data["canvasBlocker-new-domain-rnd"].rnd ); if (keys.length === 1){ @@ -55,7 +56,8 @@ verbose("send back the persistent random seeds", persistentRndStorage.persistentRnd); port.postMessage({ tabId: port.sender.tab.id, - persistentRnd: persistentRndStorage.persistentRnd + persistentRnd: persistentRndStorage.persistentRnd, + persistentIncognitoRnd: persistentRndStorage.persistentIncognitoRnd }); var url = new URL(port.sender.url); port.onMessage.addListener(function(data){ diff --git a/lib/persistentRndStorage.js b/lib/persistentRndStorage.js index ba1c8af..da56099 100644 --- a/lib/persistentRndStorage.js +++ b/lib/persistentRndStorage.js @@ -12,12 +12,13 @@ scope = {}; window.scope.persistentRndStorage = scope; } - - + + const settings = require("./settings"); const logging = require("./logging"); scope.persistentRnd = Object.create(null); + scope.persistentIncognitoRnd = Object.create(null); scope.init = function init(){ logging.message("initializing persistent rng storage"); @@ -74,6 +75,16 @@ return settings.persistentRndClearIntervalValue * units[settings.persistentRndClearIntervalUnit] || 0; }; }(); + + browser.windows.onRemoved.addListener(function(){ + browser.windows.getAll().then(function(windows){ + if (windows.every(function(window){ + return !window.incognito; + })){ + clearIncognito(); + } + }); + }); let clearTimeout; function registerTimeout(){ @@ -98,19 +109,30 @@ }); }); } + function clearIncognito(){ + scope.persistentIncognitoRnd = Object.create(null); + settings.persistentIncognitoRndStorage = JSON.stringify(scope.persistentIncognitoRnd); + } function clear(){ logging.verbose("domain rnd cleared"); scope.persistentRnd = Object.create(null); settings.persistentRndStorage = JSON.stringify(scope.persistentRnd); settings.lastPersistentRndClearing = Date.now(); + clearIncognito(); registerTimeout(); broadcast({"canvasBlocker-clear-domain-rnd": true}); } - function setDomainData(domain, rnd){ - logging.verbose("got new domain rnd for ", domain, ":", rnd); - scope.persistentRnd[domain] = rnd; - settings.persistentRndStorage = JSON.stringify(scope.persistentRnd); - broadcast({"canvasBlocker-set-domain-rnd": {domain, rnd}}); + 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}}); } scope.clear = clear; diff --git a/lib/randomSupplies.js b/lib/randomSupplies.js index 1d664dc..f581054 100644 --- a/lib/randomSupplies.js +++ b/lib/randomSupplies.js @@ -80,7 +80,11 @@ var persistentRnd = Object.create(null); settings.onloaded(function(){ try { - let storedData = JSON.parse(settings.persistentRndStorage); + let storedData = JSON.parse( + browser.extension.inIncognitoContext? + settings.persistentIncognitoRndStorage: + settings.persistentRndStorage + ); for (var domain in storedData){ var value = storedData[domain]; if ( @@ -102,8 +106,10 @@ browser.runtime.onMessage.addListener(function(data){ if (data["canvasBlocker-set-domain-rnd"]){ - var {domain, rnd} = data["canvasBlocker-set-domain-rnd"]; - persistentRnd[domain] = new Uint8Array(rnd); + var {domain, incognito, rnd} = data["canvasBlocker-set-domain-rnd"]; + if (incognito === browser.extension.inIncognitoContext){ + persistentRnd[domain] = new Uint8Array(rnd); + } } if (data["canvasBlocker-clear-domain-rnd"]){ persistentRnd = Object.create(null); @@ -117,7 +123,11 @@ persistentRnd[domain] = new Uint8Array(128); window.crypto.getRandomValues(persistentRnd[domain]); browser.runtime.sendMessage({ - "canvasBlocker-new-domain-rnd": {domain, rnd: Array.from(persistentRnd[domain])} + "canvasBlocker-new-domain-rnd": { + domain, + incognito: browser.extension.inIncognitoContext, + rnd: Array.from(persistentRnd[domain]) + } }); } return persistentRnd[domain]; diff --git a/lib/settingDefinitions.js b/lib/settingDefinitions.js index 5b690c2..4b2c093 100644 --- a/lib/settingDefinitions.js +++ b/lib/settingDefinitions.js @@ -113,6 +113,11 @@ name: "persistentRndStorage", defaultValue: "" }, + { + name: "persistentIncognitoRndStorage", + resetOnStartup: true, + defaultValue: "" + }, { name: "storePersistentRnd", defaultValue: false diff --git a/releaseNotes.txt b/releaseNotes.txt index 077bee2..4abd1d8 100644 --- a/releaseNotes.txt +++ b/releaseNotes.txt @@ -3,11 +3,13 @@ Version 0.5.2: - new features: - - + - separate persistent random numbers for incognito windows + (resets when closing all incognito windows - like cookies do) fixes: - optimized CSP - in Firefox ESR (52) iFrames with a blob-URL cannot be protected -> they have to be blocked there as well + - broken when using with Random Agent Spoofer known issues: - if a data URL is blocked the page action button does not appear