shenyu icon indicating copy to clipboard operation
shenyu copied to clipboard

[Task] Add timeout protection and documentation for RegexPredicateJudge

Open Aias00 opened this issue 5 months ago • 6 comments

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

Aias00 avatar Jun 26 '26 10:06 Aias00

👋 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! 🙏

github-actions[bot] avatar Jun 26 '26 11:06 github-actions[bot]

i want to try this

hengyuss avatar Jun 27 '26 11:06 hengyuss

#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.

hengyuss avatar Jun 27 '26 13:06 hengyuss

#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?

Aias00 avatar Jun 27 '26 14:06 Aias00

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.

hengyuss avatar Jun 28 '26 05:06 hengyuss

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

Aias00 avatar Jun 28 '26 06:06 Aias00