Skip to content

contrib: stop sqlite3, awk, and yq on timeout and bound their memory - #862

Merged
ewhauser merged 2 commits into
mainfrom
claude/nice-volhard-653458
Oct 19, 2026
Merged

ewhauser merged 2 commits into
mainfrom
claude/nice-volhard-653458

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

Fixes the timeout and resource-limit findings for sqlite3, awk, and yq from the contrib interpreter security review. The review found no host escapes, but these commands ignored ExecutionRequest.Timeout and cancellation, and could exhaust host memory or disk.

User-visible behavior

sqlite3

  • Long-running statements now stop on timeout. Before, a recursive CTE ran forever. The invocation context is now attached as the connection's interrupt context, since ncruces resets it after OpenContext.
  • Rows stream to stdout as SQLite produces them, so sqlite3 ... | head -n 3 stops in ~150ms instead of buffering every row (1.2GB in 10s before).
  • -column, -table, and -box still need every row to compute widths. They refuse results larger than max(MaxStdoutBytes, MaxFileBytes) (64 MiB when neither is set) and print sqlite3: result too large for column mode ... (exit 1).
  • Temp tables no longer go to the host's $TMPDIR (a 2.1GB host file in 6s before):
    • The connection now uses a VFS that refuses every file.
    • temp_store is forced to MEMORY, and PRAGMA temp_store=... is denied.
    • The SQLite heap is capped at 128 MiB.
  • ncruces panics on SQLITE_NOMEM. That panic is now recovered and reported as sqlite3: out of memory (exit 1). Before, it was already reachable through the 256 MB wasm cap, and it would crash the process inside a pipeline's left side.

awk

  • awk runs through goawk's ExecuteContext, so BEGIN{while(1){}} honors the timeout and exits 124.

yq

  • The process-wide yqEvalMu is removed, so one runtime's yq no longer blocks another's. yqlib's security preferences (env and file ops disabled) and its expression parser are set once in init().
  • yqlib has no context support, so gbash instruments each parsed expression. Every operator node gets its own copy of its operation type with a wrapped handler that:
    • checks ctx before running, and
    • sizes its result against a budget: an estimated 1 KiB per node plus scalar bytes, up to max(256 MiB, 4 × the estimated input size).
  • The first failure is sticky. A budget failure prints yq: evaluation exceeded the estimated N byte size limit (exit 1).
  • The ~200-byte doubling expression that reached 15.5GB now fails at about 0.4GB peak RSS. String doubling peaks at about 1.1GB, because the op that crosses the budget doubles a 256MB string.
  • Caveat: yqlib compares a few operation types by pointer while evaluating (BLOCK, UNION, ASSIGN_VARIABLE, CREATE_MAP), so those nodes keep the shared type and are not wrapped. Their children are still wrapped. Re-check grep "OperationType ==\|OperationType !=" pkg/yqlib when upgrading yq.

SPEC

Adds a paragraph to the contrib section on how interpreters handle cancellation and limits: the details above, plus a note that contrib/nodejs native builtins still can't be interrupted.

Tests

New regression tests, each failing before its fix:

  • contrib/sqlite3/sqlite3_limits_test.go
    • interrupt on timeout
    • streaming in every streaming mode
    • streaming output matches the old formatting
    • bounded column modes
    • temp storage works with SQLITE_TMPDIR pointed at a nonexistent dir (failed before with a host open error)
    • heap cap returns out of memory
  • contrib/awk/awk_limits_test.go: infinite loops in BEGIN and in a main rule hit the timeout and exit 124.
  • contrib/yq/yq_limits_test.go
    • timeout on a CPU-bound expression
    • memory budget for array, string, and assignment doubling
    • a second runtime isn't blocked
    • a large legitimate result fits the budget
    • concurrent evaluations under -race

Ran:

  • go test (and -race) for contrib/sqlite3, contrib/awk, and contrib/yq
  • go test for contrib/extras and contrib/bashtool
  • awk and yq contrib conformance against the pinned oracles
  • make lint and make lint-contrib: 0 issues

Not addressed (follow-ups)

  • awk, python, and node have no general heap cap. For example, s = s s doubling still reaches 1GB (review finding 7).
  • contrib/nodejs: goja native builtins like JSON.stringify on deep values can't be interrupted and recurse in Go. Fixing this needs a goja patch.

- sqlite3: attach the invocation context as the connection interrupt,
  stream rows to stdout (bounded buffering for -column/-table/-box),
  force in-memory temp storage behind a VFS that refuses host files,
  cap the SQLite heap at 128 MiB, and turn NOMEM panics into errors.
- awk: execute through goawk's ExecuteContext so loops honor ctx.
- yq: drop the process-wide evaluation lock, set yqlib security
  preferences once at init, and wrap each parsed operator so it checks
  ctx and sizes its result against an evaluation budget.
- SPEC: document the contrib interpreter cancellation and limit contract.
@ewhauser
ewhauser enabled auto-merge (squash) October 19, 2026 06:53
@ewhauser
ewhauser merged commit e42be69 into main Oct 19, 2026
16 checks passed
@ewhauser
ewhauser deleted the claude/nice-volhard-653458 branch October 19, 2026 07:01
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