Skip to content

fix(qqbot): guard mentionPatterns against ReDoS via compileSafeRegex - #102976

Merged
steipete merged 1 commit into
openclaw:mainfrom
hugenshen:fix/qqbot-mention-redos
Jul 13, 2026
Merged

steipete merged 1 commit into
openclaw:mainfrom
hugenshen:fix/qqbot-mention-redos

Conversation

@hugenshen

@hugenshen hugenshen commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes a ReDoS vulnerability where operator-configured mentionPatterns in the QQBot
plugin were compiled with bare new RegExp(pattern, "i") without any safety check.
A pattern containing nested repetition such as (a+)+$ compiles successfully but
causes catastrophic backtracking when matched against attacker-controlled message
content, pushing CPU to 100% and making the gateway unresponsive.

Attack chain: set mentionPatterns: ["(a+)+$"] in config → attacker sends a message
with a long run of 'a' characters → regex match never terminates → DoS.

The same class of bug was previously fixed in MCP glob patterns (#100330).
compileSafeRegex from src/security/safe-regex.ts is already exposed through
openclaw/plugin-sdk/security-runtime for exactly this use.

Why This Change Was Made

Replace new RegExp(pattern, "i") with compileSafeRegex(pattern, "i"). The helper
rejects patterns with nested repetition and returns null for both unsafe and invalid
patterns, so the loop simply skips them — same observable behavior as before for valid
safe patterns, silent rejection for dangerous ones.

User Impact

Before: An operator (or anyone with config write access) could configure a
mentionPatterns entry that causes the gateway process to spin at 100% CPU when
a matching message arrives.

After: Patterns with nested repetition are rejected at match time and skipped.
Safe patterns continue to work normally.

Evidence

Node: v22.22.0 — macOS Darwin 24.3.0 arm64

LOC — extensions/qqbot/src/engine/group/mention.ts: +4/−5 (import + replace).
extensions/qqbot/src/engine/group/mention.test.ts: +20 (2 new regression cases).

Before — catastrophic backtracking via raw new RegExp:

new RegExp("(a+)+$", "i").test("a".repeat(20)+"b") →   65.5 ms
new RegExp("(a+)+$", "i").test("a".repeat(24)+"b") →  156.9 ms
new RegExp("(a+)+$", "i").test("a".repeat(26)+"b") →  640.7 ms
new RegExp("(a+)+$", "i").test("a".repeat(28)+"b") → 2522.4 ms  (2.5 s — gateway stalls)

Each +2 characters roughly 4× the time (exponential growth). At 64 characters the gateway
process would be unresponsive for minutes.

After — compileSafeRegex guard, constant-time rejection:

compileSafeRegex("(a+)+$", "i") → null (rejected before RegExp construction)
detectWasMentioned({ content: "a".repeat(24)+"b", mentionPatterns: ["(a+)+$"] }) → false (0.065 ms)
detectWasMentioned({ content: "a".repeat(28)+"b", mentionPatterns: ["(a+)+$"] }) → false (0.003 ms)

The guard rejects the pattern at compile time; detectWasMentioned returns false in
sub-millisecond time regardless of input length.

Safe pattern still matches after a rejected unsafe one:

detectWasMentioned({
  content: "hello @bot",
  mentionPatterns: ["(a+)+$", "@bot"],
}) → true (0.130 ms)

Regression tests — 24/24:

$ node scripts/run-vitest.mjs extensions/qqbot/src/engine/group/mention.test.ts --reporter=verbose

 ✓ detectWasMentioned > rejects ReDoS patterns via compileSafeRegex guard    0ms
 ✓ detectWasMentioned > still matches safe patterns after a rejected unsafe one  0ms
 ✓ detectWasMentioned > ignores invalid regex patterns gracefully  0ms
 ... (21 more)

 Test Files  1 passed (1)
      Tests  24 passed (24)
   Duration  370ms

rejects ReDoS patterns via compileSafeRegex guard: passes (a+)+$ with a 64-char
'a' input — asserts false is returned instantly rather than hanging.

still matches safe patterns after a rejected unsafe one: confirms the unsafe pattern
is skipped and the next safe pattern still matches.

What was not tested: live QQBot message delivery with a real QQ group.
Crabbox/Testbox remote CI was not run at submission time.

Related: #100330 (MCP glob ReDoS — same fix pattern)

  • AI-assisted (Cursor / Claude Sonnet 4.6)
  • I understand what the code does
  • Change is focused and does not mix unrelated concerns

@clawsweeper

clawsweeper Bot commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs real behavior proof before merge. Reviewed July 12, 2026, 9:06 PM ET / July 13, 2026, 01:06 UTC.

Summary
The PR replaces QQBot’s raw compilation of configured mention patterns with the plugin SDK safe-regex compiler, adds bounded rejection diagnostics and compilation caching, and extends focused tests.

PR surface: Source +54, Tests +53. Total +107 across 2 files.

Reproducibility: yes. The PR supplies a concrete raw-RegExp timing reproduction and after-fix detectWasMentioned output, and the inspected current-main path matches that reproduction.

Review metrics: 1 noteworthy metric.

  • Configured regex policy: 1 existing behavior changed. QQBot mentionPatterns now rejects unsafe expressions and caches compiled results, making upgrade semantics and cache identity relevant before merge.

Stored data model
Persistent data-model change detected: persistent cache schema: extensions/qqbot/src/engine/group/mention.ts. Confirm migration or upgrade compatibility proof before merge.

Merge readiness
Overall: 🦪 silver shellfish
Proof: 🦪 silver shellfish
Patch quality: 🦐 gold shrimp
Result: blocked until stronger real behavior proof is added.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • [P1] Replace the delimiter-joined cache key and add a focused collision regression test.
  • Post redacted current-head terminal output showing unsafe rejection, safe continuation, and the corrected sequential-cache behavior.

Proof guidance:

  • [P1] Needs stronger real behavior proof before merge: The PR contains useful terminal evidence for the earlier safe-regex behavior, but the current head subsequently added caching and diagnostics without current-head real-runtime proof; after correction, provide redacted terminal output from the exact head and update the PR body to trigger re-review.

Risk before merge

  • [P1] The delimiter-based cache key can make one configured pattern list reuse another list’s compiled regexes, suppressing intended QQBot replies or triggering replies for content that should not match.
  • [P1] Unsafe patterns that previously compiled successfully will stop matching after upgrade; that fail-closed compatibility change is appropriate for the hardening goal but should remain explicit in the merge decision.

Maintainer options:

  1. Fix cache identity before merge (recommended)
    Use an unambiguous ordered-array cache key and add a sequential-call test proving distinct pattern lists never share compiled matchers.
  2. Remove the compile cache
    Compile through the safe helper per call while retaining bounded warning deduplication if maintainers prefer simplicity over the unproven optimization.
Copy recommended automerge instruction
@clawsweeper automerge

Special instructions:
Replace the delimiter-joined QQBot mention-pattern cache key with collision-free ordered-array encoding, add a regression test for ["foo", "bar"] versus ["foo^_bar"], and preserve bounded caching, safe-regex rejection, and reason-aware diagnostics.

Next step before merge

  • [P1] A narrow mechanical cache-key correction and regression test can repair the maintained PR head; refreshed real-behavior proof must still come from the contributor or maintainer setup.

Security
Cleared: The diff adds defense-in-depth regex hardening through an existing SDK surface and introduces no concrete security-boundary or supply-chain regression; the cache defect is functional message-gating correctness.

Review findings

  • [P2] Use a collision-free key for compiled pattern lists — extensions/qqbot/src/engine/group/mention.ts:65
Review details

Best possible solution:

Retain the shared safe-regex compiler and bounded reason-aware diagnostics, replace the cache key with collision-free ordered-array encoding or remove the cache, and add a sequential-call collision regression test.

Do we have a high-confidence way to reproduce the issue?

Yes. The PR supplies a concrete raw-RegExp timing reproduction and after-fix detectWasMentioned output, and the inspected current-main path matches that reproduction.

Is this the best way to solve the issue?

No, not yet. Reusing the shared safe-regex contract is the right boundary, but the new cache must identify ordered pattern arrays without collisions before this is the narrowest correct solution.

Full review comments:

  • [P2] Use a collision-free key for compiled pattern lists — extensions/qqbot/src/engine/group/mention.ts:65
    patterns.join("^_") is not injective: ["foo", "bar"] and ["foo^_bar"] produce the same key. A later call can therefore receive the first list’s regexes and make the wrong mention decision; encode the array unambiguously or remove the cache, then cover the sequential-call collision.
    Confidence: 0.99

Overall correctness: patch is incorrect
Overall confidence: 0.97

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against 6c884691105b.

Label changes

Label changes:

  • add merge-risk: 🚨 compatibility: Existing QQBot configurations containing expressions rejected by the safe compiler can stop matching after upgrade.
  • add merge-risk: 🚨 message-delivery: The cache-key collision can reuse the wrong regex set and therefore suppress or wrongly trigger QQBot group replies.
  • add rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🦐 gold shrimp.
  • add status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs stronger real behavior proof before merge: The PR contains useful terminal evidence for the earlier safe-regex behavior, but the current head subsequently added caching and diagnostics without current-head real-runtime proof; after correction, provide redacted terminal output from the exact head and update the PR body to trigger re-review.

Label justifications:

  • P2: The PR addresses a real bounded availability bug, while the remaining cache collision is a normal-priority correctness blocker limited to QQBot mention gating.
  • merge-risk: 🚨 compatibility: Existing QQBot configurations containing expressions rejected by the safe compiler can stop matching after upgrade.
  • merge-risk: 🚨 message-delivery: The cache-key collision can reuse the wrong regex set and therefore suppress or wrongly trigger QQBot group replies.
  • rating: 🦪 silver shellfish: Overall readiness is 🦪 silver shellfish; proof is 🦪 silver shellfish and patch quality is 🦐 gold shrimp.
  • status: 📣 needs proof: The PR needs real behavior proof before ClawSweeper can clear the contributor ask. Needs stronger real behavior proof before merge: The PR contains useful terminal evidence for the earlier safe-regex behavior, but the current head subsequently added caching and diagnostics without current-head real-runtime proof; after correction, provide redacted terminal output from the exact head and update the PR body to trigger re-review.
Evidence reviewed

PR surface:

Source +54, Tests +53. Total +107 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 62 8 +54
Tests 1 54 1 +53
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 116 9 +107

Acceptance criteria:

  • [P1] pnpm test extensions/qqbot/src/engine/group/mention.test.ts.
  • [P1] pnpm check:changed -- extensions/qqbot/src/engine/group/mention.ts extensions/qqbot/src/engine/group/mention.test.ts.

What I checked:

Likely related people:

  • steipete: The live timeline records assignment and multiple head updates by steipete, and the current PR commit carries the maintained implementation. (role: recent patch carrier and assignee; confidence: high; commits: 5a5de04579a5; files: extensions/qqbot/src/engine/group/mention.ts, extensions/qqbot/src/engine/group/mention.test.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.
Review history (5 earlier review cycles)
  • reviewed 2026-07-09T16:04:53.823Z sha 74a6f6c86454b91d726d1db866a8a461941794fd :: needs real behavior proof before merge. :: [P3] Warn when rejecting QQBot mention patterns
  • reviewed 2026-07-10T13:21:33.760Z sha 02b32cc9e1f5f9186b386449baa12ae4458dbec5 :: needs changes before merge. :: [P1] Remove the unrelated MCP OAuth change | [P2] Preserve rejection diagnostics for configured patterns
  • reviewed 2026-07-10T13:55:36.682Z sha a68756959479396b08977788cb5b34ebf57ae33d :: needs changes before merge. :: [P1] Remove the unrelated MCP OAuth change | [P2] Preserve diagnostics for rejected configured patterns
  • reviewed 2026-07-10T14:32:48.913Z sha fa7df5490f4c21754ea2370e392524ffdbec42ec :: needs changes before merge. :: [P2] Preserve reason-aware bounded rejection diagnostics
  • reviewed 2026-07-12T00:24:03.110Z sha 340105bd5b64568136294c6514ddeab0de739c64 :: found issues before merge. :: [P2] Preserve reason-aware bounded rejection diagnostics

@clawsweeper clawsweeper Bot added rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P2 Normal backlog priority with limited blast radius. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. labels Jul 9, 2026
@hugenshen
hugenshen requested a review from a team as a code owner July 10, 2026 12:58
@hugenshen

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@openclaw-barnacle openclaw-barnacle Bot added cli CLI command changes agents Agent runtime and tooling and removed size: XS labels Jul 10, 2026
@clawsweeper

clawsweeper Bot commented Jul 10, 2026 •

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. and removed rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. labels Jul 10, 2026
@openclaw-barnacle openclaw-barnacle Bot removed cli CLI command changes agents Agent runtime and tooling labels Jul 10, 2026
@clawsweeper clawsweeper Bot removed the merge-risk: 🚨 auth-provider 🚨 May break OAuth, tokens, provider routing, model choice, or credentials. label Jul 10, 2026
@hugenshen
hugenshen force-pushed the fix/qqbot-mention-redos branch from fa7df54 to 340105b Compare July 12, 2026 00:08
@clawsweeper clawsweeper Bot added rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. and removed rating: 🦐 gold shrimp Decent PR readiness signal, but merge confidence is limited. labels Jul 12, 2026
@steipete steipete self-assigned this Jul 13, 2026
@steipete
steipete force-pushed the fix/qqbot-mention-redos branch from 340105b to dfc7157 Compare July 13, 2026 00:50
@clawsweeper

clawsweeper Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper status: review started.

I am starting a fresh review of this pull request: fix(qqbot): guard mentionPatterns against ReDoS via compileSafeRegex This is item 1/1 in the current shard. Shard 0/1.

This placeholder means the worker is alive and reading the current context. I will edit this same comment with the actual review when the claws are done clicking.

Crustacean status: shell secured, claws on keyboard, evidence pebbles being sorted.

@steipete
steipete force-pushed the fix/qqbot-mention-redos branch from dfc7157 to 03bafe6 Compare July 13, 2026 00:58
@steipete
steipete force-pushed the fix/qqbot-mention-redos branch from 03bafe6 to 5a5de04 Compare July 13, 2026 00:59
@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. and removed proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. labels Jul 13, 2026
@steipete
steipete merged commit c93f877 into openclaw:main Jul 13, 2026
198 of 207 checks passed
@steipete

Copy link
Copy Markdown
Contributor

Merged via squash.

github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request Jul 13, 2026
RomneyDa pushed a commit that referenced this pull request Aug 30, 2026
Co-authored-by: Peter Steinberger <[email protected]>
(cherry picked from commit c93f877)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

channel: qqbot merge-risk: 🚨 compatibility 🚨 May break existing users, config, migrations, defaults, or upgrade paths. merge-risk: 🚨 message-delivery 🚨 May drop, duplicate, misroute, suppress, or wrongly target messages. P2 Normal backlog priority with limited blast radius. rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. size: S status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants