# Security & Deployment

> How Pigeon protects credentials and tokens, and how to run it remotely.

---

LLMS index: [llms.txt](/llms.txt)

---

Pigeon is local-first: mail is fetched directly from your IMAP servers, stored
in a local SQLite database and served from the same process. There is no cloud
middleman and no telemetry.

## Credentials at rest

- Account passwords are encrypted with **AES-256-GCM**.
- The 256-bit key lives in `secret.key` in the data directory, created with
  mode `0600` on first start.
- Endpoints never return passwords; responses include only non-secret account
  fields.
- **Back up `secret.key` with the database.** Without it, stored passwords
  cannot be decrypted and accounts must be re-added with their passwords.

Mail bodies, subjects and addresses in SQLite are **not** encrypted — protect
the host with disk encryption if that matters to you. All secret material is
in the data directory; `chmod 700` on it is a sensible baseline.

## API tokens

- Tokens look like `pigeon_<random>` and are returned **once**, at creation.
- Only a **SHA-256 hash** is stored; a stolen database does not reveal usable
  tokens.
- `lastUsedAt` records the most recent successful authentication.
- Revoking a token (`DELETE /api/tokens/{id}` or `pigeon token delete`) takes
  effect immediately.
- The initial token printed by `pigeon serve` on first start appears in the
  log once — treat log files as sensitive.

## Authentication model

| Request source | Access |
| --- | --- |
| Loopback (`127.0.0.1`, `::1`) | Allowed without credentials |
| Anything else | Requires `Authorization: Bearer pigeon_…` |
| `GET /api/health` | Public |

The loopback test uses the TCP **remote address**. Consequences:

- **Same-host reverse proxy**: a proxy connecting from `127.0.0.1` is trusted,
  so the API is effectively unauthenticated behind it. Either authenticate at
  the proxy or run Pigeon somewhere its clients are not loopback (for example
  in a container on a bridge network).
- **Docker `-p 8787:8787`**: connections arrive from the bridge gateway, not
  loopback, so the token is required. This is the intended remote setup.
- **SSH tunnel / VPN**: traffic to `127.0.0.1` stays loopback and skips tokens;
  the tunnel itself provides the access control.

CORS allows any origin by design (the API targets local tools and agents).
Authentication still applies to non-loopback requests; do not expose the
service to the internet without TLS and a token.

## Transport security

- IMAP: implicit TLS, STARTTLS or plain, with a minimum of TLS 1.2 for TLS
  modes.
- SMTP: TLS is mandatory by default; `tls` uses implicit TLS (SMTPS),
  `starttls` upgrades, `none` still attempts STARTTLS but permits plaintext.
- Body fetches use `BODY.PEEK`, so reading and syncing never change read state
  on the server.

## Running remotely

By default Pigeon listens on `127.0.0.1:8787`, which only this machine can
reach. To let other machines call the REST API (and the `/mcp` endpoint), bind
a reachable address with `--addr` or `PIGEON_ADDR`:

```bash
pigeon serve --addr 0.0.0.0:8787
# or
PIGEON_ADDR=0.0.0.0:8787 pigeon serve
```

`--addr` takes precedence over `PIGEON_ADDR`. Once the service is reachable
from other hosts, every request from them must carry a Bearer token:

```bash
# on the server
pigeon token create --name remote

# from another machine
curl -H "Authorization: Bearer pigeon_…" http://<server-ip>:8787/api/accounts
curl -H "Authorization: Bearer pigeon_…" \
  'http://<server-ip>:8787/api/mailboxes/1/messages?limit=10'
```

This is plain HTTP: the token crosses the network unencrypted. Use it inside a
trusted network, or put a TLS-terminating proxy in front before exposing it
beyond one.

Recommended patterns, most to least conservative:

1. **SSH tunnel** — keep Pigeon on `127.0.0.1`, forward the port over SSH; no
   public listener at all.
2. **Private network / VPN** — bind to a trusted interface with a token.
3. **Container with port mapping** — the Dockerfile runs as a non-root user;
   non-loopback requests require a token.
4. **Reverse proxy with TLS** — terminate TLS at the proxy and also do
   authentication there, because Pigeon will trust the proxy's loopback
   connection.

## What is not encrypted or sandboxed

- HTML mail in the web UI is rendered in a sandboxed iframe (no scripts, no
  same-origin access) and remote images are blocked until you click "show
  images" for that message.
- When the web UI talks to a remote server it keeps the API token in browser
  `localStorage`; anyone with access to that browser profile can read it.
  Prefer dedicated tokens and revoke them when done.
- There is no built-in rate limiting; rely on the proxy or network boundary
  for internet-facing deployments.

---

Backlinks:

- [Documentation](/docs/)
- [Accounts & Autodetection](/docs/accounts/)
- [REST API Basics](/docs/api/)
- [Features](/docs/features/)
- [Tokens & Auth](/docs/tokens/)
