Skip to content

Fix parser stack overflow and MemoryBudget bypasses - #1003

Merged
antonmedv merged 3 commits into
masterfrom
fix/security-dos
Nov 21, 2026
Merged

antonmedv merged 3 commits into
masterfrom
fix/security-dos

Conversation

@antonmedv

Copy link
Copy Markdown
Member

Two denial-of-service fixes, one per commit.

Parser: limit nesting depth

Deeply nested input (e.g. ~800KB of ( or [) overflowed the Go stack in the parser. A stack overflow is fatal and can't be recovered, and MaxNodes doesn't help because parentheses create no nodes. The parser now returns a compile error once nesting reaches parser.MaxNesting (10,000).

VM: enforce MemoryBudget for string and builtin allocations

String +, repeat, replace, join, split, splitAfter, toJSON, toBase64 and string results of single-argument builtins were not charged against MemoryBudget, so a short expression could allocate gigabytes.

  • A string result is charged for the bytes it adds beyond its largest input, so "prefix" + bigEnvString still works under the default budget.
  • Array and map inputs are charged the full output size, so aliasing (e.g. [#acc, #acc]) can't double a string for free.
  • repeat, replace and join check the remaining budget before allocating.

Breaking changes

  • builtin.Function.Safe and vm.SafeFunction take the remaining budget as their first argument.
  • New opcode OpCallBuiltinSafe for Safe builtins.
  • join returns an error for non-string elements (the same fix as return error on non-string element in join #969).
  • join or toJSON of an array or map producing more than ~1 MB, and building long strings one character at a time in reduce, now hit the default budget.

Performance

Compared with master in alternating runs, 10 rounds each:

  • The existing benchmark suite shows no significant change (geomean +0.3%).
  • Integer + is 3.4% faster and repeat is 4% faster.
  • split, join, replace and toBase64 are 3–4.5% slower (about 3 ns per call).
  • Allocations are unchanged.

Not covered

A loop that copies a large string many times (e.g. map(1..N, s + "x")) is still not fully bounded. Closing that requires charging every byte, which would break users who work with large data on the default budget.

Supersedes #938.

Deeply nested input such as ~800KB of '(' or '[' overflowed the Go stack
in the recursive-descent parser. A stack overflow is fatal and cannot be
recovered, and MaxNodes does not help because parentheses create no nodes.

Track recursion depth in parseExpression and fail with a compile error
once it reaches parser.MaxNesting (10000).
Several operations allocated without being charged against MemoryBudget,
so a small expression could allocate gigabytes:

- string concatenation (e.g. reduce(1..34, #acc + #acc, "a"))
- repeat, replace, join, split, splitAfter
- toJSON, toBase64, and string results of single-argument builtins

String results are charged for the bytes they add beyond their largest
input, so concatenating with a large env string still works. Composite
inputs are charged the full output, to prevent doubling through aliasing.

Safe builtins now receive the remaining budget, so repeat, replace and
join can refuse an oversized result before allocating it.

Breaking: builtin.Function.Safe and vm.SafeFunction take the remaining
budget as their first argument. Safe builtins are compiled to the new
OpCallBuiltinSafe opcode, which avoids pushing the function as a constant.

join now returns an error for non-string elements.
estimateFnArgsCount walked the whole bytecode on the first function call
of every run. Compute it once in NewProgram and reuse it. Programs built
without NewProgram still compute it on the fly.

This removes a 10% slowdown in BenchmarkVM/function_calls introduced by
the exact OpCallSafe/OpCallBuiltinSafe estimate, and makes it 4% faster
than before.
@antonmedv
antonmedv merged commit 62e68c0 into master Nov 21, 2026
23 checks passed
@antonmedv
antonmedv deleted the fix/security-dos branch November 25, 2026 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant