[GH-ISSUE #2936] [Issue]: Regex Script editor: Live Test preview lies, unsafe patterns slip past the editor, and import stops on the first bad script #1512

Closed
opened 2026-10-21 05:37:44 +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/2936

Originally assigned to: @SpicyMarinara on GitHub.

The regex-script editor's Live Test preview doesn't match what the chat actually does, and several validation gaps let bad scripts get saved or imported with confusing or silent failures. Each item below is independently reproducible in v2.0.6.


A. Live Test preview ignores most of the script's settings

What's wrong: The "Live Test" box shows the transform happening even when, at runtime, the script would never touch that text — because Live Test ignores Apply To (placement), Prompt Only, Specific Characters, and the Depth Range.

How to reproduce (try it yourself):

  1. Open a regex script. Set Find Pattern hello, Replace With goodbye.
  2. Set Apply To = User Input only (uncheck AI Output). Or set Depth Range Min = 3.
  3. In Live Test, type hello world.
  • You'll see: Result box shows goodbye world.
  • Expected: an indication that this script won't fire here (it only runs on user messages / only at depth ≥ 3), so the preview is misleading.

Where it is: RegexScriptEditor.tsx — the testResult memo (around lines 242–260). Its dependency list is [testInput, localFindRegex, localReplaceString, localFlags, localTrimStrings, regexError] — it never reads placement, promptOnly, target characters, or depth.

What's happening: The preview only ever runs find/replace + trim. The real engines gate on all the other fields (regex-application.ts:94-108, use-apply-regex.ts:69-101), so the preview can show a result a real run would skip.

Suggested fix: Have Live Test reflect the gating fields — at minimum show a "won't apply here" notice when the current placement/depth/scope would exclude the sample, or add a placement/depth selector to the test panel.


B. Editor lets you save (and live-test) patterns the server rejects as unsafe

What's wrong: If you type a ReDoS-prone pattern, the editor shows no error and the Save button stays enabled — but clicking Save returns a 400. Worse, the Live Test runs that same pattern synchronously, so a pathological pattern can hang the tab.

How to reproduce (try it yourself):

  1. Set Find Pattern to (a+)+$.
  • You'll see: no inline error under the field, Save button enabled. Click Save → request fails (400).
  1. Now paste a long string of as ending in X (e.g. aaaaaaaaaaaaaaaaaaaaaaaaaaX) into Live Test with that pattern.
  • You'll see: the editor tab freezes (catastrophic backtracking, no timeout).

Where it is: RegexScriptEditor.tsx — the regexError memo (~229–239) is a syntax-only new RegExp(...) check; Save is disabled only on regexError || characterScopeError (line 439). The editor never imports or calls isPatternSafe. Live Test compiles + runs the pattern inline in the testResult memo (~249–250).

What's happening: (a+)+$ is syntactically valid, so regexError is null and Save is enabled. The server schema rejects it via isPatternSafe (regex.schema.ts:15-21; regex-safety.ts maxStarHeight=1, so nested-quantifier group height 2 fails). The editor has no equivalent client-side guard.

Suggested fix: Run isPatternSafe (already in @marinara-engine/shared) in the editor: surface it as the same inline error as a syntax error, disable Save when unsafe, and skip running unsafe patterns in Live Test.


C. Save failures show a generic "Save failed" / "Validation Error" instead of the real reason

What's wrong: When a save is rejected, the red banner just says "Validation Error" — it doesn't tell the user which field failed or why (e.g. that the pattern is unsafe).

How to reproduce (try it yourself):

  1. Save a script with Find Pattern (a+)+$ (see B).
  • You'll see: red banner reads only "Validation Error".
  • Expected: something like "findRegex: Regex pattern is unsafe: it may cause catastrophic backtracking…".

Where it is: RegexScriptEditor.tsx:307 sets saveError = err.message. api-client.ts:141 constructs ApiError(status, body.error ?? statusText, body), so message is "Validation Error" (error-handler.ts:10-16). The actionable per-field text lives in error.payload.details[].message, which the editor never reads.

What's happening: The server returns { error: "Validation Error", details: [{ path, message }] }. The client surfaces only the top-level error string.

Suggested fix: When the caught error is an ApiError with payload.details, join the path: message entries into the banner text.


D. A stored unsafe script becomes uneditable — every save 400s, even renaming it

What's wrong: If a script with an unsafe pattern already exists in storage, you can't edit it at all through the editor: any Save (even just changing the name) is rejected, because the editor always resubmits the unsafe findRegex.

How to reproduce (try it yourself): This needs an unsafe pattern already in the DB (e.g. via a .maridb restore or raw DB insert — it can't be created through the editor because of B). Then open that script, change only its Name, and Save → 400.

Where it is: RegexScriptEditor.tsx:278-291 always includes findRegex in the PATCH payload. The route parses updateRegexScriptSchema (regex-scripts.routes.ts:32), which is createRegexScriptSchema.partial() (regex.schema.ts:33). .partial() only makes fields optional — it keeps the .refine on findRegex whenever it's present, so the unsafe pattern is re-validated and rejected on every save.

What's happening: The row-level enable toggle escapes this because it sends only { id, enabled } (CharacterRegexSection.tsx:318) — confirming it's the editor's blanket resubmission of findRegex that triggers the rejection.

Suggested fix: Tie this to fix B (block creating unsafe patterns up front) and/or only send changed fields on update so an untouched findRegex isn't re-validated, or surface the validation reason (fix C) so the user knows the pattern must change.


E. Importing a batch of regex scripts stops at the first rejected one

What's wrong: When you import a file with several regex scripts and one of them is rejected by the server, the import aborts there: scripts before it are created, the failing one and everything after it are not, and you get only a generic error.

How to reproduce (try it yourself):

  1. Make a JSON file with 3 regex scripts where script #2 has findRegex: "(.*)+" (unsafe).
  2. Import it (character card Advanced → regex import, or the Presets panel regex import).
  • You'll see: script #1 created, import stops at #2 with a generic error, script #3 never imported.
  • Expected: the bad one is reported/skipped and the rest still import.

Where it is: CharacterRegexSection.tsx:193-205 and PresetsPanel.tsx:407-418 both loop await createRegex.mutateAsync(...) inside a single try/catch. A thrown rejection breaks out of the loop and only sets a generic importError.

Suggested fix: Wrap each mutateAsync in its own try/catch, collect per-entry failures, continue the loop, and report "imported N, skipped M (reasons…)".


F. Live Test ignores the typographic-quotes setting that the chat applies

What's wrong: With the quote-format setting set to typographic, the Live Test preview still shows straight quotes while the actual rendered chat message shows curly ones. (No divergence at the default "straight" setting, so this is low-impact.)

How to reproduce (try it yourself):

  1. Settings → set quote format to typographic.
  2. In Live Test, input He said "hi".
  • You'll see: preview keeps "hi" straight; the rendered chat message shows curly quotes.

Where it is: The display path wraps its output in formatTextQuotes(..., quoteFormat) (use-apply-regex.ts:148,156); the editor's testResult memo does not and never imports formatTextQuotes. Default quoteFormat is "straight" (ui.store.ts:1193), so this only diverges once it's set to typographic.

Suggested fix: Apply formatTextQuotes(result, quoteFormat) to the Live Test output to match the display path.


G. Invalid regex flags (e.g. gg) save fine but silently make the whole script do nothing

What's wrong: The flags field isn't validated. A script with bad flags (like a duplicated gg) saves and imports without complaint, but at runtime it silently never matches anything — the regex constructor throws and the error is swallowed.

How to reproduce (try it yourself):

  1. Hand-edit a regex-script JSON to set "flags": "gg" and import it (or otherwise get gg into the flags).
  • You'll see: it saves/imports fine but never replaces anything in chat.
  • Expected: rejected at save/import, or flags normalized.

Where it is: regex.schema.ts:25 — flags: z.string().default("gi") with no constraint. Import copies entry.flags verbatim (CharacterRegexSection.tsx:81, and PresetsPanel via the same normalizeRegexImportEntry). At runtime new RegExp(findRegex, flags) throws "Invalid flags…" and is swallowed by the try/catch in regex-application.ts:116-127 and use-apply-regex.ts:103-117. The editor's flag buttons only offer g/i/m/s/u (line 570), so this only arrives via import/hand-edit.

Suggested fix: Constrain flags in the schema to /^[gimsuy]*$/ with no duplicates (Zod .refine), so invalid flags are rejected at save/import instead of silently no-opping the script.


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

Originally created by @kolacheee on GitHub (Jun 28, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2936 Originally assigned to: @SpicyMarinara on GitHub. The regex-script editor's Live Test preview doesn't match what the chat actually does, and several validation gaps let bad scripts get saved or imported with confusing or silent failures. Each item below is independently reproducible in v2.0.6. --- ### A. Live Test preview ignores most of the script's settings **What's wrong:** The "Live Test" box shows the transform happening even when, at runtime, the script would never touch that text — because Live Test ignores Apply To (placement), Prompt Only, Specific Characters, and the Depth Range. **How to reproduce (try it yourself):** 1. Open a regex script. Set Find Pattern `hello`, Replace With `goodbye`. 2. Set **Apply To = User Input only** (uncheck AI Output). Or set **Depth Range Min = 3**. 3. In Live Test, type `hello world`. - You'll see: Result box shows `goodbye world`. - Expected: an indication that this script won't fire here (it only runs on user messages / only at depth ≥ 3), so the preview is misleading. **Where it is:** `RegexScriptEditor.tsx` — the `testResult` memo (around lines 242–260). Its dependency list is `[testInput, localFindRegex, localReplaceString, localFlags, localTrimStrings, regexError]` — it never reads placement, promptOnly, target characters, or depth. **What's happening:** The preview only ever runs find/replace + trim. The real engines gate on all the other fields (`regex-application.ts:94-108`, `use-apply-regex.ts:69-101`), so the preview can show a result a real run would skip. **Suggested fix:** Have Live Test reflect the gating fields — at minimum show a "won't apply here" notice when the current placement/depth/scope would exclude the sample, or add a placement/depth selector to the test panel. --- ### B. Editor lets you save (and live-test) patterns the server rejects as unsafe **What's wrong:** If you type a ReDoS-prone pattern, the editor shows no error and the Save button stays enabled — but clicking Save returns a 400. Worse, the Live Test runs that same pattern synchronously, so a pathological pattern can hang the tab. **How to reproduce (try it yourself):** 1. Set Find Pattern to `(a+)+$`. - You'll see: no inline error under the field, Save button enabled. Click Save → request fails (400). 2. Now paste a long string of `a`s ending in `X` (e.g. `aaaaaaaaaaaaaaaaaaaaaaaaaaX`) into Live Test with that pattern. - You'll see: the editor tab freezes (catastrophic backtracking, no timeout). **Where it is:** `RegexScriptEditor.tsx` — the `regexError` memo (~229–239) is a syntax-only `new RegExp(...)` check; Save is disabled only on `regexError || characterScopeError` (line 439). The editor never imports or calls `isPatternSafe`. Live Test compiles + runs the pattern inline in the `testResult` memo (~249–250). **What's happening:** `(a+)+$` is syntactically valid, so `regexError` is null and Save is enabled. The server schema rejects it via `isPatternSafe` (`regex.schema.ts:15-21`; `regex-safety.ts` `maxStarHeight=1`, so nested-quantifier group height 2 fails). The editor has no equivalent client-side guard. **Suggested fix:** Run `isPatternSafe` (already in `@marinara-engine/shared`) in the editor: surface it as the same inline error as a syntax error, disable Save when unsafe, and skip running unsafe patterns in Live Test. --- ### C. Save failures show a generic "Save failed" / "Validation Error" instead of the real reason **What's wrong:** When a save is rejected, the red banner just says "Validation Error" — it doesn't tell the user which field failed or why (e.g. that the pattern is unsafe). **How to reproduce (try it yourself):** 1. Save a script with Find Pattern `(a+)+$` (see B). - You'll see: red banner reads only "Validation Error". - Expected: something like "findRegex: Regex pattern is unsafe: it may cause catastrophic backtracking…". **Where it is:** `RegexScriptEditor.tsx:307` sets `saveError = err.message`. `api-client.ts:141` constructs `ApiError(status, body.error ?? statusText, body)`, so `message` is `"Validation Error"` (`error-handler.ts:10-16`). The actionable per-field text lives in `error.payload.details[].message`, which the editor never reads. **What's happening:** The server returns `{ error: "Validation Error", details: [{ path, message }] }`. The client surfaces only the top-level `error` string. **Suggested fix:** When the caught error is an `ApiError` with `payload.details`, join the `path: message` entries into the banner text. --- ### D. A stored unsafe script becomes uneditable — every save 400s, even renaming it **What's wrong:** If a script with an unsafe pattern already exists in storage, you can't edit it at all through the editor: any Save (even just changing the name) is rejected, because the editor always resubmits the unsafe `findRegex`. **How to reproduce (try it yourself):** This needs an unsafe pattern already in the DB (e.g. via a `.maridb` restore or raw DB insert — it can't be created through the editor because of B). Then open that script, change only its Name, and Save → 400. **Where it is:** `RegexScriptEditor.tsx:278-291` always includes `findRegex` in the PATCH payload. The route parses `updateRegexScriptSchema` (`regex-scripts.routes.ts:32`), which is `createRegexScriptSchema.partial()` (`regex.schema.ts:33`). `.partial()` only makes fields optional — it keeps the `.refine` on `findRegex` whenever it's present, so the unsafe pattern is re-validated and rejected on every save. **What's happening:** The row-level enable toggle escapes this because it sends only `{ id, enabled }` (`CharacterRegexSection.tsx:318`) — confirming it's the editor's blanket resubmission of `findRegex` that triggers the rejection. **Suggested fix:** Tie this to fix B (block creating unsafe patterns up front) and/or only send changed fields on update so an untouched `findRegex` isn't re-validated, or surface the validation reason (fix C) so the user knows the pattern must change. --- ### E. Importing a batch of regex scripts stops at the first rejected one **What's wrong:** When you import a file with several regex scripts and one of them is rejected by the server, the import aborts there: scripts before it are created, the failing one and everything after it are not, and you get only a generic error. **How to reproduce (try it yourself):** 1. Make a JSON file with 3 regex scripts where script #2 has `findRegex: "(.*)+"` (unsafe). 2. Import it (character card Advanced → regex import, or the Presets panel regex import). - You'll see: script #1 created, import stops at #2 with a generic error, script #3 never imported. - Expected: the bad one is reported/skipped and the rest still import. **Where it is:** `CharacterRegexSection.tsx:193-205` and `PresetsPanel.tsx:407-418` both loop `await createRegex.mutateAsync(...)` inside a single try/catch. A thrown rejection breaks out of the loop and only sets a generic `importError`. **Suggested fix:** Wrap each `mutateAsync` in its own try/catch, collect per-entry failures, continue the loop, and report "imported N, skipped M (reasons…)". --- ### F. Live Test ignores the typographic-quotes setting that the chat applies **What's wrong:** With the quote-format setting set to typographic, the Live Test preview still shows straight quotes while the actual rendered chat message shows curly ones. (No divergence at the default "straight" setting, so this is low-impact.) **How to reproduce (try it yourself):** 1. Settings → set quote format to **typographic**. 2. In Live Test, input `He said "hi"`. - You'll see: preview keeps `"hi"` straight; the rendered chat message shows curly quotes. **Where it is:** The display path wraps its output in `formatTextQuotes(..., quoteFormat)` (`use-apply-regex.ts:148,156`); the editor's `testResult` memo does not and never imports `formatTextQuotes`. Default `quoteFormat` is `"straight"` (`ui.store.ts:1193`), so this only diverges once it's set to typographic. **Suggested fix:** Apply `formatTextQuotes(result, quoteFormat)` to the Live Test output to match the display path. --- ### G. Invalid regex flags (e.g. `gg`) save fine but silently make the whole script do nothing **What's wrong:** The `flags` field isn't validated. A script with bad flags (like a duplicated `gg`) saves and imports without complaint, but at runtime it silently never matches anything — the regex constructor throws and the error is swallowed. **How to reproduce (try it yourself):** 1. Hand-edit a regex-script JSON to set `"flags": "gg"` and import it (or otherwise get `gg` into the flags). - You'll see: it saves/imports fine but never replaces anything in chat. - Expected: rejected at save/import, or flags normalized. **Where it is:** `regex.schema.ts:25` — `flags: z.string().default("gi")` with no constraint. Import copies `entry.flags` verbatim (`CharacterRegexSection.tsx:81`, and `PresetsPanel` via the same `normalizeRegexImportEntry`). At runtime `new RegExp(findRegex, flags)` throws "Invalid flags…" and is swallowed by the try/catch in `regex-application.ts:116-127` and `use-apply-regex.ts:103-117`. The editor's flag buttons only offer `g/i/m/s/u` (line 570), so this only arrives via import/hand-edit. **Suggested fix:** Constrain `flags` in the schema to `/^[gimsuy]*$/` with no duplicates (Zod `.refine`), so invalid flags are rejected at save/import instead of silently no-opping the script. --- AI-investigated against v2.0.6; verify the reproduction before fixing.
Author
Owner

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

Addressed in the staging fixes: the regex editor now runs the shared safety check before preview/save, shows validation detail messages, applies quote formatting in Live Test, allows other fields on an unchanged unsafe stored pattern to be saved, validates flags/depth ranges, and import flows now continue past bad entries with visible skip reasons.

<!-- gh-comment-id:4827507686 --> @SpicyMarinara commented on GitHub (Jun 28, 2026): Addressed in the staging fixes: the regex editor now runs the shared safety check before preview/save, shows validation detail messages, applies quote formatting in Live Test, allows other fields on an unchanged unsafe stored pattern to be saved, validates flags/depth ranges, and import flows now continue past bad entries with visible skip reasons.
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:4827556904 --> @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#1512
No description provided.