# Secrets

A secret is a named value your team keeps with Groundbox: an API token, a password. Set it once. The value is stored encrypted and cannot be read back, by the API, the CLI or the dashboard.

```
curl -X PUT https://api.groundbox.dev/v1/secrets/NPM_TOKEN \
  -H "Authorization: Bearer $GROUNDBOX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"value": "npm_..."}'
```

A sandbox gets a secret in one of two ways. As a variable, the value is inside the sandbox. As a binding to a host, the value stays outside it and is added to the sandbox's HTTPS requests to that host.

Give a sandbox secrets by name. Every command in it gets them as environment variables. `secrets` uses the secret's name as the variable name. `env_from_secrets` picks another variable name.

```
curl https://api.groundbox.dev/v1/sandboxes \
  -H "Authorization: Bearer $GROUNDBOX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"cpus": 2, "secrets": ["NPM_TOKEN"], "env_from_secrets": {"PGPASSWORD": "DB_PASSWORD"}}'
```

- The sandbox keeps only the names. The value is read each time a command starts, so a replaced secret is used from the next command.
- A fork uses the same secrets as its source. A schedule's sandbox spec takes the same two fields.
- A template takes `secrets` for its build: the build's steps get them as variables. They are not written to the template, its sandboxes do not get them, and the build log shows the secret's name in place of its value.
- If a secret is deleted, the next command in a sandbox that uses it fails with 409 and names the secret. Set it again and the sandbox works again.
- A command's own `env` wins over a secret of the same variable name.

API keys have three scopes for this. `secrets:write` sets and deletes. `secrets:read` lists names. `secrets:use` lets a key give secrets to a sandbox, a schedule or a template, as variables or as bindings. A key that only runs commands needs none of them.

In the CLI: `groundbox secrets set NAME` reads the value from stdin, `groundbox secrets list`, and `--secret NAME` or `--bind-secret HOST=NAME` on `sandbox create`. The dashboard has a Secrets tab under Sandboxes.

What to know about secrets as variables:

- A secret given as a variable is inside the sandbox. Code that runs there can read it, print it or send it anywhere the sandbox can reach.
- Pair secrets with an allowlist, so a token can only travel to the hosts that need it. Or bind the secret to its host instead.
- Command output is not filtered. If a command prints a secret, the output has it.
- Processes that were not started through the API, such as a desktop's browser, do not get secrets.

## Secret bindings

A binding keeps a secret out of the sandbox. The box's secrets proxy adds it as a header to the sandbox's HTTPS requests to one host. Code in the sandbox calls the API as usual and never sees the value, so it cannot print it or send it anywhere else.

```
curl https://api.groundbox.dev/v1/sandboxes \
  -H "Authorization: Bearer $GROUNDBOX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "cpus": 2,
    "secret_bindings": [
      {"host": "api.openai.com", "secret": "OPENAI_KEY", "env_placeholder": "OPENAI_API_KEY"},
      {"host": "api.anthropic.com", "secret": "ANTHROPIC_KEY", "header": "x-api-key", "format": "{value}", "env_placeholder": "ANTHROPIC_API_KEY"}
    ]
  }'
```

Each binding takes:

- `host`: an exact host name, or a wildcard of one label: `*.example.com` covers `a.example.com`, not `a.b.example.com`. IP addresses and private names (`localhost`, `.internal`, `.local` and the like) are refused.
- `secret`: the name of a team secret.
- `header`: the header to set. `Authorization` by default.
- `format`: the header's value, with `{value}` where the secret goes. `Bearer {value}` by default.
- `env_placeholder` (optional): a variable the sandbox gets with the value `groundbox-secrets-proxy-placeholder`, for SDKs that will not start without a key. The proxy puts the real value in the header.

A sandbox takes up to 10 bindings, and the key needs `secrets:use`. A schedule's sandbox spec takes `secret_bindings` too. In the CLI it is `--bind-secret`, once per binding, with options after commas:

```
groundbox sandbox create --bind-secret api.openai.com=OPENAI_KEY
groundbox sandbox create --bind-secret 'api.anthropic.com=ANTHROPIC_KEY,header=x-api-key,format={value},env=ANTHROPIC_API_KEY'
```

How it works:

- The sandbox's DNS answers a bound host with the proxy's address. Every other host resolves and connects exactly as before: it is not proxied, not decrypted, and gets no added latency.
- The proxy ends TLS only for bound hosts, with a certificate from the sandbox's own CA. That CA can only sign the bound hosts, and it is put in the sandbox's trust store at boot. Commands also get `NODE_EXTRA_CA_CERTS`, `SSL_CERT_FILE`, `REQUESTS_CA_BUNDLE` and `CURL_CA_BUNDLE` pointing at it.
- The proxy then opens its own TLS connection to the real host and checks its certificate in full. HTTP/1.1, HTTP/2 (gRPC too) and WebSocket upgrades work.
- The proxy masks the value as `*`s in response headers and in response bodies that are not compressed, so an endpoint that echoes the request does not hand it back. Compressed bodies and WebSocket frames are not scanned.
- A replaced secret is used from the next request. If a bound secret is deleted, requests to its host fail with 502 and a body that names the secret.
- A fork keeps its source's bindings, and so do pause and resume.
- A binding lets its host through an [allowlist](https://groundbox.dev/docs/sandboxes/egress) without listing it. If a deny list covers the host, the deny list wins and the create is refused.
- It is opt-in. A sandbox without bindings has no proxy in its path and no extra CA. CI jobs are unchanged.

What bindings do not do:

- A client that pins certificates, or ignores both the system trust store and those variables, fails TLS to a bound host. Some Rust builds with webpki-roots do, and so does Java without the certificate in its keystore.
- Only HTTPS is proxied. A plain `http://` request to a bound host gets no header.
- Code that connects to a hardcoded IP address, or looks names up over DNS-over-HTTPS, goes around the proxy. Its requests simply carry no credential.
