github-advanced-security[bot] commented on code in PR #357:
URL: https://github.com/apache/roller/pull/357#discussion_r4648916403


##########
app/src/main/java/org/apache/roller/weblogger/util/Bannedwordslist.java:
##########
@@ -426,23 +450,250 @@
     /** Utility method to populate lists based a bannedwordslist in string 
form */
     public static void populateSpamRules(
         String bannedwordslist, List<String> stringRules, List<Pattern> 
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.
+     * 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.
+     */
+    public static void populateSpamRules(String bannedwordslist, List<String> 
stringRules,
+            List<Pattern> regexRules, String addendum, boolean 
weblogRulesAsWords) {
+        if (weblogRulesAsWords) {
+            readRules(addendum, stringRules, regexRules, false);
+            for (String rule : ruleLines(bannedwordslist)) {
+                String text = textInParentheses(rule);
+                regexRules.add(text != null ? textPattern(text) : 
wordPattern(rule));
+            }
+        } else {
+            String weblogWords = (bannedwordslist == null) ? "" : 
bannedwordslist;
+            String siteWords = (addendum != null) ? addendum : "";
+            readRules(siteWords + "\n" + weblogWords, stringRules, regexRules, 
false);
+        }
+    }
+
+    /**
+     * 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 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. A line that is text in parentheses isn't
+     * listed, since it still matches that text.
+     */
+    public static List<String> regexStyleRules(String bannedwordslist) {
+        List<String> rules = new ArrayList<>();
+        for (String rule : ruleLines(bannedwordslist)) {
+            if (textInParentheses(rule) == null
+                    && (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<String> ruleLines(String rules) {
+        List<String> 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<String> stringRules, 
List<Pattern> regexRules,
+            boolean strict) {
+        for (String token : ruleLines(rules)) {
             if (token.startsWith("(")) {
-                regexRules.add(Pattern.compile(token));
+                try {
+                    regexRules.add(Pattern.compile(token));

Review Comment:
   ## CodeQL / Regular expression injection
   
   This regular expression is constructed from a [user-provided value](1).
   
   [Show more 
details](https://github.com/apache/roller/security/code-scanning/158)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to