Skip to content

Restrict network access

Every connection a sandbox opens goes through a proxy that applies the network policy of the daemon. An agent task adds rules of its own, for its sandbox alone. This page takes you through the three steps that make the network of an agent what you mean it to be: know the baseline, declare what the agent needs, read what it reached.

1. Know the baseline

The rules of a task add to the global policy of the daemon: what the sandbox reaches without them is what that policy allows, and under the Open preset of sbx that is every host. An allow rule does not block the rest; a deny rule does block its host. The network model explains how the rules combine.

Know the baseline before you rely on a rule: sbx policy ls lists the global rules, and client.policy.check(NetworkCheck.host("example.org:443")) says what the global policy decides for a host, and which rule decides. The check ignores the rules of a sandbox and of its kits: it shows the baseline, not what a given sandbox reaches (see Network and policy).

2. Declare what it needs

docs_src/agents/network_audit.py
from pathlib import Path
from typing import Annotated

from pydantic import BaseModel

from fastsandbox import Context, FastSandbox, Network, WorkspaceDir

app = FastSandbox("upgrades")

# What the agent needs besides its own service: the package index, and `GET`
# requests to the GitHub API (an allowance: it refuses no other method).
NEEDS = Network.allow("pypi.org", "files.pythonhosted.org") | Network.allow_http(
    "api.github.com", methods=["GET"]
)


class Outcome(BaseModel):
    ok: bool
    reached: list[str]
    refused: list[str]


@app.agent("claude", network=NEEDS | Network.deny("example.org"), timeout="20m")
async def upgrade(
    ctx: Context, repo: Annotated[Path, WorkspaceDir(clone=True)], package: str
) -> Outcome:
    """Has Claude Code upgrade a dependency, then reports what the sandbox reached."""
    run = await ctx.agent.prompt(
        f"Upgrade {package} to its latest version and fix what breaks.",
        working_dir=str(repo.resolve()),
    )
    # The activity log of the policy, narrowed to the sandbox of this run.
    log = (await ctx.client.policy.log()).for_sandbox(ctx.sandbox.name)
    return Outcome(
        ok=run.ok,
        reached=[host.host for host in log.allowed_hosts],
        refused=[host.host for host in log.blocked_hosts],
    )
  • Lines 12 to 14: the hosts the agent needs for this task, the package index and GET requests to the GitHub API, named once as a Network value that other tasks can share. They are allowances: the other methods on the GitHub API are not refused, which takes a refusal of its own, Network.deny_http("api.github.com", methods=["POST", "PUT", "PATCH", "DELETE"]). They confine the agent only where the global policy denies the rest, under the Balanced or Locked Down preset; under Open they change nothing, and the run says so in a warning on the logger of the task, as in ctx.lease.report.network.warning.
  • Line 23: the rules of the task, combined with |: what it needs, and a host to keep out.
  • The hosts of the agent's own service, api.anthropic.com for Claude Code, are those its kit declares, allowed from the creation of the sandbox. A refusal of the task that covers the host a key of the agent goes to fails the run before anything is created: it would cut the agent off.
  • While the policy of the daemon allows every host, as the default policy of sbx may, the allowances restrict nothing, and the refusal is what keeps the agent out of example.org.
  • The rules are scoped to the sandbox of the run and go with it: two runs, or a run and the rest of the machine, do not see each other's rules.

The four kinds of rule, allow, deny, allow_http and deny_http, are on Network rules.

3. Read what it reached

Lines 32 to 38: after the agent, the task reads the activity log of the policy, narrowed to its sandbox, and returns the hosts it reached and the hosts it was refused along with its outcome. The command line prints it as JSON, for instance:

$ fastsandbox run docs_src/agents/network_audit.py upgrade --repo . --package httpx
{
  "ok": true,
  "reached": [
    "api.anthropic.com:443",
    "pypi.org:443",
    "files.pythonhosted.org:443"
  ],
  "refused": []
}

Read it before you trust the work: a host you did not expect is worth a look, and a refused host often explains a failure. A refused host is answered by the proxy, with HTTP 403, in place of the host.

For every agent

Deny what an unattended agent must not reach, check the global policy, and read the log while the sandbox exists. To confine an agent to a list of hosts, the global policy must deny the rest: the Balanced or Locked Down preset of sbx. The security model lists the other boundaries of a sandbox.