mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2938] [Issue]: A character card's {{roll}} or nested macros can freeze/crash the whole app (no limit on dice count or macro recursion) #1516
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#1516
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 (Jun 28, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2938
Originally assigned to: @SpicyMarinara on GitHub.
What's wrong
A single macro inside any character card, lorebook entry, persona, or preset can freeze the whole backend (every chat for every user) or crash it. Macros are run on the server with no limit on how long a dice roll loops or how deeply macros can nest, so one bad value locks up Node's single thread.
This matters because people import character cards from sites like Chub and Janny — a shared card can carry this and take the app down for whoever opens it, with no warning.
How to reproduce (try it yourself)
Dice loop (simplest):
{{roll:999999999d6}}You'll see: the request hangs for ~5 seconds (and
{{roll:10000000000d6}}hangs for ~40s+). The whole server is frozen during that time — other chats stall too.Expected: an absurd dice count should be rejected or capped instantly.
Deep nesting (the no-cap version): a card field containing a few KB of nested
{{random::…}}(each wrapped in another{{random::…}}) either hangs for tens of seconds or crashes with "Maximum call stack size exceeded".Where it is
packages/shared/src/utils/macro-engine.ts—resolveMacros. The dice handler at ~line 926 loopsfor (i = 0; i < n; i++)withntaken straight from the user. The{{random::}}(line 917),{{setvar}}(946) and{{addvar}}(950) handlers callresolveMacrosagain recursively with no depth counter. The only existing limit (MAX_CHARACTER_FIELD_RESOLUTION_DEPTH = 4, line 76) applies only to character-name/field expansion, not to these.What's happening
There is no global cap on recursion, iterations, or output size anywhere in the macro engine, and the dice loop trusts the user's count.
resolveMacrosruns synchronously on Node's single thread over fully user-controlled text during every prompt build, so one runaway value blocks all requests.Suggested fix
{{roll:NdM}}: clamp the dice count and sides to sane maximums (e.g. count ≤ 1000, sides ≤ 1,000,000) before the loop.resolveMacros(and the conditional/character resolvers), capped at a small number (8–16). When exceeded, stop expanding and leave the text as-is instead of recursing — the same idea the character-field depth-4 guard already uses, just applied everywhere.Found by a code review of v2.0.6; the dice repro above is trivial to confirm in the running app — please verify before fixing.
@SpicyMarinara commented on GitHub (Jun 28, 2026):
Fixed on staging: macro resolution now has shared recursion, expansion-count, dice-count/sides, and output-length caps so runaway nested macros and absurd dice rolls stop instead of blocking the app. Added prompt regression coverage for capped nested macro output. Validation: pnpm regression:prompt; pnpm check.