2017-01-31 21:31:55 +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/. */
|
2019-03-12 22:24:23 +01:00
|
|
|
(function(require){
|
2017-01-31 21:31:55 +01:00
|
|
|
"use strict";
|
2017-07-07 08:49:12 +02:00
|
|
|
|
2017-11-07 00:36:44 +01:00
|
|
|
const settings = require("./settings");
|
2019-04-08 00:06:25 +02:00
|
|
|
const {preIntercept: intercept} = require("./intercept");
|
|
|
|
const {ask} = require("./askForPermission");
|
2019-05-17 00:36:34 +02:00
|
|
|
const {sha256String: hashing} = require("./hash");
|
2019-04-08 00:06:25 +02:00
|
|
|
const {check: originalCheck, checkStack: originalCheckStack} = require("./check");
|
2019-03-14 16:51:20 +01:00
|
|
|
const extension = require("./extension");
|
2019-05-22 23:37:23 +02:00
|
|
|
const iframeProtection = require("./iframeProtection");
|
2017-01-31 21:31:55 +01:00
|
|
|
|
2017-09-23 23:37:46 +02:00
|
|
|
const logging = require("./logging");
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.setPrefix("frame script");
|
2017-07-27 19:14:04 +02:00
|
|
|
|
2017-01-31 21:31:55 +01:00
|
|
|
// Variable to "unload" the script
|
2019-11-28 01:26:35 +01:00
|
|
|
let enabled = true;
|
2017-01-31 21:31:55 +01:00
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.message("starting", location.href);
|
2017-01-31 21:31:55 +01:00
|
|
|
|
|
|
|
function check(message){
|
|
|
|
if (enabled){
|
|
|
|
return originalCheck(message);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return {type: [], mode: "allow"};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function checkStack(stack){
|
|
|
|
if (enabled){
|
|
|
|
return originalCheckStack(stack);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function askWrapper(data){
|
|
|
|
return ask(data, {
|
2019-03-14 16:51:20 +01:00
|
|
|
_: extension.getTranslation,
|
2017-01-31 21:31:55 +01:00
|
|
|
prefs
|
|
|
|
});
|
|
|
|
}
|
2017-07-16 00:25:19 +02:00
|
|
|
|
2019-05-17 00:36:34 +02:00
|
|
|
let extensionSecret;
|
|
|
|
function computeExtensionSecret(){
|
2020-01-23 15:36:32 +01:00
|
|
|
function hashString(string){
|
|
|
|
return hashing(new Uint16Array(
|
|
|
|
string.split("").map(function(c){
|
|
|
|
return c.charCodeAt(0);
|
|
|
|
})
|
|
|
|
));
|
|
|
|
}
|
2019-05-17 00:36:34 +02:00
|
|
|
const now = new Date();
|
|
|
|
const lastTenMinutes = Math.floor(now.getMinutes() / 10) * 10;
|
|
|
|
const nextRun = new Date(
|
|
|
|
now.getFullYear(), now.getMonth(), now.getDate(),
|
|
|
|
now.getHours(), lastTenMinutes + 10, 0, 0
|
|
|
|
);
|
|
|
|
window.setTimeout(
|
|
|
|
computeExtensionSecret,
|
|
|
|
nextRun .getTime() - now.getTime()
|
|
|
|
);
|
|
|
|
|
|
|
|
let string =
|
|
|
|
extension.extensionID +
|
|
|
|
`${now.getFullYear()}-${now.getMonth() + 1}-${now.getDate()} ${now.getHours()}:${lastTenMinutes}`;
|
|
|
|
extensionSecret = [hashString("input" + string), hashString(string + "output")];
|
|
|
|
}
|
|
|
|
computeExtensionSecret();
|
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.message("open port to background script");
|
|
|
|
const port = browser.runtime.connect();
|
2017-11-14 23:57:44 +01:00
|
|
|
if (window === window.top){
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.message("Is top level window -> tab had navigation -> clear page action");
|
2017-11-14 23:57:44 +01:00
|
|
|
port.postMessage({"canvasBlocker-clear-page-action": true});
|
|
|
|
}
|
2019-11-28 01:26:35 +01:00
|
|
|
let tabId;
|
2017-06-29 07:21:36 +02:00
|
|
|
port.onMessage.addListener(function(data){
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.message("Got data from port", data);
|
2017-06-29 07:21:36 +02:00
|
|
|
if (data.hasOwnProperty("tabId")){
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.notice("my tab id is", data.tabId);
|
2017-06-29 07:21:36 +02:00
|
|
|
tabId = data.tabId;
|
|
|
|
}
|
2019-05-29 00:37:33 +02:00
|
|
|
if (data.hasOwnProperty("cookieStoreId")){
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.notice("my tab cookie store id is", data.cookieStoreId);
|
2019-05-29 00:37:33 +02:00
|
|
|
const {persistent: persistentRnd} = require("./randomSupplies");
|
|
|
|
persistentRnd.setCookieStoreId(data.cookieStoreId);
|
2020-01-25 01:03:05 +01:00
|
|
|
const modifiedNavigatorAPI = require("./modifiedNavigatorAPI");
|
|
|
|
modifiedNavigatorAPI.setCookieStoreId(data.cookieStoreId);
|
2019-05-29 00:37:33 +02:00
|
|
|
}
|
2019-03-14 16:51:20 +01:00
|
|
|
const persistentRndName = "persistent" + (extension.inIncognitoContext? "Incognito": "") + "Rnd";
|
2018-07-28 14:13:14 +02:00
|
|
|
if (data.hasOwnProperty(persistentRndName)){
|
|
|
|
const persistentRndValue = data[persistentRndName];
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.notice("got persistent random data", persistentRndValue);
|
2019-04-08 00:06:25 +02:00
|
|
|
const {persistent: persistentRnd} = require("./randomSupplies");
|
2018-07-28 14:13:14 +02:00
|
|
|
Object.keys(persistentRndValue).forEach(function(domain){
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.verbose("random data for", domain, persistentRndValue[domain]);
|
2018-07-28 14:13:14 +02:00
|
|
|
persistentRnd.setDomainRnd(domain, persistentRndValue[domain]);
|
2017-09-23 23:37:46 +02:00
|
|
|
});
|
|
|
|
}
|
2017-06-29 07:21:36 +02:00
|
|
|
});
|
2019-11-28 01:26:35 +01:00
|
|
|
const notifications = [];
|
|
|
|
const notificationCounter = {};
|
|
|
|
const sentAPIs = {};
|
2017-01-31 21:31:55 +01:00
|
|
|
function notify(data){
|
2018-09-09 00:15:21 +02:00
|
|
|
if (!settings.ignoredAPIs[data.api]){
|
2018-09-11 23:54:59 +02:00
|
|
|
if (settings.storeNotificationData){
|
|
|
|
notifications.push(data);
|
|
|
|
}
|
2019-04-30 23:42:21 +02:00
|
|
|
if (!notificationCounter[data.messageId]){
|
|
|
|
notificationCounter[data.messageId] = {
|
|
|
|
count: 0,
|
|
|
|
api: data.api
|
|
|
|
};
|
|
|
|
}
|
|
|
|
notificationCounter[data.messageId].count += 1;
|
2018-09-16 12:15:04 +02:00
|
|
|
if (!sentAPIs[data.api]){
|
|
|
|
sentAPIs[data.api] = true;
|
|
|
|
port.postMessage({"canvasBlocker-notify": data});
|
|
|
|
}
|
2018-09-09 00:15:21 +02:00
|
|
|
}
|
2017-01-31 21:31:55 +01:00
|
|
|
}
|
|
|
|
|
2017-12-03 23:47:49 +01:00
|
|
|
function prefs(...args){
|
|
|
|
return settings.get(...args);
|
2017-01-31 21:31:55 +01:00
|
|
|
}
|
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
const interceptedWindows = new WeakMap();
|
2017-06-25 22:33:12 +02:00
|
|
|
function interceptWindow(window){
|
2019-11-28 01:26:35 +01:00
|
|
|
let wrappedTry;
|
2017-07-18 16:14:03 +02:00
|
|
|
try {
|
2019-11-28 01:26:35 +01:00
|
|
|
const href = window.location.href;
|
2019-12-12 00:09:53 +01:00
|
|
|
wrappedTry = extension.getWrapped(window);
|
2017-07-18 16:14:03 +02:00
|
|
|
}
|
2019-11-28 01:26:35 +01:00
|
|
|
catch (error){
|
2017-07-18 16:14:03 +02:00
|
|
|
// we are unable to read the location due to SOP
|
|
|
|
// therefore we also can not intercept anything.
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.notice("NOT intercepting window due to SOP", window);
|
2017-07-18 16:14:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
2019-05-24 21:46:34 +02:00
|
|
|
const wrappedWindow = wrappedTry;
|
2017-07-18 16:14:03 +02:00
|
|
|
|
2019-05-17 00:36:34 +02:00
|
|
|
if (!enabled || interceptedWindows.get(wrappedWindow)){
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-23 13:56:14 +01:00
|
|
|
const canvasBlockerData = wrappedWindow.matchMedia(extensionSecret[0]);
|
|
|
|
if (canvasBlockerData.secret === extensionSecret[1]){
|
|
|
|
if (wrappedWindow.top === wrappedWindow){
|
|
|
|
canvasBlockerData.undoIntercept(extension.extensionID);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
interceptedWindows.set(wrappedWindow, true);
|
|
|
|
return false;
|
|
|
|
}
|
2017-10-10 21:11:50 +02:00
|
|
|
}
|
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.message("intercepting window", window);
|
2017-06-25 22:33:12 +02:00
|
|
|
intercept(
|
|
|
|
{subject: window},
|
|
|
|
{check, checkStack, ask: askWrapper, notify, prefs}
|
|
|
|
);
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.message("prepare to intercept (i)frames.");
|
2019-05-22 23:37:23 +02:00
|
|
|
|
|
|
|
function interceptAllFrames(){
|
|
|
|
const currentLength = window.length;
|
|
|
|
for (let i = currentLength; i--;){
|
|
|
|
if (!interceptedWindows.get(wrappedWindow[i])){
|
|
|
|
interceptWindow(window[i]);
|
2017-06-25 22:33:12 +02:00
|
|
|
}
|
2019-05-22 23:37:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
iframeProtection.protect(window, wrappedWindow, interceptWindow, interceptAllFrames);
|
2017-06-25 22:33:12 +02:00
|
|
|
|
2019-05-17 00:36:34 +02:00
|
|
|
const matchMediaDescriptor = Object.getOwnPropertyDescriptor(wrappedWindow, "matchMedia");
|
|
|
|
const originalMatchMedia = matchMediaDescriptor.value;
|
2020-01-23 13:56:14 +01:00
|
|
|
extension.changeProperty(window, "matchMedia", {
|
|
|
|
object: wrappedWindow,
|
|
|
|
name: "matchMedia",
|
|
|
|
type: "value",
|
|
|
|
changed: extension.exportFunctionWithName(function matchMedia(query){
|
|
|
|
if (query === extensionSecret[0]){
|
|
|
|
return {
|
|
|
|
secret: extensionSecret[1],
|
|
|
|
undoIntercept: function(token){
|
|
|
|
if (token === extension.extensionID){
|
|
|
|
extension.revertProperties(window);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return arguments.length > 1?
|
|
|
|
originalMatchMedia.call(this, ...arguments):
|
|
|
|
originalMatchMedia.call(this, query);
|
|
|
|
}
|
|
|
|
}, window, originalMatchMedia.name)
|
|
|
|
});
|
2019-05-17 00:36:34 +02:00
|
|
|
|
|
|
|
interceptedWindows.set(wrappedWindow, true);
|
2017-06-25 22:33:12 +02:00
|
|
|
return true;
|
2017-10-03 15:35:31 +02:00
|
|
|
}
|
2017-06-25 22:33:12 +02:00
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.message("register listener for messages from background script");
|
2019-03-14 16:51:20 +01:00
|
|
|
extension.message.on(function(data){
|
2017-06-25 22:33:12 +02:00
|
|
|
if (data["canvasBlocker-unload"]){
|
2021-01-26 13:34:51 +01:00
|
|
|
extension.revertProperties(window);
|
|
|
|
for (let frameIndex = 0; frameIndex < window.length; frameIndex += 1){
|
|
|
|
extension.revertProperties(window[frameIndex]);
|
|
|
|
}
|
2017-06-25 22:33:12 +02:00
|
|
|
enabled = false;
|
|
|
|
}
|
2017-10-06 16:06:31 +02:00
|
|
|
if (
|
|
|
|
data.hasOwnProperty("canvasBlocker-sendNotifications") &&
|
|
|
|
data["canvasBlocker-sendNotifications"] === tabId
|
|
|
|
){
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.notice("sending notifications:", notifications);
|
2019-03-14 16:51:20 +01:00
|
|
|
extension.message.send({
|
2017-06-29 07:21:36 +02:00
|
|
|
sender: tabId,
|
2018-09-11 23:54:59 +02:00
|
|
|
url: window.location.href,
|
|
|
|
"canvasBlocker-notificationCounter": notificationCounter,
|
2017-06-29 07:21:36 +02:00
|
|
|
"canvasBlocker-notifications": notifications
|
|
|
|
});
|
2019-11-28 01:26:35 +01:00
|
|
|
logging.notice("notifications sent");
|
2017-06-29 07:21:36 +02:00
|
|
|
}
|
2017-01-31 21:31:55 +01:00
|
|
|
});
|
2017-11-14 01:04:20 +01:00
|
|
|
|
2017-12-04 00:26:26 +01:00
|
|
|
interceptWindow(window);
|
2019-03-12 22:24:23 +01:00
|
|
|
}(require));
|