Skip to content

fix(hookvm): charge a ReplaceAll result before building it - #527

Merged
BaaRai merged 2 commits into
BaaRai:mainfrom
Dev-next-gen:fix/hookvm-replace-budget
Nov 26, 2026
Merged

BaaRai merged 2 commits into
BaaRai:mainfrom
Dev-next-gen:fix/hookvm-replace-budget

Conversation

@Dev-next-gen

Copy link
Copy Markdown
Contributor

A tamper hook can take the whole server down from inside its memory budget. With this hook in a dry run (POST /api/traffic/hooks/dry-run, default 4 MiB budget):

package hook
import "strings"
func OnRequest(req *Request) error {
	s := strings.Repeat("a", 1024*1024)
	_ = strings.ReplaceAll(s, "a", strings.Repeat("b", 1024*1024))
	return nil
}

the process exits with fatal error: runtime: out of memory (the runtime asks for a 1 TiB span). The hook should instead fail open with memory_limit, as the tamper hook docs describe.

strings.ReplaceAll, bytes.ReplaceAll and regexp.ReplaceAllString in traffic/hookvm/stdlib.go build their result first and call e.alloc(len(out)) after, so the budget only sees the result once it already exists. Both inputs fit the budget (2 MiB together); the result is 1 TiB. The same hook on live traffic kills the server in the proxy path.

The fix charges the result before building it. For ReplaceAll the length is exact: len(s) + count*(len(new)-len(old)), with strings.Count/bytes.Count, which count an empty old the same way ReplaceAll does. For ReplaceAllString I count the matches and their total length with a first pass that builds nothing larger than s, then charge an upper bound: each match becomes at most the template with every $ expanded to the whole match.

The new subtest in TestResourceBoundsMemoryStepTimeoutPanicAndRegexp replaces 8 KiB of matches with 8 KiB each under a 1 MiB budget and checks TotalAlloc. Before the fix it fails on all three calls (64 MiB, 64 MiB and 386 MiB allocated, even though the run does end with ErrMemoryLimit); after it passes. I also ran the 1 MiB × 1 MiB hook through traffic.DryRunHook with the default budget: before, fatal error: runtime: out of memory; after, a trace line with outcome=error reason=memory_limit. go test ./traffic/hookvm and the TamperHook tests in ./traffic pass.

Found by a defect-hunting pipeline I build and run (Dev-next-gen), using Claude Code with Anthropic's Claude Opus 5.5.

strings.ReplaceAll, bytes.ReplaceAll and regexp.ReplaceAllString built
their result and only then charged its length against the memory budget.
A hook that replaces each byte of a 1 MiB string with another 1 MiB
string makes the runtime ask for 1 TiB, and the server exits with
"fatal error: runtime: out of memory" before the limit is checked.

The length of the result is now computed from the match count and
charged first. For a regexp template, each match is bounded by the
template with every $ reference expanded to the whole match.
@5P34R

5P34R commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Review notes

Looks good to merge.

  • The ReplaceAll length formula is exact, including an empty old: Count returns runes+1.
  • n*(newLen-oldLen) can't overflow int64 at the sizes the VM allows.
  • No other stdlib builder in traffic/hookvm/stdlib.go grows faster than linear.
  • The TotalAlloc test is safe because the package runs no t.Parallel tests.

Minor (non-blocking): for regexp replacements, every $ in the template, including a literal $$, is charged as refs*matched. So a hook like (?s)^.*?(token=\w+).*$ → "$1" on a 3 MiB body is charged about 3 MiB when the real output is tiny, and can hit a false memory_limit. That fails safe, so it's acceptable as is.

@BaaRai
BaaRai force-pushed the fix/hookvm-replace-budget branch from d4082cf to 2d93be4 Compare November 26, 2026 22:24
BaaRai added a commit to rheatkhs/ARTEX that referenced this pull request Nov 26, 2026
@BaaRai
BaaRai force-pushed the fix/hookvm-replace-budget branch from 2d93be4 to 56378da Compare November 26, 2026 22:40
BaaRai added a commit to rheatkhs/ARTEX that referenced this pull request Nov 26, 2026
@BaaRai
BaaRai force-pushed the fix/hookvm-replace-budget branch from 56378da to f0f861c Compare November 26, 2026 22:56
BaaRai added a commit to rheatkhs/ARTEX that referenced this pull request Nov 26, 2026
@BaaRai
BaaRai force-pushed the fix/hookvm-replace-budget branch from f0f861c to fb80900 Compare November 26, 2026 23:13
@BaaRai
BaaRai merged commit bc4940c into BaaRai:main Nov 26, 2026
6 checks passed
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.

3 participants