[GH-ISSUE #2364] [Issue]: Regex scripts run content-supplied findRegex with no ReDoS guard — only the ST-card import path is protected #1164

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

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

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: Medium — injection / ReDoS.

Regex scripts compile and run a content-supplied findRegex against the full transcript with no ReDoS guard — neither on the storage-validation schema nor on any of the three runtime executors. The isPatternSafe() guard exists in the repo and is enforced on the ST character-card import path and the lorebook keyword scanner, but the regex-script path skips it entirely. A catastrophically-backtracking pattern that enters storage (via a profile/preset bundle import, or pasted into the editor) freezes the single-threaded renderer on every generation turn.

Expected behavior

Every regex-script ingestion point should reject unsafe patterns at the storage boundary, and every runtime executor should refuse to run an unsafe pattern (or run it under the interruptible-worker timeout the lorebook path already uses).

Actual behavior

createRegexScriptSchema validates findRegex only as a non-empty string — no safety refinement (src/engine/contracts/schemas/regex.schema.ts:12):

export const createRegexScriptSchema = z.object({
  characterId: z.string().nullable().default(null),
  name: z.string().min(1).max(200),
  enabled: z.boolean().default(true),
  findRegex: z.string().min(1),   // <-- no isPatternSafe; only enforced in regex-script-import.ts:82
  replaceString: z.string().default(""),
  ...
});

All three runtime executors then compile and run the stored pattern with only a syntax-error try/catch — no isPatternSafe, no length cap, no timeout:

// src/engine/generation-core/regex/regex-application.ts:92
const re = new RegExp(findRegex, flags);   // run via String.replace over the full transcript

// siblings: src/engine/generation/regex-runtime.ts:39, src/features/catalog/agents/regex-application.ts:67

prompt-assembly.ts:3241-3242 loads all regex-scripts from storage and runs applyRegexScriptsToPromptMessages on every generation turn, so a stored (a+)+$-class pattern run over attacker-influenced AI output / user input causes catastrophic backtracking and freezes the JS event loop. The lorebook keyword path is hardened via vmRegexExecutor (src/engine/generation-core/lorebooks/regex-timeout.ts:142-150: 50 ms worker deadline + isPatternSafe + MAX_REGEX_SCAN_TEXT_LENGTH), but that fix is scoped only to the keyword scanner — the script executors are unprotected siblings.

Note on reachability: the ST character-card import path is already guarded (src/shared/lib/regex-script-import.ts:82 calls isPatternSafe and skips unsafe patterns before storing), so a malicious character card cannot plant an unsafe script. The genuinely unguarded ingestion paths are the full-profile / preset bundle import (ProfileImportSection.tsx → Rust commands/storage/profile/legacy.rs:49 maps regex_scripts → regex-scripts and writes records verbatim; no is_pattern_safe anywhere in src-tauri) and the in-app editor (RegexScriptEditor.tsx, which only does a new RegExp syntax check).

Steps to reproduce

  1. Craft a profile/preset bundle (.marinara envelope) whose regex-scripts collection contains a script with findRegex: "(a+)+$" (syntactically valid, catastrophically backtracking).
  2. Import the bundle via the profile import flow.
  3. Open any chat and send a turn whose transcript contains a long run of a characters (model output or user input).
  4. Observe the renderer freeze as the script runs unguarded during prompt assembly.

Impact area

  • Chat mode
  • Runtime systems: generation, world-state, visuals, tracker, or haptics
  • React-free engine behavior, contracts, agents, or capability ports
  • Tauri/Rust storage, assets, LLM, integrations, security, imports, or HTTP runtime

Environment

  • Branch or commit: refactor @ 330f69a8
  • Run mode: Tauri desktop
  • OS + version: not relevant (static source audit)
  • Node + pnpm versions: not relevant
  • Rust version, if relevant: not relevant
  • Remote Runtime URL configured? not relevant
  • Provider, model, or integration, if relevant: not relevant

Logs, screenshots, or video

Static source audit. Cited: regex.schema.ts:12 (no refinement); run-path executors generation-core/regex/regex-application.ts:92, generation/regex-runtime.ts:39, features/catalog/agents/regex-application.ts:67; the guarded reference lorebooks/regex-timeout.ts:142-150 + regex-script-import.ts:82.

Regression notes

Found via static audit; not a regression. The web build does the identical unguarded thing — packages/shared/src/schemas/regex.schema.ts has the same findRegex: z.string().min(1), and packages/server/.../regex-application.ts uses bare new RegExp with no isPatternSafe/timeout. The regex-script path was never protected in main; the refactor inherited the gap rather than introducing it. (Separately, #2199 — now fixed — hardened only the lorebook keyword scanner.)

Additional context

Single-chokepoint fix: add a pattern-safety refinement to createRegexScriptSchema / updateRegexScriptSchema (e.g. .refine((v) => isPatternSafe(v.findRegex), 'pattern fails ReDoS safety check')) so unsafe patterns are rejected wherever a script is stored, and add the equivalent check on the Rust profile-import path. Then route the three runtime executors through the same guard the lorebook path uses (isPatternSafe + MAX_REGEX_SCAN_TEXT_LENGTH, ideally vmRegexExecutor). The reference implementation already exists at regex-script-import.ts:82 and lorebooks/regex-timeout.ts.

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 5, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2364 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: Medium — injection / ReDoS.** Regex scripts compile and run a content-supplied `findRegex` against the full transcript with no ReDoS guard — neither on the storage-validation schema nor on any of the three runtime executors. The `isPatternSafe()` guard exists in the repo and is enforced on the ST character-card import path and the lorebook keyword scanner, but the regex-**script** path skips it entirely. A catastrophically-backtracking pattern that enters storage (via a profile/preset bundle import, or pasted into the editor) freezes the single-threaded renderer on every generation turn. ## Expected behavior Every regex-script ingestion point should reject unsafe patterns at the storage boundary, and every runtime executor should refuse to run an unsafe pattern (or run it under the interruptible-worker timeout the lorebook path already uses). ## Actual behavior `createRegexScriptSchema` validates `findRegex` only as a non-empty string — no safety refinement (`src/engine/contracts/schemas/regex.schema.ts:12`): ```ts export const createRegexScriptSchema = z.object({ characterId: z.string().nullable().default(null), name: z.string().min(1).max(200), enabled: z.boolean().default(true), findRegex: z.string().min(1), // <-- no isPatternSafe; only enforced in regex-script-import.ts:82 replaceString: z.string().default(""), ... }); ``` All three runtime executors then compile and run the stored pattern with only a syntax-error `try/catch` — no `isPatternSafe`, no length cap, no timeout: ```ts // src/engine/generation-core/regex/regex-application.ts:92 const re = new RegExp(findRegex, flags); // run via String.replace over the full transcript // siblings: src/engine/generation/regex-runtime.ts:39, src/features/catalog/agents/regex-application.ts:67 ``` `prompt-assembly.ts:3241-3242` loads all `regex-scripts` from storage and runs `applyRegexScriptsToPromptMessages` on **every** generation turn, so a stored `(a+)+$`-class pattern run over attacker-influenced AI output / user input causes catastrophic backtracking and freezes the JS event loop. The lorebook keyword path is hardened via `vmRegexExecutor` (`src/engine/generation-core/lorebooks/regex-timeout.ts:142-150`: 50 ms worker deadline + `isPatternSafe` + `MAX_REGEX_SCAN_TEXT_LENGTH`), but that fix is scoped only to the keyword scanner — the script executors are unprotected siblings. Note on reachability: the ST character-card import path **is** already guarded (`src/shared/lib/regex-script-import.ts:82` calls `isPatternSafe` and skips unsafe patterns before storing), so a malicious character card cannot plant an unsafe script. The genuinely unguarded ingestion paths are the **full-profile / preset bundle import** (`ProfileImportSection.tsx` → Rust `commands/storage/profile/legacy.rs:49` maps `regex_scripts` → `regex-scripts` and writes records verbatim; no `is_pattern_safe` anywhere in `src-tauri`) and the in-app editor (`RegexScriptEditor.tsx`, which only does a `new RegExp` syntax check). ## Steps to reproduce 1. Craft a profile/preset bundle (`.marinara` envelope) whose `regex-scripts` collection contains a script with `findRegex: "(a+)+$"` (syntactically valid, catastrophically backtracking). 2. Import the bundle via the profile import flow. 3. Open any chat and send a turn whose transcript contains a long run of `a` characters (model output or user input). 4. Observe the renderer freeze as the script runs unguarded during prompt assembly. ## Impact area - [x] Chat mode - [x] Runtime systems: generation, world-state, visuals, tracker, or haptics - [x] React-free engine behavior, contracts, agents, or capability ports - [x] Tauri/Rust storage, assets, LLM, integrations, security, imports, or HTTP runtime ## Environment - Branch or commit: refactor @ 330f69a8 - Run mode: Tauri desktop - OS + version: not relevant (static source audit) - Node + pnpm versions: not relevant - Rust version, if relevant: not relevant - Remote Runtime URL configured? not relevant - Provider, model, or integration, if relevant: not relevant ## Logs, screenshots, or video Static source audit. Cited: `regex.schema.ts:12` (no refinement); run-path executors `generation-core/regex/regex-application.ts:92`, `generation/regex-runtime.ts:39`, `features/catalog/agents/regex-application.ts:67`; the guarded reference `lorebooks/regex-timeout.ts:142-150` + `regex-script-import.ts:82`. ## Regression notes Found via static audit; **not a regression**. The web build does the identical unguarded thing — `packages/shared/src/schemas/regex.schema.ts` has the same `findRegex: z.string().min(1)`, and `packages/server/.../regex-application.ts` uses bare `new RegExp` with no `isPatternSafe`/timeout. The regex-script path was never protected in main; the refactor inherited the gap rather than introducing it. (Separately, #2199 — now fixed — hardened only the lorebook keyword scanner.) ## Additional context Single-chokepoint fix: add a pattern-safety refinement to `createRegexScriptSchema` / `updateRegexScriptSchema` (e.g. `.refine((v) => isPatternSafe(v.findRegex), 'pattern fails ReDoS safety check')`) so unsafe patterns are rejected wherever a script is stored, and add the equivalent check on the Rust profile-import path. Then route the three runtime executors through the same guard the lorebook path uses (`isPatternSafe` + `MAX_REGEX_SCAN_TEXT_LENGTH`, ideally `vmRegexExecutor`). The reference implementation already exists at `regex-script-import.ts:82` and `lorebooks/regex-timeout.ts`. ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:28:12 +07:00
Author
Owner

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

Same content-driven renderer-freeze class as #2363 (unbounded macro expansion / {{roll:NdM}} loop): both are imported-content DoS vectors during prompt assembly that the engine has no resource budget for. Worth addressing together when adding bounds to the content-processing paths.

<!-- gh-comment-id:4635405859 --> @Romuromylus commented on GitHub (Jun 5, 2026): Same content-driven renderer-freeze class as #2363 (unbounded macro expansion / `{{roll:NdM}}` loop): both are imported-content DoS vectors during prompt assembly that the engine has no resource budget for. Worth addressing together when adding bounds to the content-processing paths.
Author
Owner

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

Fixed by #2422, merged into refactor.

<!-- gh-comment-id:4637949013 --> @Romuromylus commented on GitHub (Jun 6, 2026): Fixed by #2422, merged into `refactor`.
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#1164
No description provided.