Skip to content

contrib: stop python, jq and async panics from crashing the process - #860

Merged
ewhauser merged 2 commits into
mainfrom
claude/modest-bhabha-36c4be
Oct 19, 2026
Merged

ewhauser merged 2 commits into
mainfrom
claude/modest-bhabha-36c4be

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

Summary

Untrusted scripts could kill the whole gbash process with contrib interpreters. Under gbash --server that process hosts every session. This PR fixes the three crash paths found in the contrib interpreter security review.

1. python: Monty resource limits

Before, python3 -c 'x="a"*(10**15)' aborted the process with SIGABRT, and while True: pass ignored the request timeout. contrib/python now passes monty.ResourceLimits to every run:

  • MaxMemory 256 MiB: oversized allocations raise MemoryError. 10**(10**12) is also rejected up front this way.
  • MaxRecursionDepth 1000 (the CPython default).
  • MaxDuration = time left before the request deadline. Monty can't see Go context cancellation while it runs bytecode, so this limit is what stops it. When it fires, python returns the context error, so the runtime reports execution timed out with exit 124.
  • With no deadline, scripts fall back to a 60s duration. The REPL's duration counts up from when the REPL is created, so only a request deadline bounds it.
$ gbash-extras -c "python3 -c 'x=\"a\"*(10**15)'; echo rc=\$?"
Traceback (most recent call last):
  ...
MemoryError: memory limit exceeded: 1000000000000064 bytes > 268435456 bytes
rc=1

2. jq: value nesting depth limit (vendored gojq)

jq -c -n 'reduce range(3000000) as $i (0;[.])' (also tojson, ==, sort, contains, flatten, *, setpath with long paths, del, error) caused a fatal Go stack overflow, which can't be recovered. The recursion is inside gojq. gojq has no hooks for it, and builtins such as to_entries can deepen values without any user-written construction. So this PR vendors gojq v0.12.18 under contrib/jq/internal/gojq, the same pattern as the vendored goawk. It includes the upstream root tests, builtin.jq and the upstream license, and a README lists the local changes.

  • MaxDepth = 10000 (the same as jq's parser depth). Comparison, the encoder, contains, flatten, object deep merge and delpaths cleanup all track depth. setpath/delpaths reject paths longer than the limit.
  • Helpers that have no error return panic with *DepthLimitError. env.Next turns that into the iterator's final value and stops, so try can't catch it and loop on it. Marshal returns the error, and error($deep) messages report it.
  • gbash's own output encoder checks depth before writing anything, so no partial output is written.
$ gbash-extras -c "jq -c -n 'reduce range(20000) as \$i (0;[.])'; echo rc=\$?"
jq: encode: value nesting exceeds depth limit of 10000
rc=5

3. interp: recover panics in goroutine-run subshells

Foreground panics are already recovered in shell.core.Run. Pipeline left-hand sides (runner_cmd.go), & background jobs and process substitutions run on their own goroutines, and nothing recovered panics there. Those goroutines now defer recoverAsyncPanic: the subshell prints the sanitized message (shell execution failed), exits with status 2, and the shell carries on. The sanitizer moved to interp.SanitizeRunnerPanic so the foreground and goroutine paths share it.

SPEC

SPEC.md now documents the python limits, the jq depth limit and the panic recovery in goroutine-run subshells.

Tests

Each regression test was confirmed to fail first: SIGABRT, hang, fatal error: stack overflow, and panic:, in that order.

  • contrib/python/python_limits_test.go: MemoryError, RecursionError, and timeout → 124 for a busy loop run with -c and from stdin.
  • contrib/jq/jq_depth_test.go: 15 deep-value cases plus a within-limit case. internal/gojq/depth_test.go: unit tests for the depth checks.
  • internal/runtime/async_panic_test.go: a panicking command on the left of a pipe, in a background job and in a process substitution (also run with -race).

Commands run: make build, go test ./internal/shell/... ./internal/runtime/..., go test ./... in contrib/jq, contrib/python and contrib/extras, the jq conformance corpus (GBASH_RUN_JQ_CONFORMANCE=1 … -run '^TestJQConformance$'), make lint and make lint-contrib (0 issues). contrib/python has no conformance suite.

Notes

  • go.mod changes: contrib/jq drops itchyny/gojq and requires timefmt-go directly. The stale gojq lines are removed from the extras, bashtool and examples modules.
  • examples doesn't build with GOWORK=off on main either (it needs go mod tidy). That predates this PR.
  • Out of scope: the timeout and memory problems in sqlite3, awk, yq and nodejs from the same review.

- python: pass Monty resource limits (256 MiB heap, recursion depth 1000,
  duration tied to the request deadline, 60s fallback) so huge allocations
  raise MemoryError and runaway loops honor the request timeout (exit 124).
- jq: vendor gojq v0.12.18 under contrib/jq/internal/gojq and bound every
  value-recursive helper (compare, encode, contains, flatten, object merge,
  setpath/delpaths) at MaxDepth=10000; the output encoder checks the same
  limit. Deep values now fail with exit 5 instead of a fatal stack overflow.
- interp: recover panics in pipeline left-hand sides, background jobs and
  process substitutions so the subshell exits 2 with a sanitized message.
- SPEC: document the python limits, jq depth limit and async panic recovery.
@ewhauser
ewhauser enabled auto-merge (squash) October 19, 2026 06:44
@ewhauser
ewhauser merged commit 33e69c1 into main Oct 19, 2026
16 checks passed
@ewhauser
ewhauser deleted the claude/modest-bhabha-36c4be branch October 19, 2026 06:52
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