Skip to content

Commit

Permalink
Fix parsing of AdGuard's #$?#-based cosmetic filters
Browse files Browse the repository at this point in the history
As reported in the following commit:
- AdguardTeam/AdguardFilters@4fe02d73cee6
  • Loading branch information
gorhill authored and hawkeye116477 committed Jun 29, 2020
1 parent 00134a9 commit cf1e6fd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/js/codemirror/ubo-static-filtering.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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;
Expand Down
24 changes: 17 additions & 7 deletions src/js/static-ext-filtering.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,11 +678,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();
Expand All @@ -700,9 +711,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 /* '$' */ ) {
Expand Down

0 comments on commit cf1e6fd

Please sign in to comment.