containerise icon indicating copy to clipboard operation
containerise copied to clipboard

Glob syntax is bad

Open indivisible opened this issue 4 years ago • 2 comments

I've noticed that glob rules suffer from some problems:

  1. they are not properly escaped before being turned into regexes. ., and every other special character, should be escaped.
  2. the ? symbol in globs matches exactly one unspecified character, not 1 or zero (so should be translated to . in regex)

indivisible avatar Sep 11 '20 11:09 indivisible

Hello, there are more severe issues with the current glob to regex implementation. Because of missing ^ (start) and $ (end) tokens, basically every filter works like *filter* matching any substring.

You could use this code for better converting. Maybe someone else verifies that I have no mistake within the sample.

host = 'test.de'
filter = '!*.test.de';
glob = filter.substr(1) // remove glob indicator (!)

pattern = '';

// ensure host begins with pattern when there no leading *-glob
if (!glob.startsWith('*'))
    pattern += '^';

// allow pure base domain on subdomain filters
// i.e. domain.com is valid for *.domain.com
if (glob.startsWith('*.'))
{
    pattern += '(.+\\.)?';   // optional allow any chars before, but if, there must be a dot behind
    glob = glob.substr(2);
}

// convert glob string to regex
pattern += glob
        .replace(/[.+^${}()|[\]\\]/g, '\\$&') // escape regex special chars
        .replace(/\*/g, '.*')                 // convert * glob into regex
        .replace(/\?/g, '.?');                // convert ? glob into regex

// ensure host ends with pattern when there is no tailing *-glob
if (!glob.endsWith('*'))
    pattern += '$';

isMatch = new RegExp(pattern).test(host);

alex-becker-startp avatar Sep 26 '21 10:09 alex-becker-startp

Yep, just found that https://github.com/mastodon/mastodon/issues/15561 is matched by !*mastodon.* , Personal and was confused...

xeruf avatar Jun 19 '22 19:06 xeruf