Skip to content
Permalink
Browse files
Fix parsing of AdGuard's #$?#-based cosmetic filters
As reported in the following commit:
- AdguardTeam/AdguardFilters@4fe02d73cee6
  • Loading branch information
gorhill authored and hawkeye116477 committed Jun 29, 2020
1 parent 00134a9 commit cf1e6fd0d511cb7f627dc4fba088025ee4cee534
Showing with 19 additions and 9 deletions.
  1. +2 −2 src/js/codemirror/ubo-static-filtering.js
  2. +17 −7 src/js/static-ext-filtering.js
@@ -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;
@@ -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();
@@ -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 /* '$' */ ) {

0 comments on commit cf1e6fd

Please sign in to comment.