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

# Firewall rules and outbound traffic

> Add firewall rules in a sandbox, open raw ports, and block outbound internet access without breaking boat.

boat has no API setting for network rules. You have root in the sandbox, so you add rules with `ufw` or `iptables`. For the full security picture, read [Security and isolation](https://docs.boat.dev/security.md).

> [!WARNING]
>
> Snapshots do not keep firewall rules. Apply your rules again after each resume and fork. For example, run them from your setup script or from a systemd service.

## What boat needs

boat reaches the sandbox in two ways. A rule that blocks either one marks the sandbox degraded.

| What | Protocol | Runs as |
| - | - | - |
| boat agent (commands, snapshots, health) | Inbound TCP port 8911 | `user` |
| Tunnel for public URLs and the agent | WireGuard, outgoing UDP to the address in `sudo wg show wg0 endpoints` | kernel, but carries packets from `user` processes |

Linux tags a tunnel packet with the process that sent the data inside it. So a rule on the `user` account also blocks the tunnel. Do not block all outbound traffic for `user`.

## Block outbound internet for untrusted code

Run the untrusted code as a separate Linux account. Block that account only. boat keeps working.

```bash theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
sudo useradd -m runner
for t in iptables ip6tables; do
  sudo $t -I OUTPUT 1 -m owner --uid-owner runner -j REJECT
  sudo $t -I OUTPUT 1 -m owner --uid-owner runner -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
  sudo $t -I OUTPUT 1 -m owner --uid-owner runner -o lo -j ACCEPT
done

sudo -u runner curl -m 5 https://example.com   # fails: "Couldn't connect to server"
```

* The `runner` account can still reach services in the sandbox on `localhost`.
* Insert the rules at the top (`-I OUTPUT 1`). Rules added at the end come after boat's own `ufw` rules, which accept new connections.
* Do not give `runner` sudo. With root, it can delete the rules.

## Block outgoing UDP for `user`

To block outgoing UDP for `user` and keep boat working, allow the tunnel endpoint first.

```bash theme={"theme":{"light":"github-light","dark":"github-dark-default"}}
WG=$(sudo wg show wg0 endpoints | awk '{print $2}' | cut -d: -f1)
sudo iptables -I OUTPUT 1 -m owner --uid-owner user -p udp ! -d 127.0.0.0/8 -j REJECT
sudo iptables -I OUTPUT 1 -p udp -d "$WG" -j ACCEPT
```

## Open an inbound port

The firewall refuses inbound connections by default. `host <port>` opens what a public URL needs. For a raw port, read [Raw TCP or UDP ports](https://docs.boat.dev/hosting#raw-tcp-or-udp-ports).

## What happens to a blocked sandbox

boat never stops a sandbox for this, or marks it `error`, if it answers on any route. Instead:

* The sandbox shows `"health": "degraded"` with a reason.
* boat sends a [`sandbox.degraded`](https://docs.boat.dev/webhooks#events) webhook.

Remove the rule. The sandbox becomes healthy again, and boat sends `sandbox.recovered`.


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