From 95b25f7d49d4987711f28b250a81ff979b204c47 Mon Sep 17 00:00:00 2001 From: gorhill Date: Thu, 19 Oct 2017 09:35:28 -0400 Subject: [PATCH] fix #3150 --- src/_locales/en/messages.json | 4 ++ src/js/hnswitches.js | 20 +++------- src/js/pagestore.js | 75 ++++++++++++++++++++++------------- src/js/traffic.js | 7 +++- src/js/ublock.js | 6 +++ src/settings.html | 1 + 6 files changed, 69 insertions(+), 44 deletions(-) diff --git a/src/_locales/en/messages.json b/src/_locales/en/messages.json index 6de18b12c..61c6f1044 100644 --- a/src/_locales/en/messages.json +++ b/src/_locales/en/messages.json @@ -251,6 +251,10 @@ "message":"Block remote fonts", "description": "" }, + "settingsNoCSPReportsPrompt":{ + "message":"Block CSP reports", + "description": "" + }, "settingsStorageUsed":{ "message":"Storage used: {{value}} bytes", "description":"English: Storage used: {{}} bytes" diff --git a/src/js/hnswitches.js b/src/js/hnswitches.js index e16c4d9ea..17954c961 100644 --- a/src/js/hnswitches.js +++ b/src/js/hnswitches.js @@ -37,19 +37,12 @@ var HnSwitches = function() { /******************************************************************************/ var switchBitOffsets = { - 'no-strict-blocking': 0, - 'no-popups': 2, - 'no-cosmetic-filtering': 4, - 'no-remote-fonts': 6, - 'no-large-media': 8 -}; - -var fromLegacySwitchNames = { - 'dontBlockDoc': 'no-strict-blocking', - 'doBlockAllPopups': 'no-popups', - 'noStrictBlocking': 'no-strict-blocking', - 'noPopups': 'no-popups', - 'noCosmeticFiltering': 'no-cosmetic-filtering' + 'no-strict-blocking': 0, + 'no-popups': 2, + 'no-cosmetic-filtering': 4, + 'no-remote-fonts': 6, + 'no-large-media': 8, + 'no-csp-reports': 10 }; var switchStateToNameMap = { @@ -315,7 +308,6 @@ HnSwitches.prototype.fromString = function(text) { continue; } switchName = switchName.slice(0, pos); - switchName = fromLegacySwitchNames[switchName] || switchName; if ( switchBitOffsets.hasOwnProperty(switchName) === false ) { continue; } diff --git a/src/js/pagestore.js b/src/js/pagestore.js index 50b70604f..8d050fb56 100644 --- a/src/js/pagestore.js +++ b/src/js/pagestore.js @@ -589,36 +589,12 @@ PageStore.prototype.filterRequest = function(context) { var requestType = context.requestType; - // https://github.com/gorhill/uBlock/issues/3140 - // Special handling of CSP reports if and only if these can't be filtered - // natively. - if ( - requestType === 'csp_report' && - vAPI.net.nativeCSPReportFiltering !== true - ) { - if ( this.internalRedirectionCount !== 0 ) { - if ( µb.logger.isEnabled() ) { - this.logData = { - result: 1, - source: 'global', - raw: 'no-spurious-csp-report' - }; - } - return 1; - } + if ( requestType === 'csp_report' && this.filterCSPReport(context) === 1 ) { + return 1; } - - if ( requestType.endsWith('font') ) { - if ( requestType === 'font' ) { - this.remoteFontCount += 1; - } - if ( µb.hnSwitches.evaluateZ('no-remote-fonts', context.rootHostname) !== false ) { - if ( µb.logger.isEnabled() ) { - this.logData = µb.hnSwitches.toLogData(); - } - return 1; - } + if ( requestType.endsWith('font') && this.filterFont(context) === 1 ) { + return 1; } var cacheableResult = this.cacheableResults[requestType] === true; @@ -675,6 +651,49 @@ PageStore.prototype.collapsibleResources = { /******************************************************************************/ +PageStore.prototype.filterCSPReport = function(context) { + if ( µb.hnSwitches.evaluateZ('no-csp-reports', context.rootHostname) !== false ) { + if ( µb.logger.isEnabled() ) { + this.logData = µb.hnSwitches.toLogData(); + } + return 1; + } + // https://github.com/gorhill/uBlock/issues/3140 + // Special handling of CSP reports if and only if these can't be filtered + // natively. + if ( + vAPI.net.nativeCSPReportFiltering !== true && + this.internalRedirectionCount !== 0 + ) { + if ( µb.logger.isEnabled() ) { + this.logData = { + result: 1, + source: 'global', + raw: 'no-spurious-csp-report' + }; + } + return 1; + } + return 0; +}; + +/******************************************************************************/ + +PageStore.prototype.filterFont = function(context) { + if ( context.requestType === 'font' ) { + this.remoteFontCount += 1; + } + if ( µb.hnSwitches.evaluateZ('no-remote-fonts', context.rootHostname) !== false ) { + if ( µb.logger.isEnabled() ) { + this.logData = µb.hnSwitches.toLogData(); + } + return 1; + } + return 0; +}; + +/******************************************************************************/ + // The caller is responsible to check whether filtering is enabled or not. PageStore.prototype.filterLargeMediaElement = function(size) { diff --git a/src/js/traffic.js b/src/js/traffic.js index 5dae0f1f9..97fb2e9b7 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -353,8 +353,11 @@ var onBeforeBehindTheSceneRequest = function(details) { // Blocking behind-the-scene requests can break a lot of stuff: prevent // browser updates, prevent extension updates, prevent extensions from // working properly, etc. - // So we filter if and only if the "advanced user" mode is selected - if ( µb.userSettings.advancedUserEnabled ) { + // So we filter if and only if the "advanced user" mode is selected. + // https://github.com/gorhill/uBlock/issues/3150 + // Ability to globally block CSP reports MUST also apply to + // behind-the-scene network requests. + if ( µb.userSettings.advancedUserEnabled || requestType === 'csp_report' ) { result = pageStore.filterRequest(context); } diff --git a/src/js/ublock.js b/src/js/ublock.js index 5ad8e8861..d82d11cad 100644 --- a/src/js/ublock.js +++ b/src/js/ublock.js @@ -304,6 +304,7 @@ var reInvalidHostname = /[^a-z0-9.\-\[\]:]/, us.noCosmeticFiltering = this.hnSwitches.evaluate('no-cosmetic-filtering', '*') === 1; us.noLargeMedia = this.hnSwitches.evaluate('no-large-media', '*') === 1; us.noRemoteFonts = this.hnSwitches.evaluate('no-remote-fonts', '*') === 1; + us.noCSPReports = this.hnSwitches.evaluate('no-csp-reports', '*') === 1; return us; } @@ -371,6 +372,11 @@ var reInvalidHostname = /[^a-z0-9.\-\[\]:]/, this.saveHostnameSwitches(); } break; + case 'noCSPReports': + if ( this.hnSwitches.toggle('no-csp-reports', '*', value ? 1 : 0) ) { + this.saveHostnameSwitches(); + } + break; case 'prefetchingDisabled': if ( this.privacySettingsSupported ) { vAPI.browserSettings.set({ 'prefetching': !value }); diff --git a/src/settings.html b/src/settings.html index 39646a3a5..ff73baf51 100644 --- a/src/settings.html +++ b/src/settings.html @@ -23,6 +23,7 @@
  • +