From 95d0ffafb7da64f9a6c4a03e3dbed6ccdd8edd90 Mon Sep 17 00:00:00 2001 From: gorhill Date: Tue, 22 Mar 2016 10:19:41 -0400 Subject: [PATCH] this fixes #870 --- platform/chromium/vapi-background.js | 21 +++++++++++-- src/js/tab.js | 4 +++ src/js/traffic.js | 44 ++++++++++++++++++++++++++-- src/settings.html | 2 +- 4 files changed, 65 insertions(+), 6 deletions(-) diff --git a/platform/chromium/vapi-background.js b/platform/chromium/vapi-background.js index 2f819c370..89502500e 100644 --- a/platform/chromium/vapi-background.js +++ b/platform/chromium/vapi-background.js @@ -1,7 +1,7 @@ /******************************************************************************* - µBlock - a browser extension to block requests. - Copyright (C) 2014 The µBlock authors + uBlock Origin - a browser extension to block requests. + Copyright (C) 2014-2016 The uBlock Origin authors This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -788,6 +788,11 @@ vAPI.net.registerListeners = function() { var µb = µBlock; var µburi = µb.URI; + // https://bugs.chromium.org/p/chromium/issues/detail?id=410382 + // Between Chromium 38-48, plug-ins' network requests were reported as + // type "other" instead of "object". + var is_v38_48 = /\bChrom[a-z]+\/(?:3[89]|4[0-8])\.[\d.]+\b/.test(navigator.userAgent); + // Chromium-based browsers understand only these network request types. var validTypes = { 'main_frame': true, @@ -836,6 +841,14 @@ vAPI.net.registerListeners = function() { var normalizeRequestDetails = function(details) { details.tabId = details.tabId.toString(); + // https://github.com/gorhill/uBlock/issues/1493 + // Chromium 49+ support a new request type: `ping`, which is fired as + // a result of using `navigator.sendBeacon`. + if ( details.type === 'ping' ) { + details.type = 'beacon'; + return; + } + // The rest of the function code is to normalize type if ( details.type !== 'other' ) { return; @@ -885,7 +898,9 @@ vAPI.net.registerListeners = function() { } // https://code.google.com/p/chromium/issues/detail?id=410382 - details.type = 'object'; + if ( is_v38_48 ) { + details.type = 'object'; + } }; var onBeforeRequestClient = this.onBeforeRequest.callback; diff --git a/src/js/tab.js b/src/js/tab.js index 41cb37724..8dd908062 100644 --- a/src/js/tab.js +++ b/src/js/tab.js @@ -807,6 +807,10 @@ vAPI.tabs.registerListeners(); return this.pageStores[tabId] || null; }; +µb.mustPageStoreFromTabId = function(tabId) { + return this.pageStores[tabId] || this.pageStores[vAPI.noTabId]; +}; + /******************************************************************************/ // Permanent page store for behind-the-scene requests. Must never be removed. diff --git a/src/js/traffic.js b/src/js/traffic.js index 51add050c..bda7615d7 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -1,7 +1,7 @@ /******************************************************************************* - uBlock - a browser extension to block requests. - Copyright (C) 2014-2015 Raymond Hill + uBlock Origin - a browser extension to block requests. + Copyright (C) 2014-2016 Raymond Hill This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -47,6 +47,12 @@ var onBeforeRequest = function(details) { return onBeforeRootFrameRequest(details); } + // https://github.com/gorhill/uBlock/issues/870 + // This work for Chromium 49+. + if ( requestType === 'beacon' ) { + return onBeforeBeacon(details); + } + // Special treatment: behind-the-scene requests var tabId = details.tabId; if ( vAPI.isBehindTheSceneTabId(tabId) ) { @@ -273,6 +279,40 @@ var toBlockDocResult = function(url, hostname, result) { /******************************************************************************/ +// https://github.com/gorhill/uBlock/issues/870 +// Finally, Chromium 49+ gained the ability to report network request of type +// `beacon`, so now we can block them according to the state of the +// "Disable hyperlink auditing/beacon" setting. + +var onBeforeBeacon = function(details) { + var µb = µBlock; + var tabId = details.tabId; + var pageStore = µb.mustPageStoreFromTabId(tabId); + var context = pageStore.createContextFromPage(); + context.requestURL = details.url; + context.requestHostname = µb.URI.hostnameFromURI(details.url); + context.requestType = details.type; + // "g" in "gb:" stands for "global setting" + var result = µb.userSettings.hyperlinkAuditingDisabled ? 'gb:' : ''; + pageStore.logRequest(context, result); + if ( µb.logger.isEnabled() ) { + µb.logger.writeOne( + tabId, + 'net', + result, + details.type, + details.url, + context.rootHostname, + context.rootHostname + ); + } + if ( result !== '' ) { + return { cancel: true }; + } +}; + +/******************************************************************************/ + // Intercept and filter behind-the-scene requests. var onBeforeBehindTheSceneRequest = function(details) { diff --git a/src/settings.html b/src/settings.html index 7d1f712d2..e87cf360f 100644 --- a/src/settings.html +++ b/src/settings.html @@ -20,7 +20,7 @@