mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #882] [MERGED] feat(lorebooks): ReDoS hardening for keyword regex activation #4817
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#4817
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?
📋 Pull Request Information
Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/882
Author: @Romuromylus
Created: 5/15/2026
Status: ✅ Merged
Merged: 5/15/2026
Merged by: @Romuromylus
Base:
staging← Head:fix/lorebook-regex-redos-hardening📝 Commits (4)
2d85e3cfeat(lorebooks): ReDoS hardening for keyword regex activation (#846)916fea3chore(lorebooks): polish regex-safety walker and timeout testda42107perf(lorebooks): skip vm executor for non-useRegex matchWholeWords path36ac134Merge branch 'staging' into fix/lorebook-regex-redos-hardening📊 Changes
6 files changed (+391 additions, -9 deletions)
View changed files
📝
packages/server/src/services/lorebook/keyword-scanner.ts(+2 -0)➕
packages/server/src/services/lorebook/regex-timeout.ts(+58 -0)➕
packages/server/test/lorebook-regex-safety.test.ts(+93 -0)📝
packages/shared/src/index.ts(+1 -0)📝
packages/shared/src/utils/lorebook-keyword-matching.ts(+31 -9)➕
packages/shared/src/utils/regex-safety.ts(+206 -0)📄 Description
Linked issue
Closes #846
Why this change
Lorebook entries support author-supplied regex keys (
entry.useRegex+entry.keys[]). WhenuseRegexis on, the matcher doesnew RegExp(keyword, flags).test(text)against chat context — and after #845, against keyword-test sample text in the editor preview. A pathological pattern like(a+)+against"a".repeat(N) + "!"triggers catastrophic backtracking and stalls the thread that runs it: server-side that affects every live chat on the instance, client-side that jams the editor tab.Trust model is mostly self-contained today (you write the regex, you run it on your own chat), so it's footgun more than vulnerability — but the SillyTavern lorebook import path crosses that boundary, and the 2026-04 issue here is that the failure mode is invisible until the right input shows up. I want a guard at the activation seam that applies uniformly across server scanning, client preview, and any future caller, without changing semantics for safe patterns.
What changed
Two-layer defense, both gated at the activation boundary instead of the leaf
testKeywordhelper (per the #845 review thread that scoped this out of the leaf):1. Static safety heuristic in
@marinara-engine/shared(isPatternSafe) —packages/shared/src/utils/regex-safety.ts(a+)+,(a*)*,(a+|b)+,((ab)+)+{n,m}repetition cap (100, also catches unbounded{1,})(?:,(?=,(?<name>) without depending onsafe-regex/ret. Keeps@marinara-engine/sharedat zero new deps.Wired into
testKeyword: whenuseRegexis on andisPatternSafereturns false, the helper falls back to literal substring matching — same posture as the existing invalid-regexcatchfallback, principle of least surprise.2. Server-side execution timeout (
vmRegexExecutor) —packages/server/src/services/lorebook/regex-timeout.tsregex.testinsidevm.runInNewContextwith a 50 ms timeout. V8's interrupt checks fire during regex execution, so catastrophic backtracking aborts instead of pinning the event loop.false. Doesn't substitute literal-substring semantics on timeout — the pattern compiled and may have matched on simpler input; switching meanings mid-run would be a silent surprise.regexExecutoronKeywordMatchOptionsso the shared helper stays runtime-agnostic; the client preview keeps using the default executor.Client-side has the static check only. A browser-side sync timeout would require re-architecting the helper around Web Workers (sync
regex.teston the main thread has no clean interrupt seam), which is a heavier change than the symptom warrants — a frozen tab is recoverable, a frozen Node event loop isn't.packages/shared/src/utils/regex-replacement.tsis intentionally out of scope. Same posture, different feature surface (regex scripts on display/output, separate call sites). Filing a follow-up so this one stays focused on the lorebook activation layer.Validation
pnpm checkpasses locallyCONTRIBUTING.mdManual verification notes
packages/server/test/lorebook-regex-safety.test.tscover both layers —isPatternSafefor safe / nested-quantifier / oversized / unbalanced patterns,testKeywordfallback semantics, and the canonical(a|aa)+×"a".repeat(28) + "!"ReDoS pair under the vm timeout (verifies the abort actually fires within ~1 s on the runner).useRegex: true: paste a pathological pattern like(a+)+and confirm the keyword-test panel from #845 stays responsive — without this change, that combo freezes the editor tab.\bword\b,foo|bar,(?:foo){1,5}. The static check accepts these; behavior should be unchanged.Lorebook regex /…/… exceeded 50ms timeout against chat context (length=…) — entry will not match this scan.Docs and release impact
UI evidence (if applicable)
No UI changes — purely runtime hardening behind the existing
useRegexflag and the keyword-test panel from #845.Summary by CodeRabbit
New Features
Tests
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.