Skip to content

Commit

Permalink
Bring header= filter option out of experimental status
Browse files Browse the repository at this point in the history
Related commits:
- bde3164
- 5db8d05

Reference documentation:
https://github.com/gorhill/uBlock/wiki/Static-filter-syntax#header

Compatible with AdGuard's `header=` option:
https://adguard.com/kb/general/ad-filtering/create-own-filters/#header-modifier

Partially compatible with ABP's `header=` option: Not compatible if
a header value is specified, as ABP requires `=` as separator between
the header name and header value, whereas uBO and AdGuard require `:`
as separator. If no header value is present, the option becomes compatible
with ABP's syntax.
https://gitlab.com/adblockinc/ext/adblockplus/adblockplusui/-/issues/898
  • Loading branch information
gorhill committed Oct 15, 2023
1 parent 7af88b0 commit c96a673
Show file tree
Hide file tree
Showing 11 changed files with 16 additions and 37 deletions.
3 changes: 0 additions & 3 deletions src/js/1p-filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ let cachedUserFilters = '';
if ( response instanceof Object === false ) { return; }
if ( response.hintUpdateToken !== undefined ) {
const mode = cmEditor.getMode();
if ( typeof response.filterOnHeaders === 'boolean' ) {
cmEditor.setOption('filterOnHeaders', response.filterOnHeaders);
}
if ( mode.setHints instanceof Function ) {
mode.setHints(response);
}
Expand Down
1 change: 0 additions & 1 deletion src/js/asset-viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ import './codemirror/ubo-static-filtering.js';
});
if ( hints instanceof Object ) {
const mode = cmEditor.getMode();
cmEditor.setOption('filterOnHeaders', hints.filterOnHeaders === true);
if ( mode.setHints instanceof Function ) {
mode.setHints(hints);
}
Expand Down
1 change: 0 additions & 1 deletion src/js/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ const hiddenSettingsDefault = {
disableWebAssembly: false,
extensionUpdateForceReload: false,
filterAuthorMode: false,
filterOnHeaders: false,
loggerPopupType: 'popup',
manualUpdateAssetFetchPeriod: 500,
modifyWebextFlavor: 'unset',
Expand Down
2 changes: 1 addition & 1 deletion src/js/benchmarks.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ const loadBenchmarkDataset = (( ) => {
staticNetFilteringEngine.matchAndFetchModifiers(fctxt, 'csp');
staticNetFilteringEngine.matchAndFetchModifiers(fctxt, 'permissions');
}
//staticNetFilteringEngine.matchHeaders(fctxt, []);
staticNetFilteringEngine.matchHeaders(fctxt, []);
} else if ( redirectEngine !== undefined ) {
staticNetFilteringEngine.redirectRequest(redirectEngine, fctxt);
}
Expand Down
17 changes: 0 additions & 17 deletions src/js/codemirror/ubo-static-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,6 @@ const originHints = [];
let hintHelperRegistered = false;


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

let filterOnHeaders = false;

CodeMirror.defineOption('filterOnHeaders', false, (cm, state) => {
filterOnHeaders = state;
self.dispatchEvent(new Event('filterOnHeaders'));
});

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

CodeMirror.defineMode('ubo-static-filtering', function() {
Expand Down Expand Up @@ -214,10 +205,6 @@ CodeMirror.defineMode('ubo-static-filtering', function() {
return '+';
};

self.addEventListener('filterOnHeaders', ( ) => {
astParser.options.filterOnHeaders = filterOnHeaders;
});

return {
lineComment: '!',
token: function(stream) {
Expand Down Expand Up @@ -990,10 +977,6 @@ CodeMirror.registerHelper('fold', 'ubo-static-filtering', (( ) => {
}
};

self.addEventListener('filterOnHeaders', ( ) => {
astParser.options.filterOnHeaders = filterOnHeaders;
});

CodeMirror.defineInitHook(cm => {
cm.on('changes', onChanges);
cm.on('beforeChange', onBeforeChanges);
Expand Down
1 change: 0 additions & 1 deletion src/js/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -1600,7 +1600,6 @@ const onMessage = function(request, sender, callback) {
response.preparseDirectiveHints =
sfp.utils.preparser.getHints();
response.expertMode = µb.hiddenSettings.filterAuthorMode;
response.filterOnHeaders = µb.hiddenSettings.filterOnHeaders;
}
if ( request.hintUpdateToken !== µb.pageStoresToken ) {
response.originHints = getOriginHints();
Expand Down
1 change: 0 additions & 1 deletion src/js/reverselookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ const fromNetFilter = async function(rawFilter) {
const writer = new CompiledListWriter();
const parser = new sfp.AstFilterParser({
expertMode: true,
filterOnHeaders: true,
maxTokenLength: staticNetFilteringEngine.MAX_TOKEN_LENGTH,
nativeCssHas: vAPI.webextFlavor.env.includes('native_css_has'),
});
Expand Down
2 changes: 1 addition & 1 deletion src/js/static-filtering-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -1279,7 +1279,7 @@ export class AstFilterParser {
realBad = isException === false || isNegated || hasValue;
break;
case NODE_TYPE_NET_OPTION_NAME_HEADER:
realBad = this.options.filterOnHeaders !== true || isNegated || hasValue === false;
realBad = isNegated || hasValue === false;
break;
case NODE_TYPE_NET_OPTION_NAME_IMPORTANT:
realBad = isException || isNegated || hasValue;
Expand Down
22 changes: 13 additions & 9 deletions src/js/static-net-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -5000,25 +5000,25 @@ FilterContainer.prototype.realmMatchString = function(
let tokenHash = DOT_TOKEN_HASH;
if (
(ibucket00 !== 0) &&
(iunit = bucket00.get(tokenHash) || 0) !== 0 &&
(iunit = bucket00.get(DOT_TOKEN_HASH) || 0) !== 0 &&
(filterMatch(iunit) === true)
) {
catBits = catBits00;
} else if (
(ibucket01 !== 0) &&
(iunit = bucket01.get(tokenHash) || 0) !== 0 &&
(iunit = bucket01.get(DOT_TOKEN_HASH) || 0) !== 0 &&
(filterMatch(iunit) === true)
) {
catBits = catBits01;
} else if (
(ibucket10 !== 0) &&
(iunit = bucket10.get(tokenHash) || 0) !== 0 &&
(iunit = bucket10.get(DOT_TOKEN_HASH) || 0) !== 0 &&
(filterMatch(iunit) === true)
) {
catBits = catBits10;
} else if (
(ibucket11 !== 0) &&
(iunit = bucket11.get(tokenHash) || 0) !== 0 &&
(iunit = bucket11.get(DOT_TOKEN_HASH) || 0) !== 0 &&
(filterMatch(iunit) === true)
) {
catBits = catBits11;
Expand Down Expand Up @@ -5200,12 +5200,16 @@ FilterContainer.prototype.matchHeaders = function(fctxt, headers) {
$httpHeaders.init(headers);

let r = 0;
if ( this.realmMatchString(HEADERS | BlockImportant, typeBits, partyBits) ) {
if ( this.realmMatchString(HEADERS | BlockAction, typeBits, partyBits) ) {
r = 1;
} else if ( this.realmMatchString(HEADERS | BlockAction, typeBits, partyBits) ) {
r = this.realmMatchString(HEADERS | AllowAction, typeBits, partyBits)
? 2
: 1;
}
if ( r !== 0 && $isBlockImportant !== true ) {
if ( this.realmMatchString(HEADERS | AllowAction, typeBits, partyBits) ) {
r = 2;
if ( this.realmMatchString(HEADERS | BlockImportant, typeBits, partyBits) ) {
r = 1;
}
}
}

$httpHeaders.reset();
Expand Down
1 change: 0 additions & 1 deletion src/js/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -1051,7 +1051,6 @@ self.addEventListener('hiddenSettingsChanged', ( ) => {
this.hiddenSettings.filterAuthorMode !== false;
const parser = new sfp.AstFilterParser({
expertMode,
filterOnHeaders: µb.hiddenSettings.filterOnHeaders,
maxTokenLength: staticNetFilteringEngine.MAX_TOKEN_LENGTH,
nativeCssHas: vAPI.webextFlavor.env.includes('native_css_has'),
});
Expand Down
2 changes: 1 addition & 1 deletion src/js/traffic.js
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ const onHeadersReceived = function(details) {
const { responseHeaders } = details;
if ( Array.isArray(responseHeaders) === false ) { return; }

if ( isRootDoc === false && µb.hiddenSettings.filterOnHeaders === true ) {
if ( isRootDoc === false ) {
const result = pageStore.filterOnHeaders(fctxt, responseHeaders);
if ( result !== 0 ) {
if ( logger.enabled ) {
Expand Down

0 comments on commit c96a673

Please sign in to comment.