Security settings: network, secrets and timeouts
A sandbox isolates code from your systems, not from the internet. For model-written code, turn outbound access off or allow only what is needed.
sandbox = Sandbox.create(allow_internet_access=False)
sandbox = Sandbox.create(network={
"deny_out": lambda ctx: [ctx.all_traffic],
"allow_out": ["api.your-company.example"],
})from e2b.sandbox.sandbox_api import SandboxNetworkOpts
for field in SandboxNetworkOpts.__annotations__:
print(field)The network options in this SDK version. allow_internet_access=False is the same as denying all outbound traffic. With deny_out set to all traffic, allow_out opens specific addresses or domains. Without it, code that reads a customer's data could post that data anywhere.
Secrets
envs on create or run_code sets environment variables inside the sandbox, and anything the code can read, a prompt-injected model can print. Do not pass database passwords or provider keys in. Give the sandbox the data it needs as files, lesson 6, or use E2B's secrets feature, where a proxy outside the sandbox adds a stored credential to allowed requests without the code ever seeing it.
A checklist for model-written code
- Outbound network off, or an allow list.
- No credentials in
envs. - A short
timeouton everyrun_code, and a sandboxtimeout. - One sandbox per user or task, killed when done, so one customer's data never sits in another's sandbox.
- Output size limits before anything goes back to the model.
- Print
SandboxNetworkOpts.__annotations__["rules"]. - Find
ALL_TRAFFICin the SDK. - Write down which hosts your own agent's code would need.
You understood something today that you didn't yesterday.