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

301 lines
9.3 KiB
JavaScript

/* 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 = {};
window.scope.sanitationRules = scope;
}
const settings = require("./settings");
scope.ruleset = [
{
name: "unnecessaryURLValue",
check: function(errorCallback){
const {url: urlContainer} = settings.getContainers();
const containerValue = urlContainer.get();
const errorMessage = browser.i18n.getMessage("sanitation_error.unnecessaryURLValue");
function createErrorMessage(setting, urlValue){
return errorMessage
.replace(/{setting-technical}/g, setting.name)
.replace(/{setting-title}/g, browser.i18n.getMessage(setting.name + "_title"))
.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: [{
label: browser.i18n.getMessage("sanitation_resolution.removeURLValue"),
callback: function(){
setting.reset(urlValues.url);
}
}]
});
}
}
});
});
}
},
{
name: "disabledFeatures",
check: function(errorCallback){
const errorMessage = browser.i18n.getMessage("sanitation_error.disabledFeatures");
function createErrorMessage(api){
return errorMessage.replace(/{api}/g, browser.i18n.getMessage("section_" + api.section));
}
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: "protectAudio", section: "Audio-API"},
{mainFlag: "protectWindow", section: "Window-API"},
{mainFlag: "protectDOMRect", section: "DOMRect-API"},
].forEach(function(api){
if (settings.get(api.mainFlag)){
let inSection = false;
let anyActive = false;
if (getSectionKeys(api.section).every(function(key){
return protectedFeaturesValue.hasOwnProperty(key) &&
!protectedFeaturesValue[key];
})){
errorCallback({
message: createErrorMessage(api),
severity: "high",
resolutions: [
{
label: browser.i18n.getMessage("sanitation_resolution.enableFeatures"),
callback: function(){
const protectedFeaturesValue = protectedFeatures.get();
getSectionKeys(api.section).forEach(function(key){
protectedFeaturesValue[key] = true;
});
protectedFeatures.set(protectedFeaturesValue);
}
},
{
label: browser.i18n.getMessage("sanitation_resolution.disableMainFlag"),
callback: function(){
settings.set(api.mainFlag, false);
}
},
]
});
}
}
});
}
},
{
name: "blockMode",
check: function(errorCallback){
const blockMode = settings.blockMode;
const protectedCanvasPart = settings.protectedCanvasPart;
if (!blockMode.match("^fake|^ask")){
errorCallback({
message: browser.i18n.getMessage("sanitation_error.badBlockMode"),
severity: "medium",
resolutions: [{
label: browser.i18n.getMessage("sanitation_resolution.switchToFake"),
callback: function(){
settings.blockMode = "fake";
}
}]
});
}
if (blockMode === "fake" && protectedCanvasPart === "input" && settings.rng === "white"){
errorCallback({
message: browser.i18n.getMessage("sanitation_error.fakeInputWithWhiteRng")
.replace(/{blockMode}/g, browser.i18n.getMessage("blockMode_options." + blockMode))
.replace(
/{protectedCanvasPart}/g,
browser.i18n.getMessage("protectedCanvasPart_options." + settings.protectedCanvasPart)
),
severity: "low",
resolutions: [
{
label: browser.i18n.getMessage("sanitation_resolution.switchToProtectReadout"),
callback: function(){
settings.protectedCanvasPart = "readout";
}
},
{
label: browser.i18n.getMessage("sanitation_resolution.switchToNonPersistentRng"),
callback: function(){
settings.rng = "nonPersistent";
}
}
]
});
}
if (blockMode === "fake" && protectedCanvasPart === "everything"){
errorCallback({
message: browser.i18n.getMessage("sanitation_error.fakeEverythingInCanvas")
.replace(/{blockMode}/g, browser.i18n.getMessage("blockMode_options." + blockMode))
.replace(
/{protectedCanvasPart}/g,
browser.i18n.getMessage("protectedCanvasPart_options." + settings.protectedCanvasPart)
),
severity: "low",
resolutions: [
{
label: browser.i18n.getMessage("sanitation_resolution.switchToProtectReadout"),
callback: function(){
settings.protectedCanvasPart = "readout";
}
},
{
label: browser.i18n.getMessage("sanitation_resolution.switchToProtectInput"),
callback: function(){
settings.protectedCanvasPart = "input";
}
}
]
});
}
}
},
{
name: "thresholds",
check: function(errorCallback){
const setToLabel = browser.i18n.getMessage("sanitation_resolution.setTo");
const tooLowLabel = browser.i18n.getMessage("sanitation_error.valueTooLow");
const tooHighLabel = browser.i18n.getMessage("sanitation_error.valueTooHigh");
if (settings.minFakeSize > 1e2){
errorCallback({
message: tooHighLabel
.replace(/{setting}/g, browser.i18n.getMessage("minFakeSize_title"))
.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
.replace(/{setting}/g, browser.i18n.getMessage("maxFakeSize_title"))
.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
.replace(/{setting}/g, browser.i18n.getMessage("ignoreFrequentColors_title"))
.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
.replace(/{setting}/g, browser.i18n.getMessage("minColors_title"))
.replace(/{value}/g, "10"),
severity: "high",
resolutions: [{
label: setToLabel.replace(/{value}/g, "10"),
callback: function(){
settings.minColors = 10;
}
}]
});
}
}
},
{
name: "performance",
check: function(errorCallback){
const disableLabel = browser.i18n.getMessage("sanitation_resolution.disableFlag");
if (settings.storeNotificationData){
errorCallback({
message: browser.i18n.getMessage("sanitation_error.storeNotificationData"),
severity: "low",
resolutions: [{
label: disableLabel
.replace(/{flag}/g, browser.i18n.getMessage("storeNotificationData_title")),
callback: function(){
settings.storeNotificationData = false;
}
}]
});
if (settings.storeImageForInspection){
errorCallback({
message: browser.i18n.getMessage("sanitation_error.storeImage"),
severity: "low",
resolutions: [{
label: disableLabel
.replace(/{flag}/g, browser.i18n.getMessage("storeImageForInspection_title")),
callback: function(){
settings.storeImageForInspection = false;
}
}]
});
}
}
}
},
{
name: "privacy",
check: function(errorCallback){
if (settings.sharePersistentRndBetweenDomains){
errorCallback({
message: browser.i18n.getMessage("sanitation_error.doNotSharePersistentRndBetweenDomains"),
severity: "high",
resolutions: [{
label: browser.i18n.getMessage("sanitation_resolution.disableFlag")
.replace(/{flag}/g, browser.i18n.getMessage("sharePersistentRndBetweenDomains_title")),
callback: function(){
settings.sharePersistentRndBetweenDomains = false;
}
}]
});
}
}
},
];
}());