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

# Limit where a machine can connect

> Block a machine's outbound connections, or allow only the domains and address ranges you list.

A network policy limits where a machine may connect to on the internet. Without a policy, a machine
can reach any public address. With one, it reaches only the domains and address ranges that you
list, or nothing at all. Use a policy when a machine runs code you do not fully trust, such as an
agent or a build, and you want it to reach GitHub and your own API and nothing else.

```bash theme={null}
instabox create --name ci \
  --allow-domain github.com \
  --allow-domain '*.githubusercontent.com'
```

A policy limits only what the machine sends out. SSH, `exec`, files, the terminal, the desktop, and
published ports into the machine work as before.

## Block all outbound connections

Pass `--block-outbound` when a machine needs no internet access. The machine then connects to
nothing, and no name resolves in it.

```bash theme={null}
instabox create --name sealed --block-outbound
```

Software that the machine downloads at start, such as packages from `apt` or `npm`, fails under this
policy. Install what you need first, save a [snapshot](https://instabox.sh/docs/machines/snapshots.md), and create the sealed
machine from that snapshot.

## Allow only listed domains

Pass `--allow-domain` once for each name. The machine then connects only to those names, over any
protocol and port, so HTTPS and `git clone git@github.com:...` over SSH both work.

```bash theme={null}
instabox create --name ci \
  --allow-domain github.com,api.example.com
```

A name is matched in one of two ways:

| You list | The machine can reach |
| - | - |
| `example.com` | `example.com` only, and not `www.example.com` |
| `*.example.com` | Every name under `example.com`, at any depth, and not `example.com` itself |

Quote a name with `*` so that your shell does not expand it. A name that you did not list does not
resolve in the machine, so a program gets "could not resolve host" at once and does not wait for a
timeout.

## Allow address ranges

Pass `--allow-cidr` for a destination that you know by address, such as a database with a fixed IP.
You can combine ranges with domains.

```bash theme={null}
instabox create --name worker \
  --allow-domain api.example.com \
  --allow-cidr 203.0.113.0/24
```

A range is IPv4 or IPv6, and one address without a length is that address alone. A connection to an
address that no range lists, and that no allowed name resolved to, is refused.

## Change the policy on a running machine

`instabox network` shows a machine's policy, and with a flag it replaces the policy as a whole.

```bash theme={null}
instabox network ci
```

```
ci: outbound restricted to domains github.com, *.githubusercontent.com
```

The new policy takes effect at once, without a restart. Connections that it no longer allows are
closed, and connections that it still allows continue.

```bash theme={null}
instabox network ci --block-outbound
```

To remove the policy, so that the machine connects anywhere again, pass `--open-outbound`.

```bash theme={null}
instabox network ci --open-outbound
```

In the dashboard, choose **Outbound network** when you create a machine, or change it under
**Settings** in the machine's sheet. The choices are **Open**, **Allowlist**, and **Blocked**.

## Keep the policy across pause, stop, and fork

A machine keeps its policy when you pause and resume it, and when you stop and start it. A
[fork](https://instabox.sh/docs/machines/fork.md) starts with its source's policy. To give the forks their own policy, pass the
same flags to `instabox fork`.

```bash theme={null}
instabox fork ci --name ci-sealed --block-outbound
```

A machine that you create from a snapshot does not take the policy of the machine the snapshot came
from. Pass the flags again on that `create`.

## Set a policy with the API

Send `networkPolicy` when you create a machine. `outbound: "restricted"` with both lists empty blocks
everything.

```bash theme={null}
curl -X POST https://api.instabox.sh/v1/machines \
  -H "X-API-Key: $INSTABOX_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Content-Type: application/json" \
  -d '{"name": "ci", "networkPolicy": {"outbound": "restricted",
       "allowDomains": ["github.com"], "allowCidrs": ["203.0.113.0/24"]}}'
```

`PATCH /v1/machines/{id}` with `networkPolicy` replaces the policy, and `outbound: "open"` removes
it. A machine without a policy has no `networkPolicy` field. The fields are in the
[API reference](https://instabox.sh/docs/api-reference/introduction.md).

## How a domain allowlist works

Every machine resolves names through InstaBox's resolver. Under a policy, the resolver answers only
the names that you listed. When it answers one, it opens the machine's firewall for the addresses in
the answer, for as long as the DNS record is valid (at least 60 seconds, at most 1 hour) and 30
seconds more. Every other address stays closed.

This works for any protocol, because the rule is on the address and not on the port. It also means
that the policy follows a name when its addresses change.

## What a domain allowlist does not guarantee

> [!WARNING]
>
> A domain allowlist is not strict data-loss prevention. It stops software from connecting to places
> that you did not list. It does not stop someone who controls both the code in the machine and a
> destination that you allowed.

* **Shared addresses.** Many sites share one address behind a CDN or a cloud load balancer. While an
  allowed name's address is open, the machine can reach every other site on that address too.
  InstaBox does not check the name inside the TLS handshake (SNI).
* **An address stays allowed until its entry expires.** After a name's DNS record changes, the old
  address stays reachable until the entry for it runs out, at most 1 hour and 30 seconds later.
* **An allowed service can relay anything.** If you allow `github.com`, the machine can push to any
  repository there.
* **Only InstaBox's resolver opens the firewall.** A program must look names up with ordinary DNS,
  which InstaBox answers. A program that uses DNS over HTTPS or DNS over TLS gets no answer that
  opens the firewall, so its connections are refused.
* **Everything inside a listed range is reachable**, under any name.

To send only to exact destinations, list address ranges that you control and no domains.

## Limits

> [accordion] How many domains and ranges
>
> A policy takes up to 100 domains and up to 50 address ranges. A machine can have up to 4096
> resolved IPv4 addresses and 4096 resolved IPv6 addresses open at one time. When that set is full,
> a new name does not resolve until older entries expire.

> [accordion] Names and ranges that are refused
>
> * A domain is a name of at least two labels, such as `example.com`, in lower case. A scheme, a
>   port, a path, or an IP address in `--allow-domain` is refused.
> * A range cannot be the whole address space (`0.0.0.0/0`). To allow everything, remove the policy.
> * A range inside private, loopback, link-local, or reserved space, such as `10.0.0.0/8`, is
>   refused. A machine cannot reach those addresses with or without a policy.
> * A range with bits set beyond its length, such as `140.82.112.9/20`, is refused, and the error
>   names the range that you probably meant.

> [accordion] Which machines can have a policy
>
> Linux machines on every plan. A macOS, Windows, or GPU machine is refused with `400`, and nothing
> is created or changed.
>
> A policy is set for one machine. There is no default for a workspace or a template.

> [accordion] What still applies under a policy
>
> A policy only narrows what a machine can reach. A destination that InstaBox blocks for every
> machine stays blocked when your policy lists it. The connection and bandwidth limits on
> [Limits](https://instabox.sh/docs/account/limits.md) apply to allowed traffic.
>
> With [Org Secrets](https://instabox.sh/docs/security/secrets.md), the outbound proxy adds credentials only to connections
> that the policy allows.

> [accordion] Errors
>
> * `400 bad_request`: a name or a range is not valid. The message names it.
> * `409 operation_conflict`: the machine has an operation in progress. Change the policy when it
>   is done.
> * `503 host_unavailable`: the machine's host did not take the policy. The earlier policy stays
>   in force. Try again.

> [card] [Org Secrets](https://instabox.sh/docs/security/secrets.md)
>
> Keep API keys out of the machine and add them to allowed requests.

> [card] [CLI reference](https://instabox.sh/docs/reference/cli.md#network)
>
> Every flag of `instabox network`.
