mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #3471] [Issue]: regex-safety validator false-positives on delimited-field patterns (([^]|]+)\|...) — safe scripts rejected as catastrophic backtracking #1875
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#1875
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 (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 passisPatternSafeand the script should import and apply. Fields split on a delimiter the class excludes are linear-time.Actual behavior
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 thecreateRegexScriptSchemarefinement,packages/shared/src/schemas/regex.schema.ts:39-40).use-apply-regex.ts:128,regex-application.ts:162).Steps to reproduce
([^\]|]+)\|([^\]|]+)\|([^\]|]+)(three or more delimited fields) — e.g. the attached "Social Links" METER script (~21 fields).Minimal reproduction against the real validator:
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
main; still present on currentstaging)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 whenbroadUnboundedCount >= 3. It incrementsbroadUnboundedCountfor 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 separateadjacentBroadUnboundedCount >= 2check (which resets on a required literal atom) already correctly handles the genuinely-dangerous adjacent case; the globalbroadUnboundedCount >= 3cap 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