Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,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.
Expand All @@ -325,19 +326,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<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.
Expand All @@ -347,22 +352,30 @@ public boolean isBannedwordslisted(
*/
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;
}
}
Expand All @@ -381,7 +394,7 @@ private static boolean testRegExRules(String str, List<Pattern> regexRules) {
*
* @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) {
Expand All @@ -400,13 +413,13 @@ private static boolean testStringRules(String source, List<String> 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;
}
Expand Down Expand Up @@ -442,6 +455,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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,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;
Expand Down Expand Up @@ -106,4 +109,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<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"));
}

}
Loading