mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #3086] [Issue]: A regex script can freeze a whole generation turn — isPatternSafe misses polynomial ReDoS and the .replace path has no timeout #1647
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#1647
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 30, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3086
Originally assigned to: @SpicyMarinara on GitHub.
What's wrong
The regex-script safety gate (
isPatternSafe) only rejects exponential backtracking (it checks star height — nested/quantified groups like(a+)+,(.+)+). It accepts polynomial-backtracking patterns built from adjacent independently-quantified atoms with no quantified group — e.g..*.*.*x,.*foo.*bar.*baz,.*\*[^*]+\*.*\*[^*]+\*.*. These compile and run through a plainString.prototype.replacewith no execution timeout, over every message of the chat history on every generation turn. A single short, innocuous-looking regex script can freeze the entire turn for tens of seconds.The VM-based regex timeout (
regex-timeout.ts) exists but is wired only into lorebook keyword scanning's.testpath — the regex-script.replacepath used for prompt/display scripts is completely unguarded.How to reproduce (try it yourself)
.*.*.*Q(or a realistic equivalent like.*foo.*bar.*baz), Replace = empty.Q).You'll see: the turn hangs for tens of seconds (the UI/generation freezes) while
String.replacebacktracks. The pattern was not rejected byisPatternSafe, so it isn't skipped.Measured against the real compiled code (
applyRegexReplacement(input, new RegExp(".*.*.*Q","gi"), "")on uniform no-match input):.*.*.*Q— 300 chars → 0.55s, 600 → 8.4s, 800 → 26.9s.*.*.*.*x— 160 chars → 1.6s, 220 → 7.4s.*foo.*bar.*bazon ~19KB → did not finish in 180sisPatternSafereturnstruefor all of the above.Where it is
packages/server/src/services/lorebook/regex-application.ts:160-166— the only pre-flight isif (!isPatternSafe(findRegex)) continue;, thennew RegExp(findRegex, flags)andapplyRegexReplacement(...)with no timeout/VM wrapper.packages/shared/src/utils/regex-replacement.ts:234-257—applyRegexReplacementis a baretext.replace(regex, ...).packages/shared/src/utils/regex-safety.ts—isPatternSafebounds star height only; polynomial chains (multiple adjacent.*/\S*/\w*with no quantified group) pass.packages/server/src/services/lorebook/regex-timeout.ts:24-33—vmRegexExecutorrecompiles + runs.testunder a VM timeout; wired only atkeyword-scanner.ts:516,572. The.replacepath is not covered.packages/server/src/routes/generate.routes.ts:2064-2069runsapplyRegexScriptsToPromptMessages(mappedMessages, regexScripts, ...)at iteration 0;applyRegexScriptsToPromptMessages(regex-application.ts:178-198) loops every message. The polynomialnis per-message content length, so one long narration message is enough — message count isn't required.What's happening
Star-height analysis catches exponential ReDoS but misses polynomial ReDoS (
O(n^k)forkadjacent unbounded quantifiers). Because the.replaceapplication path has no execution-time guard (unlike the lorebook.testpath), a pattern that passesisPatternSaferuns to completion no matter how long it takes, blocking the event loop for the whole turn.Threat model note: this is mostly self-inflicted (a user authors their own scripts), which is why it's low priority for typical installs — but regex scripts ride along in imported content (SillyTavern imports, shared character cards/presets). A malicious or careless shared asset carrying such a pattern would freeze anyone who imports and uses it, so it's worth hardening.
Suggested fix
.replacepath with the same timeout the.testpath uses. Generalize the VM executor inregex-timeout.tsto cover replacement (run the.replaceinside the VM under a time budget, or pre-flight a.testunder timeout and skip the script on timeout), and apply it inregex-application.ts:160-166.isPatternSafeto also reject low-star-height polynomial backtracking — flag patterns with ≥2 adjacent unbounded quantifiers over overlapping character classes (e.g. multiple.*/\S*/\w*) with no intervening required literal. Star height alone missesO(n^k).historyLength × scriptCountexceeds a budget.Note: the two seeded default scripts (
seed-regex.ts— the HTML-tag stripper and\n{3,}) are not affected; this needs an authored/imported polynomial pattern to trigger.AI-investigated against v2.0.8 (
main); cited lines read and benchmarked against the compiledpackages/shared/dist. Timing measured on the realapplyRegexReplacement. Please confirm before fixing.@SpicyMarinara commented on GitHub (Jun 30, 2026):
Fixed in the v2.0.9 prep patch. The shared regex safety gate now rejects the reported polynomial shapes such as
.*.*.*Q,.*foo.*bar.*baz, and similar broad unbounded chains, and the server prompt-regex replacement path now uses a VM timeout guard for long replacement inputs before running the actual replacement.\n\nAdded prompt regression coverage for the reported patterns. Verified withpnpm regression:prompt,pnpm check, andpnpm version:check.