From 805dbc6429e52139a42f3a3496e3f5ba7c46a993 Mon Sep 17 00:00:00 2001 From: Matt Raible Date: Wed, 18 Nov 2026 10:48:25 -0700 Subject: [PATCH 01/10] 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. --- .../weblogger/util/Bannedwordslist.java | 112 +++++++++++++++--- .../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 boolean isBannedwordslisted( 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 boolean isBannedwordslisted( stringRules.addAll(moreStringRules); stringRules.addAll(bannedwordslistStr); } - if (testStringRules(str, stringRules)) { - return true; - } + try { + if (testStringRules(text, stringRules)) { + return true; + } - // test regex bannedwordslisted - List regexRules = bannedwordslistRegex; - if (moreRegexRules != null && !moreRegexRules.isEmpty()) { - regexRules = new ArrayList<>(); - regexRules.addAll(moreRegexRules); - regexRules.addAll(bannedwordslistRegex); + // test regex bannedwordslisted + List 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 boolean isBannedwordslisted( */ public static boolean matchesRulesOnly( String str, List stringRules, List 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 regexRules) { + private static boolean testRegExRules(CharSequence str, List 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 @@ private static boolean testRegExRules(String str, List regexRules) { * * @return true if a match was found, otherwise false */ - private static boolean testStringRules(String source, List rules) { + private static boolean testStringRules(CharSequence source, List rules) { boolean matches = false; for (String rule : rules) { @@ -401,13 +414,13 @@ private static boolean testStringRules(String source, List rules) { 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 static void populateSpamRules( } } + /** + * 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 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 void testLongTextIsCheckedQuickly() { }); } + // 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 stringRules = new ArrayList<>(); + List 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")); + } + } From a8b787415b55388070c61a3f065a1d9eebca9781 Mon Sep 17 00:00:00 2001 From: Matt Raible Date: Thu, 19 Nov 2026 09:20:33 -0700 Subject: [PATCH 02/10] Match a weblog's own banned words as plain words when weblog admins are untrusted With weblogAdminsUntrusted=true, the default, each line of a weblog's banned words is matched as literal text, case-insensitive, at word boundaries, so a weblog admin can't write a regex rule that holds a comment post or a referrer check. The built-in and site-wide lists are still regexes, as are weblog rules when the setting is off. Weblog Settings labels the field accordingly, and the user guide and security model describe the change. --- .../ui/struts2/editor/WeblogConfig.java | 9 ++- .../weblogger/util/Bannedwordslist.java | 59 +++++++++++++++++-- .../util/BannedwordslistChecker.java | 17 +++--- .../resources/ApplicationResources.properties | 2 + .../WEB-INF/jsps/editor/WeblogConfig.jsp | 10 +++- .../weblogger/util/BannedwordslistTest.java | 24 ++++++++ docs/roller-user-guide.adoc | 16 +++-- docs/security-model.md | 2 +- 8 files changed, 114 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/WeblogConfig.java b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/WeblogConfig.java index 22ff667ff..0c50ed0c5 100644 --- a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/WeblogConfig.java +++ b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/WeblogConfig.java @@ -207,8 +207,13 @@ private void myValidate() { List stringRules = new ArrayList<>(); try { // just for testing/counting, this does not persist rules in any way - Bannedwordslist.populateSpamRules(getBean().getBannedwordslist(), stringRules, regexRules, null); - addMessage("websiteSettings.acceptedBannedwordslist", List.of(""+stringRules.size(), ""+regexRules.size())); + Bannedwordslist.populateSpamRules(getBean().getBannedwordslist(), stringRules, regexRules, null, + weblogAdminsUntrusted); + if (weblogAdminsUntrusted) { + addMessage("websiteSettings.acceptedBannedwordsAsWords", List.of(""+regexRules.size())); + } else { + addMessage("websiteSettings.acceptedBannedwordslist", List.of(""+stringRules.size(), ""+regexRules.size())); + } } catch (Exception e) { addError("websiteSettings.error.processingBannedwordslist", e.getMessage()); } 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 d15e88628..e034524d6 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 @@ -439,10 +439,36 @@ private static boolean testStringRules(CharSequence source, List rules) /** Utility method to populate lists based a bannedwordslist in string form */ public static void populateSpamRules( String bannedwordslist, List stringRules, List regexRules, String addendum) { - String weblogWords = bannedwordslist; - weblogWords = (weblogWords == null) ? "" : weblogWords; - String siteWords = (addendum != null) ? addendum : ""; - StringTokenizer toker = new StringTokenizer(siteWords + "\n" + weblogWords, "\n"); + populateSpamRules(bannedwordslist, stringRules, regexRules, addendum, false); + } + + /** + * Populates the rule lists from a weblog's banned words and the site-wide + * addendum. With weblogRulesAsWords, the weblog's own lines are matched as + * plain words (case-insensitive, at word boundaries) rather than as + * regular expressions, so an untrusted weblog admin can't write a rule + * that backtracks; the addendum's rules are still regular expressions. + */ + public static void populateSpamRules(String bannedwordslist, List stringRules, + List regexRules, String addendum, boolean weblogRulesAsWords) { + if (weblogRulesAsWords) { + readRules(addendum, stringRules, regexRules); + StringTokenizer toker = new StringTokenizer(bannedwordslist == null ? "" : bannedwordslist, "\n"); + while (toker.hasMoreTokens()) { + String token = toker.nextToken().trim(); + if (!token.isEmpty() && !token.startsWith("#")) { + regexRules.add(wordPattern(token)); + } + } + } else { + String weblogWords = (bannedwordslist == null) ? "" : bannedwordslist; + String siteWords = (addendum != null) ? addendum : ""; + readRules(siteWords + "\n" + weblogWords, stringRules, regexRules); + } + } + + private static void readRules(String rules, List stringRules, List regexRules) { + StringTokenizer toker = new StringTokenizer(rules == null ? "" : rules, "\n"); while (toker.hasMoreTokens()) { String token = toker.nextToken().trim(); if (token.startsWith("#")) { @@ -453,9 +479,30 @@ public static void populateSpamRules( } else { stringRules.add(token); } - } + } } - + + /** + * The literal text, case-insensitive, with a word boundary on each end + * that is a word character; {@code \b} next to "(" or "/" would need a + * word character beside it and so would rarely match. + */ + private static Pattern wordPattern(String word) { + StringBuilder regex = new StringBuilder(); + if (isWordChar(word.charAt(0))) { + regex.append("\\b"); + } + regex.append(Pattern.quote(word)); + if (isWordChar(word.charAt(word.length() - 1))) { + regex.append("\\b"); + } + return Pattern.compile(regex.toString(), Pattern.CASE_INSENSITIVE); + } + + private static boolean isWordChar(char c) { + return c == '_' || (c < 128 && Character.isLetterOrDigit(c)); + } + /** * 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 diff --git a/app/src/main/java/org/apache/roller/weblogger/util/BannedwordslistChecker.java b/app/src/main/java/org/apache/roller/weblogger/util/BannedwordslistChecker.java index 0961e2987..b1123d42e 100644 --- a/app/src/main/java/org/apache/roller/weblogger/util/BannedwordslistChecker.java +++ b/app/src/main/java/org/apache/roller/weblogger/util/BannedwordslistChecker.java @@ -58,12 +58,8 @@ public static boolean checkReferrer(Weblog website, String referrerURL) { if (WebloggerConfig.getBooleanProperty("site.bannedwordslist.enable.referrers")) { List stringRules = new ArrayList<>(); List regexRules = new ArrayList<>(); - Bannedwordslist.populateSpamRules( - website.getBannedwordslist(), stringRules, regexRules, null); - if (WebloggerRuntimeConfig.getProperty("spam.bannedwordslist") != null) { - Bannedwordslist.populateSpamRules( - WebloggerRuntimeConfig.getProperty("spam.bannedwordslist"), stringRules, regexRules, null); - } + Bannedwordslist.populateSpamRules(website.getBannedwordslist(), stringRules, regexRules, + WebloggerRuntimeConfig.getProperty("spam.bannedwordslist"), weblogRulesAsWords()); return Bannedwordslist.matchesRulesOnly(referrerURL, stringRules, regexRules); } return false; @@ -80,7 +76,7 @@ private static boolean testComment(WeblogEntryComment c) { Weblog website = c.getWeblogEntry().getWebsite(); Bannedwordslist.populateSpamRules( website.getBannedwordslist(), stringRules, regexRules, - WebloggerRuntimeConfig.getProperty("spam.bannedwordslist")); + WebloggerRuntimeConfig.getProperty("spam.bannedwordslist"), weblogRulesAsWords()); Bannedwordslist bannedwordslist = Bannedwordslist.getBannedwordslist(); if ( bannedwordslist.isBannedwordslisted(c.getUrl(), stringRules, regexRules) || bannedwordslist.isBannedwordslisted(c.getEmail(), stringRules, regexRules) @@ -89,5 +85,10 @@ private static boolean testComment(WeblogEntryComment c) { ret = true; } return ret; - } + } + + /** Untrusted weblog admins' banned words are matched as words, not regexes. */ + private static boolean weblogRulesAsWords() { + return WebloggerConfig.getBooleanProperty("weblogAdminsUntrusted"); + } } diff --git a/app/src/main/resources/ApplicationResources.properties b/app/src/main/resources/ApplicationResources.properties index b55815559..a113b25b8 100644 --- a/app/src/main/resources/ApplicationResources.properties +++ b/app/src/main/resources/ApplicationResources.properties @@ -1778,7 +1778,9 @@ websiteSettings.spamPrevention=Spam Prevention websiteSettings.ignoreUrls=List of words and regex expressions listed one per \ line to be added to the banned words list used to check comments and referrers. websiteSettings.bannedWordsList=Words banned in comments (regex allowed) +websiteSettings.bannedWordsListAsWords=Words banned in comments (one word or phrase per line) websiteSettings.acceptedBannedwordslist=Accepted {0} string and {1} regex banned-words list rules +websiteSettings.acceptedBannedwordsAsWords=Accepted {0} banned-words list rules websiteSettings.error.processingBannedwordslist=Error processing banned-words list: {0} # --- web analytics diff --git a/app/src/main/webapp/WEB-INF/jsps/editor/WeblogConfig.jsp b/app/src/main/webapp/WEB-INF/jsps/editor/WeblogConfig.jsp index 2bb920c14..ee73818e8 100644 --- a/app/src/main/webapp/WEB-INF/jsps/editor/WeblogConfig.jsp +++ b/app/src/main/webapp/WEB-INF/jsps/editor/WeblogConfig.jsp @@ -130,8 +130,14 @@

- + + + + + + <%-- ***** Web analytics settings ***** --%> 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 20192c626..f756a1c2d 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 @@ -129,4 +129,28 @@ public void testRuleThatBacktracksIsStopped() { assertFalse(bannedwordslist.isBannedwordslisted("an ordinary comment")); } + // with weblogAdminsUntrusted, a weblog's own rules are matched as words, + // not as regular expressions; the site-wide rules are still regexes + @Test + public void testWeblogRulesAsWords() { + List stringRules = new ArrayList<>(); + List regexRules = new ArrayList<>(); + Bannedwordslist.populateSpamRules("(a|b)\nViagra\n(foo)\nexample.com/", stringRules, regexRules, + "(c|d)", true); + assertFalse(Bannedwordslist.matchesRulesOnly("a", stringRules, regexRules)); + assertTrue(Bannedwordslist.matchesRulesOnly("spam (a|b) here", stringRules, regexRules)); + assertTrue(Bannedwordslist.matchesRulesOnly("buy VIAGRA now", stringRules, regexRules)); + assertFalse(Bannedwordslist.matchesRulesOnly("viagraphone", stringRules, regexRules)); + assertTrue(Bannedwordslist.matchesRulesOnly("see (foo) now", stringRules, regexRules)); + assertTrue(Bannedwordslist.matchesRulesOnly("http://example.com/x", stringRules, regexRules)); + // the site-wide rule is still a regex + assertTrue(Bannedwordslist.matchesRulesOnly("d", stringRules, regexRules)); + + // with the setting off, the weblog's rules are regexes as before + stringRules.clear(); + regexRules.clear(); + Bannedwordslist.populateSpamRules("(a|b)", stringRules, regexRules, null, false); + assertTrue(Bannedwordslist.matchesRulesOnly("a", stringRules, regexRules)); + } + } diff --git a/docs/roller-user-guide.adoc b/docs/roller-user-guide.adoc index 88b392927..80968f9b5 100644 --- a/docs/roller-user-guide.adoc +++ b/docs/roller-user-guide.adoc @@ -815,12 +815,16 @@ here’s how you do it: ** Go to the Weblog Settings page and scroll down to the bannedwordslist fields -** Enter your spam words, one per line -** Lines that begin with a left parenthesis will be treated as regular -expressions (see the Java API documentation for -_javax.util.regex.Pattern_ for a guide to regular expressions). Don’t -try to use a regular expression unless you really know what you’re -doing. +** Enter your spam words or phrases, one per line. Each one matches +regardless of case, as a whole word: _casino_ matches "Casino" but not +"casinos". +** By default (_weblogAdminsUntrusted=true_), every line is matched as +plain text, including lines that begin with a left parenthesis. On a site +where weblog administrators are trusted (_weblogAdminsUntrusted=false_), +lines that begin with a left parenthesis are regular expressions, and other +lines are matched as regular expressions at word boundaries (see the Java +API documentation for _java.util.regex.Pattern_). Don’t try to use a +regular expression unless you really know what you’re doing. == Choosing your weblog theme diff --git a/docs/security-model.md b/docs/security-model.md index 35915ddae..4021deeb4 100644 --- a/docs/security-model.md +++ b/docs/security-model.md @@ -283,7 +283,7 @@ A bug in an optional feature is a vulnerability when the feature is enabled. | Setting | What it changes | | --- | --- | -| `weblogAdminsUntrusted` | `true` (default) sanitizes HTML from weblog members. Set `false` only when every weblog admin is trusted, such as on a solo blog. | +| `weblogAdminsUntrusted` | `true` (default) sanitizes HTML from weblog members and matches a weblog's banned words as plain words rather than regular expressions. Set `false` only when every weblog admin is trusted, such as on a solo blog. | | `themes.customtheme.allowed` | Lets weblog Admins edit templates. Templates stay in the sandbox. | | `uploads.enabled` | Lets members upload media files. | | `webservices.enableXmlRpc`, `webservices.enableAtomPub` | Turn on the remote publishing APIs. They use the same weblog roles as the UI. | From 30682007c0640b7d758e12b1150f6923e0eff49a Mon Sep 17 00:00:00 2001 From: Matt Raible Date: Thu, 19 Nov 2026 17:22:03 -0700 Subject: [PATCH 03/10] Match a weblog's plain banned words at Unicode word edges, in any case MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A plain-word rule such as café now ignores case in any script (UNICODE_CASE), so it matches CAFÉ, and checks its word edges with lookarounds over Unicode letters, marks, digits and underscores, so it no longer matches inside caféine. Edges that aren't word characters, as in (foo) or example.com/, still get no check. --- .../weblogger/util/Bannedwordslist.java | 25 +++++++++++-------- .../weblogger/util/BannedwordslistTest.java | 13 ++++++++++ 2 files changed, 28 insertions(+), 10 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 e034524d6..cd07c6807 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 @@ -482,25 +482,30 @@ private static void readRules(String rules, List stringRules, List stringRules = new ArrayList<>(); + List regexRules = new ArrayList<>(); + // "café" is cafe with an acute accent, "élan" is elan + Bannedwordslist.populateSpamRules("café\nélan", stringRules, regexRules, null, true); + assertTrue(Bannedwordslist.matchesRulesOnly("at the café now", stringRules, regexRules)); + assertTrue(Bannedwordslist.matchesRulesOnly("AT THE CAFÉ NOW", stringRules, regexRules)); + assertFalse(Bannedwordslist.matchesRulesOnly("too much caféine", stringRules, regexRules)); + assertTrue(Bannedwordslist.matchesRulesOnly("with ÉLAN", stringRules, regexRules)); + assertFalse(Bannedwordslist.matchesRulesOnly("réélan", stringRules, regexRules)); + } + } From f0b40b22e191aecf6d537e46f1bb53b51653e551 Mon Sep 17 00:00:00 2001 From: Matt Raible Date: Fri, 20 Nov 2026 13:59:32 -0700 Subject: [PATCH 04/10] Give each banned words rule its own time limit, and let a referrer through when a rule can't check it All rules used to share one deadline per value, so a long list of plain words against a long comment could run out of time and count the comment as spam. Each rule now has its own limit. A rule that runs out of stack, such as (a|b)+c on long text, is handled like one that runs out of time instead of failing the request. A referrer that can't be checked is no longer refused, since the reader would get a 403 for a slow rule. --- .../weblogger/util/Bannedwordslist.java | 76 ++++++++++++------- .../util/BannedwordslistChecker.java | 4 +- .../weblogger/util/BannedwordslistTest.java | 40 ++++++++++ 3 files changed, 93 insertions(+), 27 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 cd07c6807..ef28c37f7 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,7 +314,6 @@ public boolean isBannedwordslisted( 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. @@ -328,7 +327,7 @@ public boolean isBannedwordslisted( stringRules.addAll(bannedwordslistStr); } try { - if (testStringRules(text, stringRules)) { + if (testStringRules(str, stringRules)) { return true; } @@ -339,8 +338,8 @@ public boolean isBannedwordslisted( regexRules.addAll(moreRegexRules); regexRules.addAll(bannedwordslistRegex); } - return testRegExRules(text, regexRules); - } catch (MatchTimeLimitException e) { + return testRegExRules(str, regexRules); + } catch (RuleNotCheckedException e) { return true; } } @@ -353,32 +352,42 @@ public boolean isBannedwordslisted( */ public static boolean matchesRulesOnly( String str, List stringRules, List regexRules) { + return matchesRulesOnly(str, stringRules, regexRules, true); + } + + /** + * Test string only against rules provided by caller, NOT against built-in bannedwordslist. + * @param str String to be checked against rules + * @param stringRules String rules to consider + * @param regexRules Regex rules to consider + * @param uncheckedMatches what to return when a rule can't be checked in + * time: true for a comment, which the weblog owner + * can review, false for a referrer, which would + * refuse the reader the page + */ + public static boolean matchesRulesOnly(String str, List stringRules, + List regexRules, boolean uncheckedMatches) { if (str == null) { return false; } - CharSequence text = new DeadlineText(str); try { - return testStringRules(text, stringRules) || testRegExRules(text, regexRules); - } catch (MatchTimeLimitException e) { - return true; + return testStringRules(str, stringRules) || testRegExRules(str, regexRules); + } catch (RuleNotCheckedException e) { + return uncheckedMatches; } } /** Test String against the RegularExpression rules. */ - private static boolean testRegExRules(CharSequence str, List regexRules) { + private static boolean testRegExRules(String str, List 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 (find(matcher, testPattern.pattern())) { + Matcher matcher = testPattern.matcher(new DeadlineText(str)); + if (find(matcher, testPattern.pattern())) { + // want to see what it is matching on, but only in debug mode + if (mLogger.isDebugEnabled()) { mLogger.debug(matcher.group() + " matched by " + testPattern.pattern()); - return true; - } - } else { - if (find(testPattern.matcher(str), testPattern.pattern())) { - return true; } + return true; } } return false; @@ -395,7 +404,7 @@ private static boolean testRegExRules(CharSequence str, List regexRules * * @return true if a match was found, otherwise false */ - private static boolean testStringRules(CharSequence source, List rules) { + private static boolean testStringRules(String source, List rules) { boolean matches = false; for (String rule : rules) { @@ -412,7 +421,7 @@ private static boolean testStringRules(CharSequence source, List rules) Pattern.CASE_INSENSITIVE); Matcher matcher; - matcher = pattern.matcher(source); + matcher = pattern.matcher(new DeadlineText(source)); matches = find(matcher, rule); if (matches) { @@ -420,7 +429,7 @@ private static boolean testStringRules(CharSequence source, List rules) } } catch (PatternSyntaxException e) { - matches = source.toString().contains(rule); + matches = source.contains(rule); if (matches) { break; } @@ -509,9 +518,12 @@ private static boolean isWordChar(int codePoint) { } /** - * 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. + * Longest one rule may take to check one value, such as a comment's + * text. A regex rule that backtracks without end would otherwise hold + * the request. The clock is checked as the match reads the text, so a + * regex that repeats without reading it, such as nested counted repeats + * of an empty group, isn't stopped; such rules come only from the + * site-wide list or from trusted weblog admins. */ static final long MATCH_TIME_LIMIT_MILLIS = 1000; @@ -520,8 +532,13 @@ private static boolean find(Matcher matcher, String rule) { 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; + + " ms and the value could not be checked; consider simplifying it: " + rule); + throw new RuleNotCheckedException(); + } catch (StackOverflowError e) { + // a regex such as (a|b)+c recurses once per repetition + mLogger.warn("Banned words rule ran out of stack and the value could not be checked;" + + " consider simplifying it: " + rule); + throw new RuleNotCheckedException(); } } @@ -531,6 +548,13 @@ private static final class MatchTimeLimitException extends RuntimeException { } } + /** A rule could not be checked against the value, which then counts as a match for a comment. */ + private static final class RuleNotCheckedException extends RuntimeException { + RuleNotCheckedException() { + 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; diff --git a/app/src/main/java/org/apache/roller/weblogger/util/BannedwordslistChecker.java b/app/src/main/java/org/apache/roller/weblogger/util/BannedwordslistChecker.java index b1123d42e..af4d0b516 100644 --- a/app/src/main/java/org/apache/roller/weblogger/util/BannedwordslistChecker.java +++ b/app/src/main/java/org/apache/roller/weblogger/util/BannedwordslistChecker.java @@ -60,7 +60,9 @@ public static boolean checkReferrer(Weblog website, String referrerURL) { List regexRules = new ArrayList<>(); Bannedwordslist.populateSpamRules(website.getBannedwordslist(), stringRules, regexRules, WebloggerRuntimeConfig.getProperty("spam.bannedwordslist"), weblogRulesAsWords()); - return Bannedwordslist.matchesRulesOnly(referrerURL, stringRules, regexRules); + // a referrer that can't be checked in time is let through, so a + // slow rule doesn't refuse the reader the page + return Bannedwordslist.matchesRulesOnly(referrerURL, stringRules, regexRules, false); } return false; } 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 e8733d5a0..35a042ab2 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 @@ -166,4 +166,44 @@ public void testWeblogRulesAsUnicodeWords() { assertFalse(Bannedwordslist.matchesRulesOnly("réélan", stringRules, regexRules)); } + // each rule has its own time limit, so a long list of plain words + // against a long comment is slow but isn't counted as a match + @Test + public void testTimeLimitIsPerRule() { + StringBuilder rules = new StringBuilder(); + for (int i = 0; i < 20000; i++) { + rules.append("spamword").append(i).append('\n'); + } + List stringRules = new ArrayList<>(); + List regexRules = new ArrayList<>(); + Bannedwordslist.populateSpamRules(rules.toString(), stringRules, regexRules, null, true); + String text = "an ordinary comment ".repeat(500); + assertTimeoutPreemptively(Duration.ofSeconds(60), () -> + assertFalse(Bannedwordslist.matchesRulesOnly(text, stringRules, regexRules))); + } + + // a referrer that can't be checked in time is let through, since the + // reader would otherwise be refused the page + @Test + public void testReferrerThatCantBeCheckedIsNoMatch() { + String text = "x".repeat(40) + "!"; + List stringRules = new ArrayList<>(); + List regexRules = new ArrayList<>(); + Bannedwordslist.populateSpamRules("((x+)+)+y", stringRules, regexRules, null); + assertTimeoutPreemptively(Duration.ofSeconds(8), () -> + assertFalse(Bannedwordslist.matchesRulesOnly(text, stringRules, regexRules, false))); + } + + // a rule that recurses once per repetition can run out of stack on long + // text; the text is then handled as if the time limit had passed + @Test + public void testRuleThatRunsOutOfStack() { + String text = "ab".repeat(50000); + List stringRules = new ArrayList<>(); + List regexRules = new ArrayList<>(); + Bannedwordslist.populateSpamRules("(a|b)+c", stringRules, regexRules, null); + assertTrue(bannedwordslist.isBannedwordslisted(text, stringRules, regexRules)); + assertFalse(Bannedwordslist.matchesRulesOnly(text, stringRules, regexRules, false)); + } + } From 89a8639b4c2be31688e6a45159ff2ade5b09d8e3 Mon Sep 17 00:00:00 2001 From: Matt Raible Date: Fri, 20 Nov 2026 14:01:14 -0700 Subject: [PATCH 05/10] Skip banned words lines that only look blank, and match a regex line that doesn't compile as plain text A line holding only a non-breaking or ideographic space became a rule that matched any comment with that character, and an empty line in the site-wide list, or in a weblog's list on a trusted site, matched every comment. Such lines are now skipped. While weblog admins are untrusted, a line such as (18+ is saved as a plain word. If the site later sets weblogAdminsUntrusted=false, that line is not a valid regex, and every comment post on the weblog failed. At check time such a line is now logged and matched as plain text, and Weblog Settings still refuses it when the list is saved as regexes. --- .../ui/struts2/editor/WeblogConfig.java | 3 + .../weblogger/util/Bannedwordslist.java | 70 +++++++++++++++---- .../weblogger/util/BannedwordslistTest.java | 34 +++++++++ 3 files changed, 94 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/WeblogConfig.java b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/WeblogConfig.java index 0c50ed0c5..18b603541 100644 --- a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/WeblogConfig.java +++ b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/WeblogConfig.java @@ -207,6 +207,9 @@ private void myValidate() { List stringRules = new ArrayList<>(); try { // just for testing/counting, this does not persist rules in any way + if (!weblogAdminsUntrusted) { + Bannedwordslist.validateRules(getBean().getBannedwordslist()); + } Bannedwordslist.populateSpamRules(getBean().getBannedwordslist(), stringRules, regexRules, null, weblogAdminsUntrusted); if (weblogAdminsUntrusted) { 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 ef28c37f7..180593c98 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 @@ -457,34 +457,78 @@ public static void populateSpamRules( * plain words (case-insensitive, at word boundaries) rather than as * regular expressions, so an untrusted weblog admin can't write a rule * that backtracks; the addendum's rules are still regular expressions. + * A regex rule that doesn't compile is logged and matched as a plain + * word; use validateRules() to refuse it when the list is saved. */ public static void populateSpamRules(String bannedwordslist, List stringRules, List regexRules, String addendum, boolean weblogRulesAsWords) { if (weblogRulesAsWords) { - readRules(addendum, stringRules, regexRules); - StringTokenizer toker = new StringTokenizer(bannedwordslist == null ? "" : bannedwordslist, "\n"); - while (toker.hasMoreTokens()) { - String token = toker.nextToken().trim(); - if (!token.isEmpty() && !token.startsWith("#")) { - regexRules.add(wordPattern(token)); - } + readRules(addendum, stringRules, regexRules, false); + for (String rule : ruleLines(bannedwordslist)) { + regexRules.add(wordPattern(rule)); } } else { String weblogWords = (bannedwordslist == null) ? "" : bannedwordslist; String siteWords = (addendum != null) ? addendum : ""; - readRules(siteWords + "\n" + weblogWords, stringRules, regexRules); + readRules(siteWords + "\n" + weblogWords, stringRules, regexRules, false); } } - private static void readRules(String rules, List stringRules, List regexRules) { + /** + * Checks that each regex rule in a banned words list compiles. + * @throws PatternSyntaxException for the first one that doesn't + */ + public static void validateRules(String bannedwordslist) { + readRules(bannedwordslist, new ArrayList<>(), new ArrayList<>(), true); + } + + /** The lines of a list that are rules, without comments or blank lines. */ + private static List ruleLines(String rules) { + List lines = new ArrayList<>(); StringTokenizer toker = new StringTokenizer(rules == null ? "" : rules, "\n"); while (toker.hasMoreTokens()) { - String token = toker.nextToken().trim(); - if (token.startsWith("#")) { - continue; + String token = stripSpaces(toker.nextToken()); + if (!token.isEmpty() && !token.startsWith("#")) { + lines.add(token); } + } + return lines; + } + + /** + * Removes white space from both ends, including spaces such as U+00A0 + * and U+3000 that trim() keeps and that look blank in a text box. + */ + private static String stripSpaces(String str) { + int start = 0; + int end = str.length(); + while (start < end && isSpace(str.charAt(start))) { + start++; + } + while (end > start && isSpace(str.charAt(end - 1))) { + end--; + } + return str.substring(start, end); + } + + private static boolean isSpace(char c) { + return Character.isWhitespace(c) || Character.isSpaceChar(c); + } + + private static void readRules(String rules, List stringRules, List regexRules, + boolean strict) { + for (String token : ruleLines(rules)) { if (token.startsWith("(")) { - regexRules.add(Pattern.compile(token)); + try { + regexRules.add(Pattern.compile(token)); + } catch (PatternSyntaxException e) { + if (strict) { + throw e; + } + mLogger.warn("Banned words rule is not a valid regex and is matched as plain text: " + + token); + regexRules.add(wordPattern(token)); + } } else { stringRules.add(token); } 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 35a042ab2..7405fab72 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 @@ -30,8 +30,11 @@ import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -166,6 +169,37 @@ public void testWeblogRulesAsUnicodeWords() { assertFalse(Bannedwordslist.matchesRulesOnly("réélan", stringRules, regexRules)); } + // a line that looks blank, such as one non-breaking space, is not a rule + @Test + public void testBlankLinesAreNotRules() { + List stringRules = new ArrayList<>(); + List regexRules = new ArrayList<>(); + Bannedwordslist.populateSpamRules("foo\n\u00A0\n\u3000\nbar", stringRules, regexRules, null, true); + assertEquals(2, regexRules.size()); + assertFalse(Bannedwordslist.matchesRulesOnly("price:\u00A010 EUR", stringRules, regexRules)); + + // a browser ends lines with \r\n, so an empty line leaves "\r" + stringRules.clear(); + regexRules.clear(); + Bannedwordslist.populateSpamRules("foo\r\n\r\nbar", stringRules, regexRules, null, false); + assertEquals(List.of("foo", "bar"), stringRules); + assertFalse(Bannedwordslist.matchesRulesOnly("a perfectly ordinary comment", stringRules, regexRules)); + } + + // a line saved as a plain word while weblog admins were untrusted isn't + // always a valid regex once they are trusted; it is then matched as text + @Test + public void testInvalidRegexRuleIsMatchedAsText() { + List stringRules = new ArrayList<>(); + List regexRules = new ArrayList<>(); + Bannedwordslist.populateSpamRules("(18+", stringRules, regexRules, null, false); + assertTrue(Bannedwordslist.matchesRulesOnly("only (18+ here", stringRules, regexRules)); + assertFalse(Bannedwordslist.matchesRulesOnly("only 18 here", stringRules, regexRules)); + // saving the list as regexes still refuses it + assertThrows(PatternSyntaxException.class, () -> Bannedwordslist.validateRules("(18+")); + Bannedwordslist.validateRules("(casino)\nviagra"); + } + // each rule has its own time limit, so a long list of plain words // against a long comment is slow but isn't counted as a match @Test From 5e03bb39e6a75823ed3873e36143e9197b49e6c7 Mon Sep 17 00:00:00 2001 From: Matt Raible Date: Fri, 20 Nov 2026 14:02:46 -0700 Subject: [PATCH 06/10] Match a weblog's banned word in Chinese, Japanese, Thai and other scripts written without spaces MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A plain-word rule checked for a word edge on each end, and in these scripts a word is usually next to other letters, so a rule such as 赌场 never matched running text. There is now no edge check next to a letter of such a script. --- .../weblogger/util/Bannedwordslist.java | 23 +++++++++++++++---- .../weblogger/util/BannedwordslistTest.java | 14 +++++++++++ docs/roller-user-guide.adoc | 3 ++- 3 files changed, 35 insertions(+), 5 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 180593c98..ffdea9906 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 @@ -35,7 +35,9 @@ import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; +import java.util.EnumSet; import java.util.List; +import java.util.Set; import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -543,24 +545,37 @@ private static void readRules(String rules, List stringRules, List SCRIPTS_WITHOUT_SPACES = EnumSet.of( + Character.UnicodeScript.HAN, Character.UnicodeScript.HIRAGANA, + Character.UnicodeScript.KATAKANA, Character.UnicodeScript.THAI, + Character.UnicodeScript.LAO, Character.UnicodeScript.KHMER, + Character.UnicodeScript.MYANMAR); + /** * Longest one rule may take to check one value, such as a comment's * text. A regex rule that backtracks without end would otherwise hold 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 7405fab72..98ac04728 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 @@ -169,6 +169,20 @@ public void testWeblogRulesAsUnicodeWords() { assertFalse(Bannedwordslist.matchesRulesOnly("réélan", stringRules, regexRules)); } + // scripts written without spaces between words have no word edges to check + @Test + public void testWeblogRulesInScriptsWithoutSpaces() { + List stringRules = new ArrayList<>(); + List regexRules = new ArrayList<>(); + // "赌场" is casino in Chinese, "カジノ" in Japanese, "คาสิโน" in Thai + Bannedwordslist.populateSpamRules("赌场\nカジノ\nคาสิโน\ncasino", stringRules, regexRules, null, true); + assertTrue(Bannedwordslist.matchesRulesOnly("在线赌场网站", stringRules, regexRules)); + assertTrue(Bannedwordslist.matchesRulesOnly("オンラインカジノです", stringRules, regexRules)); + assertTrue(Bannedwordslist.matchesRulesOnly("เล่นคาสิโนออนไลน์", stringRules, regexRules)); + assertTrue(Bannedwordslist.matchesRulesOnly("赌场casino", stringRules, regexRules)); + assertFalse(Bannedwordslist.matchesRulesOnly("casinos", stringRules, regexRules)); + } + // a line that looks blank, such as one non-breaking space, is not a rule @Test public void testBlankLinesAreNotRules() { diff --git a/docs/roller-user-guide.adoc b/docs/roller-user-guide.adoc index 80968f9b5..902d237ff 100644 --- a/docs/roller-user-guide.adoc +++ b/docs/roller-user-guide.adoc @@ -817,7 +817,8 @@ here’s how you do it: fields ** Enter your spam words or phrases, one per line. Each one matches regardless of case, as a whole word: _casino_ matches "Casino" but not -"casinos". +"casinos". A word in Chinese, Japanese, Thai or another script written +without spaces matches anywhere in the text. ** By default (_weblogAdminsUntrusted=true_), every line is matched as plain text, including lines that begin with a left parenthesis. On a site where weblog administrators are trusted (_weblogAdminsUntrusted=false_), From a48eee243e33dc217e71368ecce90e5a81038775 Mon Sep 17 00:00:00 2001 From: Matt Raible Date: Fri, 20 Nov 2026 14:04:23 -0700 Subject: [PATCH 07/10] List a weblog's banned words that look like regular expressions on Weblog Settings While weblog admins are untrusted, every line is matched as plain text, so a rule from an earlier version such as (casino) or viagra|cialis no longer matches what it used to. Weblog Settings now names those lines when the page opens and when the list is saved, so the weblog admin can rewrite them as words. --- .../ui/struts2/editor/WeblogConfig.java | 17 +++++++++++++++++ .../roller/weblogger/util/Bannedwordslist.java | 15 +++++++++++++++ .../resources/ApplicationResources.properties | 2 ++ .../weblogger/util/BannedwordslistTest.java | 8 ++++++++ docs/roller-user-guide.adoc | 3 ++- 5 files changed, 44 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/WeblogConfig.java b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/WeblogConfig.java index 18b603541..19d0083af 100644 --- a/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/WeblogConfig.java +++ b/app/src/main/java/org/apache/roller/weblogger/ui/struts2/editor/WeblogConfig.java @@ -110,6 +110,7 @@ public String execute() { // load bean with data from weblog getBean().copyFrom(getActionWeblog()); + listRulesMatchedAsText(); return INPUT; } @@ -214,6 +215,7 @@ private void myValidate() { weblogAdminsUntrusted); if (weblogAdminsUntrusted) { addMessage("websiteSettings.acceptedBannedwordsAsWords", List.of(""+regexRules.size())); + listRulesMatchedAsText(); } else { addMessage("websiteSettings.acceptedBannedwordslist", List.of(""+stringRules.size(), ""+regexRules.size())); } @@ -223,6 +225,21 @@ private void myValidate() { } + /** + * Tells the weblog admin which banned words look like regular + * expressions, since with untrusted weblog admins they are matched as + * plain text; a list from an earlier version may have such lines. + */ + private void listRulesMatchedAsText() { + if (weblogAdminsUntrusted) { + List rules = Bannedwordslist.regexStyleRules(getBean().getBannedwordslist()); + if (!rules.isEmpty()) { + addMessage("websiteSettings.bannedwordsMatchedAsText", String.join(", ", rules)); + } + } + } + + @StrutsParameter(depth = 1) public WeblogConfigBean getBean() { return bean; 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 ffdea9906..8a2fb54ac 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 @@ -484,6 +484,21 @@ public static void validateRules(String bannedwordslist) { readRules(bannedwordslist, new ArrayList<>(), new ArrayList<>(), true); } + /** + * The lines of a weblog's banned words that only mean something as a + * regular expression, so that a weblog admin can be told they are + * matched as plain words. + */ + public static List regexStyleRules(String bannedwordslist) { + List rules = new ArrayList<>(); + for (String rule : ruleLines(bannedwordslist)) { + if (rule.startsWith("(") || StringUtils.containsAny(rule, "|\\[*+?{^$")) { + rules.add(rule); + } + } + return rules; + } + /** The lines of a list that are rules, without comments or blank lines. */ private static List ruleLines(String rules) { List lines = new ArrayList<>(); diff --git a/app/src/main/resources/ApplicationResources.properties b/app/src/main/resources/ApplicationResources.properties index a113b25b8..e6a9f0211 100644 --- a/app/src/main/resources/ApplicationResources.properties +++ b/app/src/main/resources/ApplicationResources.properties @@ -1781,6 +1781,8 @@ websiteSettings.bannedWordsList=Words banned in comments (regex allowed) websiteSettings.bannedWordsListAsWords=Words banned in comments (one word or phrase per line) websiteSettings.acceptedBannedwordslist=Accepted {0} string and {1} regex banned-words list rules websiteSettings.acceptedBannedwordsAsWords=Accepted {0} banned-words list rules +websiteSettings.bannedwordsMatchedAsText=These banned words look like regular expressions but are \ +matched as plain text: {0} websiteSettings.error.processingBannedwordslist=Error processing banned-words list: {0} # --- web analytics 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 98ac04728..9738bc2aa 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 @@ -214,6 +214,14 @@ public void testInvalidRegexRuleIsMatchedAsText() { Bannedwordslist.validateRules("(casino)\nviagra"); } + // lines that only mean something as a regex are listed for the weblog admin + @Test + public void testRegexStyleRules() { + assertEquals(List.of("(casino)", "viagra|cialis", "poker-?online"), Bannedwordslist.regexStyleRules( + "casino\n(casino)\nviagra|cialis\n# (comment)\nexample.com\npoker-?online\n")); + assertEquals(List.of(), Bannedwordslist.regexStyleRules(null)); + } + // each rule has its own time limit, so a long list of plain words // against a long comment is slow but isn't counted as a match @Test diff --git a/docs/roller-user-guide.adoc b/docs/roller-user-guide.adoc index 902d237ff..22bfe1c4e 100644 --- a/docs/roller-user-guide.adoc +++ b/docs/roller-user-guide.adoc @@ -820,7 +820,8 @@ regardless of case, as a whole word: _casino_ matches "Casino" but not "casinos". A word in Chinese, Japanese, Thai or another script written without spaces matches anywhere in the text. ** By default (_weblogAdminsUntrusted=true_), every line is matched as -plain text, including lines that begin with a left parenthesis. On a site +plain text, including lines that begin with a left parenthesis, and Weblog +Settings lists the lines that look like regular expressions. On a site where weblog administrators are trusted (_weblogAdminsUntrusted=false_), lines that begin with a left parenthesis are regular expressions, and other lines are matched as regular expressions at word boundaries (see the Java From 4f853d9cc21d8c47a78cf786534e4142833c89bd Mon Sep 17 00:00:00 2001 From: Matt Raible Date: Fri, 20 Nov 2026 14:04:23 -0700 Subject: [PATCH 08/10] Check the example.com/ banned word with a site-wide rule that can't also match The site-wide rule (c|d) matched the c in the URL, so the assertion passed even without the example.com/ rule. The test now uses a rule that can't match the URL, and checks that a different domain doesn't match. --- .../apache/roller/weblogger/util/BannedwordslistTest.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 9738bc2aa..ad16ee32d 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 @@ -139,15 +139,16 @@ public void testWeblogRulesAsWords() { List stringRules = new ArrayList<>(); List regexRules = new ArrayList<>(); Bannedwordslist.populateSpamRules("(a|b)\nViagra\n(foo)\nexample.com/", stringRules, regexRules, - "(c|d)", true); + "(qq|zz)", true); assertFalse(Bannedwordslist.matchesRulesOnly("a", stringRules, regexRules)); assertTrue(Bannedwordslist.matchesRulesOnly("spam (a|b) here", stringRules, regexRules)); assertTrue(Bannedwordslist.matchesRulesOnly("buy VIAGRA now", stringRules, regexRules)); assertFalse(Bannedwordslist.matchesRulesOnly("viagraphone", stringRules, regexRules)); assertTrue(Bannedwordslist.matchesRulesOnly("see (foo) now", stringRules, regexRules)); assertTrue(Bannedwordslist.matchesRulesOnly("http://example.com/x", stringRules, regexRules)); + assertFalse(Bannedwordslist.matchesRulesOnly("http://example.org/x", stringRules, regexRules)); // the site-wide rule is still a regex - assertTrue(Bannedwordslist.matchesRulesOnly("d", stringRules, regexRules)); + assertTrue(Bannedwordslist.matchesRulesOnly("zz", stringRules, regexRules)); // with the setting off, the weblog's rules are regexes as before stringRules.clear(); From 198d5bca8d1f07ad034426ed022e412aa78ff239 Mon Sep 17 00:00:00 2001 From: Matt Raible Date: Fri, 20 Nov 2026 14:04:37 -0700 Subject: [PATCH 09/10] Describe the weblog banned words change in CHANGES --- CHANGES.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/CHANGES.md b/CHANGES.md index 914cdb79e..22f2d12c7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -240,6 +240,20 @@ domains as before but look at most 63 characters between their parts; a copy of the old file in the uploads directory is still used if present. +- **A weblog's banned words are plain words, not regular expressions.** With + `weblogAdminsUntrusted=true` (the default), each line of a weblog's banned + words on Weblog Settings matches as literal text, in any case, as a whole + word, including lines that begin with `(`. A word in Chinese, Japanese, + Thai or another script written without spaces matches anywhere in the + text. Rules written as regular expressions, such as `(casino)` or + `viagra|cialis`, no longer match what they used to; Weblog Settings lists + them so the weblog admin can rewrite them as words. The built-in list, the + site-wide list in Server Admin, and weblog rules on sites with + `weblogAdminsUntrusted=false` are still regular expressions. + A regular expression rule that takes over a second on one value, or runs + out of stack, is logged; a comment is then treated as spam, and a referrer + is let through. + ### Build and packaging - The Docker image starts Tomcat with `-XX:-UseBASE64Intrinsics`, which From 930231660c1ed52fbabcf82b8f64d3732b3b21a4 Mon Sep 17 00:00:00 2001 From: Matt Raible Date: Fri, 20 Nov 2026 16:07:09 -0700 Subject: [PATCH 10/10] Match a weblog banned words line such as (casino) as that text anywhere With weblogAdminsUntrusted=true, a weblog line that is only letters, combining marks, digits and spaces inside one pair of parentheses is matched as the text inside, anywhere in the value and in any case, so rules written for earlier versions keep matching what they used to. Weblog Settings no longer lists such lines as looking like regular expressions. Any other line, including one with other regex syntax such as (casino|poker), is still matched as literal text at word edges. --- CHANGES.md | 18 ++++++----- .../weblogger/util/Bannedwordslist.java | 31 +++++++++++++++++-- .../weblogger/util/BannedwordslistTest.java | 23 ++++++++++++-- docs/roller-user-guide.adoc | 5 ++- 4 files changed, 63 insertions(+), 14 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 22f2d12c7..5bc21ac71 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -245,14 +245,16 @@ words on Weblog Settings matches as literal text, in any case, as a whole word, including lines that begin with `(`. A word in Chinese, Japanese, Thai or another script written without spaces matches anywhere in the - text. Rules written as regular expressions, such as `(casino)` or - `viagra|cialis`, no longer match what they used to; Weblog Settings lists - them so the weblog admin can rewrite them as words. The built-in list, the - site-wide list in Server Admin, and weblog rules on sites with - `weblogAdminsUntrusted=false` are still regular expressions. - A regular expression rule that takes over a second on one value, or runs - out of stack, is logged; a comment is then treated as spam, and a referrer - is let through. + text. A line that is only letters, digits and spaces in parentheses, such + as `(casino)`, matches that text anywhere, in any case, so such rules from + earlier versions keep working. Other rules written as regular expressions, + such as `(casino|poker)` or `viagra|cialis`, no longer match what they used + to; Weblog Settings lists them so the weblog admin can rewrite them as + words. The built-in list, the site-wide list in Server Admin, and weblog + rules on sites with `weblogAdminsUntrusted=false` are still regular + expressions. A regular expression rule that takes over a second on one + value, or runs out of stack, is logged; a comment is then treated as spam, + and a referrer is let through. ### Build and packaging 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 8a2fb54ac..4265e0511 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 @@ -459,6 +459,9 @@ public static void populateSpamRules( * plain words (case-insensitive, at word boundaries) rather than as * regular expressions, so an untrusted weblog admin can't write a rule * that backtracks; the addendum's rules are still regular expressions. + * A weblog line such as (casino), written for earlier versions, that is + * only letters, digits and spaces in parentheses is matched as that text + * anywhere, case-insensitive, without word edges. * A regex rule that doesn't compile is logged and matched as a plain * word; use validateRules() to refuse it when the list is saved. */ @@ -467,7 +470,8 @@ public static void populateSpamRules(String bannedwordslist, List string if (weblogRulesAsWords) { readRules(addendum, stringRules, regexRules, false); for (String rule : ruleLines(bannedwordslist)) { - regexRules.add(wordPattern(rule)); + String text = textInParentheses(rule); + regexRules.add(text != null ? textPattern(text) : wordPattern(rule)); } } else { String weblogWords = (bannedwordslist == null) ? "" : bannedwordslist; @@ -487,12 +491,14 @@ public static void validateRules(String bannedwordslist) { /** * The lines of a weblog's banned words that only mean something as a * regular expression, so that a weblog admin can be told they are - * matched as plain words. + * matched as plain words. A line that is text in parentheses isn't + * listed, since it still matches that text. */ public static List regexStyleRules(String bannedwordslist) { List rules = new ArrayList<>(); for (String rule : ruleLines(bannedwordslist)) { - if (rule.startsWith("(") || StringUtils.containsAny(rule, "|\\[*+?{^$")) { + if (textInParentheses(rule) == null + && (rule.startsWith("(") || StringUtils.containsAny(rule, "|\\[*+?{^$"))) { rules.add(rule); } } @@ -552,6 +558,25 @@ private static void readRules(String rules, List stringRules, List stringRules = new ArrayList<>(); + List regexRules = new ArrayList<>(); + Bannedwordslist.populateSpamRules("(casino)\n(Online Poker 24)\n(casino|poker)\n( )", + stringRules, regexRules, null, true); + assertTrue(Bannedwordslist.matchesRulesOnly("http://onlinecasino.example", stringRules, regexRules)); + assertTrue(Bannedwordslist.matchesRulesOnly("BEST CASINOS", stringRules, regexRules)); + assertTrue(Bannedwordslist.matchesRulesOnly("play online poker 24/7", stringRules, regexRules)); + // a line with other regex syntax is still literal text + assertFalse(Bannedwordslist.matchesRulesOnly("poker night", stringRules, regexRules)); + assertTrue(Bannedwordslist.matchesRulesOnly("see (casino|poker)", stringRules, regexRules)); + // parentheses around spaces only are not unwrapped + assertFalse(Bannedwordslist.matchesRulesOnly("an ordinary comment", stringRules, regexRules)); + } + // each rule has its own time limit, so a long list of plain words // against a long comment is slow but isn't counted as a match @Test diff --git a/docs/roller-user-guide.adoc b/docs/roller-user-guide.adoc index 22bfe1c4e..f6850d7c4 100644 --- a/docs/roller-user-guide.adoc +++ b/docs/roller-user-guide.adoc @@ -821,7 +821,10 @@ regardless of case, as a whole word: _casino_ matches "Casino" but not without spaces matches anywhere in the text. ** By default (_weblogAdminsUntrusted=true_), every line is matched as plain text, including lines that begin with a left parenthesis, and Weblog -Settings lists the lines that look like regular expressions. On a site +Settings lists the lines that look like regular expressions. A line that is +only letters, digits and spaces in parentheses, such as _(casino)_, matches +that text anywhere, regardless of case: _(casino)_ matches "casinos" and +"onlinecasino". On a site where weblog administrators are trusted (_weblogAdminsUntrusted=false_), lines that begin with a left parenthesis are regular expressions, and other lines are matched as regular expressions at word boundaries (see the Java