[GH-ISSUE #3471] [Issue]: regex-safety validator false-positives on delimited-field patterns (([^]|]+)\|...) — safe scripts rejected as catastrophic backtracking #1875

Closed
opened 2026-10-21 05:43:27 +07:00 by bsakaguchi · 0 comments
Owner

Originally created by @kolacheee on GitHub (Jul 11, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3471

Originally assigned to: @SpicyMarinara on GitHub.

Summary

The regex-safety validator (isPatternSafe → hasPolynomialBacktrackingRisk) rejects safe "delimited-field" patterns as catastrophic backtracking. A pattern of the form ([^\]|]+)\|([^\]|]+)\|([^\]|]+)… — capture groups separated by a required \| delimiter — is flagged "unsafe" even though it is provably linear (the negated class [^\]|] cannot consume the \| delimiter, so there is no backtracking ambiguity). These are extremely common in SillyTavern-style display regex (status-bar / "METER" render scripts). Regression: such scripts worked before v2.0.9.

Expected behavior

([^\]|]+)\|([^\]|]+)\|… should pass isPatternSafe and the script should import and apply. Fields split on a delimiter the class excludes are linear-time.

Actual behavior

  • On import: Skipped 1 regex script. Entry 1 (Social Links): Regex pattern is unsafe: it may cause catastrophic backtracking. Avoid nested quantifiers and overly long patterns. (from the createRegexScriptSchema refinement, packages/shared/src/schemas/regex.schema.ts:39-40).
  • At apply-time the same gate silently skips the script (use-apply-regex.ts:128, regex-application.ts:162).

Steps to reproduce

  1. Import (or save) a regex script whose Find pattern is ([^\]|]+)\|([^\]|]+)\|([^\]|]+) (three or more delimited fields) — e.g. the attached "Social Links" METER script (~21 fields).
  2. It is skipped as "unsafe".

Minimal reproduction against the real validator:

isPatternSafe("([^\\]|]+)")                          -> true   (1 field)
isPatternSafe("([^\\]|]+)\\|([^\\]|]+)")             -> true   (2 fields)
isPatternSafe("([^\\]|]+)\\|([^\\]|]+)\\|([^\\]|]+)")-> false  (3 fields — trips)

The 3rd delimited field flips it. The full pattern is 261 chars (well under the 1000 maxLength) and has no nested quantifiers, so neither the length nor star-height checks are involved. Running the full pattern against a 50,000-char adversarial input completes in ~1 ms — confirming it is linear, not catastrophic.

Environment

  • Marinara Engine version: v2.0.9+ (on main; still present on current staging)
  • Install type: installer
  • OS + version: Windows 11
  • Browser or app shell: Chrome / app

Logs, screenshots, or video

Skip toast: "Skipped 1 regex script. Entry 1 (Social Links): Regex pattern is unsafe: it may cause catastrophic backtracking. Avoid nested quantifiers and overly long patterns."

Additional context

Root cause. hasPolynomialBacktrackingRisk (packages/shared/src/utils/regex-safety.ts:305-375) returns unsafe when broadUnboundedCount >= 3. It increments broadUnboundedCount for every "broad unbounded" atom (a +/*/{n,} on a negated or broad class such as [^\]|]+, .*, \w+, [\s\S]*?) and never resets it, so any pattern with 3+ such atoms anywhere is rejected. But a broad-unbounded atom followed by a required literal the atom's class excludes (here [^\]|]+ then \|) cannot overshoot the delimiter — the split point is unambiguous and the match is linear. The separate adjacentBroadUnboundedCount >= 2 check (which resets on a required literal atom) already correctly handles the genuinely-dangerous adjacent case; the global broadUnboundedCount >= 3 cap is the over-broad part.

Introduced in v2.0.9 (commit fe67dc5c6, "[codex] Prepare v2.0.9 stability fixes (#3097)" — CHANGELOG 2.0.9: "Hardened prompt regex scripts against polynomial ReDoS by rejecting chained broad unbounded patterns…"). The apply/import gate itself landed slightly earlier (b0a18c7df, "reject ReDoS-prone prompt regex at apply time and on save"). (Note: the reporter suspected #3166, but that PR is an unrelated macro-resolution cache perf fix; it does not add or change the safety gate.) There is precedent for fixing false positives in this validator: #3080 fixed the (a+)? star-height over-rejection.

Suggested fix direction. Don't count a broad-unbounded atom toward broadUnboundedCount (the polynomial cap) when it is immediately bounded by a required literal that its class cannot match — i.e. delimited fields like [^\|]+\| / [^,]+,. The danger is only real when the broad atoms can overlap (adjacent, or separated by a delimiter the class also matches, like .*x.*y.*). Needs care not to regress detection of the truly-polynomial shapes.

Workaround for affected users: replace the unbounded + on each field with a bounded quantifier, e.g. ([^\]|]{1,200}) instead of ([^\]|]+). A bounded {n,m} is not counted as "broad unbounded", so the pattern passes. (Band-aid; the real fix is in the validator.)

Template check

  • I DID NOT read this template and provide the requested details.
Originally created by @kolacheee on GitHub (Jul 11, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/3471 Originally assigned to: @SpicyMarinara on GitHub. ## Summary The regex-safety validator (`isPatternSafe` → `hasPolynomialBacktrackingRisk`) rejects **safe "delimited-field" patterns** as catastrophic backtracking. A pattern of the form `([^\]|]+)\|([^\]|]+)\|([^\]|]+)…` — capture groups separated by a required `\|` delimiter — is flagged "unsafe" even though it is provably **linear** (the negated class `[^\]|]` cannot consume the `\|` delimiter, so there is no backtracking ambiguity). These are extremely common in SillyTavern-style display regex (status-bar / "METER" render scripts). Regression: such scripts worked before **v2.0.9**. ## Expected behavior `([^\]|]+)\|([^\]|]+)\|…` should pass `isPatternSafe` and the script should import and apply. Fields split on a delimiter the class excludes are linear-time. ## Actual behavior - On import: `Skipped 1 regex script. Entry 1 (Social Links): Regex pattern is unsafe: it may cause catastrophic backtracking. Avoid nested quantifiers and overly long patterns.` (from the `createRegexScriptSchema` refinement, `packages/shared/src/schemas/regex.schema.ts:39-40`). - At apply-time the same gate silently skips the script (`use-apply-regex.ts:128`, `regex-application.ts:162`). ## Steps to reproduce 1. Import (or save) a regex script whose Find pattern is `([^\]|]+)\|([^\]|]+)\|([^\]|]+)` (three or more delimited fields) — e.g. the attached "Social Links" METER script (~21 fields). 2. It is skipped as "unsafe". Minimal reproduction against the real validator: ``` isPatternSafe("([^\\]|]+)") -> true (1 field) isPatternSafe("([^\\]|]+)\\|([^\\]|]+)") -> true (2 fields) isPatternSafe("([^\\]|]+)\\|([^\\]|]+)\\|([^\\]|]+)")-> false (3 fields — trips) ``` The 3rd delimited field flips it. The full pattern is 261 chars (well under the 1000 `maxLength`) and has **no** nested quantifiers, so neither the length nor star-height checks are involved. Running the full pattern against a 50,000-char adversarial input completes in **~1 ms** — confirming it is linear, not catastrophic. ## Environment - Marinara Engine version: v2.0.9+ (on `main`; still present on current `staging`) - Install type: installer - OS + version: Windows 11 - Browser or app shell: Chrome / app ## Logs, screenshots, or video Skip toast: *"Skipped 1 regex script. Entry 1 (Social Links): Regex pattern is unsafe: it may cause catastrophic backtracking. Avoid nested quantifiers and overly long patterns."* ## Additional context **Root cause.** `hasPolynomialBacktrackingRisk` (`packages/shared/src/utils/regex-safety.ts:305-375`) returns unsafe when `broadUnboundedCount >= 3`. It increments `broadUnboundedCount` for every "broad unbounded" atom (a `+`/`*`/`{n,}` on a negated or broad class such as `[^\]|]+`, `.*`, `\w+`, `[\s\S]*?`) and **never resets it**, so any pattern with 3+ such atoms anywhere is rejected. But a broad-unbounded atom followed by a **required literal the atom's class excludes** (here `[^\]|]+` then `\|`) cannot overshoot the delimiter — the split point is unambiguous and the match is linear. The separate `adjacentBroadUnboundedCount >= 2` check (which resets on a required literal atom) already correctly handles the genuinely-dangerous *adjacent* case; the global `broadUnboundedCount >= 3` cap is the over-broad part. **Introduced in v2.0.9** (commit `fe67dc5c6`, "[codex] Prepare v2.0.9 stability fixes (#3097)" — CHANGELOG 2.0.9: *"Hardened prompt regex scripts against polynomial ReDoS by rejecting chained broad unbounded patterns…"*). The apply/import gate itself landed slightly earlier (`b0a18c7df`, "reject ReDoS-prone prompt regex at apply time and on save"). *(Note: the reporter suspected #3166, but that PR is an unrelated macro-resolution cache perf fix; it does not add or change the safety gate.)* There is precedent for fixing false positives in this validator: **#3080** fixed the `(a+)?` star-height over-rejection. **Suggested fix direction.** Don't count a broad-unbounded atom toward `broadUnboundedCount` (the polynomial cap) when it is immediately bounded by a required literal that its class cannot match — i.e. delimited fields like `[^\|]+\|` / `[^,]+,`. The danger is only real when the broad atoms can overlap (adjacent, or separated by a delimiter the class also matches, like `.*x.*y.*`). Needs care not to regress detection of the truly-polynomial shapes. **Workaround for affected users:** replace the unbounded `+` on each field with a bounded quantifier, e.g. `([^\]|]{1,200})` instead of `([^\]|]+)`. A bounded `{n,m}` is not counted as "broad unbounded", so the pattern passes. (Band-aid; the real fix is in the validator.) ## Template check - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:43:27 +07:00
  • closed this issue
  • added the
    in-pr
    bug
    labels
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#1875
No description provided.