Skip to content

fix(server): harden server mode, CLI flag parsing, and readwrite-root mounting - #871

Merged
ewhauser merged 9 commits into
mainfrom
claude/fix-server-cli-review-ec68b3
Oct 19, 2026
Merged

ewhauser merged 9 commits into
mainfrom
claude/fix-server-cli-review-ec68b3

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

Fixes the verified security-review findings for server mode, the shared CLI, the wasm package, and the scripts/docker tooling.

CLI (cli/)

  • Flag injection after -c: everything after -c command_string (or a script path) is now $0 and positional args, as in bash. Before this, gbash -c 'ls' --root=/ x mounted the whole host filesystem, and anyone controlling positional args could inject --json or --readwrite-root the same way. A -- is inserted before the positionals so the invocation parser leaves them alone.
  • --readwrite-root symlink swap: the runtime now mounts the symlink-resolved path that passed the system-temp check. Previously each new server session resolved the original path again, so swapping a symlink after startup could mount any directory read-write.
  • --listen localhost now binds 127.0.0.1 directly instead of trusting /etc/hosts or DNS, and the bound address is checked again to be loopback.
  • --json adds stdoutBase64 / stderrBase64 when a stream isn't valid UTF-8. These fields are omitted otherwise, so existing consumers are unaffected.

Server (server/)

  • session.exec runs under a per-connection context, so a client disconnect cancels the exec and the session goes back to idle. Before this, the session stayed running forever, destroy and exec returned SESSION_BUSY, and the reaper skipped it. Requests are decoded on a separate goroutine so the disconnect is noticed while an exec is running. New optional Config.MaxExecTimeout.
  • Serve retries temporary Accept errors (EMFILE and similar) with backoff, and closes every live connection whenever it returns.
  • New Config.MaxRequestBytes (default 16 MiB; oversized requests get REQUEST_TOO_LARGE and the connection closes) and Config.MaxSessions (default 256; extra creates get SESSION_LIMIT, code -32012).
  • Session IDs are 128-bit random (sess-<32 hex>) instead of sequential. session.list is sorted by creation time.
  • timeout_ms values too large for a Go duration now saturate instead of overflowing (18446744073710 used to become a ~448µs timeout).
  • stdout_base64 / stderr_base64 are added when output isn't valid UTF-8.
  • JSON-RPC notifications (no id member) are processed but never answered, per JSON-RPC 2.0.
  • Internal errors are logged and sent to clients as a generic INTERNAL error, since the details could include absolute host paths.
  • Unix socket handling:
    • The socket is hard-linked into place (falling back to rename on filesystems without hard links), so a socket another server bound concurrently is never replaced.
    • Close removes the socket path only if it still names our socket (inode check).
    • The staging name is shorter, and paths over the sun_path limit fail with an error naming the requested path, not the staging path.

Other

  • packages/gbash-wasm: the custom-command fast path is skipped for commands containing $, backtick, *, ?, [, CR or LF, so its simple tokenizer never disagrees with real shell expansion.
  • scripts/collect_fuzz_failure_artifacts.sh: it requires an argument or RUNNER_TEMP (${RUNNER_TEMP:?}) and refuses /. Before, with neither set, it ran rm -rf /fuzz-failure-artifacts.
  • docker/compat/Dockerfile: the Go tarball is checked against a pinned sha256 per version and arch (1.26.0 and 1.26.1; values matched on both go.dev and dl.google.com). It can be overridden with a GO_SHA256 build-arg.

For reviewers

  • CLI behavior change: gbash flags placed after -c SCRIPT are no longer parsed. This ordering is the vulnerability itself. The README and website quick-start examples (gbash -c 'echo hello' --json) and two cmd/gbash tests were updated to put the flags first. Outside scripts that use the old ordering will see the flag as $0 instead.
  • New go.mod Go versions: the compat image build fails until a checksum row is added to the Dockerfile (or GO_SHA256 is passed).
  • server.Config gains MaxSessions, MaxRequestBytes and MaxExecTimeout (zero means the default; negative disables the session and request caps).
  • SPEC updates: the CLI bullets near --json and the §6.2 server mode bullets cover the new guarantees, including the cross-user exposure of loopback TCP (prefer --socket on multi-user hosts).

Testing

  • go test -race ./server/... ./cli/... ./cmd/... passes, and make lint is clean.
  • make bats-test passes, including new tests for the fuzz-artifacts script.
  • The wasm shared.test.mjs passes. It ran through Node's built-in TypeScript support because the package's TS dependencies weren't installed locally.
  • Not run: a Docker image build.
  • In a full local go test ./..., these fail for reasons outside this change:
    • Oracle tests that need the Nix-pinned binaries.
    • A bench-compare test that uses macOS host bash.
    • Load-sensitive tail -f CLI tests. These flake at the same rate on the base commit.

Example of the flag fix:

$ gbash -c 'echo "0=$0 1=$1"' --root=/ --json
0=--root=/ 1=--json

…eadwrite root

Arguments after `-c command_string` are now always $0 and positional
parameters, as in bash, so --root/--json/--readwrite-root can no longer be
injected through positional args. --readwrite-root now mounts the
symlink-resolved path it validated, so swapping a symlink after startup
cannot redirect later server sessions.
- run session.exec under a per-connection context that is canceled when
  the client disconnects, and add Config.MaxExecTimeout
- retry temporary Accept errors with backoff and close live connections
  whenever Serve returns
- cap request size (Config.MaxRequestBytes) and session count
  (Config.MaxSessions)
- use 128-bit random session ids
- saturate huge timeout_ms values instead of overflowing
- add stdout_base64/stderr_base64 when output is not valid UTF-8
- never answer JSON-RPC notifications
- log internal errors and return a generic INTERNAL error
- hard-link the Unix socket into place so a concurrently bound socket is
  never replaced, only remove the socket path while it is still ours, and
  report sun_path length errors against the requested path
@ewhauser
ewhauser merged commit dc9e28e into main Oct 19, 2026
17 checks passed
@ewhauser
ewhauser deleted the claude/fix-server-cli-review-ec68b3 branch October 19, 2026 18:53
@ewhauser ewhauser mentioned this pull request Oct 20, 2026
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