This is an automated email from the ASF dual-hosted git repository.

mraible pushed a commit to branch banned-words-time-limit
in repository https://gitbox.apache.org/repos/asf/roller.git

commit 805dbc6429e52139a42f3a3496e3f5ba7c46a993
Author: Matt Raible <[email protected]>
AuthorDate: Wed Nov 18 10:48:25 2026 -0700

    Stop a banned words rule that backtracks without end
    
    Banned words rules are regular expressions: rules that start with ( 
directly, and every other rule as \b(rule)\b. A weblog's own banned words and 
the site's list are compiled as written, so a rule like ((x+)+)+y held a 
comment post (checked against the commenter's name, e-mail, URL and text) or a 
referrer check for as long as it backtracked, which CodeQL flags as regex 
injection.
    
    Each value is now matched through a CharSequence that stops the match once 
1 second has passed. The value then counts as a match, so a comment that sets 
off such a rule is caught as spam, and the rule is logged so it can be 
simplified. Ordinary checks take a few milliseconds; 40,000 characters against 
the bundled list take about 24 ms.
---
 .../roller/weblogger/util/Bannedwordslist.java     | 112 +++++++++++++++++----
 .../roller/weblogger/util/BannedwordslistTest.java |  22 ++++
 2 files changed, 116 insertions(+), 18 deletions(-)

diff --git 
a/app/src/main/java/org/apache/roller/weblogger/util/Bannedwordslist.java 
b/app/src/main/java/org/apache/roller/weblogger/util/Bannedwordslist.java
index 37cb56658..d15e88628 100644
--- a/app/src/main/java/org/apache/roller/weblogger/util/Bannedwordslist.java
+++ b/app/src/main/java/org/apache/roller/weblogger/util/Bannedwordslist.java
@@ -314,6 +314,7 @@ public final class Bannedwordslist {
         if (str == null || StringUtils.isEmpty(str)) {
             return false;
         }
+        CharSequence text = new DeadlineText(str);
 
         // First iterate over bannedwordslist, doing indexOf.
         // Then iterate over bannedwordslistRegex and test.
@@ -326,19 +327,23 @@ public final class Bannedwordslist {
             stringRules.addAll(moreStringRules);
             stringRules.addAll(bannedwordslistStr);
         }
-        if (testStringRules(str, stringRules)) {
-            return true;
-        }
+        try {
+            if (testStringRules(text, stringRules)) {
+                return true;
+            }
         
-        // test regex bannedwordslisted
-        List<Pattern> regexRules = bannedwordslistRegex;
-        if (moreRegexRules != null && !moreRegexRules.isEmpty()) {
-            regexRules = new ArrayList<>();
-            regexRules.addAll(moreRegexRules);
-            regexRules.addAll(bannedwordslistRegex);
+            // test regex bannedwordslisted
+            List<Pattern> regexRules = bannedwordslistRegex;
+            if (moreRegexRules != null && !moreRegexRules.isEmpty()) {
+                regexRules = new ArrayList<>();
+                regexRules.addAll(moreRegexRules);
+                regexRules.addAll(bannedwordslistRegex);
+            }
+            return testRegExRules(text, regexRules);
+        } catch (MatchTimeLimitException e) {
+            return true;
         }
-        return testRegExRules(str, regexRules);
-    }      
+    }
 
     /** 
      * Test string only against rules provided by caller, NOT against built-in 
bannedwordslist.
@@ -348,22 +353,30 @@ public final class Bannedwordslist {
      */
     public static boolean matchesRulesOnly(
         String str, List<String> stringRules, List<Pattern> regexRules) {
-        return testStringRules(str, stringRules) || testRegExRules(str, 
regexRules);
+        if (str == null) {
+            return false;
+        }
+        CharSequence text = new DeadlineText(str);
+        try {
+            return testStringRules(text, stringRules) || testRegExRules(text, 
regexRules);
+        } catch (MatchTimeLimitException e) {
+            return true;
+        }
     }
         
     /** Test String against the RegularExpression rules. */
-    private static boolean testRegExRules(String str, List<Pattern> 
regexRules) {
+    private static boolean testRegExRules(CharSequence str, List<Pattern> 
regexRules) {
         for (Pattern testPattern : regexRules) {
             // want to see what it is matching on, but only in debug mode
             if (mLogger.isDebugEnabled()) {
                 Matcher matcher = testPattern.matcher(str);
-                if (matcher.find()) {
+                if (find(matcher, testPattern.pattern())) {
                     mLogger.debug(matcher.group()
                             + " matched by " + testPattern.pattern());
                     return true;
                 }
             } else {
-                if (testPattern.matcher(str).find()) {
+                if (find(testPattern.matcher(str), testPattern.pattern())) {
                     return true;
                 }
             }
@@ -382,7 +395,7 @@ public final class Bannedwordslist {
      *
      * @return true if a match was found, otherwise false
      */
-    private static boolean testStringRules(String source, List<String> rules) {
+    private static boolean testStringRules(CharSequence source, List<String> 
rules) {
         boolean matches = false;
         
         for (String rule : rules) {
@@ -401,13 +414,13 @@ public final class Bannedwordslist {
                 Matcher matcher;
                 matcher = pattern.matcher(source);
 
-                matches = matcher.find();
+                matches = find(matcher, rule);
                 if (matches) {
                     break;
                 }
             }
             catch (PatternSyntaxException e) {
-                matches = source.contains(rule);
+                matches = source.toString().contains(rule);
                 if (matches) {
                     break;
                 }
@@ -443,6 +456,69 @@ public final class Bannedwordslist {
         }        
     }
         
+    /**
+     * Longest the rules may take to check one value, such as a comment's
+     * text. A rule that backtracks without end, which a weblog's own banned
+     * words can include, would otherwise hold the request.
+     */
+    static final long MATCH_TIME_LIMIT_MILLIS = 1000;
+
+    private static boolean find(Matcher matcher, String rule) {
+        try {
+            return matcher.find();
+        } catch (MatchTimeLimitException e) {
+            mLogger.warn("Banned words rule took over " + 
MATCH_TIME_LIMIT_MILLIS
+                    + " ms and was counted as a match; consider simplifying 
it: " + rule);
+            throw e;
+        }
+    }
+
+    private static final class MatchTimeLimitException extends 
RuntimeException {
+        MatchTimeLimitException() {
+            super(null, null, false, false);
+        }
+    }
+
+    /** The checked text, which stops a match once the time limit has passed. 
*/
+    private static final class DeadlineText implements CharSequence {
+        private final CharSequence text;
+        private final long deadline;
+        private int reads;
+
+        DeadlineText(CharSequence text) {
+            this(text, System.nanoTime() + MATCH_TIME_LIMIT_MILLIS * 
1_000_000L);
+        }
+
+        private DeadlineText(CharSequence text, long deadline) {
+            this.text = text;
+            this.deadline = deadline;
+        }
+
+        @Override
+        public char charAt(int index) {
+            // checking the clock on every read would slow ordinary matches
+            if ((++reads & 0x3FF) == 0 && System.nanoTime() - deadline > 0) {
+                throw new MatchTimeLimitException();
+            }
+            return text.charAt(index);
+        }
+
+        @Override
+        public int length() {
+            return text.length();
+        }
+
+        @Override
+        public CharSequence subSequence(int start, int end) {
+            return new DeadlineText(text.subSequence(start, end), deadline);
+        }
+
+        @Override
+        public String toString() {
+            return text.toString();
+        }
+    }
+
     /** Return pretty list of String and RegEx rules. */
     @Override
     public String toString() {
diff --git 
a/app/src/test/java/org/apache/roller/weblogger/util/BannedwordslistTest.java 
b/app/src/test/java/org/apache/roller/weblogger/util/BannedwordslistTest.java
index 5406b6b79..20192c626 100644
--- 
a/app/src/test/java/org/apache/roller/weblogger/util/BannedwordslistTest.java
+++ 
b/app/src/test/java/org/apache/roller/weblogger/util/BannedwordslistTest.java
@@ -27,6 +27,9 @@ import org.junit.jupiter.api.Test;
 
 import java.io.File;
 import java.time.Duration;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Pattern;
 
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
@@ -107,4 +110,23 @@ public class BannedwordslistTest  {
         });
     }
 
+    // a weblog's rule that backtracks without end can't hold up the check;
+    // the text counts as a match, so a comment with it is caught as spam
+    @Test
+    public void testRuleThatBacktracksIsStopped() {
+        String text = "x".repeat(40) + "!";
+        // a regex rule, and a word rule, which is also used as a regex
+        for (String rule : new String[] {"((x+)+)+y", "x((x+)+)+y"}) {
+            List<String> stringRules = new ArrayList<>();
+            List<Pattern> regexRules = new ArrayList<>();
+            Bannedwordslist.populateSpamRules(rule, stringRules, regexRules, 
null);
+            assertTimeoutPreemptively(Duration.ofSeconds(8), () -> {
+                assertTrue(Bannedwordslist.matchesRulesOnly(text, stringRules, 
regexRules), rule);
+                assertTrue(bannedwordslist.isBannedwordslisted(text, 
stringRules, regexRules), rule);
+            }, rule);
+        }
+        // the limit applies to each value, not to every check that follows
+        assertFalse(bannedwordslist.isBannedwordslisted("an ordinary 
comment"));
+    }
+
 }

Reply via email to