mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #846] [Feature]: ReDoS hardening for the lorebook activation/regex layer #407
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#407
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 @Romuromylus on GitHub (May 14, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/846
Originally assigned to: @Romuromylus on GitHub.
What problem does this solve?
Lorebook entries support author-supplied regex keywords (
entry.useRegex+entry.keys[]). WhenuseRegexis on, the matcher constructsnew RegExp(keyword, flags)and runs.test(text)against chat context (or, after #845, against keyword-test sample text in the editor). A pathological pattern like(a+)+$against"a".repeat(N) + "!"triggers catastrophic backtracking and stalls the thread that runs it.The trust model is self-contained today: the author writes the regex, the author runs it against their own chat in their own browser/server. So this is fundamentally a self-inflicted-DoS / footgun rather than a security vulnerability — but it's still worth tracking, because:
packages/server/src/services/lorebook/keyword-scanner.ts) and now the client (packages/client/src/components/lorebooks/LorebookEditor.tsxpreview, via the shared helper atpackages/shared/src/utils/lorebook-keyword-matching.ts). A server-side stall affects all live chats on that instance; a client-side stall jams the editor tab.Proposed solution
Add a regex-safety layer at the activation boundary — not at the leaf
testKeywordhelper — so it applies uniformly across server scanning, client preview, and any future caller. Options (in roughly increasing weight):safe-regexor a hand-rolled depth check). Reject or fall back to literal substring matching for patterns that fail the check. Cheap to add, false-positives possible.regex.test. Implementable in the browser viasetTimeout+ a worker; on the server viaworker_threadswith a timer. Catches patterns that pass static checks but explode on specific inputs.re2-wasmon both client and server. Strongest, but adds ~300KB WASM to the client bundle and changes semantics (no backreferences, no lookaround). Probably too aggressive for this codebase's user expectations.A combination of #1 (cheap pre-filter) + #2 (timeout safety net) is probably the right place to land — keeps the JS regex semantics users already know, but breaks the worst-case stall.
Alternatives considered
packages/shared/src/utils/regex-replacement.ts, which has the same "author runs their own regex" trust model and no hardening today. Defensible given the trust model, but leaves the import-from-third-party case unaddressed.testKeywordhelper. Rejected during the #845 review — the leaf doesn't know the call context, and applying it there would force every caller to absorb the cost / behavior change. The activation layer (scanForActivatedEntriesand the editor preview'spreviewMatchesmemo) is the better seam.Additional context
packages/server/src/services/lorebook/keyword-scanner.ts:testPrimaryKeys(via the shared helper after #845 merges), andpackages/client/src/components/lorebooks/LorebookEditor.tsx'spreviewMatchesmemo.packages/shared/src/utils/regex-replacement.tsshould be folded into the same hardening pass — same posture, separate code path.Template check
@Romuromylus commented on GitHub (May 15, 2026):
Picking this up. Plan: hand-rolled
isPatternSafeheuristic in shared (star-height + length cap + dangerous{n,}repetition) wired into the existingtestKeywordhelper, plus a server-sidevm.runInNewContexttimeout executor injected through an optionalregexExecutorinKeywordMatchOptions. Client preview picks up the static check automatically; no browser-side timeout (sync regex on the main thread has no clean interrupt seam without workers, which would re-architect the whole sync API for limited benefit on a single tab).Keeping
packages/shared/src/utils/regex-replacement.tsout of this PR — same posture but different feature surface; will file a follow-up so this one stays focused on the lorebook activation layer.