mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[GH-ISSUE #2363] [Issue]: Macro engine has no resource bounds — exponential setvar/addvar expansion and unbounded {{roll:NdM}} loop freeze/OOM the renderer #1163
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#1163
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 (Jun 5, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2363
Originally assigned to: @Romuromylus on GitHub.
Summary
Severity: Medium — injection / content-driven denial of service.
The macro engine has no resource bounds. Two content-controlled vectors can hang or out-of-memory-crash the renderer during prompt assembly: (1) exponential
setvar/addvarre-expansion (a billion-laughs blowup), and (2) the{{roll:NdM}}dice macro loopingNtimes with no upper bound onN. Both are reachable from imported character cards, lorebook entries, personas, and presets.Expected behavior
Macro resolution should be bounded — a hostile or malformed imported field should not be able to drive cumulative variable size or loop iteration counts unbounded. The engine already has the right pattern for this (
MAX_CHARACTER_FIELD_RESOLUTION_DEPTH); the variable-op androll/randompaths should be guarded the same way.Actual behavior
Vector 1 — exponential variable expansion. In
src/engine/shared/macros/macro-engine.ts:841-873,setvar/addvarre-expand their assigned value viaresolveMacrosWithStateagainst the shared, mutablectx.variables, whilegetvarreturns the current raw value:So
{{addvar::z::{{getvar::z}}}}doublesz(z_new = z_old + z_old).replaceBalancedMacrosruns each balanced macro left-to-right against the same mutatedctx.variables, so the doublings compound in a single resolve pass. I reproduced this: a 719-byte payload{{setvar::z::AAAA}}followed by 25×{{addvar::z::{{getvar::z}}}}growszto 134,217,728 bytes (4·2^25) in one pass; ~40 doublings (~1.2 KB of literal input) reaches multiple GB and OOM/freezes the renderer. The only guard in the whole file isMAX_CHARACTER_FIELD_RESOLUTION_DEPTH = 4(line 79/192), which bounds only character-field substitution. TheMacroResolutionStatethreaded through these paths carries onlycharacterFieldDepthand is never incremented for variable re-expansion. There is no total-output-size cap, no per-expansion iteration cap, and no recursion-depth cap onsetvar/addvar/getvar.Vector 2 — unbounded dice loop. In the same file at
:832-838:n = parseInt(count, 10)is taken straight from the matched digits with no cap, then the loop runsniterations. A string{{roll:999999999d6}}spins the single-threaded renderer for ~1e9 iterations; a larger literal extends it proportionally toward a multi-minute stall. Linear (not exponential), so lower severity than vector 1, but still a trivial content-driven freeze.Steps to reproduce
{{setvar::z::AAAA}}followed by ~40 repetitions of{{addvar::z::{{getvar::z}}}}.resolveMacroson the greeting viaseedPromptVariablesFromGreeting).zgrows past available memory. (For vector 2, use a greeting containing{{roll:999999999d6}}instead and observe the multi-second-plus freeze.)Impact area
Environment
330f69a8Logs, screenshots, or video
Static source audit. Empirically reproduced in isolation: the 719-byte
setvar+25×addvarpayload growszto 134,217,728 bytes in a singleresolveMacrospass. Cited locations:src/engine/shared/macros/macro-engine.ts:841-873(variable ops),:832-838(roll),:79/:192(the only existing depth guard).Regression notes
Found via static audit; not a regression. The web build at
packages/shared/src/utils/macro-engine.tshas the identical unguarded re-expansion and the identical uncappedrollloop, so both topologies share this pre-existing behavior — the refactor did not break or drop a prior-correct guard.Additional context
The fix-pattern already exists in this file. Thread a cumulative-output-size budget plus an expansion-iteration / recursion-depth counter through
MacroResolutionState(mirroringMAX_CHARACTER_FIELD_RESOLUTION_DEPTH), cap the summed length of allctx.variablesvalues (e.g. a few hundred KB to 1 MB) and bail out when exceeded, and clampn(and the{{random:X:Y}}range) to a sane maximum before the loop. Apply the guard to thesetvar/addvar/getvar,roll, andrandom::re-expansion paths.Template check
@Romuromylus commented on GitHub (Jun 6, 2026):
Fixed by #2424, merged into
refactor.