]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Glob patterns actually allow ranges
authorVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 30 Jul 2022 19:43:23 +0000 (20:43 +0100)
committerVsevolod Stakhov <vsevolod@rspamd.com>
Sat, 30 Jul 2022 19:43:23 +0000 (20:43 +0100)
src/libutil/str_util.c

index 849d11a4f4cffadc7efad9384a3380bb146ee176..86369909ad3ef8091b7c563b9f98f2d7286c0160 100644 (file)
@@ -3203,7 +3203,6 @@ rspamd_str_regexp_escape (const gchar *pattern, gsize slen,
                switch (t) {
                case '[':
                case ']':
-               case '-':
                case '\\':
                case '{':
                case '}':
@@ -3219,6 +3218,36 @@ rspamd_str_regexp_escape (const gchar *pattern, gsize slen,
                                *d++ = '\\';
                        }
                        break;
+               case '-':
+                       if (flags & RSPAMD_REGEXP_ESCAPE_GLOB) {
+                               /*
+                                * For glob patterns, we need to ensure that a previous character is alphanumeric
+                                * and there is `[` symbol somewhere before
+                                */
+                               bool seen_brace = false;
+                               const char *search = p;
+
+                               while (search > pattern) {
+                                       if (!g_ascii_isalnum(*search) && *search != '-') {
+                                               break;
+                                       }
+                                       if (*search == '[' ) {
+                                               seen_brace = true;
+                                               break;
+                                       }
+
+                                       search --;
+                               }
+
+                               if (!seen_brace) {
+                                       /* Escape `-` symbol */
+                                       *d++ = '\\';
+                               }
+                       }
+                       else if (!(flags & RSPAMD_REGEXP_ESCAPE_RE)) {
+                               *d++ = '\\';
+                       }
+                       break;
                case '*':
                case '?':
                case '+':
@@ -3226,10 +3255,8 @@ rspamd_str_regexp_escape (const gchar *pattern, gsize slen,
                                /* Treat * as .* and ? as .? */
                                *d++ = '.';
                        }
-                       else {
-                               if (!(flags & RSPAMD_REGEXP_ESCAPE_RE)) {
-                                       *d++ = '\\';
-                               }
+                       else if (!(flags & RSPAMD_REGEXP_ESCAPE_RE)) {
+                               *d++ = '\\';
                        }
                        break;
                default: