2014-06-24 00:42:43 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
2016-03-22 15:19:41 +01:00
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-09-01 00:47:02 +02:00
|
|
|
Copyright (C) 2014-present Raymond Hill
|
2014-06-24 00:42:43 +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
|
|
|
|
*/
|
|
|
|
|
2022-11-06 22:54:32 +01:00
|
|
|
/* globals browser */
|
|
|
|
|
2016-07-01 04:03:29 +02:00
|
|
|
'use strict';
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
import htmlFilteringEngine from './html-filtering.js';
|
|
|
|
import httpheaderFilteringEngine from './httpheader-filtering.js';
|
|
|
|
import logger from './logger.js';
|
|
|
|
import scriptletFilteringEngine from './scriptlet-filtering.js';
|
|
|
|
import staticNetFilteringEngine from './static-net-filtering.js';
|
|
|
|
import textEncode from './text-encode.js';
|
|
|
|
import µb from './background.js';
|
2021-08-03 18:19:25 +02:00
|
|
|
|
|
|
|
import {
|
|
|
|
sessionFirewall,
|
|
|
|
sessionSwitches,
|
|
|
|
sessionURLFiltering,
|
|
|
|
} from './filtering-engines.js';
|
2021-07-29 01:48:38 +02:00
|
|
|
|
2021-07-25 16:55:35 +02:00
|
|
|
import {
|
|
|
|
entityFromDomain,
|
|
|
|
isNetworkURI,
|
|
|
|
} from './uri-utils.js';
|
2014-06-24 00:42:43 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-05-16 17:50:50 +02:00
|
|
|
// Platform-specific behavior.
|
|
|
|
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/42
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=1376932
|
|
|
|
// Add proper version number detection once issue is fixed in Firefox.
|
|
|
|
let dontCacheResponseHeaders =
|
|
|
|
vAPI.webextFlavor.soup.has('firefox');
|
|
|
|
|
|
|
|
// The real actual webextFlavor value may not be set in stone, so listen
|
|
|
|
// for possible future changes.
|
|
|
|
window.addEventListener('webextFlavor', function() {
|
|
|
|
dontCacheResponseHeaders =
|
|
|
|
vAPI.webextFlavor.soup.has('firefox');
|
|
|
|
}, { once: true });
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-08-02 15:23:48 +02:00
|
|
|
const patchLocalRedirectURL = url => url.charCodeAt(0) === 0x2F /* '/' */
|
|
|
|
? vAPI.getURL(url)
|
|
|
|
: url;
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2014-07-26 15:55:12 +02:00
|
|
|
// Intercept and filter web requests.
|
2014-07-14 17:24:59 +02:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const onBeforeRequest = function(details) {
|
2021-07-29 01:48:38 +02:00
|
|
|
const fctxt = µb.filteringContext.fromWebrequestDetails(details);
|
2018-12-13 18:30:54 +01:00
|
|
|
|
2014-07-26 01:29:51 +02:00
|
|
|
// Special handling for root document.
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1001
|
2015-03-13 14:48:10 +01:00
|
|
|
// This must be executed regardless of whether the request is
|
|
|
|
// behind-the-scene
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
if ( fctxt.itype === fctxt.MAIN_FRAME ) {
|
2018-12-13 18:30:54 +01:00
|
|
|
return onBeforeRootFrameRequest(fctxt);
|
2014-07-14 17:24:59 +02:00
|
|
|
}
|
|
|
|
|
2015-03-13 14:48:10 +01:00
|
|
|
// Special treatment: behind-the-scene requests
|
2018-12-13 18:30:54 +01:00
|
|
|
const tabId = details.tabId;
|
|
|
|
if ( tabId < 0 ) {
|
|
|
|
return onBeforeBehindTheSceneRequest(fctxt);
|
2015-03-13 14:48:10 +01:00
|
|
|
}
|
|
|
|
|
2014-07-26 01:29:51 +02:00
|
|
|
// Lookup the page store associated with this tab id.
|
2018-12-13 18:30:54 +01:00
|
|
|
let pageStore = µb.pageStoreFromTabId(tabId);
|
|
|
|
if ( pageStore === null ) {
|
|
|
|
const tabContext = µb.tabContextManager.mustLookup(tabId);
|
|
|
|
if ( tabContext.tabId < 0 ) {
|
|
|
|
return onBeforeBehindTheSceneRequest(fctxt);
|
2015-03-13 14:48:10 +01:00
|
|
|
}
|
2018-12-13 18:30:54 +01:00
|
|
|
vAPI.tabs.onNavigation({ tabId, frameId: 0, url: tabContext.rawURL });
|
2015-04-09 00:46:08 +02:00
|
|
|
pageStore = µb.pageStoreFromTabId(tabId);
|
2014-07-14 20:40:40 +02:00
|
|
|
}
|
2014-07-15 13:38:34 +02:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const result = pageStore.filterRequest(fctxt);
|
2014-07-30 07:05:35 +02:00
|
|
|
|
2021-02-15 12:52:31 +01:00
|
|
|
pageStore.journalAddRequest(fctxt, result);
|
2018-12-13 18:30:54 +01:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( logger.enabled ) {
|
2019-01-12 22:36:20 +01:00
|
|
|
fctxt.setRealm('network').toLogger();
|
2015-06-05 01:27:03 +02:00
|
|
|
}
|
2015-04-09 00:46:08 +02:00
|
|
|
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
// Redirected
|
|
|
|
|
|
|
|
if ( fctxt.redirectURL !== undefined ) {
|
2021-08-02 15:23:48 +02:00
|
|
|
return { redirectUrl: patchLocalRedirectURL(fctxt.redirectURL) };
|
2014-07-14 17:24:59 +02:00
|
|
|
}
|
|
|
|
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
// Not redirected
|
2014-12-23 00:38:18 +01:00
|
|
|
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
// Blocked
|
|
|
|
if ( result === 1 ) {
|
2020-10-10 14:36:30 +02:00
|
|
|
return { cancel: true };
|
2015-11-23 13:52:50 +01:00
|
|
|
}
|
2014-07-14 17:24:59 +02:00
|
|
|
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
// Not blocked
|
|
|
|
if (
|
|
|
|
fctxt.itype === fctxt.SUB_FRAME &&
|
|
|
|
details.parentFrameId !== -1 &&
|
|
|
|
details.aliasURL === undefined
|
|
|
|
) {
|
2021-01-10 17:56:27 +01:00
|
|
|
pageStore.setFrameURL(details);
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( result === 2 ) {
|
|
|
|
return { cancel: false };
|
2020-10-10 14:36:30 +02:00
|
|
|
}
|
2014-07-14 17:24:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const onBeforeRootFrameRequest = function(fctxt) {
|
|
|
|
const requestURL = fctxt.url;
|
2015-03-26 00:28:22 +01:00
|
|
|
|
2015-03-21 21:52:35 +01:00
|
|
|
// Special handling for root document.
|
2015-04-07 03:26:05 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1001
|
2018-12-13 18:30:54 +01:00
|
|
|
// This must be executed regardless of whether the request is
|
|
|
|
// behind-the-scene
|
|
|
|
const requestHostname = fctxt.getHostname();
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
let result = 0;
|
|
|
|
let logData;
|
2015-03-26 00:28:22 +01:00
|
|
|
|
2015-04-09 18:20:24 +02:00
|
|
|
// If the site is whitelisted, disregard strict blocking
|
2020-11-13 14:30:43 +01:00
|
|
|
const trusted = µb.getNetFilteringSwitch(requestURL) === false;
|
|
|
|
if ( trusted ) {
|
2017-05-12 16:35:11 +02:00
|
|
|
result = 2;
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( logger.enabled ) {
|
2017-05-12 16:35:11 +02:00
|
|
|
logData = { engine: 'u', result: 2, raw: 'whitelisted' };
|
|
|
|
}
|
2015-04-09 18:20:24 +02:00
|
|
|
}
|
|
|
|
|
2015-03-27 18:00:55 +01:00
|
|
|
// Permanently unrestricted?
|
2018-10-29 13:56:51 +01:00
|
|
|
if (
|
|
|
|
result === 0 &&
|
2021-07-29 01:48:38 +02:00
|
|
|
sessionSwitches.evaluateZ('no-strict-blocking', requestHostname)
|
2018-10-29 13:56:51 +01:00
|
|
|
) {
|
2017-05-12 16:35:11 +02:00
|
|
|
result = 2;
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( logger.enabled ) {
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
logData = {
|
|
|
|
engine: 'u',
|
|
|
|
result: 2,
|
2021-07-29 01:48:38 +02:00
|
|
|
raw: `no-strict-blocking: ${sessionSwitches.z} true`
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
};
|
2017-05-12 16:35:11 +02:00
|
|
|
}
|
2015-03-27 18:00:55 +01:00
|
|
|
}
|
|
|
|
|
2015-03-26 00:28:22 +01:00
|
|
|
// Temporarily whitelisted?
|
2018-12-13 18:30:54 +01:00
|
|
|
if ( result === 0 && strictBlockBypasser.isBypassed(requestHostname) ) {
|
2018-10-29 13:56:51 +01:00
|
|
|
result = 2;
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( logger.enabled ) {
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
logData = {
|
|
|
|
engine: 'u',
|
|
|
|
result: 2,
|
|
|
|
raw: 'no-strict-blocking: true (temporary)'
|
|
|
|
};
|
2015-04-09 18:20:24 +02:00
|
|
|
}
|
2015-03-21 21:52:35 +01:00
|
|
|
}
|
2015-03-26 00:28:22 +01:00
|
|
|
|
2021-02-19 14:38:50 +01:00
|
|
|
// Static filtering
|
2017-05-12 16:35:11 +02:00
|
|
|
if ( result === 0 ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
({ result, logData } = shouldStrictBlock(fctxt, logger.enabled));
|
2015-03-26 00:28:22 +01:00
|
|
|
}
|
|
|
|
|
2020-12-12 14:19:40 +01:00
|
|
|
const pageStore = µb.bindTabToPageStore(fctxt.tabId, 'beforeRequest');
|
2020-11-03 15:15:26 +01:00
|
|
|
if ( pageStore !== null ) {
|
2016-10-08 16:15:31 +02:00
|
|
|
pageStore.journalAddRootFrame('uncommitted', requestURL);
|
2021-02-15 12:52:31 +01:00
|
|
|
pageStore.journalAddRequest(fctxt, result);
|
2015-03-26 00:28:22 +01:00
|
|
|
}
|
2015-06-05 01:27:03 +02:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( logger.enabled ) {
|
2020-11-03 15:15:26 +01:00
|
|
|
fctxt.setFilter(logData);
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/760
|
|
|
|
// Redirect non-blocked request?
|
2020-11-13 14:30:43 +01:00
|
|
|
if (
|
|
|
|
result !== 1 &&
|
|
|
|
trusted === false &&
|
|
|
|
pageStore !== null &&
|
2021-07-29 01:48:38 +02:00
|
|
|
staticNetFilteringEngine.hasQuery(fctxt)
|
2020-11-13 14:30:43 +01:00
|
|
|
) {
|
2020-11-03 15:15:26 +01:00
|
|
|
pageStore.redirectNonBlockedRequest(fctxt);
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
}
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( logger.enabled ) {
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
fctxt.setRealm('network').toLogger();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Redirected
|
|
|
|
|
|
|
|
if ( fctxt.redirectURL !== undefined ) {
|
2021-08-02 15:23:48 +02:00
|
|
|
return { redirectUrl: patchLocalRedirectURL(fctxt.redirectURL) };
|
2015-06-05 01:27:03 +02:00
|
|
|
}
|
2015-03-26 00:28:22 +01:00
|
|
|
|
|
|
|
// Not blocked
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
|
2017-05-12 16:35:11 +02:00
|
|
|
if ( result !== 1 ) { return; }
|
2015-03-26 00:28:22 +01:00
|
|
|
|
2017-05-12 16:35:11 +02:00
|
|
|
// No log data means no strict blocking (because we need to report why
|
|
|
|
// the blocking occurs.
|
|
|
|
if ( logData === undefined ) { return; }
|
2015-06-12 01:33:30 +02:00
|
|
|
|
2015-03-26 00:28:22 +01:00
|
|
|
// Blocked
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
|
2019-11-04 19:12:40 +01:00
|
|
|
const query = encodeURIComponent(JSON.stringify({
|
2015-03-26 00:28:22 +01:00
|
|
|
url: requestURL,
|
2015-03-30 19:10:29 +02:00
|
|
|
hn: requestHostname,
|
2018-12-13 18:30:54 +01:00
|
|
|
dn: fctxt.getDomain() || requestHostname,
|
2017-05-12 16:35:11 +02:00
|
|
|
fs: logData.raw
|
2015-03-26 00:28:22 +01:00
|
|
|
}));
|
2015-03-27 18:00:55 +01:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
vAPI.tabs.replace(
|
|
|
|
fctxt.tabId,
|
|
|
|
vAPI.getURL('document-blocked.html?details=') + query
|
|
|
|
);
|
2015-03-27 18:00:55 +01:00
|
|
|
|
|
|
|
return { cancel: true };
|
2015-03-21 21:52:35 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2021-02-19 14:38:50 +01:00
|
|
|
// Strict blocking through static filtering
|
|
|
|
//
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1128
|
|
|
|
// Do not block if the match begins after the hostname,
|
|
|
|
// except when the filter is specifically of type `other`.
|
|
|
|
// https://github.com/gorhill/uBlock/issues/490
|
|
|
|
// Removing this for the time being, will need a new, dedicated type.
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/1501
|
|
|
|
// Support explicit exception filters.
|
|
|
|
//
|
|
|
|
// Let result of match for specific `document` type be `rs`
|
|
|
|
// Let result of match for no specific type be `rg` *after* going through
|
|
|
|
// confirmation necessary for implicit matches
|
|
|
|
// Let `important` be `i`
|
|
|
|
// Let final result be logical combination of `rs` and `rg` as follow:
|
|
|
|
//
|
|
|
|
// | rs |
|
|
|
|
// +--------+--------+--------+--------|
|
|
|
|
// | 0 | 1 | 1i | 2 |
|
|
|
|
// --------+--------+--------+--------+--------+--------|
|
|
|
|
// | 0 | rg | rs | rs | rs |
|
|
|
|
// rg | 1 | rg | rs | rs | rs |
|
|
|
|
// | 1i | rg | rg | rs | rg |
|
|
|
|
// | 2 | rg | rg | rs | rs |
|
|
|
|
// --------+--------+--------+--------+--------+--------+
|
|
|
|
|
|
|
|
const shouldStrictBlock = function(fctxt, loggerEnabled) {
|
2021-07-29 01:48:38 +02:00
|
|
|
const snfe = staticNetFilteringEngine;
|
2021-02-19 14:38:50 +01:00
|
|
|
|
|
|
|
// Explicit filtering: `document` option
|
2021-07-25 16:55:35 +02:00
|
|
|
const rs = snfe.matchRequest(fctxt, 0b0011);
|
2021-02-19 14:38:50 +01:00
|
|
|
const is = rs === 1 && snfe.isBlockImportant();
|
|
|
|
let lds;
|
2021-02-22 12:32:43 +01:00
|
|
|
if ( rs !== 0 || loggerEnabled ) {
|
2021-02-19 14:38:50 +01:00
|
|
|
lds = snfe.toLogData();
|
|
|
|
}
|
|
|
|
|
|
|
|
// | rs |
|
|
|
|
// +--------+--------+--------+--------|
|
|
|
|
// | 0 | 1 | 1i | 2 |
|
|
|
|
// --------+--------+--------+--------+--------+--------|
|
|
|
|
// | 0 | rg | rs | x | rs |
|
|
|
|
// rg | 1 | rg | rs | x | rs |
|
|
|
|
// | 1i | rg | rg | x | rg |
|
|
|
|
// | 2 | rg | rg | x | rs |
|
|
|
|
// --------+--------+--------+--------+--------+--------+
|
|
|
|
if ( rs === 1 && is ) {
|
|
|
|
return { result: rs, logData: lds };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Implicit filtering: no `document` option
|
|
|
|
fctxt.type = 'no_type';
|
2021-07-25 16:55:35 +02:00
|
|
|
let rg = snfe.matchRequest(fctxt, 0b0011);
|
2021-02-19 14:38:50 +01:00
|
|
|
fctxt.type = 'main_frame';
|
|
|
|
const ig = rg === 1 && snfe.isBlockImportant();
|
|
|
|
let ldg;
|
|
|
|
if ( rg !== 0 || loggerEnabled ) {
|
|
|
|
ldg = snfe.toLogData();
|
|
|
|
if ( rg === 1 && validateStrictBlock(fctxt, ldg) === false ) {
|
|
|
|
rg = 0; ldg = undefined;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// | rs |
|
|
|
|
// +--------+--------+--------+--------|
|
|
|
|
// | 0 | 1 | 1i | 2 |
|
|
|
|
// --------+--------+--------+--------+--------+--------|
|
|
|
|
// | 0 | x | rs | - | rs |
|
|
|
|
// rg | 1 | x | rs | - | rs |
|
|
|
|
// | 1i | x | x | - | x |
|
|
|
|
// | 2 | x | x | - | rs |
|
|
|
|
// --------+--------+--------+--------+--------+--------+
|
|
|
|
if ( rs === 0 || rg === 1 && ig || rg === 2 && rs !== 2 ) {
|
|
|
|
return { result: rg, logData: ldg };
|
|
|
|
}
|
|
|
|
|
|
|
|
// | rs |
|
|
|
|
// +--------+--------+--------+--------|
|
|
|
|
// | 0 | 1 | 1i | 2 |
|
|
|
|
// --------+--------+--------+--------+--------+--------|
|
|
|
|
// | 0 | - | x | - | x |
|
|
|
|
// rg | 1 | - | x | - | x |
|
|
|
|
// | 1i | - | - | - | - |
|
|
|
|
// | 2 | - | - | - | x |
|
|
|
|
// --------+--------+--------+--------+--------+--------+
|
|
|
|
return { result: rs, logData: lds };
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2017-11-08 19:29:04 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/3208
|
|
|
|
// Mind case insensitivity.
|
2020-07-18 15:40:38 +02:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/1147
|
|
|
|
// Do not strict-block if the filter pattern does not contain at least one
|
|
|
|
// token character.
|
2021-02-19 14:38:50 +01:00
|
|
|
|
|
|
|
const validateStrictBlock = function(fctxt, logData) {
|
2017-11-09 21:46:25 +01:00
|
|
|
if ( typeof logData.regex !== 'string' ) { return false; }
|
2020-07-18 15:40:38 +02:00
|
|
|
if ( typeof logData.raw === 'string' && /\w/.test(logData.raw) === false ) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-02-19 14:38:50 +01:00
|
|
|
const url = fctxt.url;
|
2018-12-13 18:30:54 +01:00
|
|
|
const re = new RegExp(logData.regex, 'i');
|
|
|
|
const match = re.exec(url.toLowerCase());
|
2017-11-08 19:29:04 +01:00
|
|
|
if ( match === null ) { return false; }
|
2015-03-30 23:42:12 +02:00
|
|
|
|
2015-04-08 13:04:29 +02:00
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1128
|
|
|
|
// https://github.com/chrisaljoudi/uBlock/issues/1212
|
2019-07-02 17:56:27 +02:00
|
|
|
// Verify that the end of the match is anchored to the end of the
|
|
|
|
// hostname.
|
2020-07-03 20:28:03 +02:00
|
|
|
// https://github.com/uBlockOrigin/uAssets/issues/7619#issuecomment-653010310
|
|
|
|
// Also match FQDN.
|
2021-02-19 14:38:50 +01:00
|
|
|
const hostname = fctxt.getHostname();
|
2020-07-03 20:28:03 +02:00
|
|
|
const hnpos = url.indexOf(hostname);
|
|
|
|
const hnlen = hostname.length;
|
|
|
|
const end = match.index + match[0].length - hnpos - hnlen;
|
|
|
|
return end === 0 || end === 1 ||
|
|
|
|
end === 2 && url.charCodeAt(hnpos + hnlen) === 0x2E /* '.' */;
|
2015-03-30 23:42:12 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-10-14 16:06:34 +02:00
|
|
|
// Intercept and filter behind-the-scene requests.
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const onBeforeBehindTheSceneRequest = function(fctxt) {
|
|
|
|
const pageStore = µb.pageStoreFromTabId(fctxt.tabId);
|
2018-02-26 19:59:16 +01:00
|
|
|
if ( pageStore === null ) { return; }
|
2016-10-14 16:06:34 +02:00
|
|
|
|
2017-10-19 15:35:28 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/3150
|
|
|
|
// Ability to globally block CSP reports MUST also apply to
|
|
|
|
// behind-the-scene network requests.
|
2018-03-30 14:55:51 +02:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
let result = 0;
|
2018-04-02 15:10:38 +02:00
|
|
|
|
2018-12-14 14:47:29 +01:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/339
|
|
|
|
// Need to also test against `-scheme` since tabOrigin is normalized.
|
|
|
|
// Not especially elegant but for now this accomplishes the purpose of
|
|
|
|
// not dealing with network requests fired from a synthetic scope,
|
|
|
|
// that is unless advanced user mode is enabled.
|
|
|
|
|
2018-04-02 15:10:38 +02:00
|
|
|
if (
|
2018-12-14 14:47:29 +01:00
|
|
|
fctxt.tabOrigin.endsWith('-scheme') === false &&
|
2021-07-25 16:55:35 +02:00
|
|
|
isNetworkURI(fctxt.tabOrigin) ||
|
2018-04-02 15:10:38 +02:00
|
|
|
µb.userSettings.advancedUserEnabled ||
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
fctxt.itype === fctxt.CSP_REPORT
|
2018-04-02 15:10:38 +02:00
|
|
|
) {
|
2018-12-13 18:30:54 +01:00
|
|
|
result = pageStore.filterRequest(fctxt);
|
2018-04-02 15:10:38 +02:00
|
|
|
|
|
|
|
// The "any-tab" scope is not whitelist-able, and in such case we must
|
|
|
|
// use the origin URL as the scope. Most such requests aren't going to
|
2020-12-12 14:19:40 +01:00
|
|
|
// be blocked, so we test for whitelisting and modify the result only
|
|
|
|
// when the request is being blocked.
|
2021-01-30 21:08:21 +01:00
|
|
|
//
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/1478
|
|
|
|
// Also remove potential redirection when request is to be
|
|
|
|
// whitelisted.
|
2018-04-02 15:10:38 +02:00
|
|
|
if (
|
|
|
|
result === 1 &&
|
2018-12-13 18:30:54 +01:00
|
|
|
µb.getNetFilteringSwitch(fctxt.tabOrigin) === false
|
2018-04-02 15:10:38 +02:00
|
|
|
) {
|
|
|
|
result = 2;
|
2021-01-30 21:08:21 +01:00
|
|
|
fctxt.redirectURL = undefined;
|
2018-12-13 18:30:54 +01:00
|
|
|
fctxt.filter = { engine: 'u', result: 2, raw: 'whitelisted' };
|
2018-04-02 15:10:38 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-24 18:06:22 +01:00
|
|
|
|
2020-12-12 14:19:40 +01:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/1204
|
|
|
|
onBeforeBehindTheSceneRequest.journalAddRequest(fctxt, result);
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( logger.enabled ) {
|
2019-01-12 22:36:20 +01:00
|
|
|
fctxt.setRealm('network').toLogger();
|
2018-12-13 18:30:54 +01:00
|
|
|
}
|
2016-07-01 04:03:29 +02:00
|
|
|
|
2020-11-10 14:58:39 +01:00
|
|
|
// Redirected
|
|
|
|
|
|
|
|
if ( fctxt.redirectURL !== undefined ) {
|
2021-08-02 15:23:48 +02:00
|
|
|
return { redirectUrl: patchLocalRedirectURL(fctxt.redirectURL) };
|
2020-11-10 14:58:39 +01:00
|
|
|
}
|
|
|
|
|
2017-05-28 19:32:08 +02:00
|
|
|
// Blocked?
|
2020-11-10 14:58:39 +01:00
|
|
|
|
2017-05-28 19:32:08 +02:00
|
|
|
if ( result === 1 ) {
|
2018-10-28 14:58:25 +01:00
|
|
|
return { cancel: true };
|
2017-05-28 19:32:08 +02:00
|
|
|
}
|
2015-01-24 18:06:22 +01:00
|
|
|
};
|
|
|
|
|
2020-12-12 14:19:40 +01:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/1204
|
|
|
|
// Report the tabless network requests to all page stores matching the
|
|
|
|
// document origin. This is an approximation, there is unfortunately no
|
|
|
|
// way to know for sure which exact page triggered a tabless network
|
|
|
|
// request.
|
|
|
|
|
2020-12-12 20:38:44 +01:00
|
|
|
{
|
2023-04-09 19:38:16 +02:00
|
|
|
const pageStores = new Set();
|
2020-12-12 14:19:40 +01:00
|
|
|
let hostname = '';
|
|
|
|
let pageStoresToken = 0;
|
|
|
|
|
|
|
|
const reset = function() {
|
|
|
|
hostname = '';
|
2023-04-09 19:38:16 +02:00
|
|
|
pageStores.clear();
|
2020-12-12 14:19:40 +01:00
|
|
|
pageStoresToken = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
const gc = ( ) => {
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( pageStoresToken !== µb.pageStoresToken ) { return reset(); }
|
2023-04-09 19:38:16 +02:00
|
|
|
gcTimer.on(30011);
|
2020-12-12 14:19:40 +01:00
|
|
|
};
|
|
|
|
|
2023-04-09 19:38:16 +02:00
|
|
|
const gcTimer = vAPI.defer.create(gc);
|
|
|
|
|
2020-12-12 20:38:44 +01:00
|
|
|
onBeforeBehindTheSceneRequest.journalAddRequest = (fctxt, result) => {
|
2020-12-14 14:38:30 +01:00
|
|
|
const docHostname = fctxt.getDocHostname();
|
2020-12-12 14:19:40 +01:00
|
|
|
if (
|
|
|
|
docHostname !== hostname ||
|
2021-07-29 01:48:38 +02:00
|
|
|
pageStoresToken !== µb.pageStoresToken
|
2020-12-12 14:19:40 +01:00
|
|
|
) {
|
|
|
|
hostname = docHostname;
|
2023-04-09 19:38:16 +02:00
|
|
|
pageStores.clear();
|
2021-07-29 01:48:38 +02:00
|
|
|
for ( const pageStore of µb.pageStores.values() ) {
|
2020-12-12 14:19:40 +01:00
|
|
|
if ( pageStore.tabHostname !== docHostname ) { continue; }
|
|
|
|
pageStores.add(pageStore);
|
|
|
|
}
|
2021-07-29 01:48:38 +02:00
|
|
|
pageStoresToken = µb.pageStoresToken;
|
2023-04-09 19:38:16 +02:00
|
|
|
gcTimer.offon(30011);
|
2020-12-12 14:19:40 +01:00
|
|
|
}
|
|
|
|
for ( const pageStore of pageStores ) {
|
2021-02-15 12:52:31 +01:00
|
|
|
pageStore.journalAddRequest(fctxt, result);
|
2020-12-12 14:19:40 +01:00
|
|
|
}
|
|
|
|
};
|
2020-12-12 20:38:44 +01:00
|
|
|
}
|
2020-12-12 14:19:40 +01:00
|
|
|
|
2015-01-24 18:06:22 +01:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-01-17 19:30:43 +01:00
|
|
|
// To handle:
|
2017-12-28 19:49:02 +01:00
|
|
|
// - Media elements larger than n kB
|
|
|
|
// - Scriptlet injection (requires ability to modify response body)
|
|
|
|
// - HTML filtering (requires ability to modify response body)
|
|
|
|
// - CSP injection
|
2014-09-24 23:38:22 +02:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const onHeadersReceived = function(details) {
|
2019-05-31 15:02:07 +02:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/610
|
|
|
|
// Process behind-the-scene requests in a special way.
|
|
|
|
if (
|
|
|
|
details.tabId < 0 &&
|
|
|
|
normalizeBehindTheSceneResponseHeaders(details) === false
|
|
|
|
) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-24 23:38:22 +02:00
|
|
|
|
2019-05-31 15:02:07 +02:00
|
|
|
const fctxt = µb.filteringContext.fromWebrequestDetails(details);
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
const isRootDoc = fctxt.itype === fctxt.MAIN_FRAME;
|
2015-11-09 23:59:19 +01:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
let pageStore = µb.pageStoreFromTabId(fctxt.tabId);
|
2017-01-18 00:18:28 +01:00
|
|
|
if ( pageStore === null ) {
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( isRootDoc === false ) { return; }
|
2020-12-12 14:19:40 +01:00
|
|
|
pageStore = µb.bindTabToPageStore(fctxt.tabId, 'beforeRequest');
|
2014-09-24 23:38:22 +02:00
|
|
|
}
|
2019-07-19 12:54:55 +02:00
|
|
|
if ( pageStore.getNetFilteringSwitch(fctxt) === false ) { return; }
|
2015-04-09 00:46:08 +02:00
|
|
|
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
if ( fctxt.itype === fctxt.IMAGE || fctxt.itype === fctxt.MEDIA ) {
|
Add support for `1P`, `3P`, `header=` filter options and other changes
New filter options
==================
Strict partyness: `1P`, `3P`
----------------------------
The current options 1p/3p are meant to "weakly" match partyness, i.e. a
network request is considered 1st-party to its context as long as both the
context and the request share the same base domain.
The new partyness options are meant to check for strict partyness, i.e. a
network request will be considered 1st-party if and only if both the context
and the request share the same hostname.
For examples:
- context: `www.example.org`
- request: `www.example.org`
- `1p`: yes, `1P`: yes
- `3p`: no, `3P`: no
- context: `www.example.org`
- request: `subdomain.example.org`
- `1p`: yes, `1P`: no
- `3p`: no, `3P`: yes
- context: `www.example.org`
- request: `www.example.com`
- `1p`: no, `1P`: no
- `3p`: yes, `3P`: yes
The strict partyness options will be visually emphasized in the editor so as
to prevent mistakenly using `1P` or `3P` where weak partyness is meant to be
used.
Filter on response headers: `header=`
-------------------------------------
Currently experimental and under evaluation. Disabled by default, enable by
toggling `filterOnHeaders` to `true` in advanced settings.
Ability to filter network requests according to whether a specific response
header is present and whether it matches or does not match a specific value.
For example:
*$1p,3P,script,header=via:1\.1\s+google
The above filter is meant to block network requests which fullfill all the
following conditions:
- is weakly 1st-party to the context
- is not strictly 1st-party to the context
- is of type `script`
- has a response HTTP header named `via`, which value matches the regular
expression `1\.1\s+google`.
The matches are always performed in a case-insensitive manner.
The header value is assumed to be a literal regular expression, except for
the following special characters:
- to anchor to start of string, use leading `|`, not `^`
- to anchor to end of string, use trailing `|`, not `$`
- to invert the test, use a leading `!`
To block a network request if it merely contains a specific HTTP header is
just a matter of specifying the header name without a header value:
*$1p,3P,script,header=via
Generic exception filters can be used to disable specific block `header=`
filters, i.e. `@@*$1p,3P,script,header` will override the block `header=`
filters given as example above.
Dynamic filtering's `allow` rules override block `headers=` filters.
Important: It is key that filter authors use as many narrowing filter options
as possible when using the `header=` option, and the `header=` option should
be used ONLY when other filter options are not sufficient.
More documentation justifying the purpose of `header=` option will be
provided eventually if ever it is decided to move it from experimental to
stable status.
To be decided: to restrict usage of this filter option to only uBO's own
filter lists or "My filters".
Changes
=======
Fine tuning `queryprune=`
-------------------------
The following changes have been implemented:
The special value `*` (i.e. `queryprune=*`) means "remove all query
parameters".
If the `queryprune=` value is made only of alphanumeric characters
(including `_`), the value will be internally converted to regex equivalent
`^value=`. This ensures a better future compatibility with AdGuard's
`removeparam=`.
If the `queryprune=` value starts with `!`, the test will be inverted. This
can be used to remove all query parameters EXCEPT those who match the
specified value.
Other
-----
The legacy code to test for spurious CSP reports has been removed. This
is no longer an issue ever since uBO redirects to local resources through
web accessible resources.
Notes
=====
The following new and recently added filter options are not compatible with
Chromium's manifest v3 changes:
- `queryprune=`
- `1P`
- `3P`
- `header=`
2020-11-23 14:22:43 +01:00
|
|
|
const result = foilLargeMediaElement(details, fctxt, pageStore);
|
|
|
|
if ( result !== undefined ) { return result; }
|
2015-04-09 00:46:08 +02:00
|
|
|
}
|
|
|
|
|
2020-10-19 14:01:03 +02:00
|
|
|
// Keep in mind response headers will be modified in-place if needed, so
|
|
|
|
// `details.responseHeaders` will always point to the modified response
|
|
|
|
// headers.
|
2021-03-13 14:53:34 +01:00
|
|
|
const { responseHeaders } = details;
|
|
|
|
if ( Array.isArray(responseHeaders) === false ) { return; }
|
2020-10-19 14:01:03 +02:00
|
|
|
|
Add support for `1P`, `3P`, `header=` filter options and other changes
New filter options
==================
Strict partyness: `1P`, `3P`
----------------------------
The current options 1p/3p are meant to "weakly" match partyness, i.e. a
network request is considered 1st-party to its context as long as both the
context and the request share the same base domain.
The new partyness options are meant to check for strict partyness, i.e. a
network request will be considered 1st-party if and only if both the context
and the request share the same hostname.
For examples:
- context: `www.example.org`
- request: `www.example.org`
- `1p`: yes, `1P`: yes
- `3p`: no, `3P`: no
- context: `www.example.org`
- request: `subdomain.example.org`
- `1p`: yes, `1P`: no
- `3p`: no, `3P`: yes
- context: `www.example.org`
- request: `www.example.com`
- `1p`: no, `1P`: no
- `3p`: yes, `3P`: yes
The strict partyness options will be visually emphasized in the editor so as
to prevent mistakenly using `1P` or `3P` where weak partyness is meant to be
used.
Filter on response headers: `header=`
-------------------------------------
Currently experimental and under evaluation. Disabled by default, enable by
toggling `filterOnHeaders` to `true` in advanced settings.
Ability to filter network requests according to whether a specific response
header is present and whether it matches or does not match a specific value.
For example:
*$1p,3P,script,header=via:1\.1\s+google
The above filter is meant to block network requests which fullfill all the
following conditions:
- is weakly 1st-party to the context
- is not strictly 1st-party to the context
- is of type `script`
- has a response HTTP header named `via`, which value matches the regular
expression `1\.1\s+google`.
The matches are always performed in a case-insensitive manner.
The header value is assumed to be a literal regular expression, except for
the following special characters:
- to anchor to start of string, use leading `|`, not `^`
- to anchor to end of string, use trailing `|`, not `$`
- to invert the test, use a leading `!`
To block a network request if it merely contains a specific HTTP header is
just a matter of specifying the header name without a header value:
*$1p,3P,script,header=via
Generic exception filters can be used to disable specific block `header=`
filters, i.e. `@@*$1p,3P,script,header` will override the block `header=`
filters given as example above.
Dynamic filtering's `allow` rules override block `headers=` filters.
Important: It is key that filter authors use as many narrowing filter options
as possible when using the `header=` option, and the `header=` option should
be used ONLY when other filter options are not sufficient.
More documentation justifying the purpose of `header=` option will be
provided eventually if ever it is decided to move it from experimental to
stable status.
To be decided: to restrict usage of this filter option to only uBO's own
filter lists or "My filters".
Changes
=======
Fine tuning `queryprune=`
-------------------------
The following changes have been implemented:
The special value `*` (i.e. `queryprune=*`) means "remove all query
parameters".
If the `queryprune=` value is made only of alphanumeric characters
(including `_`), the value will be internally converted to regex equivalent
`^value=`. This ensures a better future compatibility with AdGuard's
`removeparam=`.
If the `queryprune=` value starts with `!`, the test will be inverted. This
can be used to remove all query parameters EXCEPT those who match the
specified value.
Other
-----
The legacy code to test for spurious CSP reports has been removed. This
is no longer an issue ever since uBO redirects to local resources through
web accessible resources.
Notes
=====
The following new and recently added filter options are not compatible with
Chromium's manifest v3 changes:
- `queryprune=`
- `1P`
- `3P`
- `header=`
2020-11-23 14:22:43 +01:00
|
|
|
if ( isRootDoc === false && µb.hiddenSettings.filterOnHeaders === true ) {
|
|
|
|
const result = pageStore.filterOnHeaders(fctxt, responseHeaders);
|
|
|
|
if ( result !== 0 ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( logger.enabled ) {
|
Add support for `1P`, `3P`, `header=` filter options and other changes
New filter options
==================
Strict partyness: `1P`, `3P`
----------------------------
The current options 1p/3p are meant to "weakly" match partyness, i.e. a
network request is considered 1st-party to its context as long as both the
context and the request share the same base domain.
The new partyness options are meant to check for strict partyness, i.e. a
network request will be considered 1st-party if and only if both the context
and the request share the same hostname.
For examples:
- context: `www.example.org`
- request: `www.example.org`
- `1p`: yes, `1P`: yes
- `3p`: no, `3P`: no
- context: `www.example.org`
- request: `subdomain.example.org`
- `1p`: yes, `1P`: no
- `3p`: no, `3P`: yes
- context: `www.example.org`
- request: `www.example.com`
- `1p`: no, `1P`: no
- `3p`: yes, `3P`: yes
The strict partyness options will be visually emphasized in the editor so as
to prevent mistakenly using `1P` or `3P` where weak partyness is meant to be
used.
Filter on response headers: `header=`
-------------------------------------
Currently experimental and under evaluation. Disabled by default, enable by
toggling `filterOnHeaders` to `true` in advanced settings.
Ability to filter network requests according to whether a specific response
header is present and whether it matches or does not match a specific value.
For example:
*$1p,3P,script,header=via:1\.1\s+google
The above filter is meant to block network requests which fullfill all the
following conditions:
- is weakly 1st-party to the context
- is not strictly 1st-party to the context
- is of type `script`
- has a response HTTP header named `via`, which value matches the regular
expression `1\.1\s+google`.
The matches are always performed in a case-insensitive manner.
The header value is assumed to be a literal regular expression, except for
the following special characters:
- to anchor to start of string, use leading `|`, not `^`
- to anchor to end of string, use trailing `|`, not `$`
- to invert the test, use a leading `!`
To block a network request if it merely contains a specific HTTP header is
just a matter of specifying the header name without a header value:
*$1p,3P,script,header=via
Generic exception filters can be used to disable specific block `header=`
filters, i.e. `@@*$1p,3P,script,header` will override the block `header=`
filters given as example above.
Dynamic filtering's `allow` rules override block `headers=` filters.
Important: It is key that filter authors use as many narrowing filter options
as possible when using the `header=` option, and the `header=` option should
be used ONLY when other filter options are not sufficient.
More documentation justifying the purpose of `header=` option will be
provided eventually if ever it is decided to move it from experimental to
stable status.
To be decided: to restrict usage of this filter option to only uBO's own
filter lists or "My filters".
Changes
=======
Fine tuning `queryprune=`
-------------------------
The following changes have been implemented:
The special value `*` (i.e. `queryprune=*`) means "remove all query
parameters".
If the `queryprune=` value is made only of alphanumeric characters
(including `_`), the value will be internally converted to regex equivalent
`^value=`. This ensures a better future compatibility with AdGuard's
`removeparam=`.
If the `queryprune=` value starts with `!`, the test will be inverted. This
can be used to remove all query parameters EXCEPT those who match the
specified value.
Other
-----
The legacy code to test for spurious CSP reports has been removed. This
is no longer an issue ever since uBO redirects to local resources through
web accessible resources.
Notes
=====
The following new and recently added filter options are not compatible with
Chromium's manifest v3 changes:
- `queryprune=`
- `1P`
- `3P`
- `header=`
2020-11-23 14:22:43 +01:00
|
|
|
fctxt.setRealm('network').toLogger();
|
|
|
|
}
|
|
|
|
if ( result === 1 ) {
|
2021-02-15 12:52:31 +01:00
|
|
|
pageStore.journalAddRequest(fctxt, 1);
|
Add support for `1P`, `3P`, `header=` filter options and other changes
New filter options
==================
Strict partyness: `1P`, `3P`
----------------------------
The current options 1p/3p are meant to "weakly" match partyness, i.e. a
network request is considered 1st-party to its context as long as both the
context and the request share the same base domain.
The new partyness options are meant to check for strict partyness, i.e. a
network request will be considered 1st-party if and only if both the context
and the request share the same hostname.
For examples:
- context: `www.example.org`
- request: `www.example.org`
- `1p`: yes, `1P`: yes
- `3p`: no, `3P`: no
- context: `www.example.org`
- request: `subdomain.example.org`
- `1p`: yes, `1P`: no
- `3p`: no, `3P`: yes
- context: `www.example.org`
- request: `www.example.com`
- `1p`: no, `1P`: no
- `3p`: yes, `3P`: yes
The strict partyness options will be visually emphasized in the editor so as
to prevent mistakenly using `1P` or `3P` where weak partyness is meant to be
used.
Filter on response headers: `header=`
-------------------------------------
Currently experimental and under evaluation. Disabled by default, enable by
toggling `filterOnHeaders` to `true` in advanced settings.
Ability to filter network requests according to whether a specific response
header is present and whether it matches or does not match a specific value.
For example:
*$1p,3P,script,header=via:1\.1\s+google
The above filter is meant to block network requests which fullfill all the
following conditions:
- is weakly 1st-party to the context
- is not strictly 1st-party to the context
- is of type `script`
- has a response HTTP header named `via`, which value matches the regular
expression `1\.1\s+google`.
The matches are always performed in a case-insensitive manner.
The header value is assumed to be a literal regular expression, except for
the following special characters:
- to anchor to start of string, use leading `|`, not `^`
- to anchor to end of string, use trailing `|`, not `$`
- to invert the test, use a leading `!`
To block a network request if it merely contains a specific HTTP header is
just a matter of specifying the header name without a header value:
*$1p,3P,script,header=via
Generic exception filters can be used to disable specific block `header=`
filters, i.e. `@@*$1p,3P,script,header` will override the block `header=`
filters given as example above.
Dynamic filtering's `allow` rules override block `headers=` filters.
Important: It is key that filter authors use as many narrowing filter options
as possible when using the `header=` option, and the `header=` option should
be used ONLY when other filter options are not sufficient.
More documentation justifying the purpose of `header=` option will be
provided eventually if ever it is decided to move it from experimental to
stable status.
To be decided: to restrict usage of this filter option to only uBO's own
filter lists or "My filters".
Changes
=======
Fine tuning `queryprune=`
-------------------------
The following changes have been implemented:
The special value `*` (i.e. `queryprune=*`) means "remove all query
parameters".
If the `queryprune=` value is made only of alphanumeric characters
(including `_`), the value will be internally converted to regex equivalent
`^value=`. This ensures a better future compatibility with AdGuard's
`removeparam=`.
If the `queryprune=` value starts with `!`, the test will be inverted. This
can be used to remove all query parameters EXCEPT those who match the
specified value.
Other
-----
The legacy code to test for spurious CSP reports has been removed. This
is no longer an issue ever since uBO redirects to local resources through
web accessible resources.
Notes
=====
The following new and recently added filter options are not compatible with
Chromium's manifest v3 changes:
- `queryprune=`
- `1P`
- `3P`
- `header=`
2020-11-23 14:22:43 +01:00
|
|
|
return { cancel: true };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isRootDoc === false && fctxt.itype !== fctxt.SUB_FRAME ) { return; }
|
|
|
|
|
2017-07-22 22:58:08 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/2813
|
|
|
|
// Disable the blocking of large media elements if the document is itself
|
|
|
|
// a media element: the resource was not prevented from loading so no
|
|
|
|
// point to further block large media elements for the current document.
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( isRootDoc ) {
|
2018-12-13 18:30:54 +01:00
|
|
|
const contentType = headerValueFromName('content-type', responseHeaders);
|
2018-05-16 17:50:50 +02:00
|
|
|
if ( reMediaContentTypes.test(contentType) ) {
|
2020-10-18 16:07:46 +02:00
|
|
|
pageStore.allowLargeMediaElementsUntil = 0;
|
2021-03-06 14:18:46 +01:00
|
|
|
// Fall-through: this could be an SVG document, which supports
|
|
|
|
// script tags.
|
2017-07-22 22:58:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-16 17:50:50 +02:00
|
|
|
// At this point we have a HTML document.
|
2021-04-11 15:36:56 +02:00
|
|
|
|
2021-03-13 14:53:34 +01:00
|
|
|
const filteredHTML =
|
|
|
|
µb.canFilterResponseData && filterDocument(fctxt, details) === true;
|
2018-05-16 17:50:50 +02:00
|
|
|
|
2021-03-13 14:53:34 +01:00
|
|
|
let modifiedHeaders = false;
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( httpheaderFilteringEngine.apply(fctxt, responseHeaders) === true ) {
|
2021-03-13 14:53:34 +01:00
|
|
|
modifiedHeaders = true;
|
|
|
|
}
|
|
|
|
if ( injectCSP(fctxt, pageStore, responseHeaders) === true ) {
|
|
|
|
modifiedHeaders = true;
|
|
|
|
}
|
2018-05-16 17:50:50 +02:00
|
|
|
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=1376932
|
|
|
|
// Prevent document from being cached by the browser if we modified it,
|
|
|
|
// either through HTML filtering and/or modified response headers.
|
2018-09-18 23:05:53 +02:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/229
|
|
|
|
// Use `no-cache` instead of `no-cache, no-store, must-revalidate`, this
|
|
|
|
// allows Firefox's offline mode to work as expected.
|
2018-05-16 17:50:50 +02:00
|
|
|
if ( (filteredHTML || modifiedHeaders) && dontCacheResponseHeaders ) {
|
2021-03-13 14:53:34 +01:00
|
|
|
const cacheControl = µb.hiddenSettings.cacheControlForFirefox1376932;
|
2019-08-13 14:49:37 +02:00
|
|
|
if ( cacheControl !== 'unset' ) {
|
|
|
|
let i = headerIndexFromName('cache-control', responseHeaders);
|
|
|
|
if ( i !== -1 ) {
|
|
|
|
responseHeaders[i].value = cacheControl;
|
|
|
|
} else {
|
|
|
|
responseHeaders.push({ name: 'Cache-Control', value: cacheControl });
|
|
|
|
}
|
|
|
|
modifiedHeaders = true;
|
2018-05-16 17:50:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( modifiedHeaders ) {
|
2021-03-13 14:53:34 +01:00
|
|
|
return { responseHeaders };
|
2017-01-18 00:18:28 +01:00
|
|
|
}
|
2016-08-27 17:08:56 +02:00
|
|
|
};
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const reMediaContentTypes = /^(?:audio|image|video)\//;
|
2017-07-22 22:58:08 +02:00
|
|
|
|
2019-05-31 15:02:07 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/610
|
|
|
|
|
|
|
|
const normalizeBehindTheSceneResponseHeaders = function(details) {
|
|
|
|
if ( details.type !== 'xmlhttprequest' ) { return false; }
|
|
|
|
const headers = details.responseHeaders;
|
|
|
|
if ( Array.isArray(headers) === false ) { return false; }
|
|
|
|
const contentType = headerValueFromName('content-type', headers);
|
|
|
|
if ( contentType === '' ) { return false; }
|
|
|
|
if ( reMediaContentTypes.test(contentType) === false ) { return false; }
|
|
|
|
if ( contentType.startsWith('image') ) {
|
|
|
|
details.type = 'image';
|
|
|
|
} else {
|
|
|
|
details.type = 'media';
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2017-12-28 19:49:02 +01:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
The response body filterer is responsible for:
|
|
|
|
|
|
|
|
- HTML filtering
|
|
|
|
|
|
|
|
In the spirit of efficiency, the response body filterer works this way:
|
|
|
|
|
|
|
|
If:
|
|
|
|
- HTML filtering: no.
|
|
|
|
Then:
|
|
|
|
No response body filtering is initiated.
|
|
|
|
|
|
|
|
If:
|
|
|
|
- HTML filtering: yes.
|
|
|
|
Then:
|
|
|
|
Assemble all response body data into a single buffer. Once all the
|
|
|
|
response data has been received, create a document from it. Then:
|
|
|
|
- Remove all DOM elements matching HTML filters.
|
|
|
|
Then serialize the resulting modified document as the new response
|
|
|
|
body.
|
|
|
|
|
|
|
|
**/
|
|
|
|
|
2021-03-13 14:53:34 +01:00
|
|
|
const filterDocument = (( ) => {
|
2018-12-13 18:30:54 +01:00
|
|
|
const filterers = new Map();
|
|
|
|
let domParser, xmlSerializer,
|
2018-01-05 19:15:56 +01:00
|
|
|
utf8TextDecoder, textDecoder, textEncoder;
|
2017-12-28 19:49:02 +01:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const textDecode = function(encoding, buffer) {
|
2018-02-18 13:16:10 +01:00
|
|
|
if (
|
|
|
|
textDecoder !== undefined &&
|
|
|
|
textDecoder.encoding !== encoding
|
|
|
|
) {
|
|
|
|
textDecoder = undefined;
|
|
|
|
}
|
|
|
|
if ( textDecoder === undefined ) {
|
|
|
|
textDecoder = new TextDecoder(encoding);
|
|
|
|
}
|
|
|
|
return textDecoder.decode(buffer);
|
|
|
|
};
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const reContentTypeDocument = /^(?:text\/html|application\/xhtml\+xml)/i;
|
|
|
|
const reContentTypeCharset = /charset=['"]?([^'" ]+)/i;
|
2018-01-03 19:59:38 +01:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const mimeFromContentType = function(contentType) {
|
|
|
|
const match = reContentTypeDocument.exec(contentType);
|
2018-03-01 20:12:16 +01:00
|
|
|
if ( match !== null ) {
|
|
|
|
return match[0].toLowerCase();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const charsetFromContentType = function(contentType) {
|
|
|
|
const match = reContentTypeCharset.exec(contentType);
|
2018-01-05 00:26:52 +01:00
|
|
|
if ( match !== null ) {
|
|
|
|
return match[1].toLowerCase();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const charsetFromDoc = function(doc) {
|
|
|
|
let meta = doc.querySelector('meta[charset]');
|
2018-01-05 00:26:52 +01:00
|
|
|
if ( meta !== null ) {
|
|
|
|
return meta.getAttribute('charset').toLowerCase();
|
|
|
|
}
|
|
|
|
meta = doc.querySelector(
|
|
|
|
'meta[http-equiv="content-type" i][content]'
|
|
|
|
);
|
|
|
|
if ( meta !== null ) {
|
|
|
|
return charsetFromContentType(meta.getAttribute('content'));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const streamClose = function(filterer, buffer) {
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( buffer !== undefined ) {
|
|
|
|
filterer.stream.write(buffer);
|
|
|
|
} else if ( filterer.buffer !== undefined ) {
|
|
|
|
filterer.stream.write(filterer.buffer);
|
|
|
|
}
|
|
|
|
filterer.stream.close();
|
|
|
|
};
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const onStreamData = function(ev) {
|
|
|
|
const filterer = filterers.get(this);
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( filterer === undefined ) {
|
|
|
|
this.write(ev.data);
|
|
|
|
this.disconnect();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (
|
|
|
|
this.status !== 'transferringdata' &&
|
|
|
|
this.status !== 'finishedtransferringdata'
|
|
|
|
) {
|
|
|
|
filterers.delete(this);
|
|
|
|
this.disconnect();
|
|
|
|
return;
|
|
|
|
}
|
2017-12-30 17:21:23 +01:00
|
|
|
// TODO:
|
|
|
|
// - Possibly improve buffer growth, if benchmarking shows it's worth
|
|
|
|
// it.
|
|
|
|
// - Also evaluate whether keeping a list of buffers and then decoding
|
|
|
|
// them in sequence using TextDecoder's "stream" option is more
|
|
|
|
// efficient. Can the data buffers be safely kept around for later
|
|
|
|
// use?
|
|
|
|
// - Informal, quick benchmarks seem to show most of the overhead is
|
|
|
|
// from calling TextDecoder.decode() and TextEncoder.encode(), and if
|
|
|
|
// confirmed, there is nothing which can be done uBO-side to reduce
|
|
|
|
// overhead.
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( filterer.buffer === null ) {
|
|
|
|
filterer.buffer = new Uint8Array(ev.data);
|
|
|
|
return;
|
|
|
|
}
|
2018-12-13 18:30:54 +01:00
|
|
|
const buffer = new Uint8Array(
|
2017-12-28 19:49:02 +01:00
|
|
|
filterer.buffer.byteLength +
|
|
|
|
ev.data.byteLength
|
|
|
|
);
|
|
|
|
buffer.set(filterer.buffer);
|
|
|
|
buffer.set(new Uint8Array(ev.data), filterer.buffer.byteLength);
|
|
|
|
filterer.buffer = buffer;
|
|
|
|
};
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const onStreamStop = function() {
|
|
|
|
const filterer = filterers.get(this);
|
2017-12-28 19:49:02 +01:00
|
|
|
filterers.delete(this);
|
|
|
|
if ( filterer === undefined || filterer.buffer === null ) {
|
|
|
|
this.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ( this.status !== 'finishedtransferringdata' ) { return; }
|
|
|
|
|
|
|
|
if ( domParser === undefined ) {
|
|
|
|
domParser = new DOMParser();
|
|
|
|
xmlSerializer = new XMLSerializer();
|
|
|
|
}
|
|
|
|
if ( textEncoder === undefined ) {
|
|
|
|
textEncoder = new TextEncoder();
|
|
|
|
}
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
let doc;
|
2018-01-05 19:15:56 +01:00
|
|
|
|
|
|
|
// If stream encoding is still unknnown, try to extract from document.
|
2018-12-13 18:30:54 +01:00
|
|
|
let charsetFound = filterer.charset,
|
2018-02-18 13:16:10 +01:00
|
|
|
charsetUsed = charsetFound;
|
|
|
|
if ( charsetFound === undefined ) {
|
2018-01-05 19:15:56 +01:00
|
|
|
if ( utf8TextDecoder === undefined ) {
|
|
|
|
utf8TextDecoder = new TextDecoder();
|
|
|
|
}
|
|
|
|
doc = domParser.parseFromString(
|
2018-02-18 13:16:10 +01:00
|
|
|
utf8TextDecoder.decode(filterer.buffer.slice(0, 1024)),
|
2018-03-01 20:12:16 +01:00
|
|
|
filterer.mime
|
2018-01-05 19:15:56 +01:00
|
|
|
);
|
2018-02-18 13:16:10 +01:00
|
|
|
charsetFound = charsetFromDoc(doc);
|
2021-07-29 01:48:38 +02:00
|
|
|
charsetUsed = textEncode.normalizeCharset(charsetFound);
|
2018-02-18 13:16:10 +01:00
|
|
|
if ( charsetUsed === undefined ) {
|
|
|
|
return streamClose(filterer);
|
2018-01-05 19:15:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
doc = domParser.parseFromString(
|
2018-02-18 13:16:10 +01:00
|
|
|
textDecode(charsetUsed, filterer.buffer),
|
2018-03-01 20:12:16 +01:00
|
|
|
filterer.mime
|
2017-12-28 19:49:02 +01:00
|
|
|
);
|
|
|
|
|
2018-02-18 13:16:10 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/3507
|
|
|
|
// In case of no explicit charset found, try to find one again, but
|
|
|
|
// this time with the whole document parsed.
|
|
|
|
if ( charsetFound === undefined ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
charsetFound = textEncode.normalizeCharset(charsetFromDoc(doc));
|
2018-02-18 13:16:10 +01:00
|
|
|
if ( charsetFound !== charsetUsed ) {
|
|
|
|
if ( charsetFound === undefined ) {
|
|
|
|
return streamClose(filterer);
|
|
|
|
}
|
|
|
|
charsetUsed = charsetFound;
|
|
|
|
doc = domParser.parseFromString(
|
|
|
|
textDecode(charsetFound, filterer.buffer),
|
2018-03-01 20:12:16 +01:00
|
|
|
filterer.mime
|
2018-02-18 13:16:10 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
let modified = false;
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( filterer.selectors !== undefined ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( htmlFilteringEngine.apply(doc, filterer) ) {
|
2017-12-28 19:49:02 +01:00
|
|
|
modified = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( modified === false ) {
|
2018-02-18 13:16:10 +01:00
|
|
|
return streamClose(filterer);
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://stackoverflow.com/questions/6088972/get-doctype-of-an-html-as-string-with-javascript/10162353#10162353
|
2018-12-13 18:30:54 +01:00
|
|
|
const doctypeStr = doc.doctype instanceof Object ?
|
2017-12-28 19:49:02 +01:00
|
|
|
xmlSerializer.serializeToString(doc.doctype) + '\n' :
|
|
|
|
'';
|
|
|
|
|
2018-01-03 05:06:16 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/3391
|
2018-12-13 18:30:54 +01:00
|
|
|
let encodedStream = textEncoder.encode(
|
2018-01-03 05:06:16 +01:00
|
|
|
doctypeStr +
|
|
|
|
doc.documentElement.outerHTML
|
2017-12-28 19:49:02 +01:00
|
|
|
);
|
2018-02-18 13:16:10 +01:00
|
|
|
if ( charsetUsed !== 'utf-8' ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
encodedStream = textEncode.encode(
|
2018-02-18 13:16:10 +01:00
|
|
|
charsetUsed,
|
2018-01-03 05:06:16 +01:00
|
|
|
encodedStream
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
streamClose(filterer, encodedStream);
|
2017-12-28 19:49:02 +01:00
|
|
|
};
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const onStreamError = function() {
|
2017-12-28 19:49:02 +01:00
|
|
|
filterers.delete(this);
|
|
|
|
};
|
|
|
|
|
2021-03-13 14:53:34 +01:00
|
|
|
return function(fctxt, extras) {
|
2018-02-03 15:34:27 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/3478
|
2018-12-13 18:30:54 +01:00
|
|
|
const statusCode = extras.statusCode || 0;
|
2018-02-03 15:34:27 +01:00
|
|
|
if ( statusCode !== 0 && (statusCode < 200 || statusCode >= 300) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const hostname = fctxt.getHostname();
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( hostname === '' ) { return; }
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const domain = fctxt.getDomain();
|
2017-12-28 19:49:02 +01:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const request = {
|
2017-12-28 19:49:02 +01:00
|
|
|
stream: undefined,
|
2018-12-13 18:30:54 +01:00
|
|
|
tabId: fctxt.tabId,
|
|
|
|
url: fctxt.url,
|
2017-12-28 19:49:02 +01:00
|
|
|
hostname: hostname,
|
|
|
|
domain: domain,
|
2021-07-25 16:55:35 +02:00
|
|
|
entity: entityFromDomain(domain),
|
2017-12-28 19:49:02 +01:00
|
|
|
selectors: undefined,
|
|
|
|
buffer: null,
|
2018-04-03 00:40:29 +02:00
|
|
|
mime: 'text/html',
|
2017-12-28 19:49:02 +01:00
|
|
|
charset: undefined
|
|
|
|
};
|
2018-02-21 14:19:43 +01:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
request.selectors = htmlFilteringEngine.retrieve(request);
|
2018-04-24 23:12:41 +02:00
|
|
|
if ( request.selectors === undefined ) { return; }
|
2017-12-28 19:49:02 +01:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const headers = extras.responseHeaders;
|
|
|
|
const contentType = headerValueFromName('content-type', headers);
|
2017-12-28 19:49:02 +01:00
|
|
|
if ( contentType !== '' ) {
|
2018-03-01 20:12:16 +01:00
|
|
|
request.mime = mimeFromContentType(contentType);
|
|
|
|
if ( request.mime === undefined ) { return; }
|
2018-12-13 18:30:54 +01:00
|
|
|
let charset = charsetFromContentType(contentType);
|
2018-01-05 00:26:52 +01:00
|
|
|
if ( charset !== undefined ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
charset = textEncode.normalizeCharset(charset);
|
2018-01-03 19:59:38 +01:00
|
|
|
if ( charset === undefined ) { return; }
|
2018-01-05 00:26:52 +01:00
|
|
|
request.charset = charset;
|
2017-12-28 19:49:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// https://bugzilla.mozilla.org/show_bug.cgi?id=1426789
|
2023-05-14 15:06:21 +02:00
|
|
|
const disposition = headerValueFromName('content-disposition', headers);
|
|
|
|
if ( disposition !== '' && disposition.startsWith('inline') === false ) { return; }
|
2017-12-28 19:49:02 +01:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const stream = request.stream =
|
|
|
|
browser.webRequest.filterResponseData(extras.requestId);
|
2017-12-28 19:49:02 +01:00
|
|
|
stream.ondata = onStreamData;
|
|
|
|
stream.onstop = onStreamStop;
|
|
|
|
stream.onerror = onStreamError;
|
|
|
|
filterers.set(stream, request);
|
2018-05-16 17:50:50 +02:00
|
|
|
|
|
|
|
return true;
|
2017-12-28 19:49:02 +01:00
|
|
|
};
|
|
|
|
})();
|
|
|
|
|
2016-08-27 17:08:56 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const injectCSP = function(fctxt, pageStore, responseHeaders) {
|
|
|
|
const cspSubsets = [];
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
const requestType = fctxt.type;
|
2015-01-24 18:06:22 +01:00
|
|
|
|
2017-05-12 16:35:11 +02:00
|
|
|
// Start collecting policies >>>>>>>>
|
|
|
|
|
|
|
|
// ======== built-in policies
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const builtinDirectives = [];
|
2017-09-11 15:53:42 +02:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
if ( pageStore.filterScripting(fctxt, true) === 1 ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
builtinDirectives.push(µb.cspNoScripting);
|
|
|
|
if ( logger.enabled ) {
|
2019-01-12 22:36:20 +01:00
|
|
|
fctxt.setRealm('network').setType('scripting').toLogger();
|
2018-09-01 00:47:02 +02:00
|
|
|
}
|
2019-02-15 13:37:43 +01:00
|
|
|
}
|
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/422
|
|
|
|
// We need to derive a special context for filtering `inline-script`,
|
|
|
|
// as the embedding document for this "resource" will always be the
|
|
|
|
// frame itself, not that of the parent of the frame.
|
|
|
|
else {
|
|
|
|
const fctxt2 = fctxt.duplicate();
|
|
|
|
fctxt2.type = 'inline-script';
|
|
|
|
fctxt2.setDocOriginFromURL(fctxt.url);
|
|
|
|
const result = pageStore.filterRequest(fctxt2);
|
2018-12-21 20:16:17 +01:00
|
|
|
if ( result === 1 ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
builtinDirectives.push(µb.cspNoInlineScript);
|
2018-12-21 20:16:17 +01:00
|
|
|
}
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( result === 2 && logger.enabled ) {
|
2019-02-15 13:37:43 +01:00
|
|
|
fctxt2.setRealm('network').toLogger();
|
2018-09-01 00:47:02 +02:00
|
|
|
}
|
2016-08-27 17:08:56 +02:00
|
|
|
}
|
2015-06-05 01:27:03 +02:00
|
|
|
|
2017-09-11 15:53:42 +02:00
|
|
|
// https://github.com/gorhill/uBlock/issues/1539
|
|
|
|
// - Use a CSP to also forbid inline fonts if remote fonts are blocked.
|
2018-12-13 18:30:54 +01:00
|
|
|
fctxt.type = 'inline-font';
|
|
|
|
if ( pageStore.filterRequest(fctxt) === 1 ) {
|
2021-07-29 01:48:38 +02:00
|
|
|
builtinDirectives.push(µb.cspNoInlineFont);
|
|
|
|
if ( logger.enabled ) {
|
2019-01-12 22:36:20 +01:00
|
|
|
fctxt.setRealm('network').toLogger();
|
2017-09-11 15:53:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( builtinDirectives.length !== 0 ) {
|
2019-05-11 16:40:34 +02:00
|
|
|
cspSubsets[0] = builtinDirectives.join(', ');
|
2017-09-11 15:53:42 +02:00
|
|
|
}
|
|
|
|
|
2017-05-12 16:35:11 +02:00
|
|
|
// ======== filter-based policies
|
|
|
|
|
|
|
|
// Static filtering.
|
|
|
|
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
fctxt.type = requestType;
|
2019-09-28 17:30:26 +02:00
|
|
|
const staticDirectives =
|
2021-07-29 01:48:38 +02:00
|
|
|
staticNetFilteringEngine.matchAndFetchModifiers(fctxt, 'csp');
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
if ( staticDirectives !== undefined ) {
|
|
|
|
for ( const directive of staticDirectives ) {
|
|
|
|
if ( directive.result !== 1 ) { continue; }
|
Refactoring work in static network filtering engine
The original motivation is to further speed up launch time
for either non-selfie-based and selfie-based initialization
of the static network filtering engine (SNFE).
As a result of the refactoring:
Filters are no longer instance-based, they are sequence-of-
integer-based. This eliminates the need to create instances
of filters at launch, and consequently eliminates all the
calls to class constructors, the resulting churning of memory,
and so forth.
All the properties defining filter instances are now as much
as possible 32-bit integer-based, and these are allocated in a
single module-scoped typed array -- this eliminates the need
to allocate memory for every filter being instantiated.
Not all filter properties can be represented as a 32-bit
integer, and in this case a filter class can allocate slots
into another module-scoped array of references.
As a result, this eliminates a lot of memory allocations when
the SNFE is populated with filters, and this makes the saving
and loading of selfie more straightforward, as the operation
is reduced to saving/loading two arrays, one of 32-bit
integers, and the other, much smaller, an array JSON-able
values.
All filter classes now only contain static methods, and all
of these methods are called with an index to the specific
filter data in the module-scoped array of 32-bit integers.
The filter sequences (used to avoid the use of JS arrays) are
also allocated in the single module-scoped array of 32-bit
integers -- they used to be stored in their own dedicated
array.
Additionally, some filters are now loaded more in a deferred
way, so as reduce uBO's time-to-readiness -- the outcome of
this still needs to be evaluated, time-to-readiness is
especially a concern in Firefox for Android or less powerful
computers.
2021-12-04 17:16:44 +01:00
|
|
|
cspSubsets.push(directive.value);
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
}
|
2019-09-28 17:30:26 +02:00
|
|
|
}
|
2017-05-12 16:35:11 +02:00
|
|
|
|
|
|
|
// URL filtering `allow` rules override static filtering.
|
|
|
|
if (
|
|
|
|
cspSubsets.length !== 0 &&
|
2021-07-29 01:48:38 +02:00
|
|
|
sessionURLFiltering.evaluateZ(
|
2018-12-13 18:30:54 +01:00
|
|
|
fctxt.getTabHostname(),
|
|
|
|
fctxt.url,
|
|
|
|
'csp'
|
|
|
|
) === 2
|
2017-05-12 16:35:11 +02:00
|
|
|
) {
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( logger.enabled ) {
|
2019-01-12 22:36:20 +01:00
|
|
|
fctxt.setRealm('network')
|
2018-12-13 18:30:54 +01:00
|
|
|
.setType('csp')
|
2021-07-29 01:48:38 +02:00
|
|
|
.setFilter(sessionURLFiltering.toLogData())
|
2018-12-13 18:30:54 +01:00
|
|
|
.toLogger();
|
2017-01-18 00:18:28 +01:00
|
|
|
}
|
2017-05-12 16:35:11 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-17 15:25:11 +02:00
|
|
|
// Dynamic filtering `allow` rules override static filtering.
|
2017-05-12 16:35:11 +02:00
|
|
|
if (
|
|
|
|
cspSubsets.length !== 0 &&
|
|
|
|
µb.userSettings.advancedUserEnabled &&
|
2021-07-29 01:48:38 +02:00
|
|
|
sessionFirewall.evaluateCellZY(
|
2018-12-13 18:30:54 +01:00
|
|
|
fctxt.getTabHostname(),
|
|
|
|
fctxt.getTabHostname(),
|
|
|
|
'*'
|
|
|
|
) === 2
|
2017-05-12 16:35:11 +02:00
|
|
|
) {
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( logger.enabled ) {
|
2019-01-12 22:36:20 +01:00
|
|
|
fctxt.setRealm('network')
|
2018-12-13 18:30:54 +01:00
|
|
|
.setType('csp')
|
2021-07-29 01:48:38 +02:00
|
|
|
.setFilter(sessionFirewall.toLogData())
|
2018-12-13 18:30:54 +01:00
|
|
|
.toLogger();
|
2017-02-06 21:34:31 +01:00
|
|
|
}
|
2017-05-12 16:35:11 +02:00
|
|
|
return;
|
2016-08-27 17:08:56 +02:00
|
|
|
}
|
|
|
|
|
2017-05-17 15:25:11 +02:00
|
|
|
// <<<<<<<< All policies have been collected
|
|
|
|
|
2017-05-12 16:35:11 +02:00
|
|
|
// Static CSP policies will be applied.
|
2019-09-28 17:30:26 +02:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( logger.enabled && staticDirectives !== undefined ) {
|
Add new filter option `queryprune=`
Related issue:
- https://github.com/uBlockOrigin/uBlock-issues/issues/760
The purpose of this new network filter option is to remove
query parameters form the URL of network requests.
The name `queryprune` has been picked over `querystrip`
since the purpose of the option is to remove some
parameters from the URL rather than all parameters.
`queryprune` is a modifier option (like `csp`) in that it
does not cause a network request to be blocked but rather
modified before being emitted.
`queryprune` must be assigned a value, which value will
determine which parameters from a query string will be
removed. The syntax for the value is that of regular
expression *except* for the following rules:
- do not wrap the regex directive between `/`
- do not use regex special values `^` and `$`
- do not use literal comma character in the value,
though you can use hex-encoded version, `\x2c`
- to match the start of a query parameter, prepend `|`
- to match the end of a query parameter, append `|`
`queryprune` regex-like values will be tested against each
key-value parameter pair as `[key]=[value]` string. This
way you can prune according to either the key, the value,
or both.
This commit introduces the concept of modifier filter
options, which as of now are:
- `csp=`
- `queryprune=`
They both work in similar way when used with `important`
option or when used in exception filters. Modifier
options can apply to any network requests, hence the
logger reports the type of the network requests, and no
longer use the modifier as the type, i.e. `csp` filters
are no longer reported as requests of type `csp`.
Though modifier options can apply to any network requests,
for the time being the `csp=` modifier option still apply
only to top or embedded (frame) documents, just as before.
In some future we may want to apply `csp=` directives to
network requests of type script, to control the behavior
of service workers for example.
A new built-in filter expression has been added to the
logger: "modified", which allow to see all the network
requests which were modified before being emitted. The
translation work for this new option will be available
in a future commit.
2020-10-31 15:42:53 +01:00
|
|
|
fctxt.setRealm('network')
|
|
|
|
.pushFilters(staticDirectives.map(a => a.logData()))
|
|
|
|
.toLogger();
|
2017-05-12 16:35:11 +02:00
|
|
|
}
|
2016-07-01 04:03:29 +02:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
if ( cspSubsets.length === 0 ) { return; }
|
2017-05-17 15:25:11 +02:00
|
|
|
|
2023-04-13 17:41:52 +02:00
|
|
|
µb.updateToolbarIcon(fctxt.tabId, 0b0010);
|
2014-09-24 23:38:22 +02:00
|
|
|
|
2018-03-13 22:24:07 +01:00
|
|
|
// Use comma to merge CSP directives.
|
2017-05-12 16:35:11 +02:00
|
|
|
// Ref.: https://www.w3.org/TR/CSP2/#implementation-considerations
|
2018-03-13 22:24:07 +01:00
|
|
|
//
|
|
|
|
// https://github.com/gorhill/uMatrix/issues/967
|
2018-03-14 17:06:49 +01:00
|
|
|
// Inject a new CSP header rather than modify an existing one, except
|
|
|
|
// if the current environment does not support merging headers:
|
|
|
|
// Firefox 58/webext and less can't merge CSP headers, so we will merge
|
|
|
|
// them here.
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
responseHeaders.push({
|
2017-05-12 16:35:11 +02:00
|
|
|
name: 'Content-Security-Policy',
|
2018-03-13 22:24:07 +01:00
|
|
|
value: cspSubsets.join(', ')
|
2017-05-12 16:35:11 +02:00
|
|
|
});
|
|
|
|
|
2018-05-16 17:50:50 +02:00
|
|
|
return true;
|
2018-04-04 18:42:01 +02:00
|
|
|
};
|
|
|
|
|
2015-08-13 22:03:37 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2016-01-17 19:30:43 +01:00
|
|
|
// https://github.com/gorhill/uBlock/issues/1163
|
2020-10-19 14:01:03 +02:00
|
|
|
// "Block elements by size".
|
|
|
|
// https://github.com/gorhill/uBlock/issues/1390#issuecomment-187310719
|
|
|
|
// Do not foil when the media element is fetched from the browser
|
|
|
|
// cache. This works only when the webext API supports the `fromCache`
|
|
|
|
// property (Firefox).
|
|
|
|
|
|
|
|
const foilLargeMediaElement = function(details, fctxt, pageStore) {
|
|
|
|
if ( details.fromCache === true ) { return; }
|
2016-01-17 19:30:43 +01:00
|
|
|
|
2019-04-24 14:30:54 +02:00
|
|
|
let size = 0;
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( µb.userSettings.largeMediaSize !== 0 ) {
|
2020-10-19 14:01:03 +02:00
|
|
|
const headers = details.responseHeaders;
|
|
|
|
const i = headerIndexFromName('content-length', headers);
|
2019-04-24 14:30:54 +02:00
|
|
|
if ( i === -1 ) { return; }
|
2020-10-19 14:01:03 +02:00
|
|
|
size = parseInt(headers[i].value, 10) || 0;
|
2019-04-24 14:30:54 +02:00
|
|
|
}
|
2016-11-08 21:53:08 +01:00
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const result = pageStore.filterLargeMediaElement(fctxt, size);
|
2017-05-12 16:35:11 +02:00
|
|
|
if ( result === 0 ) { return; }
|
2016-01-17 19:30:43 +01:00
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
if ( logger.enabled ) {
|
2019-01-12 22:36:20 +01:00
|
|
|
fctxt.setRealm('network').toLogger();
|
2016-01-17 19:30:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return { cancel: true };
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2015-08-13 22:03:37 +02:00
|
|
|
// Caller must ensure headerName is normalized to lower case.
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const headerIndexFromName = function(headerName, headers) {
|
|
|
|
let i = headers.length;
|
2015-08-13 22:03:37 +02:00
|
|
|
while ( i-- ) {
|
|
|
|
if ( headers[i].name.toLowerCase() === headerName ) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return -1;
|
2014-09-24 23:38:22 +02:00
|
|
|
};
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const headerValueFromName = function(headerName, headers) {
|
|
|
|
const i = headerIndexFromName(headerName, headers);
|
2017-07-22 22:58:08 +02:00
|
|
|
return i !== -1 ? headers[i].value : '';
|
|
|
|
};
|
|
|
|
|
2014-09-24 23:38:22 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
2018-12-13 18:30:54 +01:00
|
|
|
const strictBlockBypasser = {
|
2018-10-29 13:56:51 +01:00
|
|
|
hostnameToDeadlineMap: new Map(),
|
2023-04-09 19:38:16 +02:00
|
|
|
cleanupTimer: vAPI.defer.create(( ) => {
|
|
|
|
strictBlockBypasser.cleanup();
|
|
|
|
}),
|
2018-10-29 13:56:51 +01:00
|
|
|
|
|
|
|
cleanup: function() {
|
2018-12-13 18:30:54 +01:00
|
|
|
for ( const [ hostname, deadline ] of this.hostnameToDeadlineMap ) {
|
2018-10-29 13:56:51 +01:00
|
|
|
if ( deadline <= Date.now() ) {
|
|
|
|
this.hostnameToDeadlineMap.delete(hostname);
|
2015-04-06 16:26:32 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-29 13:56:51 +01:00
|
|
|
},
|
|
|
|
|
2023-04-09 19:38:16 +02:00
|
|
|
revokeTime: function() {
|
|
|
|
return Date.now() + µb.hiddenSettings.strictBlockingBypassDuration * 1000;
|
|
|
|
},
|
|
|
|
|
2018-10-29 13:56:51 +01:00
|
|
|
bypass: function(hostname) {
|
|
|
|
if ( typeof hostname !== 'string' || hostname === '' ) { return; }
|
2023-04-09 19:38:16 +02:00
|
|
|
this.hostnameToDeadlineMap.set(hostname, this.revokeTime());
|
2018-10-29 13:56:51 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
isBypassed: function(hostname) {
|
|
|
|
if ( this.hostnameToDeadlineMap.size === 0 ) { return false; }
|
2023-04-09 19:38:16 +02:00
|
|
|
this.cleanupTimer.on({ sec: µb.hiddenSettings.strictBlockingBypassDuration + 10 });
|
2018-10-29 13:56:51 +01:00
|
|
|
for (;;) {
|
2018-12-13 18:30:54 +01:00
|
|
|
const deadline = this.hostnameToDeadlineMap.get(hostname);
|
2018-10-29 13:56:51 +01:00
|
|
|
if ( deadline !== undefined ) {
|
|
|
|
if ( deadline > Date.now() ) {
|
2023-04-09 19:38:16 +02:00
|
|
|
this.hostnameToDeadlineMap.set(hostname, this.revokeTime());
|
2018-10-29 13:56:51 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
this.hostnameToDeadlineMap.delete(hostname);
|
|
|
|
}
|
2018-12-13 18:30:54 +01:00
|
|
|
const pos = hostname.indexOf('.');
|
2018-10-29 13:56:51 +01:00
|
|
|
if ( pos === -1 ) { break; }
|
|
|
|
hostname = hostname.slice(pos + 1);
|
|
|
|
}
|
|
|
|
return false;
|
2015-04-06 16:26:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2022-11-06 22:54:32 +01:00
|
|
|
// https://github.com/uBlockOrigin/uBlock-issues/issues/2350
|
|
|
|
// Added scriptlet injection attempt at onResponseStarted time as per
|
|
|
|
// https://github.com/AdguardTeam/AdguardBrowserExtension/issues/1029 and
|
|
|
|
// https://github.com/AdguardTeam/AdguardBrowserExtension/blob/9ab85be5/Extension/src/background/webrequest.js#L620
|
|
|
|
|
2021-07-29 01:48:38 +02:00
|
|
|
const webRequest = {
|
2021-10-07 20:41:29 +02:00
|
|
|
onBeforeRequest,
|
|
|
|
|
2019-06-30 16:09:27 +02:00
|
|
|
start: (( ) => {
|
|
|
|
vAPI.net = new vAPI.Net();
|
2022-02-13 15:24:57 +01:00
|
|
|
if ( vAPI.Net.canSuspend() ) {
|
|
|
|
vAPI.net.suspend();
|
|
|
|
}
|
2018-12-23 23:59:31 +01:00
|
|
|
|
2023-04-14 04:24:49 +02:00
|
|
|
return ( ) => {
|
2019-06-30 16:09:27 +02:00
|
|
|
vAPI.net.setSuspendableListener(onBeforeRequest);
|
2018-10-28 14:58:25 +01:00
|
|
|
vAPI.net.addListener(
|
|
|
|
'onHeadersReceived',
|
|
|
|
onHeadersReceived,
|
Add support for `1P`, `3P`, `header=` filter options and other changes
New filter options
==================
Strict partyness: `1P`, `3P`
----------------------------
The current options 1p/3p are meant to "weakly" match partyness, i.e. a
network request is considered 1st-party to its context as long as both the
context and the request share the same base domain.
The new partyness options are meant to check for strict partyness, i.e. a
network request will be considered 1st-party if and only if both the context
and the request share the same hostname.
For examples:
- context: `www.example.org`
- request: `www.example.org`
- `1p`: yes, `1P`: yes
- `3p`: no, `3P`: no
- context: `www.example.org`
- request: `subdomain.example.org`
- `1p`: yes, `1P`: no
- `3p`: no, `3P`: yes
- context: `www.example.org`
- request: `www.example.com`
- `1p`: no, `1P`: no
- `3p`: yes, `3P`: yes
The strict partyness options will be visually emphasized in the editor so as
to prevent mistakenly using `1P` or `3P` where weak partyness is meant to be
used.
Filter on response headers: `header=`
-------------------------------------
Currently experimental and under evaluation. Disabled by default, enable by
toggling `filterOnHeaders` to `true` in advanced settings.
Ability to filter network requests according to whether a specific response
header is present and whether it matches or does not match a specific value.
For example:
*$1p,3P,script,header=via:1\.1\s+google
The above filter is meant to block network requests which fullfill all the
following conditions:
- is weakly 1st-party to the context
- is not strictly 1st-party to the context
- is of type `script`
- has a response HTTP header named `via`, which value matches the regular
expression `1\.1\s+google`.
The matches are always performed in a case-insensitive manner.
The header value is assumed to be a literal regular expression, except for
the following special characters:
- to anchor to start of string, use leading `|`, not `^`
- to anchor to end of string, use trailing `|`, not `$`
- to invert the test, use a leading `!`
To block a network request if it merely contains a specific HTTP header is
just a matter of specifying the header name without a header value:
*$1p,3P,script,header=via
Generic exception filters can be used to disable specific block `header=`
filters, i.e. `@@*$1p,3P,script,header` will override the block `header=`
filters given as example above.
Dynamic filtering's `allow` rules override block `headers=` filters.
Important: It is key that filter authors use as many narrowing filter options
as possible when using the `header=` option, and the `header=` option should
be used ONLY when other filter options are not sufficient.
More documentation justifying the purpose of `header=` option will be
provided eventually if ever it is decided to move it from experimental to
stable status.
To be decided: to restrict usage of this filter option to only uBO's own
filter lists or "My filters".
Changes
=======
Fine tuning `queryprune=`
-------------------------
The following changes have been implemented:
The special value `*` (i.e. `queryprune=*`) means "remove all query
parameters".
If the `queryprune=` value is made only of alphanumeric characters
(including `_`), the value will be internally converted to regex equivalent
`^value=`. This ensures a better future compatibility with AdGuard's
`removeparam=`.
If the `queryprune=` value starts with `!`, the test will be inverted. This
can be used to remove all query parameters EXCEPT those who match the
specified value.
Other
-----
The legacy code to test for spurious CSP reports has been removed. This
is no longer an issue ever since uBO redirects to local resources through
web accessible resources.
Notes
=====
The following new and recently added filter options are not compatible with
Chromium's manifest v3 changes:
- `queryprune=`
- `1P`
- `3P`
- `header=`
2020-11-23 14:22:43 +01:00
|
|
|
{ urls: [ 'http://*/*', 'https://*/*' ] },
|
2018-10-28 14:58:25 +01:00
|
|
|
[ 'blocking', 'responseHeaders' ]
|
|
|
|
);
|
2022-11-06 22:54:32 +01:00
|
|
|
vAPI.net.addListener(
|
|
|
|
'onResponseStarted',
|
|
|
|
details => {
|
2023-03-03 18:14:36 +01:00
|
|
|
if ( details.tabId === -1 ) { return; }
|
2022-11-06 22:54:32 +01:00
|
|
|
const pageStore = µb.pageStoreFromTabId(details.tabId);
|
|
|
|
if ( pageStore === null ) { return; }
|
|
|
|
if ( pageStore.getNetFilteringSwitch() === false ) { return; }
|
|
|
|
scriptletFilteringEngine.injectNow(details);
|
|
|
|
},
|
|
|
|
{
|
|
|
|
types: [ 'main_frame', 'sub_frame' ],
|
|
|
|
urls: [ 'http://*/*', 'https://*/*' ]
|
|
|
|
}
|
|
|
|
);
|
2023-04-14 15:55:06 +02:00
|
|
|
vAPI.defer.once({ sec: µb.hiddenSettings.toolbarWarningTimeout }).then(( ) => {
|
2023-04-13 17:41:52 +02:00
|
|
|
if ( vAPI.net.hasUnprocessedRequest() === false ) { return; }
|
|
|
|
vAPI.net.removeUnprocessedRequest();
|
2023-04-13 21:03:19 +02:00
|
|
|
return vAPI.tabs.getCurrent();
|
|
|
|
}).then(tab => {
|
|
|
|
if ( tab instanceof Object === false ) { return; }
|
|
|
|
µb.updateToolbarIcon(tab.id, 0b0110);
|
2023-04-13 17:41:52 +02:00
|
|
|
});
|
2021-12-30 15:24:38 +01:00
|
|
|
vAPI.net.unsuspend({ all: true });
|
2018-10-28 14:58:25 +01:00
|
|
|
};
|
|
|
|
})(),
|
2015-03-26 00:28:22 +01:00
|
|
|
|
Add support for `1P`, `3P`, `header=` filter options and other changes
New filter options
==================
Strict partyness: `1P`, `3P`
----------------------------
The current options 1p/3p are meant to "weakly" match partyness, i.e. a
network request is considered 1st-party to its context as long as both the
context and the request share the same base domain.
The new partyness options are meant to check for strict partyness, i.e. a
network request will be considered 1st-party if and only if both the context
and the request share the same hostname.
For examples:
- context: `www.example.org`
- request: `www.example.org`
- `1p`: yes, `1P`: yes
- `3p`: no, `3P`: no
- context: `www.example.org`
- request: `subdomain.example.org`
- `1p`: yes, `1P`: no
- `3p`: no, `3P`: yes
- context: `www.example.org`
- request: `www.example.com`
- `1p`: no, `1P`: no
- `3p`: yes, `3P`: yes
The strict partyness options will be visually emphasized in the editor so as
to prevent mistakenly using `1P` or `3P` where weak partyness is meant to be
used.
Filter on response headers: `header=`
-------------------------------------
Currently experimental and under evaluation. Disabled by default, enable by
toggling `filterOnHeaders` to `true` in advanced settings.
Ability to filter network requests according to whether a specific response
header is present and whether it matches or does not match a specific value.
For example:
*$1p,3P,script,header=via:1\.1\s+google
The above filter is meant to block network requests which fullfill all the
following conditions:
- is weakly 1st-party to the context
- is not strictly 1st-party to the context
- is of type `script`
- has a response HTTP header named `via`, which value matches the regular
expression `1\.1\s+google`.
The matches are always performed in a case-insensitive manner.
The header value is assumed to be a literal regular expression, except for
the following special characters:
- to anchor to start of string, use leading `|`, not `^`
- to anchor to end of string, use trailing `|`, not `$`
- to invert the test, use a leading `!`
To block a network request if it merely contains a specific HTTP header is
just a matter of specifying the header name without a header value:
*$1p,3P,script,header=via
Generic exception filters can be used to disable specific block `header=`
filters, i.e. `@@*$1p,3P,script,header` will override the block `header=`
filters given as example above.
Dynamic filtering's `allow` rules override block `headers=` filters.
Important: It is key that filter authors use as many narrowing filter options
as possible when using the `header=` option, and the `header=` option should
be used ONLY when other filter options are not sufficient.
More documentation justifying the purpose of `header=` option will be
provided eventually if ever it is decided to move it from experimental to
stable status.
To be decided: to restrict usage of this filter option to only uBO's own
filter lists or "My filters".
Changes
=======
Fine tuning `queryprune=`
-------------------------
The following changes have been implemented:
The special value `*` (i.e. `queryprune=*`) means "remove all query
parameters".
If the `queryprune=` value is made only of alphanumeric characters
(including `_`), the value will be internally converted to regex equivalent
`^value=`. This ensures a better future compatibility with AdGuard's
`removeparam=`.
If the `queryprune=` value starts with `!`, the test will be inverted. This
can be used to remove all query parameters EXCEPT those who match the
specified value.
Other
-----
The legacy code to test for spurious CSP reports has been removed. This
is no longer an issue ever since uBO redirects to local resources through
web accessible resources.
Notes
=====
The following new and recently added filter options are not compatible with
Chromium's manifest v3 changes:
- `queryprune=`
- `1P`
- `3P`
- `header=`
2020-11-23 14:22:43 +01:00
|
|
|
strictBlockBypass: hostname => {
|
2018-10-29 13:56:51 +01:00
|
|
|
strictBlockBypasser.bypass(hostname);
|
Add support for `1P`, `3P`, `header=` filter options and other changes
New filter options
==================
Strict partyness: `1P`, `3P`
----------------------------
The current options 1p/3p are meant to "weakly" match partyness, i.e. a
network request is considered 1st-party to its context as long as both the
context and the request share the same base domain.
The new partyness options are meant to check for strict partyness, i.e. a
network request will be considered 1st-party if and only if both the context
and the request share the same hostname.
For examples:
- context: `www.example.org`
- request: `www.example.org`
- `1p`: yes, `1P`: yes
- `3p`: no, `3P`: no
- context: `www.example.org`
- request: `subdomain.example.org`
- `1p`: yes, `1P`: no
- `3p`: no, `3P`: yes
- context: `www.example.org`
- request: `www.example.com`
- `1p`: no, `1P`: no
- `3p`: yes, `3P`: yes
The strict partyness options will be visually emphasized in the editor so as
to prevent mistakenly using `1P` or `3P` where weak partyness is meant to be
used.
Filter on response headers: `header=`
-------------------------------------
Currently experimental and under evaluation. Disabled by default, enable by
toggling `filterOnHeaders` to `true` in advanced settings.
Ability to filter network requests according to whether a specific response
header is present and whether it matches or does not match a specific value.
For example:
*$1p,3P,script,header=via:1\.1\s+google
The above filter is meant to block network requests which fullfill all the
following conditions:
- is weakly 1st-party to the context
- is not strictly 1st-party to the context
- is of type `script`
- has a response HTTP header named `via`, which value matches the regular
expression `1\.1\s+google`.
The matches are always performed in a case-insensitive manner.
The header value is assumed to be a literal regular expression, except for
the following special characters:
- to anchor to start of string, use leading `|`, not `^`
- to anchor to end of string, use trailing `|`, not `$`
- to invert the test, use a leading `!`
To block a network request if it merely contains a specific HTTP header is
just a matter of specifying the header name without a header value:
*$1p,3P,script,header=via
Generic exception filters can be used to disable specific block `header=`
filters, i.e. `@@*$1p,3P,script,header` will override the block `header=`
filters given as example above.
Dynamic filtering's `allow` rules override block `headers=` filters.
Important: It is key that filter authors use as many narrowing filter options
as possible when using the `header=` option, and the `header=` option should
be used ONLY when other filter options are not sufficient.
More documentation justifying the purpose of `header=` option will be
provided eventually if ever it is decided to move it from experimental to
stable status.
To be decided: to restrict usage of this filter option to only uBO's own
filter lists or "My filters".
Changes
=======
Fine tuning `queryprune=`
-------------------------
The following changes have been implemented:
The special value `*` (i.e. `queryprune=*`) means "remove all query
parameters".
If the `queryprune=` value is made only of alphanumeric characters
(including `_`), the value will be internally converted to regex equivalent
`^value=`. This ensures a better future compatibility with AdGuard's
`removeparam=`.
If the `queryprune=` value starts with `!`, the test will be inverted. This
can be used to remove all query parameters EXCEPT those who match the
specified value.
Other
-----
The legacy code to test for spurious CSP reports has been removed. This
is no longer an issue ever since uBO redirects to local resources through
web accessible resources.
Notes
=====
The following new and recently added filter options are not compatible with
Chromium's manifest v3 changes:
- `queryprune=`
- `1P`
- `3P`
- `header=`
2020-11-23 14:22:43 +01:00
|
|
|
},
|
2015-03-26 00:28:22 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
2021-07-29 01:48:38 +02:00
|
|
|
|
2021-10-07 20:41:29 +02:00
|
|
|
export default webRequest;
|
2021-07-29 01:48:38 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|