uBlock/platform/webext/vapi-webrequest.js

279 lines
9.4 KiB
JavaScript
Raw Normal View History

2017-09-02 12:11:33 +02:00
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
2018-02-26 19:59:16 +01:00
Copyright (C) 2017-2018 Raymond Hill
2017-09-02 12:11:33 +02: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 21:00:22 +02:00
vAPI.net = {
onBeforeRequest: {},
onBeforeMaybeSpuriousCSPReport: {},
onHeadersReceived: {},
nativeCSPReportFiltering: true,
webRequest: browser.webRequest,
canFilterResponseBody:
typeof browser.webRequest === 'object' &&
typeof browser.webRequest.filterResponseData === 'function'
2017-10-18 21:00:22 +02:00
};
/******************************************************************************/
2017-09-02 12:11:33 +02: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
let mustPunycode = false;
2017-09-02 12:11:33 +02:00
(function() {
if (
typeof browser === 'object' &&
browser !== null &&
browser.runtime instanceof Object &&
typeof browser.runtime.getBrowserInfo === 'function'
) {
browser.runtime.getBrowserInfo().then(info => {
mustPunycode = info.name === 'Firefox' &&
/^5[0-6]\./.test(info.version);
2017-09-02 12:11:33 +02:00
});
}
})();
let wrApi = browser.webRequest;
2017-09-02 12:11:33 +02:00
// legacy Chromium understands only these network request types.
let validTypes = new Set([
2018-01-16 15:13:51 +01:00
'image',
'main_frame',
'object',
'other',
'script',
'stylesheet',
'sub_frame',
'xmlhttprequest',
]);
2017-09-02 12:11:33 +02:00
// modern Chromium/WebExtensions: more types available.
if ( wrApi.ResourceType ) {
for ( let typeKey in wrApi.ResourceType ) {
if ( wrApi.ResourceType.hasOwnProperty(typeKey) ) {
2018-01-16 15:13:51 +01:00
validTypes.add(wrApi.ResourceType[typeKey]);
2017-09-02 12:11:33 +02:00
}
}
2017-09-02 12:11:33 +02:00
}
let denormalizeTypes = function(aa) {
2017-09-02 12:11:33 +02:00
if ( aa.length === 0 ) {
2018-01-16 15:13:51 +01:00
return Array.from(validTypes);
2017-09-02 12:11:33 +02:00
}
let out = new Set(),
2018-01-16 15:13:51 +01:00
i = aa.length;
2017-09-02 12:11:33 +02:00
while ( i-- ) {
let type = aa[i];
2018-01-16 15:13:51 +01:00
if ( validTypes.has(type) ) {
out.add(type);
2017-09-02 12:11:33 +02:00
}
2018-01-16 15:13:51 +01:00
if ( type === 'image' && validTypes.has('imageset') ) {
out.add('imageset');
2017-09-02 12:11:33 +02:00
}
}
2018-01-16 15:13:51 +01:00
return Array.from(out);
2017-09-02 12:11:33 +02:00
};
let punycode = self.punycode;
let reAsciiHostname = /^https?:\/\/[0-9a-z_.:@-]+[/?#]/;
let reNetworkURI = /^(?:ftps?|https?|wss?)/;
let parsedURL = new URL('about:blank');
2017-09-02 12:11:33 +02:00
let normalizeRequestDetails = function(details) {
2018-02-26 19:59:16 +01:00
if (
details.tabId === vAPI.noTabId &&
reNetworkURI.test(details.documentUrl)
) {
details.tabId = vAPI.anyTabId;
}
2017-09-02 12:11:33 +02:00
if ( mustPunycode && !reAsciiHostname.test(details.url) ) {
2017-09-02 12:11:33 +02:00
parsedURL.href = details.url;
details.url = details.url.replace(
parsedURL.hostname,
punycode.toASCII(parsedURL.hostname)
);
}
let type = details.type;
2017-09-02 12:11:33 +02: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;
}
};
// This is to work around Firefox's inability to redirect xmlhttprequest
// to data: URI.
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;
}
},
start: function(requestId, redirectUrl) {
if ( this.dec === null ) { this.init(); }
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); }
let dec = pr.dec;
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;
}
if ( n < sizeIn ) {
let b0 = dec[bufIn.charCodeAt(iIn++)];
let b1 = dec[bufIn.charCodeAt(iIn++)];
if ( (sizeIn & 3) === 2 ) {
let b2 = dec[bufIn.charCodeAt(iIn++)];
bufOut[iOut++] = (b0 << 2) & 0xFC | (b1 >>> 4);
bufOut[iOut++] = (b1 << 4) & 0xF0 | (b2 >>> 2);
} else {
bufOut[iOut++] = (b0 << 2) & 0xFC | (b1 >>> 4);
}
}
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 12:11:33 +02:00
normalizeRequestDetails(details);
let r = onBeforeRequestClient(details);
if (
r !== undefined &&
r.redirectUrl !== undefined &&
details.type === 'xmlhttprequest'
) {
r.redirectUrl = pseudoRedirector.start(
details.requestId,
r.redirectUrl
);
}
return r;
2017-09-02 12:11:33 +02:00
};
if ( onBeforeRequest ) {
let urls = this.onBeforeRequest.urls || ['<all_urls>'];
let types = this.onBeforeRequest.types || undefined;
if (
2018-01-16 15:13:51 +01:00
(validTypes.has('websocket')) &&
2017-09-02 12:11:33 +02: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 21:00:22 +02: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' ]
);
}
let onHeadersReceivedClient = this.onHeadersReceived.callback,
2017-10-18 21:00:22 +02:00
onHeadersReceivedClientTypes = this.onHeadersReceived.types.slice(0),
onHeadersReceivedTypes = denormalizeTypes(onHeadersReceivedClientTypes);
let onHeadersReceived = function(details) {
2017-10-18 21:00:22 +02:00
normalizeRequestDetails(details);
if (
onHeadersReceivedClientTypes.length !== 0 &&
onHeadersReceivedClientTypes.indexOf(details.type) === -1
) {
return;
}
return onHeadersReceivedClient(details);
};
2017-09-02 12:11:33 +02:00
if ( onHeadersReceived ) {
let urls = this.onHeadersReceived.urls || ['<all_urls>'];
let types = onHeadersReceivedTypes;
wrApi.onHeadersReceived.addListener(
onHeadersReceived,
{ urls: urls, types: types },
this.onHeadersReceived.extra
);
}
};
/******************************************************************************/