From 83b342d4e4d768d57f1c982d9bf6b673ad35526e Mon Sep 17 00:00:00 2001 From: gorhill Date: Sat, 30 Jul 2016 09:58:13 -0400 Subject: [PATCH] fix #597 + code review + merge media and image --- platform/chromium/vapi-background.js | 26 +++++++++++-------------- platform/firefox/vapi-background.js | 24 +++++++++++------------ src/js/traffic.js | 29 +++++++++++++++------------- src/js/uritools.js | 18 ++++++++++++----- 4 files changed, 51 insertions(+), 46 deletions(-) diff --git a/platform/chromium/vapi-background.js b/platform/chromium/vapi-background.js index ced1f89..52ae5c0 100644 --- a/platform/chromium/vapi-background.js +++ b/platform/chromium/vapi-background.js @@ -1,7 +1,7 @@ /******************************************************************************* - µMatrix - a browser extension to block requests. - Copyright (C) 2014 The uBlock authors + uMatrix - a browser extension to block requests. + Copyright (C) 2014-2016 The uBlock 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 @@ -16,19 +16,19 @@ You should have received a copy of the GNU General Public License along with this program. If not, see {http://www.gnu.org/licenses/}. - Home: https://github.com/gorhill/uBlock + Home: https://github.com/gorhill/uMatrix */ /* global self, µMatrix */ // For background page +'use strict'; + /******************************************************************************/ (function() { -'use strict'; - /******************************************************************************/ var vAPI = self.vAPI = self.vAPI || {}; @@ -584,7 +584,6 @@ vAPI.net = {}; vAPI.net.registerListeners = function() { var µm = µMatrix; - var µmuri = µm.URI; var httpRequestHeadersJunkyard = []; // Abstraction layer to deal with request headers @@ -659,10 +658,7 @@ vAPI.net.registerListeners = function() { // Normalizing request types // >>>>>>>> var normalizeRequestDetails = function(details) { - µmuri.set(details.url); - details.tabId = details.tabId.toString(); - details.hostname = µmuri.hostnameFromURI(details.url); // The rest of the function code is to normalize request type if ( details.type !== 'other' ) { @@ -676,9 +672,9 @@ vAPI.net.registerListeners = function() { } } - var tail = µmuri.path.slice(-6); - var pos = tail.lastIndexOf('.'); - + var path = µm.URI.pathFromURI(details.url), + pos = path.indexOf('.', path.length - 6); + // https://github.com/chrisaljoudi/uBlock/issues/862 // If no transposition possible, transpose to `object` as per // Chromium bug 410382 (see below) @@ -686,14 +682,14 @@ vAPI.net.registerListeners = function() { return; } - var ext = tail.slice(pos) + '.'; - if ( '.eot.ttf.otf.svg.woff.woff2.'.indexOf(ext) !== -1 ) { + var needle = path.slice(pos) + '.'; + if ( '.eot.ttf.otf.svg.woff.woff2.'.indexOf(needle) !== -1 ) { details.type = 'font'; return; } // Still need this because often behind-the-scene requests are wrongly // categorized as 'other' - if ( '.ico.png.gif.jpg.jpeg.webp.'.indexOf(ext) !== -1 ) { + if ( '.ico.png.gif.jpg.jpeg.mp3.mp4.webm.webp.'.indexOf(needle) !== -1 ) { details.type = 'image'; return; } diff --git a/platform/firefox/vapi-background.js b/platform/firefox/vapi-background.js index a0d56bb..24e9da1 100644 --- a/platform/firefox/vapi-background.js +++ b/platform/firefox/vapi-background.js @@ -24,12 +24,12 @@ // For background page +'use strict'; + /******************************************************************************/ (function() { -'use strict'; - /******************************************************************************/ // Useful links @@ -1714,7 +1714,7 @@ var httpObserver = { 11: 'xmlhttprequest', 12: 'object', 14: 'font', - 15: 'media', + 15: 'image', 16: 'websocket', 21: 'image' }, @@ -1875,7 +1875,6 @@ var httpObserver = { var onBeforeRequest = vAPI.net.onBeforeRequest; if ( onBeforeRequest.types === null || onBeforeRequest.types.has(type) ) { var result = onBeforeRequest.callback({ - hostname: URI.asciiHost, parentFrameId: type === 'main_frame' ? -1 : 0, tabId: tabId, type: type, @@ -1891,7 +1890,6 @@ var httpObserver = { if ( onBeforeSendHeaders.types === null || onBeforeSendHeaders.types.has(type) ) { var requestHeaders = httpRequestHeadersFactory(channel); onBeforeSendHeaders.callback({ - hostname: URI.asciiHost, parentFrameId: type === 'main_frame' ? -1 : 0, requestHeaders: requestHeaders, tabId: tabId, @@ -2009,7 +2007,6 @@ var httpObserver = { } result = vAPI.net.onHeadersReceived.callback({ - hostname: URI.asciiHost, parentFrameId: type === 'main_frame' ? -1 : 0, responseHeaders: result ? [{name: topic, value: result}] : [], tabId: channelData[0], @@ -2506,14 +2503,15 @@ vAPI.toolbarButton = { toolbarButton.parentNode.removeChild(toolbarButton); } } - if ( sss === null ) { - return; - } - if ( sss.sheetRegistered(styleSheetUri, sss.AUTHOR_SHEET) ) { - sss.unregisterSheet(styleSheetUri, sss.AUTHOR_SHEET); + + if ( styleSheetUri !== null ) { + var sss = Cc["@mozilla.org/content/style-sheet-service;1"] + .getService(Ci.nsIStyleSheetService); + if ( sss.sheetRegistered(styleSheetUri, sss.AUTHOR_SHEET) ) { + sss.unregisterSheet(styleSheetUri, sss.AUTHOR_SHEET); + } + styleSheetUri = null; } - sss = null; - styleSheetUri = null; vAPI.messaging.globalMessageManager.removeMessageListener( location.host + ':closePopup', diff --git a/src/js/traffic.js b/src/js/traffic.js index 4ceb58b..7dc36e9 100644 --- a/src/js/traffic.js +++ b/src/js/traffic.js @@ -1,7 +1,7 @@ /******************************************************************************* - µMatrix - a Chromium browser extension to black/white list requests. - Copyright (C) 2014 Raymond Hill + uMatrix - a Chromium browser extension to black/white list 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 @@ -22,14 +22,14 @@ /* global chrome, µMatrix */ /* jshint boss: true */ +'use strict'; + /******************************************************************************/ // Start isolation from global scope µMatrix.webRequest = (function() { -'use strict'; - /******************************************************************************/ // Intercept and filter web requests according to white and black lists. @@ -37,6 +37,7 @@ var onBeforeRootFrameRequestHandler = function(details) { var µm = µMatrix; var requestURL = details.url; + var requestHostname = µm.URI.hostnameFromURI(requestURL); var tabId = details.tabId; µm.tabContextManager.push(tabId, requestURL); @@ -45,7 +46,7 @@ var onBeforeRootFrameRequestHandler = function(details) { var rootHostname = tabContext.rootHostname; // Disallow request as per matrix? - var block = µm.mustBlock(rootHostname, details.hostname, 'doc'); + var block = µm.mustBlock(rootHostname, requestHostname, 'doc'); var pageStore = µm.pageStoreFromTabId(tabId); pageStore.recordRequest('doc', requestURL, block); @@ -61,7 +62,7 @@ var onBeforeRootFrameRequestHandler = function(details) { // Blocked var query = btoa(JSON.stringify({ url: requestURL, - hn: details.hostname, + hn: requestHostname, why: '?' })); @@ -75,11 +76,12 @@ var onBeforeRootFrameRequestHandler = function(details) { // Intercept and filter web requests according to white and black lists. var onBeforeRequestHandler = function(details) { - var µm = µMatrix; + var µm = µMatrix, + µmuri = µm.URI; // rhill 2014-02-17: Ignore 'filesystem:': this can happen when listening // to 'chrome-extension://'. - var requestScheme = µm.URI.schemeFromURI(details.url); + var requestScheme = µmuri.schemeFromURI(details.url); if ( requestScheme === 'filesystem' ) { return; } @@ -118,7 +120,7 @@ var onBeforeRequestHandler = function(details) { var block = false; if ( tabContext.secure && - µm.URI.isSecureScheme(requestScheme) === false && + µmuri.isSecureScheme(requestScheme) === false && µm.tMatrix.evaluateSwitchZ('https-strict', rootHostname) ) { block = true; @@ -126,7 +128,7 @@ var onBeforeRequestHandler = function(details) { // Disallow request as per temporary matrix? if ( block === false ) { - block = µm.mustBlock(rootHostname, details.hostname, requestType); + block = µm.mustBlock(rootHostname, µmuri.hostnameFromURI(requestURL), requestType); } // Record request. @@ -208,15 +210,16 @@ var onBeforeSendHeadersHandler = function(details) { // If we reach this point, request is not blocked, so what is left to do // is to sanitize headers. + var requestHostname = µm.URI.hostnameFromURI(requestURL); - if ( µm.mustBlock(pageStore.pageHostname, details.hostname, 'cookie') ) { + if ( µm.mustBlock(pageStore.pageHostname, requestHostname, 'cookie') ) { if ( details.requestHeaders.setHeader('cookie', '') ) { µm.cookieHeaderFoiledCounter++; } } if ( µm.tMatrix.evaluateSwitchZ('referrer-spoof', pageStore.pageHostname) ) { - foilRefererHeaders(µm, details.hostname, details); + foilRefererHeaders(µm, requestHostname, details); } if ( µm.tMatrix.evaluateSwitchZ('ua-spoof', pageStore.pageHostname) ) { @@ -315,7 +318,7 @@ var onHeadersReceived = function(details) { return; } - if ( µm.mustAllow(tabContext.rootHostname, details.hostname, 'script') ) { + if ( µm.mustAllow(tabContext.rootHostname, µm.URI.hostnameFromURI(requestURL), 'script') ) { return; } diff --git a/src/js/uritools.js b/src/js/uritools.js index cff07c0..8f1c55f 100644 --- a/src/js/uritools.js +++ b/src/js/uritools.js @@ -1,7 +1,7 @@ /******************************************************************************* - µMatrix - a Chromium browser extension to black/white list requests. - Copyright (C) 2014 Raymond Hill + uMatrix - a Chromium browser extension to black/white list 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 @@ -21,6 +21,8 @@ /* global µMatrix, publicSuffixList */ +'use strict'; + /******************************************************************************* RFC 3986 as reference: http://tools.ietf.org/html/rfc3986#appendix-A @@ -33,8 +35,6 @@ Naming convention from https://en.wikipedia.org/wiki/URI_scheme#Examples µMatrix.URI = (function() { -'use strict'; - /******************************************************************************/ // Favorite regex tool: http://regex101.com/ @@ -50,6 +50,7 @@ var reRFC3986 = /^([^:\/?#]+:)?(\/\/[^\/?#]*)?([^?#]*)(\?[^#]*)?(#.*)?/; var reSchemeFromURI = /^[^:\/?#]+:/; var reAuthorityFromURI = /^(?:[^:\/?#]+:)?(\/\/[^\/?#]+)/; var reCommonHostnameFromURL = /^https?:\/\/([0-9a-z_][0-9a-z._-]*[0-9a-z])\//; +var rePathFromURI = /^(?:[^:\/?#]+:)?(?:\/\/[^\/?#]*)?([^?#]*)/; // These are to parse authority field, not parsed by above official regex // IPv6 is seen as an exception: a non-compatible IPv6 is first tried, and @@ -311,7 +312,14 @@ var psl = publicSuffixList; /******************************************************************************/ -// Trying to alleviate the worries of looking up too often the domain name from +URI.pathFromURI = function(uri) { + var matches = rePathFromURI.exec(uri); + return matches !== null ? matches[1] : ''; + }; + +/******************************************************************************/ + + // Trying to alleviate the worries of looking up too often the domain name from // a hostname. With a cache, uBlock benefits given that it deals with a // specific set of hostnames within a narrow time span -- in other words, I // believe probability of cache hit are high in uBlock.