1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-11-09 22:58:52 +01:00
CanvasBlocker/lib/frame.js

230 lines
6.8 KiB
JavaScript
Raw Normal View History

/* 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){
"use strict";
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");
const extension = require("./extension");
2019-05-22 23:37:23 +02:00
const iframeProtection = require("./iframeProtection");
const logging = require("./logging");
2019-11-28 01:26:35 +01:00
logging.setPrefix("frame script");
// Variable to "unload" the script
2019-11-28 01:26:35 +01:00
let enabled = true;
2019-11-28 01:26:35 +01:00
logging.message("starting", location.href);
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, {
_: extension.getTranslation,
prefs
});
}
2017-07-16 00:25:19 +02:00
2019-05-17 00:36:34 +02:00
let extensionSecret;
function computeExtensionSecret(){
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();
if (window === window.top){
2019-11-28 01:26:35 +01:00
logging.message("Is top level window -> tab had navigation -> clear page action");
port.postMessage({"canvasBlocker-clear-page-action": true});
}
2019-11-28 01:26:35 +01:00
let tabId;
port.onMessage.addListener(function(data){
2019-11-28 01:26:35 +01:00
logging.message("Got data from port", data);
if (data.hasOwnProperty("tabId")){
2019-11-28 01:26:35 +01:00
logging.notice("my tab id is", data.tabId);
tabId = data.tabId;
}
if (data.hasOwnProperty("cookieStoreId")){
2019-11-28 01:26:35 +01:00
logging.notice("my tab cookie store id is", data.cookieStoreId);
const {persistent: persistentRnd} = require("./randomSupplies");
persistentRnd.setCookieStoreId(data.cookieStoreId);
const modifiedNavigatorAPI = require("./modifiedNavigatorAPI");
modifiedNavigatorAPI.setCookieStoreId(data.cookieStoreId);
}
const persistentRndName = "persistent" + (extension.inIncognitoContext? "Incognito": "") + "Rnd";
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");
Object.keys(persistentRndValue).forEach(function(domain){
2019-11-28 01:26:35 +01:00
logging.verbose("random data for", domain, persistentRndValue[domain]);
persistentRnd.setDomainRnd(domain, persistentRndValue[domain]);
});
}
});
2019-11-28 01:26:35 +01:00
const notifications = [];
const notificationCounter = {};
const sentAPIs = {};
function notify(data){
if (!settings.ignoredAPIs[data.api]){
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;
if (!sentAPIs[data.api]){
sentAPIs[data.api] = true;
port.postMessage({"canvasBlocker-notify": data});
}
}
}
function prefs(...args){
return settings.get(...args);
}
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;
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);
}
2019-11-28 01:26:35 +01:00
catch (error){
// 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);
return false;
}
const wrappedWindow = wrappedTry;
2019-05-17 00:36:34 +02:00
if (!enabled || interceptedWindows.get(wrappedWindow)){
return false;
}
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;
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");
extension.message.on(function(data){
2017-06-25 22:33:12 +02:00
if (data["canvasBlocker-unload"]){
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;
}
if (
data.hasOwnProperty("canvasBlocker-sendNotifications") &&
data["canvasBlocker-sendNotifications"] === tabId
){
2019-11-28 01:26:35 +01:00
logging.notice("sending notifications:", notifications);
extension.message.send({
sender: tabId,
url: window.location.href,
"canvasBlocker-notificationCounter": notificationCounter,
"canvasBlocker-notifications": notifications
});
2019-11-28 01:26:35 +01:00
logging.notice("notifications sent");
}
});
interceptWindow(window);
2019-03-12 22:24:23 +01:00
}(require));