Skip to content

fix(alert): escape LIKE wildcards in rule name and metric search - #4233

Closed
89799969 wants to merge 1 commit into
apache:masterfrom
89799969:fix/alert-search-like-escape
Closed

89799969 wants to merge 1 commit into
apache:masterfrom
89799969:fix/alert-search-like-escape

Conversation

@89799969

@89799969 89799969 commented Sep 11, 2026

Copy link
Copy Markdown
Contributor

What is the purpose of the change

Fixes #4232.

Alert-rule inventory search treats % and _ as SQL LIKE wildcards instead of literal characters:

  • GET /api/alert-rules/page?search= / GET /api/business-alert-rules/page?search= filter name with unescaped LIKE (MybatisPlusAlertRepository.ruleQuery).
  • Domain-scoped paging (AlertRuleQuery.search) filters name and metric the same way (MybatisPlusAlertRepository.findRulesPage).

Alert-rule names and metric identifiers commonly contain underscores (consumer_lag, disk_usage). Searching for consumer_lag therefore also matches consumerXlag, and a trailing % matches any suffix — so the inventory silently returns unrelated rules or hides the intended one.

Brief changelog

  • Add a private escapeLike helper (same behaviour as the existing QueryHistoryService.escapeLike) and apply it to the legacy name-only page search and the domain page name/metric search.
  • Two regression tests assert the bound LIKE value for prod_lag% / consumer_lag% is escaped (%prod\_lag\%%) rather than treated as a wildcard pattern.

Verifying this change

$ mvn -f server/pom.xml test -Dtest='Alert*Test,MybatisPlusAlertRepositoryTest'
[INFO] Tests run: 198, Failures: 0, Errors: 0, Skipped: 0
[INFO] BUILD SUCCESS

Checkstyle: 0 violations (module validate phase).

Before the fix the two new tests fail because the bound parameters are %prod_lag% / %consumer_lag%; after it they bind %prod\_lag\%% / %consumer\_lag\%%.

Related peers: #4229/#4230 (ACL inventory), #4223/#4224 (Studio user search), #4192#4194 (audit / instance / cloud-credential LIKE escape).

@RockteMQ-AI RockteMQ-AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Clean fix for SQL LIKE wildcard injection in alert-rule search. The escapeLike helper correctly escapes \, %, and _ in the right order (backslash first to avoid double-escaping), and the unit tests verify both positive (escaped chars present in query) and negative (unescaped values absent) cases.

Findings

  • [Info] MybatisPlusAlertRepository.java:371 — The escapeLike method is duplicated across multiple repositories in this codebase (see also QueryHistoryService.escapeLike, MybatisPlusAclRepository.escapeLike, AuthService.escapeLike). Consider extracting to a shared utility class (e.g. SqlLikeEscape) to reduce drift risk.

LGTM — good fix with solid test coverage.

@lizhimins lizhimins left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same single item as on #4224 and #4230: replace the private escapeLike copy with the shared utility in common/util, and add an explicit ESCAPE clause rather than relying on MySQL's implicit backslash (H2, used by five tests in this suite, has no default LIKE escape character). Please keep the tests.

Use the shared SqlLikeUtil helper with an explicit ESCAPE clause so underscore-bearing rule names and metrics match literally on both MySQL and H2.

Fixes apache#4332

Signed-off-by: halaxy <63827956+89799969@users.noreply.github.com>
@89799969
89799969 force-pushed the fix/alert-search-like-escape branch from e465e0c to 6f89b25 Compare September 16, 2026 08:59
@89799969

Copy link
Copy Markdown
Contributor Author

Same mechanical fix as requested on #4224/#4230: shared SqlLikeUtil + explicit ESCAPE clause. Tests kept.

@89799969

Copy link
Copy Markdown
Contributor Author

@lizhimins Following up - shared SqlLikeUtil + explicit ESCAPE is pushed (6f89b257) and this branch is MERGEABLE/CLEAN on current rocketmq-studio. Ready for re-review.

@RockteMQ-AI RockteMQ-AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Code changes look good.


Automated review by github-manager-bot

@89799969
89799969 changed the base branch from rocketmq-studio to master September 16, 2026 12:48
@lizhimins
lizhimins deleted the branch apache:master September 16, 2026 12:48
@lizhimins lizhimins closed this Sep 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Studio][Bug] Alert-rule search does not escape SQL LIKE wildcards

3 participants