From 5d8bae3da2fae6a8a3d551090b521725dac7c10c Mon Sep 17 00:00:00 2001 From: Raymond Hill Date: Wed, 19 Nov 2014 23:17:24 -0200 Subject: [PATCH] this fixes #59 --- src/js/matrix.js | 7 +++--- src/js/messaging-handlers.js | 1 + src/js/storage.js | 15 +++++++++--- src/js/traffic.js | 43 +++++++++++++++------------------ src/popup.html | 1 + src/privacy.html | 2 +- tools/_locales/en/messages.json | 6 ++++- 7 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/js/matrix.js b/src/js/matrix.js index 4efadb8..2017c69 100644 --- a/src/js/matrix.js +++ b/src/js/matrix.js @@ -82,9 +82,10 @@ var nameToStateMap = { }; var switchBitOffsets = { - 'matrix-off': 0, - 'https-strict': 2, - 'ua-spoof': 4 + 'matrix-off': 0, + 'https-strict': 2, + 'ua-spoof': 4, + 'referrer-spoof': 6 }; var switchStateToNameMap = { diff --git a/src/js/messaging-handlers.js b/src/js/messaging-handlers.js index a6560a6..7244469 100644 --- a/src/js/messaging-handlers.js +++ b/src/js/messaging-handlers.js @@ -462,6 +462,7 @@ var onMessage = function(request, sender, callback) { matrixSwitches: { 'https-strict': µm.pMatrix.evaluateSwitch('https-strict', '*') === 1, 'ua-spoof': µm.pMatrix.evaluateSwitch('ua-spoof', '*') === 1, + 'referrer-spoof': µm.pMatrix.evaluateSwitch('referrer-spoof', '*') === 1 } }; break; diff --git a/src/js/storage.js b/src/js/storage.js index dfe8943..0414c31 100644 --- a/src/js/storage.js +++ b/src/js/storage.js @@ -404,19 +404,26 @@ var matrixReady = false; // TODO: to remove when everybody (and their backup file) has their - // ua-spoof setting converted into a matrix switch. + // ua-spoof/referrer-spoof setting converted into a matrix switch. var onSettingsAndMatrixReady = function() { if ( !settingsReady || !matrixReady ) { return; } - if ( µm.userSettings.hasOwnProperty('spoofUserAgent') === false ) { - return; - } + var saveMatrix = false; if ( µm.userSettings.spoofUserAgent ) { µm.tMatrix.setSwitch('ua-spoof', '*', 1); µm.pMatrix.setSwitch('ua-spoof', '*', 1); + saveMatrix = true; + } + if ( µm.userSettings.processReferer ) { + µm.tMatrix.setSwitch('referrer-spoof', '*', 1); + µm.pMatrix.setSwitch('referrer-spoof', '*', 1); + saveMatrix = true; + } + if ( saveMatrix ) { µm.saveMatrix(); } + delete µm.userSettings.processReferer; delete µm.userSettings.spoofUserAgent; µm.saveUserSettings(); }; diff --git a/src/js/traffic.js b/src/js/traffic.js index c0e2732..9707d23 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -467,13 +467,10 @@ var onBeforeSendHeadersHandler = function(details) { changed = foilCookieHeaders(µm, details) || changed; } - // TODO: use cookie cell to determine whether the referrer info must be - // foiled. - if ( µm.userSettings.processReferer && µm.mustBlock(pageStore.pageHostname, reqHostname, '*') ) { + if ( µm.tMatrix.evaluateSwitchZ('referrer-spoof', pageStore.pageHostname) ) { changed = foilRefererHeaders(µm, reqHostname, details) || changed; } - // TODO: move the master ua-spoofing switch into the matrix. if ( µm.tMatrix.evaluateSwitchZ('ua-spoof', pageStore.pageHostname) ) { changed = foilUserAgent(µm, details) || changed; // https://github.com/gorhill/httpswitchboard/issues/252 @@ -559,29 +556,29 @@ var foilCookieHeaders = function(µm, details) { var foilRefererHeaders = function(µm, toHostname, details) { var headers = details.requestHeaders; - var header; - var fromDomain, toDomain; - var i = headers.length; + var i = headers.length, header; while ( i-- ) { header = headers[i]; - if ( header.name.toLowerCase() !== 'referer' ) { - continue; - } - fromDomain = µm.URI.domainFromURI(header.value); - if ( !toDomain ) { - toDomain = µm.URI.domainFromHostname(toHostname); - } - if ( toDomain === fromDomain ) { - continue; + if ( header.name.toLowerCase() === 'referer' ) { + break; } - // console.debug('foilRefererHeaders()> foiled referer "%s" for "%s"', fromDomain, toDomain); - // https://github.com/gorhill/httpswitchboard/issues/222#issuecomment-44828402 - // Splicing instead of blanking: not sure how much it helps - headers.splice(i, 1); - µm.refererHeaderFoiledCounter++; - return true; } - return false; + if ( i === -1 ) { + return false; + } + var µmuri = µm.URI; + var fromDomain = µmuri.domainFromURI(header.value); + var toDomain = µmuri.domainFromHostname(toHostname); + if ( toDomain === fromDomain ) { + return false; + } + //console.debug('foilRefererHeaders()> foiled referer for "%s"', details.url); + //console.debug('\treferrer "%s"', header.value); + // https://github.com/gorhill/httpswitchboard/issues/222#issuecomment-44828402 + header.value = µmuri.schemeFromURI(details.url) + '://' + toHostname + '/'; + //console.debug('\treplaced with "%s"', header.value); + µm.refererHeaderFoiledCounter++; + return true; }; /******************************************************************************/ diff --git a/src/popup.html b/src/popup.html index c4a3201..9bbb34b 100644 --- a/src/popup.html +++ b/src/popup.html @@ -35,6 +35,7 @@ diff --git a/src/privacy.html b/src/privacy.html index 6628d17..41ddf55 100644 --- a/src/privacy.html +++ b/src/privacy.html @@ -80,7 +80,7 @@ html.rtl #spoof-user-agent-with {
  • - +
  • diff --git a/tools/_locales/en/messages.json b/tools/_locales/en/messages.json index d9bcfff..c3d8eb6 100644 --- a/tools/_locales/en/messages.json +++ b/tools/_locales/en/messages.json @@ -115,6 +115,10 @@ "message": "User agent spoofing", "description": "" }, + "matrixSwitchReferrerSpoof" : { + "message": "Referrer spoofing", + "description": "" + }, "statsPageTitle" : { @@ -342,7 +346,7 @@ "description": "" }, "privacyProcessRefererPrompt" : { - "message": "Remove third-party HTTP referer information from requests to non-whitelisted hostnames.", + "message": "Spoof HTTP referrer string of third-party requests.", "description": "" }, "privacyProcessRefererHelp" : {