[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

Closed
opened 2026-10-21 05:37:48 +07:00 by bsakaguchi · 1 comment
Owner

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):

  1. Edit any character's Description (or send it in a message) and put this in it: {{roll:999999999d6}}
  2. Start/continue a chat with that character so the prompt gets built.

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 loops for (i = 0; i < n; i++) with n taken straight from the user. The {{random::}} (line 917), {{setvar}} (946) and {{addvar}} (950) handlers call resolveMacros again 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. resolveMacros runs 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.
  • Add one global recursion/iteration counter threaded through 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.
  • Optionally cap total output length to stop runaway growth.

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.

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): 1. Edit any character's **Description** (or send it in a message) and put this in it: `{{roll:999999999d6}}` 2. Start/continue a chat with that character so the prompt gets built. **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 loops `for (i = 0; i < n; i++)` with `n` taken straight from the user. The `{{random::}}` (line 917), `{{setvar}}` (946) and `{{addvar}}` (950) handlers call `resolveMacros` again 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. `resolveMacros` runs 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. - Add one global recursion/iteration counter threaded through `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. - Optionally cap total output length to stop runaway growth. --- *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.*
Author
Owner

@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.

<!-- gh-comment-id:4827607970 --> @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.
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#1516
No description provided.