1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-12-23 05:10:27 +01:00
CanvasBlocker/options/sanitationRules.js

327 lines
10 KiB
JavaScript
Raw Normal View History

2018-10-09 08:14:50 +02: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/. */
(function(){
"use strict";
2019-11-28 01:26:35 +01:00
let scope;
2018-10-09 08:14:50 +02:00
if ((typeof exports) !== "undefined"){
scope = exports;
}
else {
2019-03-12 22:24:23 +01:00
scope = require.register("./sanitationRules", {});
2018-10-09 08:14:50 +02:00
}
2019-04-09 08:29:52 +02:00
const extension = require("../lib/extension");
2019-04-08 00:02:29 +02:00
const settings = require("../lib/settings");
2018-10-09 08:14:50 +02:00
scope.ruleset = [
{
name: "unnecessaryURLValue",
check: function(errorCallback){
const {url: urlContainer} = settings.getContainers();
const containerValue = urlContainer.get();
2019-04-09 08:29:52 +02:00
const errorMessage = extension.getTranslation("sanitation_error.unnecessaryURLValue");
2018-10-09 08:14:50 +02:00
function createErrorMessage(setting, urlValue){
return errorMessage
.replace(/{setting-technical}/g, setting.name)
2019-04-09 08:29:52 +02:00
.replace(/{setting-title}/g, extension.getTranslation(setting.name + "_title"))
2018-10-09 08:14:50 +02:00
.replace(/{url}/g, urlValue.url);
}
containerValue.forEach(function(urlValues){
Object.keys(urlValues).filter(function(key){
return key !== "url";
}).forEach(function(key){
const setting = settings.getDefinition(key);
if (setting && setting.urlSpecific){
const globalValue = setting.get();
if (urlValues[key] === globalValue){
errorCallback({
message: createErrorMessage(setting, urlValues),
severity: "low",
resolutions: [{
2019-04-09 08:29:52 +02:00
label: extension.getTranslation("sanitation_resolution.removeURLValue"),
2018-10-09 08:14:50 +02:00
callback: function(){
setting.reset(urlValues.url);
}
}]
});
}
}
});
});
}
},
{
name: "disabledFeatures",
check: function(errorCallback){
const allErrorMessage = extension.getTranslation("sanitation_error.disabledFeatures");
const someErrorMessage = extension.getTranslation("sanitation_error.disabledSomeFeatures");
function createErrorMessage(api, all = false){
return (all? allErrorMessage: someErrorMessage).replace(
/{api}/g,
extension.getTranslation("section_" + api.section));
2018-10-09 08:14:50 +02:00
}
const protectedFeatures = settings.getDefinition("protectedAPIFeatures");
const protectedFeaturesValue = protectedFeatures.get();
function getSectionKeys(section){
let inSection = false;
return protectedFeatures.keys.filter(function(key){
if (typeof key === "string"){
return inSection;
}
else {
if (key.level === 1){
inSection = key.name === section;
}
return false;
}
});
}
[
{mainFlag: "protectedCanvasPart", mainFlagDisabledValue: "nothing", section: "Canvas-API"},
2018-10-09 08:14:50 +02:00
{mainFlag: "protectAudio", section: "Audio-API"},
{mainFlag: "protectWindow", section: "Window-API"},
{mainFlag: "protectDOMRect", section: "DOMRect-API"},
{mainFlag: "protectTextMetrics", section: "TextMetrics-API"},
{mainFlag: "protectNavigator", section: "Navigator-API"},
{mainFlag: "protectScreen", section: "Screen-API"},
2018-10-09 08:14:50 +02:00
].forEach(function(api){
if (settings.get(api.mainFlag) !== (api.mainFlagDisabledValue || false)){
const sectionKeys = getSectionKeys(api.section);
if (sectionKeys.some(function(key){
2018-10-09 08:14:50 +02:00
return protectedFeaturesValue.hasOwnProperty(key) &&
!protectedFeaturesValue[key];
})){
const all = sectionKeys.every(function(key){
return protectedFeaturesValue.hasOwnProperty(key) &&
!protectedFeaturesValue[key];
});
const resolutions = [
{
label: extension.getTranslation("sanitation_resolution.enableFeatures"),
callback: function(){
const protectedFeaturesValue = protectedFeatures.get();
sectionKeys.forEach(function(key){
protectedFeaturesValue[key] = true;
});
protectedFeatures.set(protectedFeaturesValue);
}
},
];
if (all){
resolutions.push({
label: extension.getTranslation("sanitation_resolution.disableMainFlag"),
callback: function(){
settings.set(api.mainFlag, api.mainFlagDisabledValue || false);
}
});
}
2018-10-09 08:14:50 +02:00
errorCallback({
message: createErrorMessage(api, all),
severity: all? "high": "medium",
resolutions
2018-10-09 08:14:50 +02:00
});
}
}
});
}
},
{
name: "blockMode",
check: function(errorCallback){
const blockMode = settings.blockMode;
const protectedCanvasPart = settings.protectedCanvasPart;
2018-10-09 08:14:50 +02:00
if (!blockMode.match("^fake|^ask")){
errorCallback({
2019-04-09 08:29:52 +02:00
message: extension.getTranslation("sanitation_error.badBlockMode"),
2018-10-09 08:14:50 +02:00
severity: "medium",
resolutions: [{
2019-04-09 08:29:52 +02:00
label: extension.getTranslation("sanitation_resolution.switchToFake"),
callback: function(){
settings.blockMode = "fake";
}
}]
2018-10-09 08:14:50 +02:00
});
}
if (blockMode === "fake" && protectedCanvasPart === "input" && settings.rng === "white"){
2018-10-09 08:14:50 +02:00
errorCallback({
2019-04-09 08:29:52 +02:00
message: extension.getTranslation("sanitation_error.fakeInputWithWhiteRng")
.replace(/{blockMode}/g, extension.getTranslation("blockMode_options." + blockMode))
.replace(
/{protectedCanvasPart}/g,
2019-04-09 08:29:52 +02:00
extension.getTranslation("protectedCanvasPart_options." + settings.protectedCanvasPart)
),
2018-10-09 08:14:50 +02:00
severity: "low",
resolutions: [
{
2019-04-09 08:29:52 +02:00
label: extension.getTranslation("sanitation_resolution.switchToProtectReadout"),
callback: function(){
settings.protectedCanvasPart = "readout";
}
},
{
2019-04-09 08:29:52 +02:00
label: extension.getTranslation("sanitation_resolution.switchToNonPersistentRng"),
callback: function(){
settings.rng = "nonPersistent";
}
2018-10-09 08:14:50 +02:00
}
]
});
}
if (blockMode === "fake" && protectedCanvasPart === "everything"){
errorCallback({
2019-04-09 08:29:52 +02:00
message: extension.getTranslation("sanitation_error.fakeEverythingInCanvas")
.replace(/{blockMode}/g, extension.getTranslation("blockMode_options." + blockMode))
.replace(
/{protectedCanvasPart}/g,
2019-04-09 08:29:52 +02:00
extension.getTranslation("protectedCanvasPart_options." + settings.protectedCanvasPart)
),
severity: "low",
resolutions: [
{
2019-04-09 08:29:52 +02:00
label: extension.getTranslation("sanitation_resolution.switchToProtectReadout"),
callback: function(){
settings.protectedCanvasPart = "readout";
}
},
{
2019-04-09 08:29:52 +02:00
label: extension.getTranslation("sanitation_resolution.switchToProtectInput"),
callback: function(){
settings.protectedCanvasPart = "input";
}
}
]
2018-10-09 08:14:50 +02:00
});
}
}
},
{
name: "thresholds",
check: function(errorCallback){
2019-04-09 08:29:52 +02:00
const setToLabel = extension.getTranslation("sanitation_resolution.setTo");
const tooLowLabel = extension.getTranslation("sanitation_error.valueTooLow");
const tooHighLabel = extension.getTranslation("sanitation_error.valueTooHigh");
2018-10-09 08:14:50 +02:00
if (settings.minFakeSize > 1e2){
errorCallback({
message: tooHighLabel
2019-04-09 08:29:52 +02:00
.replace(/{setting}/g, extension.getTranslation("minFakeSize_title"))
2018-10-09 08:14:50 +02:00
.replace(/{value}/g, "100"),
severity: "high",
resolutions: [{
label: setToLabel.replace(/{value}/g, "100"),
callback: function(){
settings.minFakeSize = 1e2;
}
}]
});
}
if (settings.maxFakeSize !== 0 && settings.maxFakeSize < 1e6){
errorCallback({
message: tooLowLabel
2019-04-09 08:29:52 +02:00
.replace(/{setting}/g, extension.getTranslation("maxFakeSize_title"))
2018-10-09 08:14:50 +02:00
.replace(/{value}/g, "1 000 000"),
severity: "high",
resolutions: [{
label: setToLabel.replace(/{value}/g, "1 000 000"),
callback: function(){
settings.maxFakeSize = 1e6;
}
}]
});
}
if (settings.ignoreFrequentColors > 3){
errorCallback({
message: tooHighLabel
2019-04-09 08:29:52 +02:00
.replace(/{setting}/g, extension.getTranslation("ignoreFrequentColors_title"))
2018-10-09 08:14:50 +02:00
.replace(/{value}/g, "3"),
severity: "high",
resolutions: [{
label: setToLabel.replace(/{value}/g, "3"),
callback: function(){
settings.ignoreFrequentColors = 3;
}
}]
});
}
if (settings.minColors > 10){
errorCallback({
message: tooHighLabel
2019-04-09 08:29:52 +02:00
.replace(/{setting}/g, extension.getTranslation("minColors_title"))
2018-10-09 08:14:50 +02:00
.replace(/{value}/g, "10"),
severity: "high",
resolutions: [{
label: setToLabel.replace(/{value}/g, "10"),
callback: function(){
2018-10-09 13:06:55 +02:00
settings.minColors = 10;
2018-10-09 08:14:50 +02:00
}
}]
});
}
}
},
{
name: "performance",
check: function(errorCallback){
2019-04-09 08:29:52 +02:00
const disableLabel = extension.getTranslation("sanitation_resolution.disableFlag");
2018-10-09 08:14:50 +02:00
if (settings.storeNotificationData){
errorCallback({
2019-04-09 08:29:52 +02:00
message: extension.getTranslation("sanitation_error.storeNotificationData"),
2018-10-09 08:14:50 +02:00
severity: "low",
resolutions: [{
label: disableLabel
2019-04-09 08:29:52 +02:00
.replace(/{flag}/g, extension.getTranslation("storeNotificationData_title")),
2018-10-09 08:14:50 +02:00
callback: function(){
settings.storeNotificationData = false;
}
}]
});
if (settings.storeImageForInspection){
errorCallback({
2019-04-09 08:29:52 +02:00
message: extension.getTranslation("sanitation_error.storeImage"),
2018-10-09 08:14:50 +02:00
severity: "low",
resolutions: [{
label: disableLabel
2019-04-09 08:29:52 +02:00
.replace(/{flag}/g, extension.getTranslation("storeImageForInspection_title")),
2018-10-09 08:14:50 +02:00
callback: function(){
settings.storeImageForInspection = false;
}
}]
});
}
}
}
},
{
name: "privacy",
check: function(errorCallback){
if (settings.sharePersistentRndBetweenDomains){
errorCallback({
2019-04-09 08:29:52 +02:00
message: extension.getTranslation("sanitation_error.doNotSharePersistentRndBetweenDomains"),
severity: "high",
resolutions: [{
2019-04-09 08:29:52 +02:00
label: extension.getTranslation("sanitation_resolution.disableFlag")
.replace(/{flag}/g, extension.getTranslation("sharePersistentRndBetweenDomains_title")),
callback: function(){
settings.sharePersistentRndBetweenDomains = false;
}
}]
});
}
if (settings.protectScreen && settings.screenSize){
errorCallback({
message: extension.getTranslation("sanitation_error.customScreenSize"),
severity: "medium",
resolutions: [{
label: extension.getTranslation("sanitation_resolution.setTo")
.replace(/{value}/g, "\"\""),
callback: function(){
settings.screenSize = "";
}
}]
});
}
}
},
2018-10-09 08:14:50 +02:00
];
}());