A Claude Code hook can send risky install commands to a sandbox, not your laptop
A short script can intercept an agent's npm install or pip install and run it in an isolated machine, which a written rule in a project file cannot guarantee.
The short version
- A PreToolUse hook runs before each tool call and can deny it or rewrite its input.
- The sample script matches installs and test runs, then swaps in a command that runs in a sandbox.
- Uploads exclude .env files, private keys, cloud credentials, .git and anything gitignored.
- Commands the script does not list, file edits and Monitor commands still run locally.
- The author, Sandscale, sells the sandbox product the walkthrough uses.
The problem with asking nicely
Claude Code's Bash tool runs commands on the same machine the agent lives on. Most of the time that is fine. The worry, as dev.to's post from the Sandscale team lays out, is a small and predictable set of commands: npm install in a repository you just cloned, pip install from a requirements file in a pull request, or a test suite whose setup scripts nobody has read.
On a laptop, those commands run as you. Your SSH keys and cloud credentials are right there. The post notes that this is the same logic as the fake plugin story in today's lead: code you did not write runs with your permissions.
The usual answer is a line in a project instruction file such as CLAUDE.md saying installs and tests belong in a sandbox. The post's point is that this is only a request. Claude Code's own documentation, it says, treats those files as context for the model, not as enforced settings.
Why it matters
Installing packages from a project you just downloaded runs other people's code, and on a laptop that code runs as you, next to your keys. A hook turns "please use the sandbox" from a polite request into something the tool actually enforces. It is a narrow fix, but one you can add to a team project in an afternoon.
What a hook adds
A hook is a small program that Claude Code runs automatically at set moments. The one used here, called PreToolUse, fires just before the agent uses a tool. It receives the planned action as structured data and can answer in a few ways: allow it, deny it, ask the user, or defer. It can also replace the command with a different one before anything runs.
That last ability is what makes routing possible. The script reads the command Claude wants to run, decides whether it belongs somewhere safer, and hands back a rewritten command. The post also notes that deny and ask rules in your normal permission settings are still checked, whatever the hook returns.
How the sample setup works
First you create a sandbox for the session with the Sandscale command line tool and store its id in an environment variable before launching Claude Code. According to the CLI's help text, quoted in the post, the sandbox has networking off by default. An allowlist mode can open access to package registries, if your organization's policy and plan permit it. Installing dependencies needs that; tests that fetch nothing do not.
The hook script, saved in the project's .claude/hooks folder, does three things:
- It leaves a command alone if it already targets Sandscale, or if no sandbox id is set.
- It checks the command against a list covering npm, pnpm and yarn installs and tests, pip install and pytest.
- For a match, it swaps in a two-step command: upload the current project folder to the sandbox, then run the original command there and print the result.
The upload step matters for safety. Per the CLI help, the server leaves out files ignored by git, plus a platform blacklist that includes .env files, private keys, cloud credentials and the .git folder. Secrets in your working copy should not travel with the project.
You then register the hook in .claude/settings.json. Commit both files and everyone on the team gets the same routing.
Ask first, then allow
The sample hook answers "ask", so each rerouted command appears in the permission prompt in its rewritten form. For the first few days that is deliberate: you can see exactly what is uploaded and run. Once the team trusts it, switching to "allow" removes the prompt.
One detail trips people up. The post says permission rules are checked against the command the hook returns, not the one Claude first issued. So an allow rule has to name the sandscale sandbox command, not npm test.
If you prefer refusal to rewriting, the hook can deny the call and give a reason. Claude reads the reason and reissues the command the right way.
What it does not cover
The post is candid about the gaps, and they are worth reading closely:
- Unlisted commands. The pattern list is an allowlist of what to move. Anything that does not match still runs locally, so you extend the list as you notice more.
- Edits. Claude Code still changes files in your local working copy. Each upload sends the current state, so the sandbox sees recent changes, but the changes are made locally.
- Monitor commands. The hooks reference, as consulted on 1 December 2026, says hooks apply to shell commands started through the Monitor tool, but allow and input rewriting have no effect on them.
- The rest of the laptop. The hook shrinks what runs locally. It does not turn your laptop into a different machine.
Note also that the post is written by Sandscale, which sells the sandbox, and states it was written with AI help and reviewed by the team. The hook idea itself works with any isolated environment you can call from a command line.
The bigger step
The post's suggested next move is to stop splitting the work at all. Once a team runs several agents, it says, a simpler arrangement is to run Claude Code itself on its own remote machine, one workspace per agent, so commands, edits and installs all happen away from anyone's laptop. The hook still works there as an extra boundary for untrusted installs.
Either way, the vendor says each sandbox or workspace start, every command and every file upload is recorded in the organization's audit log. That record is what lets a team answer later where an agent ran something.
What to watch
- Whether teams that switch the hook from "ask" to "allow" keep the command list up to date as agents run new kinds of commands.
- Whether Claude Code closes the Monitor-command gap, where rewriting currently has no effect.
Try it
List the three or four commands you consider predictably risky, such as installs in a fresh clone. Then check whether your CLAUDE.md only requests a safe place to run them. If so, that is the gap a hook fills.
More stories
ClaudeFake Claude Code add-ons stole Windows logins, and one passed three security checks
ClaudeSachin Sharma open-sources Claude-BugHunter, a bug bounty toolkit for Claude Code
ClaudeAI agent Claude Code identifies hidden homelab failures
ClaudeEtherPK connects its markdown Kanban board to Claude Code through an MCP server
Get The Daily
AI news, explained in 5 minutes a day. Free. The first issue is coming soon.
No password. Unsubscribe any time. Privacy
Say why
Liked it? Say why
Add a line for other readers.
No account needed. We never show your email. Comments with links, ads or abuse are held and not shown. Your comment shows after a quick check.
Sources
The reporting and documents this story is built from. Each link opens the original.
- ReportingDEV Community: the article we sourced dev.to
Written with AI by the SI Agent News Desk from the reporting above. Every number is checked against it.Spot a mistake or a concern? Contact us · Corrections.