Skip to content
This repository has been archived by the owner on Jul 21, 2021. It is now read-only.

Commit

Permalink
Add ability to block early at launch; adapt to changes in Chromium 72+
Browse files Browse the repository at this point in the history
Related issues:

- "Requests bypass uMatrix on Firefox start"
  <uBlockOrigin/uMatrix-issues#69>

  Using same approach as with uBO:
  https://github.com/gorhill/uBloc/commit/41548be6be35

  `suspendTabsUntilReady` advanced setting added to "More" pane,
  useful only for Chromium -- the blocking of early network
  requests is enforced unconditionally on Firefox (because it
  supports returning Promises from webRequest handlers).

- "Cookies leaking temporarily"
  <uBlockOrigin/uMatrix-issues#74>

  Changes in the webRequest API in Chromium 72+ caused uMatrix
  to fail to process `Cookie` and `Referer` headers on that
  platform.
  • Loading branch information
gorhill committed Jan 1, 2019
1 parent 3cc56af commit fb94c85
Show file tree
Hide file tree
Showing 13 changed files with 623 additions and 460 deletions.
234 changes: 0 additions & 234 deletions platform/chromium/polyfill.js

This file was deleted.

133 changes: 51 additions & 82 deletions platform/chromium/vapi-background.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,90 +504,59 @@ vAPI.messaging.broadcast = function(message) {
/******************************************************************************/
/******************************************************************************/

vAPI.net = {};

/******************************************************************************/

vAPI.net.registerListeners = function() {
var µm = µMatrix;

// Normalizing request types
// >>>>>>>>
var extToTypeMap = new Map([
['eot','font'],['otf','font'],['svg','font'],['ttf','font'],['woff','font'],['woff2','font'],
['mp3','media'],['mp4','media'],['webm','media'],
['gif','image'],['ico','image'],['jpeg','image'],['jpg','image'],['png','image'],['webp','image']
]);

var normalizeRequestDetails = function(details) {
if (
details.tabId === -1 &&
details.documentUrl === undefined &&
details.initiator !== undefined
) {
details.documentUrl = details.initiator;
}

// The rest of the function code is to normalize request type
if ( details.type !== 'other' ) { return; }

// Try to map known "extension" part of URL to request type.
var path = µm.URI.pathFromURI(details.url),
pos = path.indexOf('.', path.length - 6);
if ( pos !== -1 ) {
var type = extToTypeMap.get(path.slice(pos + 1));
if ( type !== undefined ) {
details.type = type;
vAPI.net = {
listenerMap: new WeakMap(),
// legacy Chromium understands only these network request types.
validTypes: (function() {
let types = new Set([
'main_frame',
'sub_frame',
'stylesheet',
'script',
'image',
'object',
'xmlhttprequest',
'other'
]);
let wrrt = browser.webRequest.ResourceType;
if ( wrrt instanceof Object ) {
for ( let typeKey in wrrt ) {
if ( wrrt.hasOwnProperty(typeKey) ) {
types.add(wrrt[typeKey]);
}
}
}
};
// <<<<<<<<
// End of: Normalizing request types

// Network event handlers
// >>>>>>>>
var onBeforeRequestClient = this.onBeforeRequest.callback;
chrome.webRequest.onBeforeRequest.addListener(
function(details) {
normalizeRequestDetails(details);
return onBeforeRequestClient(details);
},
{
'urls': this.onBeforeRequest.urls || [ '<all_urls>' ],
'types': this.onBeforeRequest.types || undefined
},
this.onBeforeRequest.extra
);

var onBeforeSendHeadersClient = this.onBeforeSendHeaders.callback;
var onBeforeSendHeaders = function(details) {
normalizeRequestDetails(details);
return onBeforeSendHeadersClient(details);
};
chrome.webRequest.onBeforeSendHeaders.addListener(
onBeforeSendHeaders,
{
'urls': this.onBeforeSendHeaders.urls || [ '<all_urls>' ],
'types': this.onBeforeSendHeaders.types || undefined
},
this.onBeforeSendHeaders.extra
);

var onHeadersReceivedClient = this.onHeadersReceived.callback;
var onHeadersReceived = function(details) {
normalizeRequestDetails(details);
return onHeadersReceivedClient(details);
};
chrome.webRequest.onHeadersReceived.addListener(
onHeadersReceived,
{
'urls': this.onHeadersReceived.urls || [ '<all_urls>' ],
'types': this.onHeadersReceived.types || undefined
},
this.onHeadersReceived.extra
);
// <<<<<<<<
// End of: Network event handlers
return types;
})(),
denormalizeFilters: null,
normalizeDetails: null,
addListener: function(which, clientListener, filters, options) {
if ( typeof this.denormalizeFilters === 'function' ) {
filters = this.denormalizeFilters(filters);
}
let actualListener;
if ( typeof this.normalizeDetails === 'function' ) {
actualListener = function(details) {
vAPI.net.normalizeDetails(details);
return clientListener(details);
};
this.listenerMap.set(clientListener, actualListener);
}
browser.webRequest[which].addListener(
actualListener || clientListener,
filters,
options
);
},
removeListener: function(which, clientListener) {
let actualListener = this.listenerMap.get(clientListener);
if ( actualListener !== undefined ) {
this.listenerMap.delete(clientListener);
}
browser.webRequest[which].removeListener(
actualListener || clientListener
);
},
};

/******************************************************************************/
Expand Down
Loading

2 comments on commit fb94c85

@mikhaelkh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct link - gorhill/uBlock@41548be6be35
What should I do if I use both uBO and uMatrix - should I enable suspendTabsUntilReady in them both?

@uBlock-user
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should I do if I use both uBO and uMatrix - should I enable suspendTabsUntilReady in them both?

Yes, that's how I use it.

Please sign in to comment.