[PR #2424] [MERGED] Bound macro variable expansion and dice roll resource use #5600

Closed
opened 2026-10-22 04:17:43 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/2424
Author: @Romuromylus
Created: 6/6/2026
Status: ✅ Merged
Merged: 6/6/2026
Merged by: @Romuromylus

Base: refactor ← Head: fix/macro-resource-bounds


📝 Commits (1)

  • 2aa6b7b Bound macro variable expansion and dice roll resource use

📊 Changes

1 file changed (+40 additions, -7 deletions)

View changed files

📝 src/engine/shared/macros/macro-engine.ts (+40 -7)

📄 Description

Linked issue

Closes #2363

Why this change

  • The shared macro engine had no resource bounds on three content-controlled paths reachable from imported cards/lorebooks/personas: an uncapped {{roll:NdM}} loop, an unclamped {{random:X:Y}} range, and setvar/addvar value re-expansion against shared mutable ctx.variables. A billion-laughs {{addvar::z::{{getvar::z}}}} chain could OOM the renderer; a hostile {{roll:999999999d6}} could freeze it.

What changed

  • src/engine/shared/macros/macro-engine.ts:
    • Threads a cumulative ~1MB variable-size budget (MAX_TOTAL_VARIABLE_SIZE) and an expansion-recursion cap (MAX_VARIABLE_EXPANSION_DEPTH = 8) through MacroResolutionState; setvar/addvar bail gracefully (return "", no throw, prior writes preserved) once over budget/depth.
    • Clamps {{roll:NdM}} count (MAX_DICE_ROLL_COUNT = 10_000) and sides (MAX_DICE_SIDES = 1_000_000).
    • Clamps the {{random:X:Y}} span (MAX_RANDOM_RANGE = 1_000_000) and fixes a latent inverted-range underflow (hi = Math.max(lo, …)).
  • Caps are deliberately generous so legitimate Celia-style {{cb1}}…{{cbN}} variable assembly (a few KB per block) still resolves fully.

Refactor impact

Primary owner: engine generation (shared macros)

Impact areas reviewed:

  • src/engine/shared/macros/macro-engine.ts (resolveMacrosWithState, MacroResolutionState)

Boundary notes:

  • None. Single engine/shared file; the new variableExpansionDepth field is optional so existing { characterFieldDepth } constructors stay valid. The web build's packages/shared copy is a separate repo/topology and intentionally out of scope.

Pressure points touched:

  • None.

Validation

  • Matching validation command passes locally
  • Full pnpm check passes before PR push/handoff
  • Human/manual validation completed by contributor or reviewer

Manual verification notes

  • pnpm vitest run (macro-engine) — local-only tests confirmed: a billion-laughs doubling chain is bounded near 1MB and returns in <5s (was multi-GB OOM); {{roll:999999999d6}} clamps to 10000 and returns promptly; {{random:10:5}} no longer underflows; and a 3×50KB Celia-style assembly still resolves fully (cap is generous). {{roll:2d6}} regression case unchanged.
  • pnpm exec tsc -b and pnpm exec eslint …macro-engine.ts — clean.

Feature Discoverability

  • Updated src/features/shell/discovery/
  • N/A because this PR is a DoS-hardening bugfix.

Reason:

  • No new discoverable surface.

Docs and release impact

  • No docs changes needed

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/2424 **Author:** [@Romuromylus](https://github.com/Romuromylus) **Created:** 6/6/2026 **Status:** ✅ Merged **Merged:** 6/6/2026 **Merged by:** [@Romuromylus](https://github.com/Romuromylus) **Base:** `refactor` ← **Head:** `fix/macro-resource-bounds` --- ### 📝 Commits (1) - [`2aa6b7b`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2aa6b7bdf8438b50fe420039c5999c6ad6d61861) Bound macro variable expansion and dice roll resource use ### 📊 Changes **1 file changed** (+40 additions, -7 deletions) <details> <summary>View changed files</summary> 📝 `src/engine/shared/macros/macro-engine.ts` (+40 -7) </details> ### 📄 Description ## Linked issue Closes #2363 ## Why this change - The shared macro engine had no resource bounds on three content-controlled paths reachable from imported cards/lorebooks/personas: an uncapped `{{roll:NdM}}` loop, an unclamped `{{random:X:Y}}` range, and setvar/addvar value re-expansion against shared mutable `ctx.variables`. A billion-laughs `{{addvar::z::{{getvar::z}}}}` chain could OOM the renderer; a hostile `{{roll:999999999d6}}` could freeze it. ## What changed - `src/engine/shared/macros/macro-engine.ts`: - Threads a cumulative ~1MB variable-size budget (`MAX_TOTAL_VARIABLE_SIZE`) and an expansion-recursion cap (`MAX_VARIABLE_EXPANSION_DEPTH = 8`) through `MacroResolutionState`; setvar/addvar bail gracefully (return `""`, no throw, prior writes preserved) once over budget/depth. - Clamps `{{roll:NdM}}` count (`MAX_DICE_ROLL_COUNT = 10_000`) and sides (`MAX_DICE_SIDES = 1_000_000`). - Clamps the `{{random:X:Y}}` span (`MAX_RANDOM_RANGE = 1_000_000`) and fixes a latent inverted-range underflow (`hi = Math.max(lo, …)`). - Caps are deliberately generous so legitimate Celia-style `{{cb1}}…{{cbN}}` variable assembly (a few KB per block) still resolves fully. ## Refactor impact Primary owner: engine generation (shared macros) Impact areas reviewed: - `src/engine/shared/macros/macro-engine.ts` (`resolveMacrosWithState`, `MacroResolutionState`) Boundary notes: - None. Single engine/shared file; the new `variableExpansionDepth` field is optional so existing `{ characterFieldDepth }` constructors stay valid. The web build's `packages/shared` copy is a separate repo/topology and intentionally out of scope. Pressure points touched: - None. ## Validation - [x] Matching validation command passes locally - [ ] Full `pnpm check` passes before PR push/handoff - [ ] Human/manual validation completed by contributor or reviewer ### Manual verification notes - `pnpm vitest run` (macro-engine) — local-only tests confirmed: a billion-laughs doubling chain is bounded near 1MB and returns in <5s (was multi-GB OOM); `{{roll:999999999d6}}` clamps to 10000 and returns promptly; `{{random:10:5}}` no longer underflows; and a 3×50KB Celia-style assembly still resolves fully (cap is generous). `{{roll:2d6}}` regression case unchanged. - `pnpm exec tsc -b` and `pnpm exec eslint …macro-engine.ts` — clean. ## Feature Discoverability - [ ] Updated `src/features/shell/discovery/` - [x] N/A because this PR is a DoS-hardening bugfix. Reason: - No new discoverable surface. ## Docs and release impact - [x] No docs changes needed --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:17:43 +07:00
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#5600
No description provided.