[GH-ISSUE #2620] [Issue]: Server prompt-context regex scripts run with no execution timeout (event-loop-blocking ReDoS) #1295

Closed
opened 2026-10-21 05:30:53 +07:00 by bsakaguchi · 1 comment
Owner

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

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: Low — performance / security (ReDoS). The server prompt-context regex engine compiles and runs promptOnly regex scripts against every prompt message on every /generate request with no execution timeout. A catastrophic-backtracking pattern stalls the Node event loop for the whole process, hanging generation for all users. The sibling untrusted-regex path (lorebook keyword scan) already defends against exactly this with a 50ms vm timeout + isPatternSafe; the prompt engine has neither.

Expected behavior

Prompt-context regex execution should be bounded (timeout) and/or statically pre-filtered (isPatternSafe), the same way the lorebook keyword scanner is, so a pathological pattern cannot block the event loop.

Actual behavior

// packages/server/src/services/regex/regex-application.ts (~113-117)
const re = new RegExp(findRegex, flags);
// applyRegexReplacement -> text.replace(re, ...) with NO timeout (shared/regex-replacement.ts:138)

applyRegexScriptsToPromptMessages runs on the per-request generation path (generate.routes.ts:1443, chats.routes.ts:1740, dry-run-route.ts:849). The sibling lorebook path runs regex inside a vm with DEFAULT_REGEX_TIMEOUT_MS = 50 (services/lorebook/regex-timeout.ts) and statically pre-filters via isPatternSafe (lorebook-keyword-matching.ts:40); the prompt engine applies neither. The POST/PATCH /regex-scripts route validates only via createRegexScriptSchema (findRegex is z.string().min(1)) and never calls isPatternSafe, so a hostile/buggy pattern is stored verbatim and recompiled+run every turn. Empirically, /(a|a)+$/ against 'a'.repeat(30)+'!' ran ~98s in Node. Note: the ST card importer forces promptOnly:false (st-character.importer.ts:148) and the prompt engine only runs promptOnly===true scripts, so this is a self/co-tenant DoS reachable by an authenticated operator, not a remote card-injection vector.

Steps to reproduce

  1. Create a regex script with promptOnly=true and a catastrophic pattern, e.g. findRegex (a|a)+$ (via the authenticated POST /regex-scripts).
  2. Trigger a /generate so the script is compiled and applied to each prompt message.
  3. Observe the event loop block for all concurrent requests until V8 finishes.

Environment

  • Marinara Engine version: staging @ 56f700cd (v2.0.0)
  • Install type: source
  • OS + version: n/a — static source audit
  • Browser or app shell: n/a

Logs, screenshots, or video

Static source audit, cited to file:line above. Empirically: (a|a)+$ on a 31-char input ran ~98s in Node.

Additional context

Fix sketch. Gate promptOnly regex compilation in applyRegexScriptsToPromptText behind isPatternSafe(findRegex) (skip / literal-fallback on failure), mirroring lorebook-keyword-matching.ts:40, and/or run the replace under the same vm-timeout posture as regex-timeout.ts so a pathological pattern aborts at ~50ms instead of stalling the loop. Also apply isPatternSafe in the POST/PATCH /regex-scripts route validation. Reference implementations: services/lorebook/regex-timeout.ts and lorebook-keyword-matching.ts.

Regression notes. Not a regression — main carries byte-equivalent prompt-regex code (same no-timeout new RegExp + applyRegexReplacement), the same unguarded route validation, and already has the lorebook regex-timeout.ts guard. The inconsistency is pre-existing on the release baseline. Distinct from #2526 (lorebook scan perf).

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 18, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2620 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: Low — performance / security (ReDoS).** The server prompt-context regex engine compiles and runs `promptOnly` regex scripts against every prompt message on every `/generate` request with no execution timeout. A catastrophic-backtracking pattern stalls the Node event loop for the whole process, hanging generation for all users. The sibling untrusted-regex path (lorebook keyword scan) already defends against exactly this with a 50ms vm timeout + `isPatternSafe`; the prompt engine has neither. ## Expected behavior Prompt-context regex execution should be bounded (timeout) and/or statically pre-filtered (`isPatternSafe`), the same way the lorebook keyword scanner is, so a pathological pattern cannot block the event loop. ## Actual behavior ```ts // packages/server/src/services/regex/regex-application.ts (~113-117) const re = new RegExp(findRegex, flags); // applyRegexReplacement -> text.replace(re, ...) with NO timeout (shared/regex-replacement.ts:138) ``` `applyRegexScriptsToPromptMessages` runs on the per-request generation path (`generate.routes.ts:1443`, `chats.routes.ts:1740`, `dry-run-route.ts:849`). The sibling lorebook path runs regex inside a vm with `DEFAULT_REGEX_TIMEOUT_MS = 50` (`services/lorebook/regex-timeout.ts`) **and** statically pre-filters via `isPatternSafe` (`lorebook-keyword-matching.ts:40`); the prompt engine applies neither. The `POST/PATCH /regex-scripts` route validates only via `createRegexScriptSchema` (`findRegex` is `z.string().min(1)`) and never calls `isPatternSafe`, so a hostile/buggy pattern is stored verbatim and recompiled+run every turn. Empirically, `/(a|a)+$/` against `'a'.repeat(30)+'!'` ran ~98s in Node. Note: the ST card importer forces `promptOnly:false` (`st-character.importer.ts:148`) and the prompt engine only runs `promptOnly===true` scripts, so this is a self/co-tenant DoS reachable by an authenticated operator, not a remote card-injection vector. ## Steps to reproduce 1. Create a regex script with `promptOnly=true` and a catastrophic pattern, e.g. `findRegex` `(a|a)+$` (via the authenticated `POST /regex-scripts`). 2. Trigger a `/generate` so the script is compiled and applied to each prompt message. 3. Observe the event loop block for all concurrent requests until V8 finishes. ## Environment - Marinara Engine version: `staging @ 56f700cd` (v2.0.0) - Install type: source - OS + version: n/a — static source audit - Browser or app shell: n/a ## Logs, screenshots, or video Static source audit, cited to file:line above. Empirically: `(a|a)+$` on a 31-char input ran ~98s in Node. ## Additional context **Fix sketch.** Gate `promptOnly` regex compilation in `applyRegexScriptsToPromptText` behind `isPatternSafe(findRegex)` (skip / literal-fallback on failure), mirroring `lorebook-keyword-matching.ts:40`, and/or run the replace under the same vm-timeout posture as `regex-timeout.ts` so a pathological pattern aborts at ~50ms instead of stalling the loop. Also apply `isPatternSafe` in the `POST/PATCH /regex-scripts` route validation. Reference implementations: `services/lorebook/regex-timeout.ts` and `lorebook-keyword-matching.ts`. **Regression notes.** Not a regression — `main` carries byte-equivalent prompt-regex code (same no-timeout `new RegExp` + `applyRegexReplacement`), the same unguarded route validation, and already has the lorebook `regex-timeout.ts` guard. The inconsistency is pre-existing on the release baseline. Distinct from #2526 (lorebook scan perf). ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:30:53 +07:00
  • closed this issue
  • added the
    server
    bug
    labels
Author
Owner

@Romuromylus commented on GitHub (Jun 18, 2026):

Fixed in #2623, merged to staging (e1132e5b). Closing manually since the PR targeted staging rather than the default branch.

<!-- gh-comment-id:4742297322 --> @Romuromylus commented on GitHub (Jun 18, 2026): Fixed in #2623, merged to `staging` (e1132e5b). Closing manually since the PR targeted `staging` rather than the default branch.
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#1295
No description provided.