mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2933] [Issue]: A bad regex script can freeze the whole chat tab while messages render #1510
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#1510
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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)
(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.acharacters (e.g.aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa). Streaming one in works too.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 extraa, so ~30as 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, functionapplyScripts, lines 103-117. It doesnew RegExp(findRegex, script.flags)and runsapplyRegexReplacementinside a baretry/catch— no safety check, no timeout, no input-length cap. Compare the server pathpackages/server/src/services/regex/regex-application.ts:114, which guards withif (!isPatternSafe(findRegex)) continue;before compiling. A search forisPatternSafeacrosspackages/clientreturns zero hits.Called from
ChatMessage.tsx:1454andGameNarration.tsx:1394, both inside auseMemokeyed 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)*chas star height 1 — the catastrophic part is the overlapping alternation(a|a), which the walker doesn't detect — soisPatternSafe('(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 istext.replace(...)(regex-replacement.ts:138), which is synchronous, so the surroundingtry/catchcan never interrupt the backtracking once it starts.Suggested fix
Gate the client display path the same way the server does: call
isPatternSafe(findRegex)beforenew RegExp(...)inapplyScriptsand 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)*cstar-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 atregex.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)*crepro above stands on its own.)Bug 2 (LOW) —
applyPromptOnlyis dead codeWhat's wrong
The
useApplyRegexhook exports a functionapplyPromptOnlythat 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/clientforapplyPromptOnly. You get exactly two hits — the definition (use-apply-regex.ts:161) and thereturnstatement (: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
parseScriptlacks anArray.isArrayguard fortrimStrings/placement(use-apply-regex.ts:16-29). Confirmed latent only — every write path coercestrimStringsto 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.)@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.
@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.