fix #597 + code review + merge media and image

pull/2/head
gorhill 8 years ago
parent 6df6794f7a
commit 83b342d4e4

@ -1,7 +1,7 @@
/******************************************************************************* /*******************************************************************************
µMatrix - a browser extension to block requests. uMatrix - a browser extension to block requests.
Copyright (C) 2014 The uBlock authors Copyright (C) 2014-2016 The uBlock authors
This program is free software: you can redistribute it and/or modify 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 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 You should have received a copy of the GNU General Public License
along with this program. If not, see {http://www.gnu.org/licenses/}. 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 */ /* global self, µMatrix */
// For background page // For background page
'use strict';
/******************************************************************************/ /******************************************************************************/
(function() { (function() {
'use strict';
/******************************************************************************/ /******************************************************************************/
var vAPI = self.vAPI = self.vAPI || {}; var vAPI = self.vAPI = self.vAPI || {};
@ -584,7 +584,6 @@ vAPI.net = {};
vAPI.net.registerListeners = function() { vAPI.net.registerListeners = function() {
var µm = µMatrix; var µm = µMatrix;
var µmuri = µm.URI;
var httpRequestHeadersJunkyard = []; var httpRequestHeadersJunkyard = [];
// Abstraction layer to deal with request headers // Abstraction layer to deal with request headers
@ -659,10 +658,7 @@ vAPI.net.registerListeners = function() {
// Normalizing request types // Normalizing request types
// >>>>>>>> // >>>>>>>>
var normalizeRequestDetails = function(details) { var normalizeRequestDetails = function(details) {
µmuri.set(details.url);
details.tabId = details.tabId.toString(); details.tabId = details.tabId.toString();
details.hostname = µmuri.hostnameFromURI(details.url);
// The rest of the function code is to normalize request type // The rest of the function code is to normalize request type
if ( details.type !== 'other' ) { if ( details.type !== 'other' ) {
@ -676,8 +672,8 @@ vAPI.net.registerListeners = function() {
} }
} }
var tail = µmuri.path.slice(-6); var path = µm.URI.pathFromURI(details.url),
var pos = tail.lastIndexOf('.'); pos = path.indexOf('.', path.length - 6);
// https://github.com/chrisaljoudi/uBlock/issues/862 // https://github.com/chrisaljoudi/uBlock/issues/862
// If no transposition possible, transpose to `object` as per // If no transposition possible, transpose to `object` as per
@ -686,14 +682,14 @@ vAPI.net.registerListeners = function() {
return; return;
} }
var ext = tail.slice(pos) + '.'; var needle = path.slice(pos) + '.';
if ( '.eot.ttf.otf.svg.woff.woff2.'.indexOf(ext) !== -1 ) { if ( '.eot.ttf.otf.svg.woff.woff2.'.indexOf(needle) !== -1 ) {
details.type = 'font'; details.type = 'font';
return; return;
} }
// Still need this because often behind-the-scene requests are wrongly // Still need this because often behind-the-scene requests are wrongly
// categorized as 'other' // 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'; details.type = 'image';
return; return;
} }

@ -24,12 +24,12 @@
// For background page // For background page
'use strict';
/******************************************************************************/ /******************************************************************************/
(function() { (function() {
'use strict';
/******************************************************************************/ /******************************************************************************/
// Useful links // Useful links
@ -1714,7 +1714,7 @@ var httpObserver = {
11: 'xmlhttprequest', 11: 'xmlhttprequest',
12: 'object', 12: 'object',
14: 'font', 14: 'font',
15: 'media', 15: 'image',
16: 'websocket', 16: 'websocket',
21: 'image' 21: 'image'
}, },
@ -1875,7 +1875,6 @@ var httpObserver = {
var onBeforeRequest = vAPI.net.onBeforeRequest; var onBeforeRequest = vAPI.net.onBeforeRequest;
if ( onBeforeRequest.types === null || onBeforeRequest.types.has(type) ) { if ( onBeforeRequest.types === null || onBeforeRequest.types.has(type) ) {
var result = onBeforeRequest.callback({ var result = onBeforeRequest.callback({
hostname: URI.asciiHost,
parentFrameId: type === 'main_frame' ? -1 : 0, parentFrameId: type === 'main_frame' ? -1 : 0,
tabId: tabId, tabId: tabId,
type: type, type: type,
@ -1891,7 +1890,6 @@ var httpObserver = {
if ( onBeforeSendHeaders.types === null || onBeforeSendHeaders.types.has(type) ) { if ( onBeforeSendHeaders.types === null || onBeforeSendHeaders.types.has(type) ) {
var requestHeaders = httpRequestHeadersFactory(channel); var requestHeaders = httpRequestHeadersFactory(channel);
onBeforeSendHeaders.callback({ onBeforeSendHeaders.callback({
hostname: URI.asciiHost,
parentFrameId: type === 'main_frame' ? -1 : 0, parentFrameId: type === 'main_frame' ? -1 : 0,
requestHeaders: requestHeaders, requestHeaders: requestHeaders,
tabId: tabId, tabId: tabId,
@ -2009,7 +2007,6 @@ var httpObserver = {
} }
result = vAPI.net.onHeadersReceived.callback({ result = vAPI.net.onHeadersReceived.callback({
hostname: URI.asciiHost,
parentFrameId: type === 'main_frame' ? -1 : 0, parentFrameId: type === 'main_frame' ? -1 : 0,
responseHeaders: result ? [{name: topic, value: result}] : [], responseHeaders: result ? [{name: topic, value: result}] : [],
tabId: channelData[0], tabId: channelData[0],
@ -2506,14 +2503,15 @@ vAPI.toolbarButton = {
toolbarButton.parentNode.removeChild(toolbarButton); toolbarButton.parentNode.removeChild(toolbarButton);
} }
} }
if ( sss === null ) {
return; if ( styleSheetUri !== null ) {
} var sss = Cc["@mozilla.org/content/style-sheet-service;1"]
.getService(Ci.nsIStyleSheetService);
if ( sss.sheetRegistered(styleSheetUri, sss.AUTHOR_SHEET) ) { if ( sss.sheetRegistered(styleSheetUri, sss.AUTHOR_SHEET) ) {
sss.unregisterSheet(styleSheetUri, sss.AUTHOR_SHEET); sss.unregisterSheet(styleSheetUri, sss.AUTHOR_SHEET);
} }
sss = null;
styleSheetUri = null; styleSheetUri = null;
}
vAPI.messaging.globalMessageManager.removeMessageListener( vAPI.messaging.globalMessageManager.removeMessageListener(
location.host + ':closePopup', location.host + ':closePopup',

@ -1,7 +1,7 @@
/******************************************************************************* /*******************************************************************************
µMatrix - a Chromium browser extension to black/white list requests. uMatrix - a Chromium browser extension to black/white list requests.
Copyright (C) 2014 Raymond Hill Copyright (C) 2014-2016 Raymond Hill
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -22,14 +22,14 @@
/* global chrome, µMatrix */ /* global chrome, µMatrix */
/* jshint boss: true */ /* jshint boss: true */
'use strict';
/******************************************************************************/ /******************************************************************************/
// Start isolation from global scope // Start isolation from global scope
µMatrix.webRequest = (function() { µMatrix.webRequest = (function() {
'use strict';
/******************************************************************************/ /******************************************************************************/
// Intercept and filter web requests according to white and black lists. // Intercept and filter web requests according to white and black lists.
@ -37,6 +37,7 @@
var onBeforeRootFrameRequestHandler = function(details) { var onBeforeRootFrameRequestHandler = function(details) {
var µm = µMatrix; var µm = µMatrix;
var requestURL = details.url; var requestURL = details.url;
var requestHostname = µm.URI.hostnameFromURI(requestURL);
var tabId = details.tabId; var tabId = details.tabId;
µm.tabContextManager.push(tabId, requestURL); µm.tabContextManager.push(tabId, requestURL);
@ -45,7 +46,7 @@ var onBeforeRootFrameRequestHandler = function(details) {
var rootHostname = tabContext.rootHostname; var rootHostname = tabContext.rootHostname;
// Disallow request as per matrix? // 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); var pageStore = µm.pageStoreFromTabId(tabId);
pageStore.recordRequest('doc', requestURL, block); pageStore.recordRequest('doc', requestURL, block);
@ -61,7 +62,7 @@ var onBeforeRootFrameRequestHandler = function(details) {
// Blocked // Blocked
var query = btoa(JSON.stringify({ var query = btoa(JSON.stringify({
url: requestURL, url: requestURL,
hn: details.hostname, hn: requestHostname,
why: '?' why: '?'
})); }));
@ -75,11 +76,12 @@ var onBeforeRootFrameRequestHandler = function(details) {
// Intercept and filter web requests according to white and black lists. // Intercept and filter web requests according to white and black lists.
var onBeforeRequestHandler = function(details) { var onBeforeRequestHandler = function(details) {
var µm = µMatrix; var µm = µMatrix,
µmuri = µm.URI;
// rhill 2014-02-17: Ignore 'filesystem:': this can happen when listening // rhill 2014-02-17: Ignore 'filesystem:': this can happen when listening
// to 'chrome-extension://'. // to 'chrome-extension://'.
var requestScheme = µm.URI.schemeFromURI(details.url); var requestScheme = µmuri.schemeFromURI(details.url);
if ( requestScheme === 'filesystem' ) { if ( requestScheme === 'filesystem' ) {
return; return;
} }
@ -118,7 +120,7 @@ var onBeforeRequestHandler = function(details) {
var block = false; var block = false;
if ( if (
tabContext.secure && tabContext.secure &&
µm.URI.isSecureScheme(requestScheme) === false && µmuri.isSecureScheme(requestScheme) === false &&
µm.tMatrix.evaluateSwitchZ('https-strict', rootHostname) µm.tMatrix.evaluateSwitchZ('https-strict', rootHostname)
) { ) {
block = true; block = true;
@ -126,7 +128,7 @@ var onBeforeRequestHandler = function(details) {
// Disallow request as per temporary matrix? // Disallow request as per temporary matrix?
if ( block === false ) { if ( block === false ) {
block = µm.mustBlock(rootHostname, details.hostname, requestType); block = µm.mustBlock(rootHostname, µmuri.hostnameFromURI(requestURL), requestType);
} }
// Record request. // 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 // If we reach this point, request is not blocked, so what is left to do
// is to sanitize headers. // 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', '') ) { if ( details.requestHeaders.setHeader('cookie', '') ) {
µm.cookieHeaderFoiledCounter++; µm.cookieHeaderFoiledCounter++;
} }
} }
if ( µm.tMatrix.evaluateSwitchZ('referrer-spoof', pageStore.pageHostname) ) { 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) ) { if ( µm.tMatrix.evaluateSwitchZ('ua-spoof', pageStore.pageHostname) ) {
@ -315,7 +318,7 @@ var onHeadersReceived = function(details) {
return; return;
} }
if ( µm.mustAllow(tabContext.rootHostname, details.hostname, 'script') ) { if ( µm.mustAllow(tabContext.rootHostname, µm.URI.hostnameFromURI(requestURL), 'script') ) {
return; return;
} }

@ -1,7 +1,7 @@
/******************************************************************************* /*******************************************************************************
µMatrix - a Chromium browser extension to black/white list requests. uMatrix - a Chromium browser extension to black/white list requests.
Copyright (C) 2014 Raymond Hill Copyright (C) 2014-2016 Raymond Hill
This program is free software: you can redistribute it and/or modify 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 it under the terms of the GNU General Public License as published by
@ -21,6 +21,8 @@
/* global µMatrix, publicSuffixList */ /* global µMatrix, publicSuffixList */
'use strict';
/******************************************************************************* /*******************************************************************************
RFC 3986 as reference: http://tools.ietf.org/html/rfc3986#appendix-A 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() { µMatrix.URI = (function() {
'use strict';
/******************************************************************************/ /******************************************************************************/
// Favorite regex tool: http://regex101.com/ // Favorite regex tool: http://regex101.com/
@ -50,6 +50,7 @@ var reRFC3986 = /^([^:\/?#]+:)?(\/\/[^\/?#]*)?([^?#]*)(\?[^#]*)?(#.*)?/;
var reSchemeFromURI = /^[^:\/?#]+:/; var reSchemeFromURI = /^[^:\/?#]+:/;
var reAuthorityFromURI = /^(?:[^:\/?#]+:)?(\/\/[^\/?#]+)/; var reAuthorityFromURI = /^(?:[^:\/?#]+:)?(\/\/[^\/?#]+)/;
var reCommonHostnameFromURL = /^https?:\/\/([0-9a-z_][0-9a-z._-]*[0-9a-z])\//; 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 // 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 // IPv6 is seen as an exception: a non-compatible IPv6 is first tried, and
@ -309,6 +310,13 @@ URI.domain = function() {
// somewhere. Cache it. See <https://github.com/gorhill/publicsuffixlist.js>. // somewhere. Cache it. See <https://github.com/gorhill/publicsuffixlist.js>.
var psl = publicSuffixList; var psl = publicSuffixList;
/******************************************************************************/
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 // Trying to alleviate the worries of looking up too often the domain name from

Loading…
Cancel
Save