From d01ab961b87d63248e3bc7d83f9c601304f4820e Mon Sep 17 00:00:00 2001
From: Raymond Hill
Date: Wed, 19 Nov 2014 11:59:38 -0200
Subject: [PATCH] this fixes #7, #36
---
src/js/messaging-handlers.js | 48 ++++++++++++++++++++
src/js/privacy.js | 79 ++++++++++++++++++++-------------
src/js/storage.js | 40 ++++++++++++++---
src/popup.html | 4 +-
src/privacy.html | 18 +++++---
tools/_locales/en/messages.json | 16 +++++++
6 files changed, 159 insertions(+), 46 deletions(-)
diff --git a/src/js/messaging-handlers.js b/src/js/messaging-handlers.js
index 7673529..a6560a6 100644
--- a/src/js/messaging-handlers.js
+++ b/src/js/messaging-handlers.js
@@ -439,6 +439,54 @@ var onMessage = function(request, sender, callback) {
/******************************************************************************/
/******************************************************************************/
+// privacy.js
+
+(function() {
+
+var onMessage = function(request, sender, callback) {
+ var µm = µMatrix;
+
+ // Async
+ switch ( request.what ) {
+ default:
+ break;
+ }
+
+ // Sync
+ var response;
+
+ switch ( request.what ) {
+ case 'getPrivacySettings':
+ response = {
+ userSettings: µm.userSettings,
+ matrixSwitches: {
+ 'https-strict': µm.pMatrix.evaluateSwitch('https-strict', '*') === 1,
+ 'ua-spoof': µm.pMatrix.evaluateSwitch('ua-spoof', '*') === 1,
+ }
+ };
+ break;
+
+ case 'setMatrixSwitch':
+ µm.tMatrix.setSwitch(request.switchName, '*', request.state);
+ if ( µm.pMatrix.setSwitch(request.switchName, '*', request.state) ) {
+ µm.saveMatrix();
+ }
+ break;
+
+ default:
+ return µm.messaging.defaultHandler(request, sender, callback);
+ }
+
+ callback(response);
+};
+
+µMatrix.messaging.listen('privacy.js', onMessage);
+
+})();
+
+/******************************************************************************/
+/******************************************************************************/
+
// user-rules.js
(function() {
diff --git a/src/js/privacy.js b/src/js/privacy.js
index fdc18d3..4ffd6c5 100644
--- a/src/js/privacy.js
+++ b/src/js/privacy.js
@@ -19,6 +19,8 @@
Home: https://github.com/gorhill/uMatrix
*/
+/* global messaging, uDom */
+
/******************************************************************************/
(function() {
@@ -27,7 +29,7 @@
messaging.start('privacy.js');
-var cachedUserSettings = {};
+var cachedPrivacySettings = {};
/******************************************************************************/
@@ -41,8 +43,18 @@ function changeUserSettings(name, value) {
/******************************************************************************/
+function changeMatrixSwitch(name, state) {
+ messaging.tell({
+ what: 'setMatrixSwitch',
+ switchName: name,
+ state: state
+ });
+}
+
+/******************************************************************************/
+
function onChangeValueHandler(uelem, setting, min, max) {
- var oldVal = cachedUserSettings[setting];
+ var oldVal = cachedPrivacySettings.userSettings[setting];
var newVal = Math.round(parseFloat(uelem.val()));
if ( typeof newVal !== 'number' ) {
newVal = oldVal;
@@ -67,33 +79,26 @@ function prepareToDie() {
/******************************************************************************/
var installEventHandlers = function() {
- uDom('#delete-unused-session-cookies').on('change', function(){
- changeUserSettings('deleteUnusedSessionCookies', this.checked);
+ uDom('[data-setting-bool]').on('change', function(){
+ var settingName = this.getAttribute('data-setting-bool');
+ if ( typeof settingName === 'string' && settingName !== '' ) {
+ changeUserSettings(settingName, this.checked);
+ }
});
+
+ uDom('[data-matrix-switch]').on('change', function(){
+ var switchName = this.getAttribute('data-matrix-switch');
+ if ( typeof switchName === 'string' && switchName !== '' ) {
+ changeMatrixSwitch(switchName, this.checked);
+ }
+ });
+
uDom('#delete-unused-session-cookies-after').on('change', function(){
onChangeValueHandler(uDom(this), 'deleteUnusedSessionCookiesAfter', 15, 1440);
});
- uDom('#delete-blacklisted-cookies').on('change', function(){
- changeUserSettings('deleteCookies', this.checked);
- });
- uDom('#delete-blacklisted-localstorage').on('change', function(){
- changeUserSettings('deleteLocalStorage', this.checked);
- });
- uDom('#clear-browser-cache').on('change', function(){
- changeUserSettings('clearBrowserCache', this.checked);
- });
uDom('#clear-browser-cache-after').on('change', function(){
onChangeValueHandler(uDom(this), 'clearBrowserCacheAfter', 15, 1440);
});
- uDom('#process-referer').on('change', function(){
- changeUserSettings('processReferer', this.checked);
- });
- uDom('#process-hyperlink-auditing').on('change', function(){
- changeUserSettings('processHyperlinkAuditing', this.checked);
- });
- uDom('#spoof-user-agent').on('change', function(){
- changeUserSettings('spoofUserAgent', this.checked);
- });
uDom('#spoof-user-agent-every').on('change', function(){
onChangeValueHandler(uDom(this), 'spoofUserAgentEvery', 2, 999);
});
@@ -108,25 +113,35 @@ var installEventHandlers = function() {
/******************************************************************************/
uDom.onLoad(function() {
- var onUserSettingsReceived = function(userSettings) {
+ var onSettingsReceived = function(privacySettings) {
// Cache copy
- cachedUserSettings = userSettings;
+ cachedPrivacySettings = privacySettings;
+
+ var userSettings = privacySettings.userSettings;
+ var matrixSwitches = privacySettings.matrixSwitches;
+
+ uDom('[data-setting-bool]').forEach(function(elem){
+ var settingName = elem.attr('data-setting-bool');
+ if ( typeof settingName === 'string' && settingName !== '' ) {
+ elem.prop('checked', userSettings[settingName] === true);
+ }
+ });
+
+ uDom('[data-matrix-switch]').forEach(function(elem){
+ var switchName = elem.attr('data-matrix-switch');
+ if ( typeof switchName === 'string' && switchName !== '' ) {
+ elem.prop('checked', matrixSwitches[switchName] === true);
+ }
+ });
- uDom('#delete-unused-session-cookies').prop('checked', userSettings.deleteUnusedSessionCookies === true);
uDom('#delete-unused-session-cookies-after').val(userSettings.deleteUnusedSessionCookiesAfter);
- uDom('#delete-blacklisted-cookies').prop('checked', userSettings.deleteCookies === true);
- uDom('#delete-blacklisted-localstorage').prop('checked', userSettings.deleteLocalStorage);
- uDom('#clear-browser-cache').prop('checked', userSettings.clearBrowserCache === true);
uDom('#clear-browser-cache-after').val(userSettings.clearBrowserCacheAfter);
- uDom('#process-referer').prop('checked', userSettings.processReferer);
- uDom('#process-hyperlink-auditing').prop('checked', userSettings.processHyperlinkAuditing);
- uDom('#spoof-user-agent').prop('checked', userSettings.spoofUserAgent);
uDom('#spoof-user-agent-every').val(userSettings.spoofUserAgentEvery);
uDom('#spoof-user-agent-with').val(userSettings.spoofUserAgentWith);
installEventHandlers();
};
- messaging.ask({ what: 'getUserSettings' }, onUserSettingsReceived);
+ messaging.ask({ what: 'getPrivacySettings' }, onSettingsReceived);
});
/******************************************************************************/
diff --git a/src/js/storage.js b/src/js/storage.js
index 07c36cd..dfe8943 100644
--- a/src/js/storage.js
+++ b/src/js/storage.js
@@ -83,7 +83,10 @@
/******************************************************************************/
-µMatrix.loadMatrix = function() {
+µMatrix.loadMatrix = function(callback) {
+ if ( typeof callback !== 'function' ) {
+ callback = this.noopFunc;
+ }
var µm = this;
var onLoaded = function(bin) {
if ( bin.hasOwnProperty('userMatrix') ) {
@@ -95,6 +98,7 @@
µm.pMatrix.setCell('*', '*', 'doc', µm.Matrix.Green);
µm.saveMatrix();
µm.tMatrix.assign(µm.pMatrix);
+ callback();
}
};
this.XAL.keyvalGetOne('userMatrix', onLoaded);
@@ -396,9 +400,28 @@
}
var µm = this;
+ var settingsReady = false;
+ var matrixReady = false;
- // User settings are in memory
- var onUserSettingsReady = function(settings) {
+ // TODO: to remove when everybody (and their backup file) has their
+ // ua-spoof setting converted into a matrix switch.
+ var onSettingsAndMatrixReady = function() {
+ if ( !settingsReady || !matrixReady ) {
+ return;
+ }
+ if ( µm.userSettings.hasOwnProperty('spoofUserAgent') === false ) {
+ return;
+ }
+ if ( µm.userSettings.spoofUserAgent ) {
+ µm.tMatrix.setSwitch('ua-spoof', '*', 1);
+ µm.pMatrix.setSwitch('ua-spoof', '*', 1);
+ µm.saveMatrix();
+ }
+ delete µm.userSettings.spoofUserAgent;
+ µm.saveUserSettings();
+ };
+
+ var onSettingsReady = function(settings) {
// Never auto-update at boot time
µm.loadUpdatableAssets(false, callback);
@@ -406,9 +429,16 @@
if ( settings.autoUpdate ) {
µm.updater.restart(µm.firstUpdateAfter);
}
+ settingsReady = true;
+ onSettingsAndMatrixReady();
};
- this.loadUserSettings(onUserSettingsReady);
- this.loadMatrix();
+ var onMatrixReady = function() {
+ matrixReady = true;
+ onSettingsAndMatrixReady();
+ };
+
+ this.loadUserSettings(onSettingsReady);
+ this.loadMatrix(onMatrixReady);
this.getBytesInUse();
};
diff --git a/src/popup.html b/src/popup.html
index 7a6d4ed..8c3cb9a 100644
--- a/src/popup.html
+++ b/src/popup.html
@@ -33,8 +33,8 @@
diff --git a/src/privacy.html b/src/privacy.html
index 1df9ebe..6628d17 100644
--- a/src/privacy.html
+++ b/src/privacy.html
@@ -46,11 +46,11 @@ html.rtl #spoof-user-agent-with {
-
+
-
+
@@ -73,23 +73,27 @@ html.rtl #spoof-user-agent-with {
of these cookies so that they cannot be used to track you.
-->
-
+
-
+
-
+
-
+
+
+
+
+
-
+
diff --git a/tools/_locales/en/messages.json b/tools/_locales/en/messages.json
index 4857a39..d9bcfff 100644
--- a/tools/_locales/en/messages.json
+++ b/tools/_locales/en/messages.json
@@ -107,6 +107,14 @@
"message": "{{count}} blacklisted hostname(s)",
"description": "Appears in the metadata row of bottom-most group: **mind the limited width**"
},
+ "matrixSwitchNoMixedContent" : {
+ "message": "Strict HTTPS",
+ "description": ""
+ },
+ "matrixSwitchUASpoof" : {
+ "message": "User agent spoofing",
+ "description": ""
+ },
"statsPageTitle" : {
@@ -341,6 +349,14 @@
"message": "From Wikipedia: “HTTP referer is an HTTP header field that identifies the address of the webpage that linked to the resource being requested. ... Because referer information can violate privacy, some web browsers allow the user to disable the sending of referer information. ”
If this setting is checked, µMatrix will remove the HTTP referer information if both of the following conditions are true:
The domain name of the HTTP referer does not match the domain name of the URL of the request (that is, the referer is third-party to the web page); The hostname of the request is not whitelisted.
",
"description": ""
},
+ "privacyNoMixedContentPrompt" : {
+ "message": "Strict HTTPS: forbid mixed content.",
+ "description": ""
+ },
+ "privacyNoMixedContentHelp" : {
+ "message": "From Mozilla Developer Network :
If [a] HTTPS page includes content retrieved through regular, cleartext HTTP, then the connection is only partially encrypted: the unencrypted content is accessible to sniffers and can be modified by man-in-the-middle attackers, and therefore the connection is not safeguarded anymore. When a webpage exhibits this behavior, it is called a mixed content page. ",
+ "description": ""
+ },
"privacyProcessHyperlinkAuditingPrompt" : {
"message": "Block all hyperlink auditing attempts.",
"description": ""