Network rules¶
The network option of a task gives the network rules of its sandbox, as a
Network. They are those of the
sandbox spec the sandbox of each run is
launched from, scoped to that sandbox: they add to the policy of the daemon for
this sandbox alone, and go away with it. This page shows how to build them and
what they change.
Allowing and refusing hosts¶
from fastsandbox import Context, FastSandbox, Network
PYPI = Network.allow("pypi.org", "files.pythonhosted.org")
GITHUB_GET = Network.allow_http("api.github.com", methods=["GET"])
app = FastSandbox("network")
@app.task(kit="shell", network=PYPI | GITHUB_GET | Network.deny("example.org"))
async def probe(ctx: Context, url: str) -> str:
"""The HTTP status a URL answers from inside the sandbox."""
output = await ctx.sandbox.exec(
["curl", "-sS", "-m", "10", "-o", "/dev/null", "-w", "%{http_code}", url]
)
return output.stdout
| Rule | What it does |
|---|---|
Network.allow(*hosts) |
Allows connections to the hosts, from the creation of the sandbox. |
Network.deny(*hosts) |
Refuses connections to the hosts, whatever else allows them, from the creation. |
Network.allow_http(*hosts, methods, path="/**") |
Allows the HTTP requests of methods (GET, …) to the path pattern path on the hosts, from the start of the sandbox. |
Network.deny_http(*hosts, methods, path="/**") |
Refuses those requests, likewise. |
Each takes host patterns. | combines rules into one Network: a Network is
a value, which can be named once, as PYPI here, and shared by several tasks.
It holds its rules as a spec does, allowed, denied, http_allowed and
http_denied, and apply_to(spec) gives them
to a SandboxSpec, for a sandbox launched with the
client.
- The host rules are a mixin of the kit of the sandbox,
/kits/docker-sandbox-network: the daemon creates them with the sandbox, and applies them from its creation. - The HTTP rules are applied before the sandbox starts: its startup commands run with them, its install commands, run at its creation, without them. Each names its methods.
A host is a host pattern, example.com, *.example.com or example.com:443
for instance (see
The hosts a rule names). A URL,
a host with a path or a bare * fails the run with
InvalidSpecError before anything is
created, as does a refusal that covers the host of a key of the agent.
A refused host is answered by the proxy of the sandbox in place of the host:
probe then gets the status of the proxy, not an error of the connection.
What the rules change¶
The rules of a task add to the global policy of the daemon, and a refusal
wins over any allowance. The global policy of sbx may allow every host,
under its Open preset: an allowance then restricts nothing, and a refusal is
what keeps a sandbox from a host (see the
network model). A run whose task allows hosts while
the global policy allows every host logs a warning saying so, on the logger of
the task, fastsandbox.task.<name>. GITHUB_GET above allows GET requests to the
GitHub API, and refuses no other method: that takes
Network.deny_http("api.github.com", methods=[...]), naming the methods to
refuse. For an unattended task, and above all for an
agent, refuse what it must not reach, and read what it reached with the
activity log of the policy before the task returns (see
Network and policy).
The rules are scoped to the sandbox of the run: two runs of the same task, or a
task and the rest of the machine, do not see each other's rules. An HTTP rule
the daemon refuses makes the run raise
LaunchError (step="network") before
the task is called, and the sandbox is removed.
For an agent task, the hosts the agent needs are those its kit declares: see Agents.