mirror of
https://github.com/kkapsner/CanvasBlocker
synced 2025-07-04 20:46:39 +02:00
Added navigator protection
This commit is contained in:
parent
479ee74903
commit
e56df7160f
23 changed files with 711 additions and 1 deletions
|
@ -140,6 +140,9 @@
|
|||
message("Initialize data-URL workaround.");
|
||||
require("./dataUrls").init();
|
||||
|
||||
message("Initialize navigator HTTP header protection.");
|
||||
require("./navigator").init();
|
||||
|
||||
browser.runtime.onInstalled.addListener(function(details){
|
||||
function openOptions(reason){
|
||||
if (
|
||||
|
|
|
@ -540,4 +540,5 @@
|
|||
appendModified(require("./modifiedHistoryAPI"));
|
||||
appendModified(require("./modifiedWindowAPI"));
|
||||
appendModified(modifiedDOMRectAPI);
|
||||
appendModified(require("./modifiedNavigatorAPI"));
|
||||
}());
|
52
lib/modifiedNavigatorAPI.js
Normal file
52
lib/modifiedNavigatorAPI.js
Normal file
|
@ -0,0 +1,52 @@
|
|||
/* 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";
|
||||
|
||||
var scope;
|
||||
if ((typeof exports) !== "undefined"){
|
||||
scope = exports;
|
||||
}
|
||||
else {
|
||||
scope = require.register("./modifiedNavigatorAPI", {});
|
||||
}
|
||||
|
||||
const {checkerWrapper} = require("./modifiedAPIFunctions");
|
||||
const navigator = require("./navigator");
|
||||
|
||||
scope.changedGetters = navigator.allProperties.map(function(property){
|
||||
return {
|
||||
objectGetters: [function(window){return window.Navigator && window.Navigator.prototype;}],
|
||||
name: property,
|
||||
getterGenerator: function(checker){
|
||||
const temp = {
|
||||
get [property](){
|
||||
return checkerWrapper(checker, this, arguments, function(args, check){
|
||||
const {prefs, notify, window, original} = check;
|
||||
const originalValue = original.apply(this, window.Array.from(args));
|
||||
const returnValue = navigator.getNavigatorValue(property);
|
||||
if (originalValue !== returnValue){
|
||||
notify("fakedNavigatorReadout");
|
||||
}
|
||||
return returnValue;
|
||||
});
|
||||
}
|
||||
};
|
||||
return Object.getOwnPropertyDescriptor(temp, property).get;
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
function getStatus(obj, status, prefs){
|
||||
status = Object.create(status);
|
||||
status.active = prefs("protectNavigator", status.url);
|
||||
return status;
|
||||
}
|
||||
|
||||
scope.changedGetters.forEach(function(changedGetter){
|
||||
changedGetter.type = "readout";
|
||||
changedGetter.getStatus = getStatus;
|
||||
changedGetter.api = "navigator";
|
||||
});
|
||||
}());
|
116
lib/navigator.js
Normal file
116
lib/navigator.js
Normal file
|
@ -0,0 +1,116 @@
|
|||
/* 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";
|
||||
|
||||
var scope;
|
||||
if ((typeof exports) !== "undefined"){
|
||||
scope = exports;
|
||||
}
|
||||
else {
|
||||
scope = require.register("./navigator", {});
|
||||
}
|
||||
|
||||
const settings = require("./settings");
|
||||
const logging = require("./logging");
|
||||
|
||||
scope.allProperties = [
|
||||
"appCodeName", "appName",
|
||||
"appVersion", "buildID", "oscpu", "platform",
|
||||
"product",
|
||||
"productSub", "userAgent", "vendor", "vendorSub"];
|
||||
const original = {};
|
||||
scope.allProperties.forEach(function(property){
|
||||
original[property] = window.navigator[property];
|
||||
});
|
||||
original["real Firefox version"] = window.navigator.userAgent.replace(/^.+Firefox\//, "");
|
||||
|
||||
let changedValues = {};
|
||||
|
||||
settings.onloaded(function(){
|
||||
changedValues = settings.navigatorDetails;
|
||||
});
|
||||
settings.on("navigatorDetails", function({newValue}){
|
||||
changedValues = newValue;
|
||||
});
|
||||
|
||||
|
||||
function getValue(name, stack = []){
|
||||
if (stack.indexOf(name) !== -1){
|
||||
return "[ERROR: loop in property definition]";
|
||||
}
|
||||
stack.push(name);
|
||||
|
||||
switch (name){
|
||||
case "original value":
|
||||
return original[stack[stack.length - 2]];
|
||||
case "random":
|
||||
return String.fromCharCode(Math.floor(65 + 85 * Math.random()));
|
||||
default:
|
||||
if (changedValues.hasOwnProperty(name)){
|
||||
return parseString(changedValues[name], stack.slice());
|
||||
}
|
||||
else {
|
||||
return original[name];
|
||||
}
|
||||
}
|
||||
}
|
||||
function parseString(string, stack){
|
||||
return string.replace(/{([a-z[\]_. -]*)}/ig, function(m, name){
|
||||
return getValue(name, stack.slice());
|
||||
});
|
||||
}
|
||||
|
||||
scope.getNavigatorValue = function getNavigatorValue(name){
|
||||
return getValue(name);
|
||||
};
|
||||
|
||||
function changeHTTPHeader(details){
|
||||
if (
|
||||
settings.protectNavigator &&
|
||||
(
|
||||
!settings.protectedAPIFeatures.hasOwnProperty("userAgent") ||
|
||||
settings.protectedAPIFeatures.userAgent
|
||||
)
|
||||
){
|
||||
for (var header of details.requestHeaders){
|
||||
if (header.name.toLowerCase() === "user-agent"){
|
||||
header.value = getValue("userAgent");
|
||||
}
|
||||
}
|
||||
}
|
||||
return details;
|
||||
}
|
||||
|
||||
scope.registerHeaderChange = function(){
|
||||
logging.message("Register HTTP header modification for navigator protection.");
|
||||
if (!browser.webRequest.onBeforeSendHeaders.hasListener(changeHTTPHeader)){
|
||||
browser.webRequest.onBeforeSendHeaders.addListener(
|
||||
changeHTTPHeader,
|
||||
{
|
||||
urls: ["<all_urls>"],
|
||||
},
|
||||
["blocking", "requestHeaders"]);
|
||||
}
|
||||
};
|
||||
scope.unregisterHeaderChange = function(){
|
||||
logging.message("Removing header modification for navigator protection.");
|
||||
browser.webRequest.onBeforeSendHeaders.removeListener(changeHTTPHeader);
|
||||
};
|
||||
scope.init = function (){
|
||||
settings.onloaded(function(){
|
||||
if (!settings.protectNavigator){
|
||||
scope.unregisterHeaderChange();
|
||||
}
|
||||
});
|
||||
settings.on("protectNavigator", function({newValue}){
|
||||
if (newValue){
|
||||
scope.registerHeaderChange();
|
||||
}
|
||||
else {
|
||||
scope.unregisterHeaderChange();
|
||||
}
|
||||
});
|
||||
};
|
||||
}());
|
|
@ -117,6 +117,18 @@
|
|||
"intersectionRect",
|
||||
"boundingClientRect",
|
||||
"rootBounds",
|
||||
{name: "Navigator-API", level: 1},
|
||||
"appCodeName",
|
||||
"appName",
|
||||
"appVersion",
|
||||
"buildID",
|
||||
"oscpu",
|
||||
"platform",
|
||||
"product",
|
||||
"productSub",
|
||||
"userAgent",
|
||||
"vendor",
|
||||
"vendorSub"
|
||||
],
|
||||
defaultKeyValue: true
|
||||
},
|
||||
|
@ -222,6 +234,7 @@
|
|||
"history",
|
||||
"window",
|
||||
"domRect",
|
||||
"navigator",
|
||||
],
|
||||
defaultKeyValue: false
|
||||
},
|
||||
|
@ -293,6 +306,14 @@
|
|||
name: "blockDataURLs",
|
||||
defaultValue: true
|
||||
},
|
||||
{
|
||||
name: "protectNavigator",
|
||||
defaultValue: false
|
||||
},
|
||||
{
|
||||
name: "navigatorDetails",
|
||||
defaultValue: {},
|
||||
},
|
||||
{
|
||||
name: "displayAdvancedSettings",
|
||||
defaultValue: false
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue