# HTTPS and passkeys

> Let the Hub serve HTTPS itself with Let's Encrypt or your own certificate, how passkeys are bound to your domain, changing the domain later, and why self-signed certificates do not work.

Source: https://docs.posternmail.app/reach/https/

This page covers HTTPS without a separate proxy: the Hub can get a certificate from Let's
Encrypt by itself, or use one you give it. It also explains how passkeys depend on your
domain, what happens when you change the domain, and why self-signed certificates are not an
option. If you already use [Tailscale](https://docs.posternmail.app/reach/tailscale/), a [reverse proxy](https://docs.posternmail.app/reach/reverse-proxy/) or
[Cloudflare Tunnel](https://docs.posternmail.app/reach/cloudflare/), you have HTTPS and only need the passkey sections.

> [!NOTE]
> The [installer](https://docs.posternmail.app/setup/) does not set up the Hub's own HTTPS: it uses Caddy, Tailscale or
> Cloudflare instead. The passkey sections below apply to every setup.

## Which way to HTTPS

| Way | Certificate | Notes |
|---|---|---|
| [Tailscale](https://docs.posternmail.app/reach/tailscale/) | Automatic, for `*.ts.net` | Only devices in your tailnet. |
| [Reverse proxy](https://docs.posternmail.app/reach/reverse-proxy/) | Automatic (Caddy, Traefik) or certbot (nginx) | Best when the machine also serves other sites. |
| [Cloudflare Tunnel](https://docs.posternmail.app/reach/cloudflare/) | Cloudflare's | Cloudflare decrypts the traffic. |
| [Built-in, Let's Encrypt](https://docs.posternmail.app/reach/https/#built-in-tls-with-lets-encrypt) | Automatic | The Hub needs ports 80 and 443 to itself. |
| [Built-in, your own certificate](https://docs.posternmail.app/reach/https/#your-own-certificate) | Yours | You renew it and restart the Hub. |

**Desktop app (built-in Hub):**
None of this applies to the desktop app's built-in Hub. It runs at `http://localhost:<port>`,
which browsers treat as secure, so passkeys work there without a certificate. Other devices
cannot reach it at all. For HTTPS that your phone can use, run the Hub on a server
([Set up Postern](https://docs.posternmail.app/setup/)) and pick that method above.
**Docker Compose, Debian and Ubuntu, Fedora and RHEL and Raspberry Pi OS:**
With built-in TLS nothing sits in front of the Hub, so `POSTERN_TRUST_PROXY` stays off (the
default).

## Built-in TLS with Let's Encrypt

With `POSTERN_TLS_DOMAIN` set, the Hub requests a certificate for that one domain from Let's
Encrypt on the first HTTPS request, keeps it in `<data-dir>/acme` and renews it by itself.

| Variable | Meaning |
|---|---|
| `POSTERN_TLS_DOMAIN` | The domain, for example `mail.example.com`. The Hub only requests a certificate for exactly this name. |
| `POSTERN_TLS_EMAIL` | Contact address for the Let's Encrypt account, for expiry notices. Optional, recommended. |
| `POSTERN_PUBLIC_URL` | `https://mail.example.com`. |
| `POSTERN_LISTEN` | Where HTTPS is served. Only when it is exactly `:8080` (the default) does the Hub switch to `:443` by itself. Any other value is used as it is. |

The Hub also listens on port **80**, always on all addresses: Let's Encrypt checks the domain
there, and the Hub redirects every other plain HTTP request to HTTPS. If it cannot open port 80,
it logs `http->https redirect listener failed` and keeps running; certificates can then still be
issued over port 443, as long as HTTPS is served on 443.

Before you start:

- An `A` (and, if it works, `AAAA`) record for `mail.example.com` points to the machine's
  public address. See [Point your domain at the server](https://docs.posternmail.app/reach/reverse-proxy/#1-point-your-domain-at-the-server).
- Ports 80 and 443 reach the machine from the internet (router forwarding, firewall). See
  [Open ports 80 and 443](https://docs.posternmail.app/reach/reverse-proxy/#2-open-ports-80-and-443).
- Nothing else on the machine uses ports 80 or 443.

### Set it up

**Docker Compose:**
The image sets `POSTERN_LISTEN=:8080`, so with `POSTERN_TLS_DOMAIN` the Hub inside the container
switches to `:443` and also opens `:80`. Three things change in Compose: the TLS variables, the
published ports, and the image's health check, which still asks
`http://127.0.0.1:8080/api/v1/info` and would mark the container unhealthy.

In `~/postern/.env`:

```sh
POSTERN_PUBLIC_URL=https://mail.example.com
POSTERN_TRUST_PROXY=false
```

Create `~/postern/compose.override.yaml` (or add to it):

```yaml
services:
  hub:
    environment:
      POSTERN_TLS_DOMAIN: mail.example.com
      POSTERN_TLS_EMAIL: you@example.com
    ports:
      - "80:80"
      - "443:443"
    healthcheck:
      disable: true
```

The ports are added to the `127.0.0.1:8080` mapping from `compose.yaml`, which simply stays
unused. Certificates are kept in the data volume (`/data/acme`), so they survive updates.

Docker Engine lets processes inside containers open ports below 1024 without extra
privileges, so the Hub's non-root user (uid 10001) can bind 80 and 443 despite `cap_drop: ALL`.
If the log shows `listen tcp :443: bind: permission denied`, your Docker does not do that by
default; add to the `hub` service in the override file:

```yaml
    sysctls:
      net.ipv4.ip_unprivileged_port_start: 0
```

Apply:

```sh
cd ~/postern && docker compose up -d
```
**Debian and Ubuntu, Fedora and RHEL and Raspberry Pi OS:**
Ports 80 and 443 are privileged. The unit runs the Hub as the `postern` user with all
capabilities removed, so it must be allowed to bind them. Create a drop-in:

```sh
sudo systemctl edit postern-hub
```

and enter, between the comment lines the editor shows:

```ini
[Service]
AmbientCapabilities=CAP_NET_BIND_SERVICE
CapabilityBoundingSet=CAP_NET_BIND_SERVICE
```

Then set the Hub's variables in `/etc/postern/hub.env` (`sudoedit /etc/postern/hub.env`). The
file sets `POSTERN_LISTEN=127.0.0.1:8080`; change it to `:443`, because the Hub only switches
to 443 by itself when the value is `:8080`:

```ini
POSTERN_LISTEN=:443
POSTERN_PUBLIC_URL=https://mail.example.com
POSTERN_TLS_DOMAIN=mail.example.com
POSTERN_TLS_EMAIL=you@example.com
```

Remove `POSTERN_TRUST_PROXY=true` if it is there. Apply:

```sh
sudo systemctl restart postern-hub
```

Certificates are kept in `/var/lib/postern/acme`.
**Desktop app (built-in Hub):**
Not available for the built-in Hub.

> **Check:** the Hub's log shows `listening (TLS)` with `addr=:443`. Open
> `https://mail.example.com` in a browser; the first request takes a few seconds while the
> certificate is issued. Then:
>
> ```sh
> curl -sI https://mail.example.com/api/v1/info
> ```
>
> prints status 200, and `postern-hub doctor` shows `✓ automatic TLS (ACME) for mail.example.com`
> and `✓ TLS certificate for mail.example.com valid until ...`.

> **If it did not work:**
> - `listen tcp :443: bind: permission denied`: the capability drop-in is missing (systemd) or
>   the sysctl above is needed (Docker). `systemctl cat postern-hub` shows whether the drop-in
>   is active.
> - `bind: address already in use`: another program uses the port. `sudo ss -tlnp | grep -E ':(80|443) '`
>   names it.
> - The browser shows a certificate error and the log has `acme/autocert` errors: Let's Encrypt
>   cannot reach the machine on port 80 or 443, or the DNS record points elsewhere. Fix that, then
>   try again. Do not restart the Hub over and over: Let's Encrypt limits failed attempts per
>   hour.
> - The address you open is not exactly `POSTERN_TLS_DOMAIN` (for example `www.mail.example.com`
>   or the IP address): the Hub does not request certificates for other names.

## Your own certificate

If you already have a certificate (from your own ACME client, a company CA, or a wildcard
certificate), give the Hub the files:

| Variable | Meaning |
|---|---|
| `POSTERN_TLS_CERT` | PEM file with the certificate and its chain (`fullchain.pem`). |
| `POSTERN_TLS_KEY` | PEM file with the private key. |

Both must be set together, or the Hub refuses to start with `POSTERN_TLS_CERT and POSTERN_TLS_KEY
must be set together`. The files must be readable by the user the Hub runs as.

With a certificate file, the Hub serves HTTPS on `POSTERN_LISTEN` as it is (no switch to 443) and
opens no port 80. The Hub reads the files once at start: after you renew the certificate,
restart the Hub.

**Docker Compose:**
Mount the files read-only and serve HTTPS on host port 443. In `~/postern/.env`:

```sh
POSTERN_BIND=443
POSTERN_PUBLIC_URL=https://mail.example.com
```

`POSTERN_BIND=443` publishes host port 443 on all addresses to the container's port 8080, where
the Hub now speaks HTTPS. In `~/postern/compose.override.yaml`:

```yaml
services:
  hub:
    environment:
      POSTERN_TLS_CERT: /tls/fullchain.pem
      POSTERN_TLS_KEY: /tls/privkey.pem
    volumes:
      - /etc/postern/tls:/tls:ro
    healthcheck:
      disable: true
```

The health check is turned off because it asks for plain HTTP on port 8080. The files must be
readable by uid 10001:

```sh
sudo chown -R 10001:10001 /etc/postern/tls
sudo chmod 600 /etc/postern/tls/privkey.pem
```

Apply:

```sh
cd ~/postern && docker compose up -d
```
**Debian and Ubuntu, Fedora and RHEL and Raspberry Pi OS:**
Put the files in `/etc/postern/tls/` (the unit cannot read files under `/home`) and make them
readable by the `postern` user:

```sh
sudo install -d -o root -g postern -m 0750 /etc/postern/tls
sudo install -o root -g postern -m 0644 fullchain.pem /etc/postern/tls/fullchain.pem
sudo install -o root -g postern -m 0640 privkey.pem /etc/postern/tls/privkey.pem
```

In `/etc/postern/hub.env`:

```ini
POSTERN_LISTEN=:443
POSTERN_PUBLIC_URL=https://mail.example.com
POSTERN_TLS_CERT=/etc/postern/tls/fullchain.pem
POSTERN_TLS_KEY=/etc/postern/tls/privkey.pem
```

For port 443, add the capability drop-in from [the section above](https://docs.posternmail.app/reach/https/#set-it-up). With a port above
1024 (for example `POSTERN_LISTEN=:8443` and `POSTERN_PUBLIC_URL=https://mail.example.com:8443`)
you do not need it. Apply:

```sh
sudo systemctl restart postern-hub
```
**Desktop app (built-in Hub):**
Not available for the built-in Hub.

> **Check:** `postern-hub doctor` shows `✓ TLS certificate for /etc/postern/tls/fullchain.pem valid
> until ...` (in Docker, the path inside the container), and
> `curl -sI https://mail.example.com/api/v1/info` prints status 200.

> **If it did not work:** doctor's messages name the problem: `cannot read POSTERN_TLS_CERT ...`
> (permissions or path), `POSTERN_TLS_CERT ... is not a PEM certificate` (wrong format: convert
> it with `openssl x509 -in cert.der -inform der -out fullchain.pem`), or
> `TLS certificate for ... expires in N days` (renew it, then restart the Hub).

## HSTS

Whenever a request arrives over HTTPS (directly, or through a trusted proxy that says so), the
Hub sends `Strict-Transport-Security: max-age=31536000`. Browsers then refuse plain HTTP for that
host name for a year. You cannot switch the same name back to `http://` later; browsers will
insist on HTTPS. Use another name if you ever need to.

## Passkeys and your domain

A passkey is bound to a domain, the "relying party ID". The Hub uses the host name of
`POSTERN_PUBLIC_URL` (or, when that is empty, of the address in the browser):

- `https://mail.example.com` gives passkeys for `mail.example.com`.
- `https://hub.tailnet-1234.ts.net` gives passkeys for `hub.tailnet-1234.ts.net`.
- `http://localhost:8080` works too, because browsers treat localhost as secure.

The Hub does not offer passkeys:

- over plain HTTP on any name other than localhost,
- on an IP address, even with HTTPS (`https://192.168.1.20` has no passkeys).

In the web app, **Settings → Security** then says "Passkeys only work when your Hub is opened
over a secure (https) address." Password with TOTP still works. How to add and use passkeys:
[Sign-in methods](https://docs.posternmail.app/admin/sign-in/).

## Changing the domain later

When `POSTERN_PUBLIC_URL` changes to another host name (for example from Tailscale to your own
domain), the browser and the phone treat the new name as a different site:

1. **Passkeys stop working.** They belong to the old host name. Sign in with your password and
   TOTP, then add a new passkey in **Settings → Security** and remove the old one. Make sure
   every user has a password and TOTP (or another way in) before you switch.
2. **Signed-in devices keep the old address.** Sign the desktop apps in again with the new
   address, and pair phones again ([Pairing](https://docs.posternmail.app/apps/pairing/)).
3. **Google and Microsoft redirect addresses change.** If you use your own OAuth clients, add
   `https://<new host>/api/v1/oauth/callback/google` (and `/microsoft`) to them
   ([Gmail](https://docs.posternmail.app/providers/gmail/), [Microsoft](https://docs.posternmail.app/providers/microsoft/)). The same goes for a
   single sign-on provider (`/api/v1/auth/oidc/callback`).
4. **Invite links** already sent point to the old address. Send new ones.

Your mail, mailboxes and settings do not change.

## Self-signed certificates

A self-signed certificate (or one from your own CA that the devices do not know) does not work
with the apps:

- The **desktop app** checks the certificate with your computer's trust store and never skips
  that check. It says "Your Hub's certificate isn't trusted" and suggests a certificate from
  Let's Encrypt or Tailscale, or adding your own certificate authority to the computer's
  trusted certificates.
- The **phone apps** use the phone's trust store. They only accept your own CA if you install it
  on every phone and mark it as trusted, which is fiddly and easy to get wrong.
- **Browsers** show a full-page warning. Even if you click through, passkeys and service workers
  do not work on a page with a certificate error.

If your Hub is only at home and you want HTTPS, [Tailscale](https://docs.posternmail.app/reach/tailscale/) gives you a real
certificate without opening anything. If you own a domain, a reverse proxy with Let's Encrypt
works for addresses that are only reachable at home too, as long as Let's Encrypt can check the
domain; Caddy and certbot also support the DNS check for that, which is not covered here.
