[Task] Add timeout protection and documentation for RegexPredicateJudge
Description
RegexPredicateJudge.judge() evaluates rule/selector conditions using Pattern.matches(regex, input) with no timeout. The input (realData) is fully attacker-controlled via the incoming HTTP request (URI path, headers, query parameters, etc.). A regex condition with catastrophic backtracking potential (e.g., ([a-zA-Z]+)*$) configured by an Admin can be exploited by an external attacker to pin Netty worker threads indefinitely, causing gateway-wide availability degradation. Affected File:
shenyu-plugin/shenyu-plugin-base/src/main/java/org/apache/shenyu/plugin/base/condition/judge/RegexPredicateJudge.java Proposed Change:
Add a configurable match timeout in RegexPredicateJudge.judge(). If the match does not complete within the timeout, return false and log a warning. Add regex safety validation in RuleServiceImpl and SelectorServiceImpl when saving conditions with operator=regex. At minimum, verify the pattern compiles without error. Optionally, reject known dangerous patterns (nested quantifiers). Add documentation guidance on safe regex pattern writing for rule/selector conditions.
Acceptance Criteria:
A catastrophically backtracking regex condition does not permanently pin a Netty worker thread. Admin-configured regex patterns are validated at save time. Official documentation includes a section on safe regex usage in conditions.
Task List
No response
👋 Hello @Aias00
A new issue has been opened by @Aias00:
- Title: [Task] Add timeout protection and documentation for RegexPredicateJudge
- Link: https://github.com/apache/shenyu/issues/6404
Please review when you have time. Thank you! 🙏
i want to try this
#6335 in this pr, i add com.google.re2j dependency in dependencyManagement in root pom.xml , so i'm considering whether we should allow using com.google.re2j.Pattern to replace java.util.regex.Pattern to resolve this issue.
#6335 in this pr, i add com.google.re2j dependency in dependencyManagement in root pom.xml , so i'm considering whether we should allow using com.google.re2j.Pattern to replace java.util.regex.Pattern to resolve this issue.
It`s better? can u add some description?
java.util.regex use Backtracking NFA, so it will cause time complexity O(2^n) but re2j use Thompson NFA can't cause backtracking and guarantee O(n). if we add timeout protection to prevent redos. first the truth is attack has happend, and event loop will be block until timeout, but re2j can't happen the redos attack.
java.util.regex use Backtracking NFA, so it will cause time complexity O(2^n) but re2j use Thompson NFA can't cause backtracking and guarantee O(n). if we add timeout protection to prevent redos. first the truth is attack has happend, and event loop will be block until timeout, but re2j can't happen the redos attack.
sounds good to me