# Hardening a production install (https://docs.opensequence.dev/docs/security/security-hardening)

What to lock down before going live: encryption keys and their rotation, the operator token, network layout, webhook rules, rate limits, a checklist.

This page lists the settings and network rules that protect a production install. Go through the checklist at the end before you expose the API.

```bash
SEQ_ENCRYPTION_KEYS='k2new:…,k1old:…'   # the first entry encrypts, every entry decrypts
SEQ_ADMIN_TOKEN="$(openssl rand -base64 32)"
NODE_ENV=production                      # already set in the image: refuses mock providers, plain JSON logs
```

## Encryption keys [#encryption-keys]

Every stored secret is AES-256-GCM ciphertext under `SEQ_ENCRYPTION_KEYS`: each sender's SMTP and IMAP passwords, the Allo and Attio keys, and the webhook signing secrets. Each row records the id of the key that wrote it.

The value is a comma-separated list of `id:base64key`. Each key must decode to 32 bytes, ids must be unique and at most 16 characters. `node dist/cli.js secrets new-key` prints a valid entry.

| Entry | Role |
|---|---|
| first | encrypts every new secret |
| every entry | decrypts the rows written with it |

<Warning>The unsubscribe links in sent emails are signed with a key derived from the whole `SEQ_ENCRYPTION_KEYS` value. Any change to it (adding, removing or reordering an entry) makes every link already sent answer "This link has expired". Plan rotations knowing this.</Warning>

### Rotate a key [#rotate-a-key]

1. Generate a new entry with `secrets new-key` and put it first. Keep the old entries after it.
2. Restart both roles. New secrets are now written under the new key; old ones still decrypt.
3. Each secret moves to the new key when it is written again: a provider credential saved again, a sender's mailbox saved again.

Check what still uses an old key:

```sql
select 'credentials' as t, key_id, count(*) from seq_auth.credentials group by key_id
union all select 'connections', key_id, count(*) from seq_auth.connections group by key_id
union all select 'webhook_endpoints', key_id, count(*) from seq.webhook_endpoints group by key_id;
```

Drop an old entry only when nothing uses it. No command re-encrypts everything at once yet (the `oseq secrets rotate` command in the architecture document does not exist), so keeping old entries in the list is the safe default.

If a webhook endpoint's key is gone, its next delivery disables it with a reason. `POST /v1/webhooks/{id}/rotate-secret` issues a new signing secret under the current key (update your receiver), then `POST /v1/webhooks/{id}/enable` resumes it.

## The operator token [#the-operator-token]

`SEQ_ADMIN_TOKEN` protects `/v1/admin/*` only: creating projects and their keys, and platform health. It is compared in constant time and never stored. Only the `api` and `all` roles need it. Keep it out of application code, which uses project keys. To rotate it, change the variable and restart the api.

## Project keys [#project-keys]

Keys are `sk_live_…` with 256 bits of entropy, stored as SHA-256 hashes, shown once, revocable with `DELETE /v1/api-keys/{id}`. Give each caller the narrowest scope:

| Scope | For |
|---|---|
| `read` | dashboards and reporting |
| `enroll` | the part of your application that reacts to user actions. It cannot publish a sequence or grant outreach consent |
| `admin` | configuration jobs only |

## Network layout [#network-layout]

| Component | Exposure |
|---|---|
| `api` | public over HTTPS behind a TLS terminator. Needs inbound from your application, Allo (webhooks) and mail clients (unsubscribe links) |
| `worker` | private. Its port answers the API routes too, so never route traffic to it. Needs outbound to your senders' mail servers (SMTP and IMAP), Allo, Attio and your webhook endpoints |
| Postgres, Redis | private |

- `SEQ_PUBLIC_URL` must be the HTTPS origin the world sees. It is written into unsubscribe links and sent to Allo when you save the Allo key. If it changes, save the Allo key again so Allo posts to the new address.
- The API trusts `X-Forwarded-For` to find the client address, which the per-IP rate limit uses. Make sure your proxy overwrites that header and that nothing reaches the api around the proxy.
- The api serves an interactive API explorer at `/docs`. Block that path at your proxy if you do not want it public.

## Outbound webhooks [#outbound-webhooks]

Endpoint URLs must use `https`, carry no credentials, not point at `localhost`, `.local` or `.internal`, and resolve to public addresses. Loopback, private, link-local (cloud metadata included), CGNAT, multicast and reserved ranges are refused, for IPv4 and IPv6. The host is resolved again at every attempt, redirects are not followed, and each attempt times out after 10 seconds.

So a receiver must be reachable at a public HTTPS address, even when it runs next to OpenSequence.

## Inbound webhooks [#inbound-webhooks]

| Path | Check |
|---|---|
| `/hooks/allo/{slug}` | Standard Webhooks signature with the secret Allo issued when the service registered the endpoint; timestamps older than 5 minutes are rejected |
| `/hooks/mock/{slug}` | exists only with mock providers on, which `NODE_ENV=production` refuses |

## Rate limits and body size [#rate-limits-and-body-size]

600 requests a minute per `Authorization` value, or per client address without one. Over that, `429` with `Retry-After`. Bodies over 2 MB are refused. With several api replicas each keeps its own counter, so put a shared limiter at the edge if you need an exact global cap.

## Tenant isolation [#tenant-isolation]

Every table carries `project_id` and every query filters on it. A key resolves to one project, and another project's ids answer `404`.

## Before going live [#before-going-live]

- `NODE_ENV=production`, `SEQ_DEV_MOCK_PROVIDERS` unset.
- Real `SEQ_ENCRYPTION_KEYS` and `SEQ_ADMIN_TOKEN`, in a secret store and in your backups.
- TLS in front of the api; the worker port not routable.
- `SEQ_PUBLIC_URL` set to the public HTTPS origin before you save any provider credential.
- A webhook endpoint registered and verifying signatures ([Verifying](/docs/webhooks/verifying)).
- Application keys with the narrowest scope.
