2017-09-02 12:11:33 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-12-13 18:30:54 +01:00
|
|
|
Copyright (C) 2017-present 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';
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-06-30 16:09:27 +02:00
|
|
|
(( ) => {
|
2019-01-28 22:12:26 +01:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/407
|
|
|
|
if ( vAPI.webextFlavor.soup.has('firefox') === false ) { return; }
|
2017-09-02 12:11:33 +02:00
|
|
|
|
|
|
|
// https://github.com/gorhill/uBlock/issues/2950
|
2018-04-05 13:29:15 +02:00
|
|
|
// Firefox 56 does not normalize URLs to ASCII, uBO must do this itself.
|
2017-09-02 12:11:33 +02:00
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=945240
|
2019-06-30 16:09:27 +02:00
|
|
|
const evalMustPunycode = ( ) => {
|
2018-04-05 13:29:15 +02:00
|
|
|
return vAPI.webextFlavor.soup.has('firefox') &&
|
|
|
|
vAPI.webextFlavor.major < 57;
|
2018-04-04 18:42:01 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
let mustPunycode = evalMustPunycode();
|
|
|
|
|
|
|
|
// The real actual webextFlavor value may not be set in stone, so listen
|
|
|
|
// for possible future changes.
|
2018-12-16 16:51:25 +01:00
|
|
|
window.addEventListener('webextFlavor', ( ) => {
|
2018-04-04 18:42:01 +02:00
|
|
|
mustPunycode = evalMustPunycode();
|
|
|
|
}, { once: true });
|
2017-09-02 12:11:33 +02:00
|
|
|
|
2018-12-16 16:51:25 +01:00
|
|
|
const punycode = self.punycode;
|
|
|
|
const reAsciiHostname = /^https?:\/\/[0-9a-z_.:@-]+[/?#]/;
|
|
|
|
const parsedURL = new URL('about:blank');
|
2017-09-02 12:11:33 +02:00
|
|
|
|
2020-03-23 18:31:43 +01:00
|
|
|
// Canonical name-uncloaking feature.
|
2021-03-02 19:00:56 +01:00
|
|
|
let cnameUncloakEnabled = browser.dns instanceof Object;
|
2020-03-23 18:31:43 +01:00
|
|
|
let cnameUncloakProxied = false;
|
|
|
|
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/911
|
|
|
|
// We detect here whether network requests are proxied, and if so,
|
|
|
|
// de-aliasing of hostnames will be disabled to avoid possible
|
|
|
|
// DNS leaks.
|
|
|
|
const proxyDetector = function(details) {
|
|
|
|
if ( details.proxyInfo instanceof Object ) {
|
2021-03-02 19:00:56 +01:00
|
|
|
cnameUncloakEnabled = false;
|
2020-03-23 18:31:43 +01:00
|
|
|
proxyDetectorTryCount = 0;
|
|
|
|
}
|
|
|
|
if ( proxyDetectorTryCount === 0 ) {
|
|
|
|
browser.webRequest.onHeadersReceived.removeListener(proxyDetector);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
proxyDetectorTryCount -= 1;
|
|
|
|
};
|
|
|
|
let proxyDetectorTryCount = 0;
|
|
|
|
|
2019-06-30 16:09:27 +02:00
|
|
|
// Related issues:
|
|
|
|
// - https://github.com/gorhill/uBlock/issues/1327
|
|
|
|
// - https://github.com/uBlockOrigin/uBlock-issues/issues/128
|
|
|
|
// - https://bugzilla.mozilla.org/show_bug.cgi?id=1503721
|
2017-09-02 12:11:33 +02:00
|
|
|
|
2019-06-30 16:09:27 +02:00
|
|
|
// Extend base class to normalize as per platform.
|
2017-09-02 12:11:33 +02:00
|
|
|
|
2019-06-30 16:09:27 +02:00
|
|
|
vAPI.Net = class extends vAPI.Net {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.pendingRequests = [];
|
2021-03-02 19:00:56 +01:00
|
|
|
this.canUncloakCnames = browser.dns instanceof Object;
|
2019-11-20 16:45:17 +01:00
|
|
|
this.cnames = new Map([ [ '', '' ] ]);
|
2019-11-19 18:05:33 +01:00
|
|
|
this.cnameIgnoreList = null;
|
2019-11-23 18:46:52 +01:00
|
|
|
this.cnameIgnore1stParty = true;
|
2019-12-01 18:05:49 +01:00
|
|
|
this.cnameIgnoreExceptions = true;
|
2019-11-23 18:46:52 +01:00
|
|
|
this.cnameIgnoreRootDocument = true;
|
2019-12-31 22:36:51 +01:00
|
|
|
this.cnameMaxTTL = 120;
|
2019-11-23 18:46:52 +01:00
|
|
|
this.cnameReplayFullURL = false;
|
2019-12-31 22:36:51 +01:00
|
|
|
this.cnameFlushTime = Date.now() + this.cnameMaxTTL * 60000;
|
2019-11-19 18:05:33 +01:00
|
|
|
}
|
|
|
|
setOptions(options) {
|
|
|
|
super.setOptions(options);
|
2021-03-02 19:00:56 +01:00
|
|
|
if ( 'cnameUncloakEnabled' in options ) {
|
|
|
|
cnameUncloakEnabled =
|
|
|
|
this.canUncloakCnames &&
|
|
|
|
options.cnameUncloakEnabled !== false;
|
2020-03-23 18:31:43 +01:00
|
|
|
}
|
|
|
|
if ( 'cnameUncloakProxied' in options ) {
|
|
|
|
cnameUncloakProxied = options.cnameUncloakProxied === true;
|
2020-03-22 19:52:58 +01:00
|
|
|
}
|
|
|
|
if ( 'cnameIgnoreList' in options ) {
|
|
|
|
this.cnameIgnoreList =
|
|
|
|
this.regexFromStrList(options.cnameIgnoreList);
|
|
|
|
}
|
|
|
|
if ( 'cnameIgnore1stParty' in options ) {
|
|
|
|
this.cnameIgnore1stParty =
|
|
|
|
options.cnameIgnore1stParty !== false;
|
|
|
|
}
|
|
|
|
if ( 'cnameIgnoreExceptions' in options ) {
|
|
|
|
this.cnameIgnoreExceptions =
|
|
|
|
options.cnameIgnoreExceptions !== false;
|
|
|
|
}
|
|
|
|
if ( 'cnameIgnoreRootDocument' in options ) {
|
|
|
|
this.cnameIgnoreRootDocument =
|
|
|
|
options.cnameIgnoreRootDocument !== false;
|
|
|
|
}
|
|
|
|
if ( 'cnameMaxTTL' in options ) {
|
|
|
|
this.cnameMaxTTL = options.cnameMaxTTL || 120;
|
|
|
|
}
|
|
|
|
if ( 'cnameReplayFullURL' in options ) {
|
|
|
|
this.cnameReplayFullURL = options.cnameReplayFullURL === true;
|
|
|
|
}
|
2019-11-20 16:45:17 +01:00
|
|
|
this.cnames.clear(); this.cnames.set('', '');
|
2019-12-31 22:36:51 +01:00
|
|
|
this.cnameFlushTime = Date.now() + this.cnameMaxTTL * 60000;
|
2020-03-23 18:31:43 +01:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/911
|
|
|
|
// Install/remove proxy detector.
|
2020-10-14 19:37:09 +02:00
|
|
|
if ( vAPI.webextFlavor.major < 80 ) {
|
|
|
|
const wrohr = browser.webRequest.onHeadersReceived;
|
2021-03-02 19:00:56 +01:00
|
|
|
if ( cnameUncloakEnabled === false || cnameUncloakProxied ) {
|
2020-10-14 19:37:09 +02:00
|
|
|
if ( wrohr.hasListener(proxyDetector) ) {
|
|
|
|
wrohr.removeListener(proxyDetector);
|
|
|
|
}
|
|
|
|
} else if ( wrohr.hasListener(proxyDetector) === false ) {
|
|
|
|
wrohr.addListener(
|
|
|
|
proxyDetector,
|
|
|
|
{ urls: [ '*://*/*' ] },
|
|
|
|
[ 'blocking' ]
|
|
|
|
);
|
2020-03-23 18:31:43 +01:00
|
|
|
}
|
2020-10-14 19:37:09 +02:00
|
|
|
proxyDetectorTryCount = 32;
|
2020-03-23 18:31:43 +01:00
|
|
|
}
|
2017-09-02 12:11:33 +02:00
|
|
|
}
|
2019-06-30 16:09:27 +02:00
|
|
|
normalizeDetails(details) {
|
|
|
|
if ( mustPunycode && !reAsciiHostname.test(details.url) ) {
|
|
|
|
parsedURL.href = details.url;
|
|
|
|
details.url = details.url.replace(
|
|
|
|
parsedURL.hostname,
|
|
|
|
punycode.toASCII(parsedURL.hostname)
|
|
|
|
);
|
|
|
|
}
|
2017-09-02 12:11:33 +02:00
|
|
|
|
2019-06-30 16:09:27 +02:00
|
|
|
const type = details.type;
|
2018-12-16 16:51:25 +01:00
|
|
|
|
2019-06-30 16:09:27 +02:00
|
|
|
if ( type === 'imageset' ) {
|
|
|
|
details.type = 'image';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/345
|
|
|
|
// Re-categorize an embedded object as a `sub_frame` if its
|
|
|
|
// content type is that of a HTML document.
|
|
|
|
if ( type === 'object' && Array.isArray(details.responseHeaders) ) {
|
|
|
|
for ( const header of details.responseHeaders ) {
|
|
|
|
if ( header.name.toLowerCase() === 'content-type' ) {
|
|
|
|
if ( header.value.startsWith('text/html') ) {
|
|
|
|
details.type = 'sub_frame';
|
|
|
|
}
|
|
|
|
break;
|
2018-12-16 16:51:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-30 16:09:27 +02:00
|
|
|
denormalizeTypes(types) {
|
|
|
|
if ( types.length === 0 ) {
|
|
|
|
return Array.from(this.validTypes);
|
2017-09-02 12:11:33 +02:00
|
|
|
}
|
2019-06-30 16:09:27 +02:00
|
|
|
const out = new Set();
|
|
|
|
for ( const type of types ) {
|
|
|
|
if ( this.validTypes.has(type) ) {
|
|
|
|
out.add(type);
|
|
|
|
}
|
|
|
|
if ( type === 'image' && this.validTypes.has('imageset') ) {
|
|
|
|
out.add('imageset');
|
|
|
|
}
|
|
|
|
if ( type === 'sub_frame' ) {
|
|
|
|
out.add('object');
|
|
|
|
}
|
2017-09-02 12:11:33 +02:00
|
|
|
}
|
2019-06-30 16:09:27 +02:00
|
|
|
return Array.from(out);
|
2017-09-02 12:11:33 +02:00
|
|
|
}
|
2019-12-31 22:36:51 +01:00
|
|
|
canonicalNameFromHostname(hn) {
|
|
|
|
const cn = this.cnames.get(hn);
|
|
|
|
if ( cn !== undefined && cn !== '' ) {
|
|
|
|
return cn;
|
|
|
|
}
|
|
|
|
}
|
2019-11-23 18:46:52 +01:00
|
|
|
processCanonicalName(hn, cn, details) {
|
|
|
|
const hnBeg = details.url.indexOf(hn);
|
|
|
|
if ( hnBeg === -1 ) { return; }
|
|
|
|
const oldURL = details.url;
|
|
|
|
let newURL = oldURL.slice(0, hnBeg) + cn;
|
|
|
|
const hnEnd = hnBeg + hn.length;
|
|
|
|
if ( this.cnameReplayFullURL ) {
|
|
|
|
newURL += oldURL.slice(hnEnd);
|
|
|
|
} else {
|
|
|
|
const pathBeg = oldURL.indexOf('/', hnEnd);
|
|
|
|
if ( pathBeg !== -1 ) {
|
|
|
|
newURL += oldURL.slice(hnEnd, pathBeg + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
details.url = newURL;
|
|
|
|
details.aliasURL = oldURL;
|
2019-11-19 18:05:33 +01:00
|
|
|
return super.onBeforeSuspendableRequest(details);
|
|
|
|
}
|
|
|
|
recordCanonicalName(hn, record) {
|
2019-12-31 22:36:51 +01:00
|
|
|
if ( (this.cnames.size & 0b111111) === 0 ) {
|
|
|
|
const now = Date.now();
|
|
|
|
if ( now >= this.cnameFlushTime ) {
|
|
|
|
this.cnames.clear(); this.cnames.set('', '');
|
|
|
|
this.cnameFlushTime = now + this.cnameMaxTTL * 60000;
|
|
|
|
}
|
|
|
|
}
|
2019-11-19 18:05:33 +01:00
|
|
|
let cname =
|
|
|
|
typeof record.canonicalName === 'string' &&
|
|
|
|
record.canonicalName !== hn
|
|
|
|
? record.canonicalName
|
|
|
|
: '';
|
|
|
|
if (
|
|
|
|
cname !== '' &&
|
|
|
|
this.cnameIgnore1stParty &&
|
|
|
|
vAPI.domainFromHostname(cname) === vAPI.domainFromHostname(hn)
|
|
|
|
) {
|
|
|
|
cname = '';
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
cname !== '' &&
|
|
|
|
this.cnameIgnoreList !== null &&
|
|
|
|
this.cnameIgnoreList.test(cname)
|
|
|
|
) {
|
|
|
|
cname = '';
|
|
|
|
}
|
|
|
|
this.cnames.set(hn, cname);
|
|
|
|
return cname;
|
|
|
|
}
|
|
|
|
regexFromStrList(list) {
|
|
|
|
if (
|
|
|
|
typeof list !== 'string' ||
|
|
|
|
list.length === 0 ||
|
2019-11-19 22:48:53 +01:00
|
|
|
list === 'unset' ||
|
|
|
|
browser.dns instanceof Object === false
|
2019-11-19 18:05:33 +01:00
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ( list === '*' ) {
|
|
|
|
return /^./;
|
|
|
|
}
|
|
|
|
return new RegExp(
|
|
|
|
'(?:^|\.)(?:' +
|
|
|
|
list.trim()
|
|
|
|
.split(/\s+/)
|
|
|
|
.map(a => a.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'))
|
|
|
|
.join('|') +
|
|
|
|
')$'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
onBeforeSuspendableRequest(details) {
|
2019-12-01 18:05:49 +01:00
|
|
|
const r = super.onBeforeSuspendableRequest(details);
|
2021-03-02 19:00:56 +01:00
|
|
|
if ( cnameUncloakEnabled === false ) { return r; }
|
2019-12-01 18:05:49 +01:00
|
|
|
if ( r !== undefined ) {
|
|
|
|
if (
|
|
|
|
r.cancel === true ||
|
|
|
|
r.redirectUrl !== undefined ||
|
|
|
|
this.cnameIgnoreExceptions
|
|
|
|
) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
details.type === 'main_frame' &&
|
|
|
|
this.cnameIgnoreRootDocument
|
|
|
|
) {
|
2019-11-23 18:46:52 +01:00
|
|
|
return;
|
|
|
|
}
|
2019-11-20 16:45:17 +01:00
|
|
|
const hn = vAPI.hostnameFromNetworkURL(details.url);
|
2019-12-05 15:21:02 +01:00
|
|
|
const cname = this.cnames.get(hn);
|
2019-11-19 18:05:33 +01:00
|
|
|
if ( cname === '' ) { return; }
|
|
|
|
if ( cname !== undefined ) {
|
2019-11-23 18:46:52 +01:00
|
|
|
return this.processCanonicalName(hn, cname, details);
|
2019-11-19 18:05:33 +01:00
|
|
|
}
|
2019-11-21 18:04:19 +01:00
|
|
|
return browser.dns.resolve(hn, [ 'canonical_name' ]).then(
|
|
|
|
rec => {
|
|
|
|
const cname = this.recordCanonicalName(hn, rec);
|
|
|
|
if ( cname === '' ) { return; }
|
2019-11-23 18:46:52 +01:00
|
|
|
return this.processCanonicalName(hn, cname, details);
|
2019-11-21 18:04:19 +01:00
|
|
|
},
|
|
|
|
( ) => {
|
|
|
|
this.cnames.set(hn, '');
|
|
|
|
}
|
|
|
|
);
|
2019-11-19 18:05:33 +01:00
|
|
|
}
|
2019-06-30 16:09:27 +02:00
|
|
|
suspendOneRequest(details) {
|
|
|
|
const pending = {
|
|
|
|
details: Object.assign({}, details),
|
|
|
|
resolve: undefined,
|
|
|
|
promise: undefined
|
|
|
|
};
|
2019-10-07 14:13:37 +02:00
|
|
|
pending.promise = new Promise(resolve => {
|
2019-06-30 16:09:27 +02:00
|
|
|
pending.resolve = resolve;
|
|
|
|
});
|
|
|
|
this.pendingRequests.push(pending);
|
|
|
|
return pending.promise;
|
|
|
|
}
|
2019-11-19 18:05:33 +01:00
|
|
|
unsuspendAllRequests() {
|
2019-06-30 16:09:27 +02:00
|
|
|
const pendingRequests = this.pendingRequests;
|
|
|
|
this.pendingRequests = [];
|
|
|
|
for ( const entry of pendingRequests ) {
|
2019-11-19 18:05:33 +01:00
|
|
|
entry.resolve(this.onBeforeSuspendableRequest(entry.details));
|
2018-12-23 23:59:31 +01:00
|
|
|
}
|
2019-06-30 16:09:27 +02:00
|
|
|
}
|
|
|
|
canSuspend() {
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-23 23:59:31 +01:00
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
|
|
|
/******************************************************************************/
|