# Sandbox

> Run Chromium's helper processes in the operating system's sandbox.

Chromium's sandbox confines its helper processes. A renderer then runs your page with the few rights the operating system's sandbox leaves it, instead of the user's.

Kurogane runs the helpers unsandboxed by default. [`App::sandbox_mode`](https://kurogane-rs.org/docs/api/app/app.md#method.sandbox_mode) turns the sandbox on:

```rust
use kurogane::{App, SandboxMode};

fn main() {
    App::new("content")
        .sandbox_mode(SandboxMode::Chromium)
        .run_or_exit();
}
```

## Choosing a mode

| [`SandboxMode`](https://kurogane-rs.org/docs/api/app/sandboxmode.md) | Use it for |
|---|---|
| `Chromium` | production builds and untrusted content |
| `Disabled` (default) | development without a usable sandbox, containers, CI and embedded targets |

Both modes apply to apps that own their windows and to embedded apps.

## What each platform needs

Each platform has a condition the sandbox needs. Kurogane checks it before Chromium starts. It refuses to start unsandboxed and says what is missing instead.

| Platform | Condition |
|---|---|
| Linux | unprivileged user namespaces or a usable setuid `chrome-sandbox` |
| macOS | the app runs from a `.app` bundle |
| Windows | CEF's sandbox bootstrap starts the app |

The process model stays the same under the sandbox. Helpers still run your binary, `is_browser_process` still guards one-time work and everything before `App::run` behaves as before.

## Linux

Chromium uses unprivileged user namespaces where the kernel allows them. Most distributions do. Ubuntu 24.04 restricts them through AppArmor. Lift the restriction:

```bash
sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
```

Without user namespaces Chromium uses the setuid `chrome-sandbox` helper that ships with every Linux runtime. Make it setuid root:

```bash
sudo chown root:root ~/.local/share/tanso/cef/<version>/cef_linux_x86_64/chrome-sandbox
sudo chmod 4755 ~/.local/share/tanso/cef/<version>/cef_linux_x86_64/chrome-sandbox
```

When neither is usable the app prints these instructions and doesn't start. AppImages and Nix store installations can't use the setuid helper. They need user namespaces.

## macOS

The app must run from a `.app` bundle. Build one with `kurogane bundle --format app`. An unbundled run such as `kurogane dev` refuses to start in `Chromium` mode. No entitlements need changing.

## Windows

CEF's Windows sandbox starts your app from CEF's bootstrap executable. The bootstrap loads your app as a library and brokers the sandboxed helpers. That takes two changes to the crate and one line in `kurogane.toml`.

Give the crate a library target and keep the binary. Unsandboxed runs and `cargo run` still use the binary:

```toml
[lib]
name = "myapp_lib"
crate-type = ["cdylib", "rlib"]

[[bin]]
name = "myapp"
```

The library takes a name of its own. Cargo warns when a library and a binary of the same name write the same `.pdb`. The bootstrap and the bundle still take the binary's name.

Move the app into `src/lib.rs` and declare the entry points the bootstrap calls with [`sandbox_entry!`](https://kurogane-rs.org/docs/api/app/sandbox_entry.md):

```rust
// src/lib.rs
pub fn run() {
    kurogane::App::new("content")
        .sandbox_mode(kurogane::SandboxMode::Chromium)
        .run_or_exit();
}

kurogane::sandbox_entry!(run);
```

```rust
// src/main.rs
fn main() {
    myapp_lib::run()
}
```

Off Windows `sandbox_entry!` only checks the signature. The same source builds on every platform.

Tell the CLI the app is sandboxed:

```toml
[app]
sandbox = true
```

`kurogane run` and `kurogane dev` then build the library and stage it in `target/<profile>/sandbox/` with the Chromium runtime and CEF's console bootstrap. They start the app from there. `kurogane bundle` packages it with the windowed bootstrap. No console appears.

The `sandbox` key does nothing on Linux and macOS. One `kurogane.toml` serves every platform.

An app that asks for `Chromium` mode without the bootstrap refuses to start and says why. It refuses as well when the bootstrap's sandbox ABI differs from the one `libcef.dll` was built with.

### The GPU on Windows

Under the sandbox the GPU runs in a process of its own at low integrity. An out-of-process GPU restarts after a lost device and falls back to software rendering after repeated losses.

Without the sandbox Kurogane runs GPU work in the browser process. It waits there for a lost device to recover. That GPU work runs with the user's full rights.
