Fix parsing of AdGuard's `#$?#`-based cosmetic filters

As reported in the following commit:
- https://github.com/AdguardTeam/AdguardFilters/commit/4fe02d73cee6
This commit is contained in:
Raymond Hill 2019-03-05 10:10:40 -05:00
parent c92bf080e1
commit 388c1c98ec
No known key found for this signature in database
GPG Key ID: 25E1490B761470C2
2 changed files with 19 additions and 9 deletions

View File

@ -1,7 +1,7 @@
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
Copyright (C) 2018 Raymond Hill
Copyright (C) 2018-present Raymond Hill
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
@ -26,7 +26,7 @@
CodeMirror.defineMode("ubo-static-filtering", function() {
var reComment1 = /^\s*!/;
var reComment2 = /^\s*#/;
var reExt = /^(\s*[^#]*)(#(?:#|@#|\$#|@\$#|\?#|@\?#))(.+)$/;
var reExt = /^(\s*[^#]*)(#@?(?:\$\??|\?)?#)(.+)$/;
var reNet = /^(.*?)(?:(\$)([^$]+)?)?$/;
var reNetAllow = /^\s*@@/;
var lineStyle = null;

View File

@ -741,11 +741,22 @@
if ( rpos === -1 ) { return false; }
}
// https://github.com/AdguardTeam/AdguardFilters/commit/4fe02d73cee6
// AdGuard also uses `$?` to force inline-based style rather than
// stylesheet-based style.
// Coarse-check that the anchor is valid.
// `##`: l = 1
// `#@#`, `#$#`, `#%#`, `#?#`: l = 2
// `#@$#`, `#@%#`, `#@?#`: l = 3
if ( (rpos - lpos) > 3 ) { return false; }
// `##`: l === 1
// `#@#`, `#$#`, `#%#`, `#?#`: l === 2
// `#@$#`, `#@%#`, `#@?#`, `#$?#`: l === 3
// `#@$?#`: l === 4
const anchorLen = rpos - lpos;
if ( anchorLen > 4 ) { return false; }
if (
anchorLen > 1 &&
/^@?(?:\$\??|%|\?)$/.test(raw.slice(lpos + 1, rpos)) === false
) {
return false;
}
// Extract the selector.
let suffix = raw.slice(rpos + 1).trim();
@ -763,9 +774,8 @@
if ( cCode !== 0x23 /* '#' */ && cCode !== 0x40 /* '@' */ ) {
// Adguard's scriptlet injection: not supported.
if ( cCode === 0x25 /* '%' */ ) { return true; }
// Not a known extended filter.
if ( cCode !== 0x24 /* '$' */ && cCode !== 0x3F /* '?' */ ) {
return false;
if ( cCode === 0x3F /* '?' */ && anchorLen > 2 ) {
cCode = raw.charCodeAt(rpos - 2);
}
// Adguard's style injection: translate to uBO's format.
if ( cCode === 0x24 /* '$' */ ) {