From 79a9034051e71b47f57a6cb820b64da86732b543 Mon Sep 17 00:00:00 2001 From: kkapsner Date: Fri, 17 May 2019 00:36:34 +0200 Subject: [PATCH] Preventing double interception --- lib/frame.js | 52 +++++++++++++++++++++++++++++++++++++++++++++--- releaseNotes.txt | 1 + 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/lib/frame.js b/lib/frame.js index 1282d25..de73007 100644 --- a/lib/frame.js +++ b/lib/frame.js @@ -7,7 +7,7 @@ const settings = require("./settings"); const {preIntercept: intercept} = require("./intercept"); const {ask} = require("./askForPermission"); - const lists = require("./lists"); + const {sha256String: hashing} = require("./hash"); const {check: originalCheck, checkStack: originalCheckStack} = require("./check"); const {getWrapped} = require("./modifiedAPIFunctions"); const extension = require("./extension"); @@ -44,6 +44,33 @@ }); } + let extensionSecret; + function computeExtensionSecret(){ + function hashString(string){ + return hashing(new Uint16Array( + string.split("").map(function(c){ + return c.charCodeAt(0); + }) + )); + } + 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(); + message("open port to background script"); var port = browser.runtime.connect(); if (window === window.top){ @@ -106,8 +133,13 @@ warning("NOT intercepting window due to SOP", window); return false; } + const wrappedWindow = getWrapped(window); - if (!enabled || interceptedWindows.get(getWrapped(window))){ + if (!enabled || interceptedWindows.get(wrappedWindow)){ + return false; + } + if (wrappedWindow.matchMedia(extensionSecret[0]) === extensionSecret[1]){ + interceptedWindows.set(wrappedWindow, true); return false; } @@ -153,7 +185,21 @@ ); }); - interceptedWindows.set(getWrapped(window), true); + const matchMediaDescriptor = Object.getOwnPropertyDescriptor(wrappedWindow, "matchMedia"); + const originalMatchMedia = matchMediaDescriptor.value; + matchMediaDescriptor.value = exportFunction(function matchMedia(query){ + if (query === extensionSecret[0]){ + return extensionSecret[1]; + } + else { + return arguments.length > 1? + originalMatchMedia.apply(this, wrappedWindow.Array.from(arguments)): + originalMatchMedia.call(this, query); + } + }, window); + Object.defineProperty(wrappedWindow, "matchMedia", matchMediaDescriptor); + + interceptedWindows.set(wrappedWindow, true); return true; } diff --git a/releaseNotes.txt b/releaseNotes.txt index 5e07abd..19f80ed 100644 --- a/releaseNotes.txt +++ b/releaseNotes.txt @@ -23,6 +23,7 @@ Version 0.5.9: - fixed description for "show notifications" - improved DOMRect performance - improved general performance when stack list is disabled + - preventing double interception (increased performance and reduced detectability) known issues: - if a data URL is blocked the page action button does not appear