Open History to see the last commit
1---
2title: Policy
3id: policy
4order: 7
5description: "Set allow, ask, and deny rules for the commands and capabilities the in-sandbox agent can use. Write them once, and each harness maps them to its own permissions."
6---
7
8A policy allows a command, asks for approval, or blocks it. Use it to stop `sudo`, a repo delete, or a call to an unknown host.
9
10A policy holds those rules. `defineSandboxPolicy()` writes them once, and each harness adapter maps them to its own permission system.
11
12## Add a policy
13
141. Write the rules.
152. Pass them as `policy` to `defineSandbox`.
16
17```ts
18import {
19 defineSandbox,
20 defineSandboxPolicy,
21 defineWorkspace,
22 githubRepo,
23} from '@tanstack/ai-sandbox'
24import { dockerSandbox } from '@tanstack/ai-sandbox-docker'
25
26const policy = defineSandboxPolicy({
27 commands: {
28 allow: ['pnpm test', 'pnpm typecheck', 'git diff'],
29 ask: ['pnpm install', 'curl *'],
30 deny: ['sudo *', 'rm -rf *'],
31 },
32 capabilities: { fileWrite: 'allow', network: 'ask' },
33 default: 'ask',
34})
35
36export const sandbox = defineSandbox({
37 id: 'repo-agent',
38 provider: dockerSandbox({ image: 'node:22' }),
39 workspace: defineWorkspace({
40 source: githubRepo({ repo: 'owner/app' }),
41 setup: ['corepack enable', 'pnpm install'],
42 }),
43 policy,
44})
45```
46
47Every run that uses this sandbox now follows the policy, including the bridged [tools](./tools). The policy does not check the [workspace](./workspace) `setup` commands. They come from your own config and run as written.
48
49## Decisions
50
51Each command or capability gets one of three decisions:
52
53| Decision | What happens |
54| --- | --- |
55| `allow` | The agent runs it. |
56| `ask` | The agent pauses. The harness sends an approval request, and the client answers it before the action continues. |
57| `deny` | The action is blocked. The agent cannot run it. |
58
59## Commands
60
61`commands` has three lists of patterns: `allow`, `ask`, and `deny`. A pattern matches the command that the agent is about to run.
62
63- `*` is a glob. `curl *` matches every `curl` command. `sudo *` matches everything run through `sudo`.
64- A string with no `*`, such as `pnpm test`, matches only that command.
65
66Put your named [workspace scripts](./workspace#scripts) (`pnpm test`, `pnpm build`) in `allow`. They give the policy stable names to match.
67
68## Capabilities
69
70`capabilities` uses the same decisions for broad abilities:
71
72```ts
73import { defineSandboxPolicy } from '@tanstack/ai-sandbox'
74
75const policy = defineSandboxPolicy({
76 capabilities: {
77 fileWrite: 'allow', // the agent can edit the working tree
78 network: 'ask', // pause for approval before any outbound request
79 },
80})
81```
82
83This is the backstop. A network command that is not in your `commands` lists still needs approval under `network: 'ask'`.
84
85If you omit a capability, it uses the policy `default`. Some providers also enforce `network: 'deny'` when they create the sandbox. See [Providers (Advanced)](./providers-advanced).
86
87## Precedence: deny > ask > allow
88
89When more than one rule matches, the strictest one wins:
90
911. If a matching rule says `deny`, the action is blocked.
922. Else, if a matching rule says `ask`, the action needs approval.
933. Else, if a rule says `allow`, the action runs.
944. If nothing matches, the `default` applies.
95
96So you can allow a broad family and block one case:
97
98```ts
99import { defineSandboxPolicy } from '@tanstack/ai-sandbox'
100
101const policy = defineSandboxPolicy({
102 commands: {
103 // Every `curl` is allowed...
104 allow: ['curl *'],
105 // ...but `deny` wins, so this host is always blocked.
106 deny: ['curl * internal.example.com*'],
107 },
108 default: 'deny',
109})
110```
111
112## The default
113
114`default` is the decision for anything that no rule matches. If you omit it, it is `'ask'`.
115
116- `'allow'`: only what you `ask` about or `deny` is gated. Good for trusted dev loops.
117- `'ask'`: unknown actions pause for approval. A safe middle.
118- `'deny'`: the agent runs only what you `allow`. The strictest choice, for untrusted or production runs.
119
120## How `ask` reaches the user
121
122When the agent tries an `ask` action, the harness pauses it and sends an approval request into the run stream. Your client approves or rejects it. Until the client answers, the action waits.
123
124Use `ask` for actions that are usually fine but sometimes dangerous, such as `pnpm install` with a new dependency, or an outbound `curl`. The human stays in the loop, and you do not have to list every safe command.
125
126## Advanced
127
128### How adapters map a policy
129
130Each harness adapter translates the same policy into its own permissions:
131
132- Grok Build: the permission flags of the `grok` CLI.
133- Claude Code: its permission rules (allowed, ask, and denied tool and command rules).
134- Codex: its approval and sandbox settings.
135- Other harnesses: whatever native gate they have.
136
137If a harness cannot express a rule, it skips that rule with a warning. The run does not fail.
138
139A deny-only list with `default: 'allow'` stays permissive on Grok Build and Codex. Those harnesses keep auto-approve and do not enforce `commands.deny`. The outer sandbox is the isolation there. If you need a deny rule for each command, use Claude Code.
140
141Some providers run as a non-root user, so package installs in `setup` need `sudo`. Do not deny `sudo *` on those providers. See [Providers (Advanced)](./providers-advanced) for the list, and [Workspace](./workspace) for how to write setup commands.
142
143### Network policy on Docker Sandboxes
144
145Most providers leave `capabilities.network` to the harness. `sbxSandbox()` also enforces it on the host HTTP and HTTPS proxy (`networkPolicy: true`).
146
147`sbx policy` can allow or deny hosts. It has no `ask`. Deny wins over allow, so `network: 'deny'` becomes an allowlist, not a full deny.
148
149| TanStack `capabilities.network` | What `sbxSandbox()` writes |
150| --- | --- |
151| No policy, and no `allowNetwork` or `denyNetwork` | If the policy list is empty, `sbxSandbox()` runs `sbx policy init deny-all`. A known adapter (`grok-build`, `claude-code`, `codex`) then allows its model API host and `localhost` for this sandbox. Unknown adapters stay on the machine preset. |
152| No policy, and `allowNetwork` | Allow the model API host (for a known adapter), `localhost`, and `allowNetwork` for this sandbox. Then apply `denyNetwork`. |
153| No policy, and `denyNetwork` only | Deny those hosts for this sandbox. Allow stays empty: no automatic hosts and no `localhost`. This adds deny rules on top of the machine preset. |
154| `allow` | Allow `**`, then apply `denyNetwork`. |
155| `deny` | Allow the model API host, `localhost`, and `allowNetwork`. Then apply `denyNetwork`. |
156| `ask` (or the policy `default` when `network` is unset) | The same allowlist as `deny`. The harness still asks for tools and commands. |
157
158Model hosts that are allowed automatically:
159
160- `grok-build`: `api.x.ai`
161- `claude-code`: `api.anthropic.com`
162- `codex`: `api.openai.com`
163- `opencode` or an unknown adapter: none
164
165If the allowlist is empty under `deny` or `ask`, create throws. Pass `allowNetwork`, or use `grokBuildText`, `claudeCodeText`, or `codexText`.
166
167The guest calls `host.docker.internal` for the tool bridge. The `sbx` proxy changes that host to `localhost` before the policy match. `sbxSandbox()` adds `localhost` when it writes a real allowlist. It does not add it for `denyNetwork` only.
168
169```ts
170import { sbxSandbox } from '@tanstack/ai-sandbox-docker'
171import { defineSandbox, defineSandboxPolicy } from '@tanstack/ai-sandbox'
172
173const sandbox = defineSandbox({
174 id: 'repo-agent',
175 provider: sbxSandbox({
176 allowNetwork: ['*.npmjs.org', 'registry.npmjs.org'],
177 }),
178 policy: defineSandboxPolicy({
179 capabilities: { network: 'deny' },
180 commands: { allow: ['pnpm test'], deny: ['sudo *'] },
181 }),
182})
183```
184
185Command rules and `fileWrite` stay with the harness, because `sbx` does not see command lines.