2017-09-02 04:11:33 -06:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-02-26 11:59:16 -07:00
|
|
|
Copyright (C) 2017-2018 Raymond Hill
|
2017-09-02 04:11:33 -06:00
|
|
|
|
|
|
|
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
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
// For background page
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-10-18 13:00:22 -06:00
|
|
|
vAPI.net = {
|
|
|
|
onBeforeRequest: {},
|
|
|
|
onBeforeMaybeSpuriousCSPReport: {},
|
|
|
|
onHeadersReceived: {},
|
2017-12-28 11:49:02 -07:00
|
|
|
nativeCSPReportFiltering: true,
|
|
|
|
webRequest: browser.webRequest,
|
|
|
|
canFilterResponseBody:
|
|
|
|
typeof browser.webRequest === 'object' &&
|
|
|
|
typeof browser.webRequest.filterResponseData === 'function'
|
2017-10-18 13:00:22 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2017-09-02 04:11:33 -06:00
|
|
|
|
|
|
|
vAPI.net.registerListeners = function() {
|
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2950
|
|
|
|
// Firefox 55 does not normalize URLs to ASCII, uBO must do this itself.
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=945240
|
2018-04-04 10:42:01 -06:00
|
|
|
let evalMustPunycode = function() {
|
|
|
|
return /^Mozilla-Firefox-5[0-6]/.test(vAPI.webextFlavor);
|
|
|
|
};
|
|
|
|
|
|
|
|
let mustPunycode = evalMustPunycode();
|
|
|
|
|
|
|
|
// The real actual webextFlavor value may not be set in stone, so listen
|
|
|
|
// for possible future changes.
|
|
|
|
window.addEventListener('webextFlavor', function() {
|
|
|
|
mustPunycode = evalMustPunycode();
|
|
|
|
}, { once: true });
|
2017-09-02 04:11:33 -06:00
|
|
|
|
2018-03-31 16:47:56 -06:00
|
|
|
let wrApi = browser.webRequest;
|
2017-09-02 04:11:33 -06:00
|
|
|
|
|
|
|
// legacy Chromium understands only these network request types.
|
2018-03-31 16:47:56 -06:00
|
|
|
let validTypes = new Set([
|
2018-01-16 07:13:51 -07:00
|
|
|
'image',
|
|
|
|
'main_frame',
|
|
|
|
'object',
|
|
|
|
'other',
|
|
|
|
'script',
|
|
|
|
'stylesheet',
|
|
|
|
'sub_frame',
|
|
|
|
'xmlhttprequest',
|
|
|
|
]);
|
2017-09-02 04:11:33 -06:00
|
|
|
// modern Chromium/WebExtensions: more types available.
|
|
|
|
if ( wrApi.ResourceType ) {
|
2017-09-02 07:57:44 -06:00
|
|
|
for ( let typeKey in wrApi.ResourceType ) {
|
|
|
|
if ( wrApi.ResourceType.hasOwnProperty(typeKey) ) {
|
2018-01-16 07:13:51 -07:00
|
|
|
validTypes.add(wrApi.ResourceType[typeKey]);
|
2017-09-02 04:11:33 -06:00
|
|
|
}
|
2017-09-02 07:57:44 -06:00
|
|
|
}
|
2017-09-02 04:11:33 -06:00
|
|
|
}
|
|
|
|
|
2018-03-31 16:47:56 -06:00
|
|
|
let denormalizeTypes = function(aa) {
|
2017-09-02 04:11:33 -06:00
|
|
|
if ( aa.length === 0 ) {
|
2018-01-16 07:13:51 -07:00
|
|
|
return Array.from(validTypes);
|
2017-09-02 04:11:33 -06:00
|
|
|
}
|
2018-03-31 16:47:56 -06:00
|
|
|
let out = new Set(),
|
2018-01-16 07:13:51 -07:00
|
|
|
i = aa.length;
|
2017-09-02 04:11:33 -06:00
|
|
|
while ( i-- ) {
|
2018-03-31 16:47:56 -06:00
|
|
|
let type = aa[i];
|
2018-01-16 07:13:51 -07:00
|
|
|
if ( validTypes.has(type) ) {
|
|
|
|
out.add(type);
|
2017-09-02 04:11:33 -06:00
|
|
|
}
|
2018-01-16 07:13:51 -07:00
|
|
|
if ( type === 'image' && validTypes.has('imageset') ) {
|
|
|
|
out.add('imageset');
|
2017-09-02 04:11:33 -06:00
|
|
|
}
|
|
|
|
}
|
2018-01-16 07:13:51 -07:00
|
|
|
return Array.from(out);
|
2017-09-02 04:11:33 -06:00
|
|
|
};
|
|
|
|
|
2018-03-31 16:47:56 -06:00
|
|
|
let punycode = self.punycode;
|
|
|
|
let reAsciiHostname = /^https?:\/\/[0-9a-z_.:@-]+[/?#]/;
|
|
|
|
let parsedURL = new URL('about:blank');
|
2017-09-02 04:11:33 -06:00
|
|
|
|
2018-03-31 16:47:56 -06:00
|
|
|
let normalizeRequestDetails = function(details) {
|
2018-02-26 11:59:16 -07:00
|
|
|
if (
|
|
|
|
details.tabId === vAPI.noTabId &&
|
2018-04-02 07:10:38 -06:00
|
|
|
typeof details.documentUrl === 'string'
|
2018-02-26 11:59:16 -07:00
|
|
|
) {
|
|
|
|
details.tabId = vAPI.anyTabId;
|
|
|
|
}
|
2017-09-02 04:11:33 -06:00
|
|
|
|
2017-09-13 11:08:31 -06:00
|
|
|
if ( mustPunycode && !reAsciiHostname.test(details.url) ) {
|
2017-09-02 04:11:33 -06:00
|
|
|
parsedURL.href = details.url;
|
|
|
|
details.url = details.url.replace(
|
|
|
|
parsedURL.hostname,
|
|
|
|
punycode.toASCII(parsedURL.hostname)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-31 16:47:56 -06:00
|
|
|
let type = details.type;
|
2017-09-02 04:11:33 -06:00
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1493
|
|
|
|
// Chromium 49+/WebExtensions support a new request type: `ping`,
|
|
|
|
// which is fired as a result of using `navigator.sendBeacon`.
|
|
|
|
if ( type === 'ping' ) {
|
|
|
|
details.type = 'beacon';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( type === 'imageset' ) {
|
|
|
|
details.type = 'image';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-31 16:47:56 -06:00
|
|
|
// This is to work around Firefox's inability to redirect xmlhttprequest
|
2018-04-01 04:58:20 -06:00
|
|
|
// requests to data: URIs.
|
2018-03-31 16:47:56 -06:00
|
|
|
let pseudoRedirector = {
|
|
|
|
filters: new Map(),
|
|
|
|
reDataURI: /^data:\w+\/\w+;base64,/,
|
|
|
|
dec: null,
|
|
|
|
init: function() {
|
|
|
|
this.dec = new Uint8Array(128);
|
|
|
|
let s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
|
|
for ( let i = 0, n = s.length; i < n; i++ ) {
|
|
|
|
this.dec[s.charCodeAt(i)] = i;
|
|
|
|
}
|
2018-04-01 04:58:20 -06:00
|
|
|
return this.dec;
|
2018-03-31 16:47:56 -06:00
|
|
|
},
|
|
|
|
start: function(requestId, redirectUrl) {
|
|
|
|
let match = this.reDataURI.exec(redirectUrl);
|
|
|
|
if ( match === null ) { return redirectUrl; }
|
|
|
|
let s = redirectUrl.slice(match[0].length).replace(/=*$/, '');
|
|
|
|
let f = browser.webRequest.filterResponseData(requestId);
|
|
|
|
f.onstop = this.done;
|
|
|
|
f.onerror = this.disconnect;
|
|
|
|
this.filters.set(f, s);
|
|
|
|
},
|
|
|
|
done: function() {
|
|
|
|
let pr = pseudoRedirector;
|
|
|
|
let bufIn = pr.filters.get(this);
|
|
|
|
if ( bufIn === undefined ) { return pr.disconnect(this); }
|
2018-04-01 04:58:20 -06:00
|
|
|
let dec = pr.dec || pr.init();
|
2018-03-31 16:47:56 -06:00
|
|
|
let sizeIn = bufIn.length;
|
|
|
|
let iIn = 0;
|
|
|
|
let sizeOut = sizeIn * 6 >>> 3;
|
|
|
|
let bufOut = new Uint8Array(sizeOut);
|
|
|
|
let iOut = 0;
|
|
|
|
let n = sizeIn & ~3;
|
|
|
|
while ( iIn < n ) {
|
|
|
|
let b0 = dec[bufIn.charCodeAt(iIn++)];
|
|
|
|
let b1 = dec[bufIn.charCodeAt(iIn++)];
|
|
|
|
let b2 = dec[bufIn.charCodeAt(iIn++)];
|
|
|
|
let b3 = dec[bufIn.charCodeAt(iIn++)];
|
|
|
|
bufOut[iOut++] = (b0 << 2) & 0xFC | (b1 >>> 4);
|
|
|
|
bufOut[iOut++] = (b1 << 4) & 0xF0 | (b2 >>> 2);
|
|
|
|
bufOut[iOut++] = (b2 << 6) & 0xC0 | b3;
|
|
|
|
}
|
2018-03-31 17:41:59 -06:00
|
|
|
if ( iIn !== sizeIn ) {
|
2018-03-31 16:47:56 -06:00
|
|
|
let b0 = dec[bufIn.charCodeAt(iIn++)];
|
|
|
|
let b1 = dec[bufIn.charCodeAt(iIn++)];
|
2018-03-31 17:41:59 -06:00
|
|
|
bufOut[iOut++] = (b0 << 2) & 0xFC | (b1 >>> 4);
|
|
|
|
if ( iIn !== sizeIn ) {
|
2018-03-31 16:47:56 -06:00
|
|
|
let b2 = dec[bufIn.charCodeAt(iIn++)];
|
|
|
|
bufOut[iOut++] = (b1 << 4) & 0xF0 | (b2 >>> 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.write(bufOut);
|
|
|
|
pr.disconnect(this);
|
|
|
|
},
|
|
|
|
disconnect: function(f) {
|
|
|
|
let pr = pseudoRedirector;
|
|
|
|
pr.filters.delete(f);
|
|
|
|
f.disconnect();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let onBeforeRequestClient = this.onBeforeRequest.callback;
|
|
|
|
let onBeforeRequest = function(details) {
|
2017-09-02 04:11:33 -06:00
|
|
|
normalizeRequestDetails(details);
|
2018-04-02 02:47:24 -06:00
|
|
|
return onBeforeRequestClient(details);
|
2017-09-02 04:11:33 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
if ( onBeforeRequest ) {
|
|
|
|
let urls = this.onBeforeRequest.urls || ['<all_urls>'];
|
|
|
|
let types = this.onBeforeRequest.types || undefined;
|
|
|
|
if (
|
2018-01-16 07:13:51 -07:00
|
|
|
(validTypes.has('websocket')) &&
|
2017-09-02 04:11:33 -06:00
|
|
|
(types === undefined || types.indexOf('websocket') !== -1) &&
|
|
|
|
(urls.indexOf('<all_urls>') === -1)
|
|
|
|
) {
|
|
|
|
if ( urls.indexOf('ws://*/*') === -1 ) {
|
|
|
|
urls.push('ws://*/*');
|
|
|
|
}
|
|
|
|
if ( urls.indexOf('wss://*/*') === -1 ) {
|
|
|
|
urls.push('wss://*/*');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
wrApi.onBeforeRequest.addListener(
|
|
|
|
onBeforeRequest,
|
|
|
|
{ urls: urls, types: types },
|
|
|
|
this.onBeforeRequest.extra
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-10-18 13:00:22 -06:00
|
|
|
// https://github.com/gorhill/uBlock/issues/3140
|
|
|
|
if ( typeof this.onBeforeMaybeSpuriousCSPReport.callback === 'function' ) {
|
|
|
|
wrApi.onBeforeRequest.addListener(
|
|
|
|
this.onBeforeMaybeSpuriousCSPReport.callback,
|
|
|
|
{
|
|
|
|
urls: [ 'http://*/*', 'https://*/*' ],
|
|
|
|
types: [ 'csp_report' ]
|
|
|
|
},
|
|
|
|
[ 'blocking', 'requestBody' ]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-31 16:47:56 -06:00
|
|
|
let onHeadersReceivedClient = this.onHeadersReceived.callback,
|
2017-10-18 13:00:22 -06:00
|
|
|
onHeadersReceivedClientTypes = this.onHeadersReceived.types.slice(0),
|
|
|
|
onHeadersReceivedTypes = denormalizeTypes(onHeadersReceivedClientTypes);
|
2018-03-31 16:47:56 -06:00
|
|
|
let onHeadersReceived = function(details) {
|
2017-10-18 13:00:22 -06:00
|
|
|
normalizeRequestDetails(details);
|
|
|
|
if (
|
|
|
|
onHeadersReceivedClientTypes.length !== 0 &&
|
|
|
|
onHeadersReceivedClientTypes.indexOf(details.type) === -1
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return onHeadersReceivedClient(details);
|
|
|
|
};
|
|
|
|
|
2017-09-02 04:11:33 -06:00
|
|
|
if ( onHeadersReceived ) {
|
|
|
|
let urls = this.onHeadersReceived.urls || ['<all_urls>'];
|
|
|
|
let types = onHeadersReceivedTypes;
|
|
|
|
wrApi.onHeadersReceived.addListener(
|
|
|
|
onHeadersReceived,
|
|
|
|
{ urls: urls, types: types },
|
|
|
|
this.onHeadersReceived.extra
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|