> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fibre.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Security and isolation

> What keeps one sandbox from another and from your account, how keys and secrets are handled, and the settings that make it safe to run untrusted and AI-generated code.

Sandboxes exist to run code that **you do not fully trust**: code a model wrote, files a user uploaded, packages an
agent decided to install. This page explains how Fibre keeps that code contained, and which settings you can use
to contain it even further. It ends with a short checklist for running untrusted code.

## What Fibre isolates

Fibre keeps several boundaries in place for you, without any setting:

| Boundary | What it means for you |
| - | - |
| **Sandbox from sandbox** | Each sandbox has its own processes, files, memory and network. Code in one cannot see, signal or connect to another, yours or anyone else's |
| **Sandbox from Fibre** | Code in a sandbox cannot reach Fibre's own systems, its API with your account's rights, or the machine underneath |
| **Runtime** | `standard` runs every sandbox behind an isolation layer of its own. `vm` gives each sandbox **its own kernel** in a hardware virtual machine |
| **Resources** | Each sandbox gets its size's CPU, memory and disk. One sandbox running away cannot starve the others |
| **Account from account** | Your sandboxes, images, snapshots and keys are visible to your account only |

## What you control

On top of Fibre's own boundaries, a few settings and habits make a big difference.

### Keys

* **Give each job its own key**, with only the [scopes](https://docs.fibre.dev/get-started/api-keys.md#scopes) it needs and the shortest
  lifetime that works. If one key leaks, only that job is affected.
* **Inside a sandbox, use a key bound to that sandbox** (with `resource`). An agent holding it can manage its own
  sandbox and nothing else, and the key stops working when the sandbox is deleted. See
  [a key for one sandbox](https://docs.fibre.dev/get-started/api-keys.md#a-key-for-one-sandbox).
* **Never put your account's main key in a sandbox.** Any program running there could read it.

### Secrets in sandboxes

Your agent may need a secret, such as a model API key or a database password. **Store it in Fibre** and give it to
the command that needs it by name: it reaches that command only, Fibre keeps it encrypted, and its value is never
shown again. A sandbox that names no secret gets none. See [secrets](https://docs.fibre.dev/production/secrets.md).

Avoid putting secrets in `env_vars` when you create a sandbox: those are visible to every command in it, and they are
copied into snapshots made from the sandbox.

> [!WARNING]
>
> **Keep in mind that anything code in a sandbox can read, it could also send somewhere.** If untrusted code has to
> work with a secret, limit its network (see below), or make the calls that need the secret from your own server, so
> the sandbox never sees it at all.

### Users inside a sandbox

Commands run as `root` inside the sandbox. That is root of the sandbox's own machine, which is isolated from Fibre's
hosts and from every other sandbox, so it can install packages and change any file in the sandbox, and nothing
beyond it.

Some programs refuse to run as root, and sometimes you want a tool to have less power than the agent that started
it. Create an ordinary user in the sandbox and run those commands as that user:

```bash title="CLI" run theme={null}
SBX=$(fibre create --lifetime 5m --label docs=security --json | jq -r .id)
fibre exec "$SBX" -- whoami                                         # root
fibre exec "$SBX" -- sh -c 'useradd -m app && su app -c whoami'      # app
fibre delete "$SBX"
```

```typescript title="TypeScript" run theme={null}
import { Sandbox } from "@fibre-dev/sdk";

const sbx = await Sandbox.create({ lifetime: 300, labels: { docs: "security" } });
console.log((await sbx.run("whoami")).stdout); // root
console.log((await sbx.run("useradd -m app && su app -c whoami")).stdout); // app
await sbx.delete();
```

```python title="Python" run theme={null}
from fibre import Sandbox

sbx = Sandbox.create(lifetime=300, labels={"docs": "security"})
print(sbx.run("whoami").stdout)  # root
print(sbx.run("useradd -m app && su app -c whoami").stdout)  # app
sbx.delete()
```

```bash title="curl" run theme={null}
SBX=$(curl -sS -X POST "$FIBRE_API_URL/v1/sandboxes" -H "Authorization: Bearer $FIBRE_API_KEY" \
  -H "Content-Type: application/json" -d '{"lifetime_sec": 300, "labels": {"docs": "security"}}' | jq -r .id)
curl -sS -X POST "$FIBRE_API_URL/v1/sandboxes/$SBX/exec" -H "Authorization: Bearer $FIBRE_API_KEY" \
  -H "Content-Type: application/json" -d '{"command": ["sh", "-c", "useradd -m app && su app -c whoami"]}'
curl -sS -o /dev/null -X DELETE "$FIBRE_API_URL/v1/sandboxes/$SBX" -H "Authorization: Bearer $FIBRE_API_KEY"
```

> [accordion] Response
>
> ```json title="response" expected theme={null}
> {
>   "exit_code": 0,
>   "stdout": "app\n"
> }
> ```

### Network

By default a sandbox can reach the whole internet. For untrusted code, **allow only the hosts it really needs**, or
none at all:

```json title="create body" theme={null}
{ "network": { "allow": ["pypi.org", "files.pythonhosted.org"] } }
```

[Network access](https://docs.fibre.dev/production/network.md) has a full example: code that can install packages but cannot send your
data anywhere else.

### Public URLs

A sandbox's [`url`](https://docs.fibre.dev/connect/public-urls) is public: anyone who has the address can reach whatever the sandbox
serves on its exposed port. Only serve what you mean to share, and add your own sign-in to anything private.

### Browser and desktop links

`cdp_url` gives full control of the sandbox's browser, so **treat it like a password** and keep it on your server.
`live_view_url` and desktop links only show the screen and pass on clicks, so they are safe to show a user.
Desktop links also work only once.

## How Fibre handles your data

This is how Fibre treats the sensitive things you give it:

| | |
| - | - |
| **API keys** | Shown once. Fibre stores only a digest; a lost key cannot be recovered, only replaced |
| **Registry logins** | Encrypted at rest, never returned, used only to pull your images |
| **Webhook secrets** | Shown once; every delivery is signed so you can verify it came from Fibre |
| **A sandbox's files and memory** | Kept while it exists, including while it sleeps |
| **After delete** | The sandbox, its files and its saved memory are gone and cannot be restored. Images and snapshots you made stay until you delete them |

## A checklist for untrusted code

When you run code you do not trust, follow these steps:

1. **Use a fresh sandbox for each job**, and delete it when the job is done. Starting from a
   [snapshot](https://docs.fibre.dev/setup/snapshots.md) keeps this fast.
2. **Set a network allow list**, or `{"block": true}` if the code needs no network at all.
3. **Keep long-lived secrets out of the sandbox.** If the code must call an API, give it a short-lived key with
   narrow scopes.
4. **Give the sandbox a short lifetime**, so that a forgotten sandbox ends by itself.
5. **Use the `standard` runtime** unless the job needs Docker or another kernel feature. Both runtimes are
   isolated; `vm` adds a kernel of its own.
6. **Take results out through the files API**, and check them before your app uses them.

## Related

> [card] [Network access](https://docs.fibre.dev/production/network.md)
>
> Allow lists and fully offline sandboxes.

> [card] [API keys](https://docs.fibre.dev/get-started/api-keys.md)
>
> Scoped, bound and expiring keys.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.