{"slug": "claude-code-hooks-that-send-the-agent-s-bash-commands-to-its-own-machine-not", "title": "Claude Code hooks that send the agent's Bash commands to its own machine, not your laptop", "summary": "A developer published a Claude Code PreToolUse hook that intercepts Bash commands matching installs and test runs (npm, pnpm, yarn, pip, pytest) and rewrites them via updatedInput to execute on a Sandscale Firecracker microVM sandbox instead of the local laptop. The hook uploads the working directory to the sandbox, which excludes .gitignore'd files and platform deny-listed secrets such as .env files, private keys and cloud credentials, and sets permissionDecision to \"ask\" so the user confirms each rerouted command. The author argues instruction files like CLAUDE.md are context rather than enforced configuration, while hooks can deny or modify tool calls before they run.", "body_md": "Claude Code runs its Bash tool on the machine it is running on. For most commands that is fine. The ones that make teams nervous are a small, predictable set: `npm install` in a repository someone just cloned, `pip install -r requirements.txt` from a pull request, a test suite whose setup scripts nobody has read. On a laptop, those run as you, next to your SSH keys and cloud credentials.\n\nYou can write a rule in `CLAUDE.md` or `AGENTS.md` that says \"run installs and tests on a sandbox\". We covered that approach for Cursor in [a project rule that sends installs and tests to a sandbox](https://sandscale.dev/blog/cursor-rule-run-tests-in-a-sandbox?ref=devto-claude-code-hooks-route-bash-to-a-sandbox). The weakness of a rule is that it is a request. Claude Code's own documentation says instruction files are context, not enforced configuration.\n\nHooks are different. A `PreToolUse` hook is a command Claude Code runs before a tool call, and it can deny the call or change its input. This post shows a hook that catches the Bash commands you choose and rewrites them to run on a [Sandscale sandbox](https://sandscale.dev/sandboxes?ref=devto-claude-code-hooks-route-bash-to-a-sandbox), the agent's own Firecracker microVM, using the Sandscale CLI (1.4.1). The hook fields come from Claude Code's [hooks reference](https://code.claude.com/docs/en/hooks), read on 1 December 2026.\n\nFrom the hooks reference:\n\n`matcher` (here `Bash`) and a command to run. The command gets the tool call as JSON on standard input, so `jq -r '.tool_input.command'` gives you the shell command Claude wants to run.`hookSpecificOutput`. `permissionDecision` can be `allow`, `deny`, `ask` or `defer`. `updatedInput` replaces the tool's input before it runs.\nThat is enough to route a command: read it, decide whether it should run elsewhere, and hand back a different command.\n\nCreate one sandbox for the session and keep its id in the environment Claude Code starts from:\n\n```\nsandscale sandbox create --template-id builtin:node\nexport SANDSCALE_SANDBOX_ID=SANDBOX_ID\nclaude\n```\n\n`create` answers with the sandbox id. The CLI's help says network is off by default (`--network none`), and that `allowlist` reaches only your organization's egress allowlist and package registry presets, where your organization's policy and plan allow it. An install needs that; a test suite that does not fetch anything does not.\n\nSave this as `.claude/hooks/route-to-sandbox.sh` and make it executable:\n\n``` bash\n#!/bin/bash\n# Rewrites installs and test runs so they run on the agent's sandbox.\nCMD=$(jq -r '.tool_input.command')\n\n# Already going to Sandscale, or no sandbox for this session: leave it alone.\ncase \"$CMD\" in sandscale\\ *) exit 0 ;; esac\n[ -z \"$SANDSCALE_SANDBOX_ID\" ] && exit 0\n\nif echo \"$CMD\" | grep -Eq '^(npm (install|ci|test|run test)|pnpm (install|test)|yarn (install|test)|pip install|pytest)( |$)'; then\n  NEW=\"sandscale sandbox instances-project --id $SANDSCALE_SANDBOX_ID --dir /workspace/project --body @. --yes && sandscale sandbox run $SANDSCALE_SANDBOX_ID --workdir /workspace/project --yes $(printf '%q' \"$CMD\")\"\n  jq -n --arg cmd \"$NEW\" '{\n    hookSpecificOutput: {\n      hookEventName: \"PreToolUse\",\n      permissionDecision: \"ask\",\n      permissionDecisionReason: \"Routed to the agent sandbox\",\n      updatedInput: { command: $cmd }\n    }\n  }'\nfi\nexit 0\n```\n\nThe rewritten command does two things. `sandbox instances-project` uploads the current folder into the sandbox (`--body @.` sends the folder as an archive), and `sandbox run` runs the original command there, in that folder, and prints its exit code, output and errors. The help lists what the upload leaves out on the server side: files your `.gitignore` ignores, and files on the platform deny-list, including `.env` files, private keys, cloud credentials and `.git`. So the secrets in your working copy do not travel with it.\n\nThen register the hook in `.claude/settings.json`:\n\n```\n{\n  \"hooks\": {\n    \"PreToolUse\": [\n      {\n        \"matcher\": \"Bash\",\n        \"hooks\": [\n          { \"type\": \"command\", \"command\": \"${CLAUDE_PROJECT_DIR}/.claude/hooks/route-to-sandbox.sh\" }\n        ]\n      }\n    ]\n  }\n}\n```\n\nCommit both files, and everyone on the team gets the same routing.\n\nThe hook returns `ask`, so each routed command shows up in the permission prompt in its rewritten form. That is deliberate for the first days: you see exactly what is uploaded and run. When the team trusts the pattern, change `ask` to `allow` and routed commands run without a prompt. The hooks reference notes that permission rules are evaluated against the input the hook returns, not the input Claude sent, so write any allow rules for `sandscale sandbox` commands, not for `npm test`.\n\nIf you would rather refuse than rewrite, return `deny` with a reason such as \"Run installs with sandscale sandbox run on the session sandbox\". Claude reads the reason and can issue the command itself.\n\nThe hook moves the riskiest commands. The simpler arrangement, once a team runs several agents, is to run Claude Code itself on its own machine: a [workspace](https://sandscale.dev/workspaces?ref=devto-claude-code-hooks-route-bash-to-a-sandbox) per agent, where every command, edit and install happens away from anyone's laptop.\n\n```\nsandscale workspace create --name agent-1 --template-id TEMPLATE_ID\nsandscale workspace ssh agent-1\n```\n\nThe same hook still works there if you want a second boundary for untrusted installs. Either way, every sandbox and workspace start, every command run and every file upload lands in your organization's [audit log](https://sandscale.dev/docs/concepts/audit?ref=devto-claude-code-hooks-route-bash-to-a-sandbox).\n\nTo set up the CLI and connect Claude Code, start with the [Quickstart](https://sandscale.dev/docs/quickstart?ref=devto-claude-code-hooks-route-bash-to-a-sandbox).\n\n*Originally published on the [Sandscale blog](https://sandscale.dev/blog/claude-code-hooks-route-bash-to-a-sandbox?ref=devto-claude-code-hooks-route-bash-to-a-sandbox).*", "url": "https://wpnews.pro/news/claude-code-hooks-that-send-the-agent-s-bash-commands-to-its-own-machine-not", "canonical_source": "https://dev.to/sandscale/claude-code-hooks-that-send-the-agents-bash-commands-to-its-own-machine-not-your-laptop-5bbb", "published_at": "2026-12-01 13:13:30+00:00", "updated_at": "2026-12-01 13:15:33.347560+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "ai-safety"], "entities": ["Claude Code", "Anthropic", "Sandscale", "Firecracker", "Cursor"], "also_reported_by": [], "takeaway": "A PreToolUse hook can match Bash commands with a regex and return updatedInput to rewrite installs and test runs into sandscale sandbox run calls, keeping secrets out of the sandbox because uploads skip .gitignore'd files and deny-listed items like .env and private keys.", "recommendation": {"advice": "Route risky Bash commands like npm install and pytest through a PreToolUse hook that rewrites them to run in an isolated sandbox, and rely on upload filtering to keep .env files and credentials off the agent's machine.", "playbook": "agent-security", "playbook_url": "https://wpnews.pro/playbooks/agent-security"}, "alternates": {"html": "https://wpnews.pro/news/claude-code-hooks-that-send-the-agent-s-bash-commands-to-its-own-machine-not", "markdown": "https://wpnews.pro/news/claude-code-hooks-that-send-the-agent-s-bash-commands-to-its-own-machine-not.md", "text": "https://wpnews.pro/news/claude-code-hooks-that-send-the-agent-s-bash-commands-to-its-own-machine-not.txt", "jsonld": "https://wpnews.pro/news/claude-code-hooks-that-send-the-agent-s-bash-commands-to-its-own-machine-not.jsonld"}}