feat(guard): wazero security hardening — memory cap, backend call limit, interpreter tests#7938
Conversation
…interpreter tests
There was a problem hiding this comment.
Pull request overview
This PR hardens the wazero-backed WASM guard integration by adding resource limits (memory and backend call count), simplifying guard module naming, and optimizing/selecting the interpreter runtime for unit tests to reduce unnecessary JIT overhead.
Changes:
- Add a per-invocation
call_backendcall counter with a hard limit to prevent runaway backend calls. - Add a runtime memory cap to constrain guard-controlled linear memory growth.
- Switch several unit tests to use the interpreter runtime and add a new test covering the backend call limit behavior.
Show a summary per file
| File | Description |
|---|---|
internal/guard/wasm.go |
Adds runtime memory cap, simplifies WithName setup, and enforces/reset a per-invocation backend call limit. |
internal/guard/wasm_test.go |
Uses interpreter runtime in tests and adds coverage for the backend call limit + counter reset behavior. |
Copilot's findings
Tip
Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Files reviewed: 2/2 changed files
- Comments generated: 1
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
|
@copilot fix the failed ci check https://github.com/github/gh-aw-mcpg/actions/runs/27976677887/job/82796097537?pr=7938 |
Addressed in 906538a. The failed check was a |
Go Fan review (#7927) identified security and quality gaps in the wazero WASM guard integration. This PR addresses all five recommendations.
Security
Memory cap: Add
WithMemoryLimitPages(256)(16 MiB) to the productionRuntimeConfig. Without this, a guard declaring a large(memory max ...)can grow to consume unbounded host RAM.Backend call limit: Add
maxBackendCallsPerInvocation = 50counter inhostCallBackend, reset at the start of eachcallWasmGuardFunction. Prevents a buggy or malicious guard from loopingcall_backendindefinitely within a single label call.Performance
wazero.NewRuntime(ctx)calls inwasm_test.gotowazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfigInterpreter()). JIT compilation is pure overhead for hand-crafted test WASM byte slices.Readability
WithNameIIFE: Replace the immediately-invoked function expression with a plainguardNamevariable.Tests
TestHostCallBackendCallLimit: verifies the error sentinel is returned when the limit is exceeded, and thatbackendCallCountis reset to zero at the start of each invocation.TestIsWasmTrap/actual_wazero_trap_still_uses_wasm_error_prefixalready serves as a live version assertion for the"wasm error:"string — it instantiates a real trap with the installed wazero and fails if the prefix changes on upgrade.