[GH-ISSUE #2933] [Issue]: A bad regex script can freeze the whole chat tab while messages render #1510

Closed
opened 2026-10-21 05:37:38 +07:00 by bsakaguchi · 2 comments
Owner

Originally created by @kolacheee on GitHub (Jun 28, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2933

Originally assigned to: @SpicyMarinara on GitHub.

Bug 1 (HIGH) — A regex script with a "trap" pattern can freeze the entire chat tab

What's wrong
Regex scripts run twice: once on the server (when building the prompt) and once in the browser (when displaying a message). The server checks every pattern against a safety filter before running it, but the browser display path does not check at all. So a regex that the safety filter happens to miss will compile and run with no protection while a message is rendering — and because it runs on the main thread with no timeout, it can lock up the whole tab for a minute or more. The user just sees a frozen, unresponsive page.

This is reachable two ways: (1) a user creates the script themselves, or (2) the pattern arrives inside a shared SillyTavern character card and gets imported.

How to reproduce (try it yourself)

  1. Create a regex script: Find = (a|a)*c, Flags = g, Placement = AI Output, Prompt Only = OFF (so it's a display script). It saves without complaint — the safety filter accepts this shape.
  2. Get an assistant message whose text contains a run of about 30 a characters (e.g. aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa). Streaming one in works too.
  3. When that message renders, the tab's main thread blocks.

You'll see: the tab hangs / becomes unresponsive for a long time (with ~26 as the regex already takes ~7 seconds; it roughly doubles per extra a, so ~30 as is well over a minute). Measured locally: new RegExp('(a|a)*c','g').test('a'.repeat(26)) took ~6.8s.
Expected: a slow or malicious pattern should be skipped or aborted, the way the server already does it.

Where it is
packages/client/src/hooks/use-apply-regex.ts, function applyScripts, lines 103-117. It does new RegExp(findRegex, script.flags) and runs applyRegexReplacement inside a bare try/catch — no safety check, no timeout, no input-length cap. Compare the server path packages/server/src/services/regex/regex-application.ts:114, which guards with if (!isPatternSafe(findRegex)) continue; before compiling. A search for isPatternSafe across packages/client returns zero hits.

Called from ChatMessage.tsx:1454 and GameNarration.tsx:1394, both inside a useMemo keyed on the message content.

What's happening
The safety heuristic (packages/shared/src/utils/regex-safety.ts) only flags nested quantifiers like (a+)+ (star height 2). (a|a)*c has star height 1 — the catastrophic part is the overlapping alternation (a|a), which the walker doesn't detect — so isPatternSafe('(a|a)*c') returns true. That means it passes the create-script schema (regex.schema.ts:19) and survives the ST card importer gate (st-character.importer.ts:136). The actual replace is text.replace(...) (regex-replacement.ts:138), which is synchronous, so the surrounding try/catch can never interrupt the backtracking once it starts.

Suggested fix
Gate the client display path the same way the server does: call isPatternSafe(findRegex) before new RegExp(...) in applyScripts and skip (continue) if it fails. Better still, also improve the heuristic to catch single-star-height alternation overlap like (a|a)*. Consider a length cap on the input text and/or moving display-time regex off the main thread for very long messages.

Note: the issue's original secondary pattern (a+)+$ is not reproducible in-app — isPatternSafe('(a+)+$') correctly returns false, so both the route schema and the importer reject it. Only the (a|a)*c star-height-1 shape is a valid in-app repro. (One related bypass to keep in mind: the schema strips {{macro}} braces before the safety check at regex.schema.ts:19, and macros are expanded at apply time, so a macro that expands into a catastrophic pattern would also slip past the gate — but the (a|a)*c repro above stands on its own.)


Bug 2 (LOW) — applyPromptOnly is dead code

What's wrong
The useApplyRegex hook exports a function applyPromptOnly that nothing ever calls. It's harmless today but it's misleading surface area: it runs regex in "prompt" mode entirely client-side, which would bypass the server's safety gate if anyone ever wired it up.

How to reproduce
Static check: search packages/client for applyPromptOnly. You get exactly two hits — the definition (use-apply-regex.ts:161) and the return statement (:175). No component consumes it, so it has no runtime effect.

Suggested fix
Remove applyPromptOnly (and stop exporting it) unless there's a planned consumer. If it's kept, ensure it routes through the same safety gate Bug 1 adds.


AI-investigated against v2.0.6; verify the reproduction before fixing.

(Dropped as not-reproducible: the original issue also flagged that parseScript lacks an Array.isArray guard for trimStrings/placement (use-apply-regex.ts:16-29). Confirmed latent only — every write path coerces trimStrings to a JSON array via the Zod schema (regex.schema.ts:23) and storage (regex-scripts.storage.ts), so the throw is unreachable except by directly editing the DB. Not user-reproducible.)

Originally created by @kolacheee on GitHub (Jun 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2933 Originally assigned to: @SpicyMarinara on GitHub. ## Bug 1 (HIGH) — A regex script with a "trap" pattern can freeze the entire chat tab **What's wrong** Regex scripts run twice: once on the server (when building the prompt) and once in the browser (when displaying a message). The server checks every pattern against a safety filter before running it, but the **browser display path does not check at all**. So a regex that the safety filter happens to miss will compile and run with no protection while a message is rendering — and because it runs on the main thread with no timeout, it can lock up the whole tab for a minute or more. The user just sees a frozen, unresponsive page. This is reachable two ways: (1) a user creates the script themselves, or (2) the pattern arrives inside a shared SillyTavern character card and gets imported. **How to reproduce (try it yourself)** 1. Create a regex script: **Find** = `(a|a)*c`, **Flags** = `g`, **Placement** = AI Output, **Prompt Only** = OFF (so it's a display script). It saves without complaint — the safety filter accepts this shape. 2. Get an assistant message whose text contains a run of about 30 `a` characters (e.g. `aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`). Streaming one in works too. 3. When that message renders, the tab's main thread blocks. You'll see: the tab hangs / becomes unresponsive for a long time (with ~26 `a`s the regex already takes ~7 seconds; it roughly doubles per extra `a`, so ~30 `a`s is well over a minute). Measured locally: `new RegExp('(a|a)*c','g').test('a'.repeat(26))` took ~6.8s. Expected: a slow or malicious pattern should be skipped or aborted, the way the server already does it. **Where it is** `packages/client/src/hooks/use-apply-regex.ts`, function `applyScripts`, lines 103-117. It does `new RegExp(findRegex, script.flags)` and runs `applyRegexReplacement` inside a bare `try/catch` — no safety check, no timeout, no input-length cap. Compare the server path `packages/server/src/services/regex/regex-application.ts:114`, which guards with `if (!isPatternSafe(findRegex)) continue;` before compiling. A search for `isPatternSafe` across `packages/client` returns zero hits. Called from `ChatMessage.tsx:1454` and `GameNarration.tsx:1394`, both inside a `useMemo` keyed on the message content. **What's happening** The safety heuristic (`packages/shared/src/utils/regex-safety.ts`) only flags *nested* quantifiers like `(a+)+` (star height 2). `(a|a)*c` has star height 1 — the catastrophic part is the overlapping alternation `(a|a)`, which the walker doesn't detect — so `isPatternSafe('(a|a)*c')` returns true. That means it passes the create-script schema (`regex.schema.ts:19`) and survives the ST card importer gate (`st-character.importer.ts:136`). The actual replace is `text.replace(...)` (`regex-replacement.ts:138`), which is synchronous, so the surrounding `try/catch` can never interrupt the backtracking once it starts. **Suggested fix** Gate the client display path the same way the server does: call `isPatternSafe(findRegex)` before `new RegExp(...)` in `applyScripts` and skip (continue) if it fails. Better still, also improve the heuristic to catch single-star-height alternation overlap like `(a|a)*`. Consider a length cap on the input text and/or moving display-time regex off the main thread for very long messages. Note: the issue's original secondary pattern `(a+)+$` is **not** reproducible in-app — `isPatternSafe('(a+)+$')` correctly returns false, so both the route schema and the importer reject it. Only the `(a|a)*c` star-height-1 shape is a valid in-app repro. (One related bypass to keep in mind: the schema strips `{{macro}}` braces before the safety check at `regex.schema.ts:19`, and macros are expanded at apply time, so a macro that expands into a catastrophic pattern would also slip past the gate — but the `(a|a)*c` repro above stands on its own.) --- ## Bug 2 (LOW) — `applyPromptOnly` is dead code **What's wrong** The `useApplyRegex` hook exports a function `applyPromptOnly` that nothing ever calls. It's harmless today but it's misleading surface area: it runs regex in "prompt" mode entirely client-side, which would bypass the server's safety gate if anyone ever wired it up. **How to reproduce** Static check: search `packages/client` for `applyPromptOnly`. You get exactly two hits — the definition (`use-apply-regex.ts:161`) and the `return` statement (`:175`). No component consumes it, so it has no runtime effect. **Suggested fix** Remove `applyPromptOnly` (and stop exporting it) unless there's a planned consumer. If it's kept, ensure it routes through the same safety gate Bug 1 adds. --- AI-investigated against v2.0.6; verify the reproduction before fixing. (Dropped as not-reproducible: the original issue also flagged that `parseScript` lacks an `Array.isArray` guard for `trimStrings`/`placement` (`use-apply-regex.ts:16-29`). Confirmed latent only — every write path coerces `trimStrings` to a JSON array via the Zod schema (`regex.schema.ts:23`) and storage (`regex-scripts.storage.ts`), so the throw is unreachable except by directly editing the DB. Not user-reproducible.)
Author
Owner

@SpicyMarinara commented on GitHub (Jun 28, 2026):

Addressed in the staging fixes: the display-side regex path now parses placements/trim strings defensively, skips unsafe resolved patterns with the shared isPatternSafe gate before RegExp compilation, and removes the unused client applyPromptOnly surface. Broader runtime timeout / worker hardening remains tracked separately in #2930.

<!-- gh-comment-id:4827507684 --> @SpicyMarinara commented on GitHub (Jun 28, 2026): Addressed in the staging fixes: the display-side regex path now parses placements/trim strings defensively, skips unsafe resolved patterns with the shared isPatternSafe gate before RegExp compilation, and removes the unused client applyPromptOnly surface. Broader runtime timeout / worker hardening remains tracked separately in #2930.
Author
Owner

@kolacheee commented on GitHub (Jun 28, 2026):

Re-verified this against v2.0.6 (read the code + reproduced each part) and rewrote the description in plain language with concrete, try-it-yourself reproduction steps. Any original claims I could not actually reproduce were dropped (and called out at the end where relevant). Should be much easier to follow and confirm now — sorry the first version was so dense.

<!-- gh-comment-id:4827556790 --> @kolacheee commented on GitHub (Jun 28, 2026): Re-verified this against v2.0.6 (read the code + reproduced each part) and rewrote the description in plain language with concrete, try-it-yourself reproduction steps. Any original claims I could not actually reproduce were dropped (and called out at the end where relevant). Should be much easier to follow and confirm now — sorry the first version was so dense.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#1510
No description provided.