Preview TLS front door: lab terminates TLS on the preview listener with an operator certificate or a wildcard leaf under a lab-minted CA, 308-redirects plain HTTP on a second address, and the NixOS module binds :443/:80 through an ambient capability¶
ADR-0084 shipped the preview listener as plain HTTP. Browsers treat that as an insecure origin: no service workers, no secure cookies, no crypto.subtle, mixed-content blocks when a preview embeds https resources, and Safari complains about every PWA-shaped app under test. The operator reaches previews from a phone over WireGuard, so "just click through the warning" is not an answer on the device that matters most. Lab had no server-side TLS at all; the only TLS code was client-side, in the Warpgate and OneCLI clients.
The shape is constrained from three sides. The maintainer wanted no nginx in the path, so previews work on any lab install. The fleet already issues ACME certificates by DNS-01, so a real wildcard has to slot in without a restart on renewal. And an operator without a domain still needs a path with no warnings, which on iOS is narrow: a profile can only be trusted as a CA, and Apple refuses server certificates valid for more than 825 days even under a user-trusted root. Those settle the design: lab serves TLS itself, from an operator pair when one is configured, otherwise from a leaf it renews under a CA the operator trusts once.
Decisions, pinned:
- Lab terminates TLS itself. The preview
http.Servergets atls.ConfigwhoseGetCertificatehands out the current pair. ACertManagerininternal/previewowns loading, minting, persistence, renewal and reload, so a renewed or reloaded certificate takes effect on the next handshake without rebuilding the server. Standard library only (crypto/x509,crypto/ecdsa); no new dependency. TLS 1.2 is the floor. - Two certificate sources, the operator's first.
--preview-cert-fileand--preview-key-file(LAB_PREVIEW_CERT_FILE,LAB_PREVIEW_KEY_FILE) both set select the operator source; both unset select the lab CA. Exactly one set is a startup error naming both flags (the pairing rule of the Warpgate url/token flags), and both are errors without--preview-addr. A pair that does not load at startup is fatal. - A lab-minted CA plus a renewable wildcard leaf, not a single self-signed leaf. iOS only lets a certificate with
basicConstraintsCA:true be trusted through a profile, and Apple rejects a server certificate valid longer than 825 days even under a user-trusted root, so a long-lived self-signed leaf can never be trusted on the operator's phone. A CA trusted once, with leaves rotated underneath, is the only no-warnings path without an external CA. The CA is valid 10 years with a CN naming lab and the host,pathlen:0and key usage limited to certificate and CRL signing. The leaf has SANs*.<domain>and<domain>and is valid 1 year, well under 825 days by construction. Both are backdated an hour so a device whose clock runs slightly behind does not see a not-yet-valid certificate. - P-256 keys, not Ed25519. The issue allowed either. Ed25519 support in certificate chains is still uneven across the browsers, OS trust stores and TLS stacks a preview has to open in, and the whole point of the CA is to work on the operator's phone. ECDSA P-256 is universally accepted and still small and fast.
- Files under
<state>/preview/, keys 0600.ca.crt,ca.key,leaf.crt,leaf.key, in a 0700 directory that is re-tightened at every start. Certificates are 0644 (the CA certificate is exactly what the operator downloads). Every file is written to a temporary name and renamed into place, so a crash never leaves half a file; the CA key is renamed before the CA certificate, so a crash between the two leaves an incomplete CA (refused, below) rather than a certificate whose key is missing. - The CA is never replaced silently. If
ca.crtandca.keyare both missing, lab mints a CA. If exactly one exists, or both exist but do not parse, do not match, are not a CA or have expired, startup fails naming the files and the remedy. Devices may trust the existing CA, and quietly minting over it would break every preview on every one of them with no explanation. The operator deletes both files to get a new CA and then trusts it everywhere again. This was preferred over self-healing because the failure it prevents is silent, per-device and far from its cause. - The leaf is re-minted on need, the CA is reused. At every startup and once a day while running, the leaf is re-minted when it is missing or unusable, is not signed by the current CA, no longer covers exactly
{*.<domain>, <domain>}(a changed--preview-domain), is not yet valid, or has under 30 days left. A changed domain therefore costs the operator nothing: only the leaf changes and the devices still trust the CA. Thirty days leaves a month of daily retries if renewal keeps failing. - The operator pair reloads on mtime change. Lab polls both files about once a minute and re-reads the pair when either one's modification time changes, so an ACME renewal that rewrites files in place is picked up without a restart (and without
reloadServicesin the NixOS ACME config). A bad reload keeps the previous pair, logs, and surfaces ascert.errorin the status endpoint and Settings; it retries on the next tick. A reload that fails does not record the new stamp, so the next tick tries again, which also covers a certificate written a moment before its key. - HTTP/2 stays on; ALPN is
h2, http/1.1unlessGODEBUG=http2xconnect=1. The issue asked to confirm that no HTTP/2 interaction breaks the reverse proxy's WebSocket handling. A WebSocket (Vite HMR) reaches the run as an HTTP/1.1Upgradethroughhttputil.ReverseProxy, which hijacks the connection and cannot do that on an HTTP/2 stream. Over HTTP/2 the browser would have to use RFC 8441 extended CONNECT, which the proxy does not translate. Browsers only try that when the server advertisesSETTINGS_ENABLE_CONNECT_PROTOCOL; Go's HTTP/2 server does not (golang/go#71128; the only switch is theGODEBUGhttp2xconnect=1), and without the setting Chrome and Firefox open their WebSockets on a separate HTTP/1.1 connection, where the upgrade works. So HTTP/2 is kept for ordinary requests (a Vite dev server serves hundreds of unbundled modules, and multiplexing beats six HTTP/1.1 connections). If the operator sets thatGODEBUG, lab pins ALPN tohttp/1.1and the server's protocol set with it, becausenet/httpre-addsh2toNextProtoswhile HTTP/2 is enabled on the server. A test reads the server's SETTINGS frame off the wire, so a Go upgrade that flips the default fails a test rather than breaking HMR. - The redirect listener answers 308 and nothing else.
--preview-redirect-addr(unset = none) answers a request whoseHostis the preview domain or a name under it with a 308 tohttps://<same host><:port unless 443><same path and query>; everything else gets a plain 404. It is the only unauthenticated plain-HTTP surface lab exposes, so it must never grow a download or a status page. 308 and not 301 so a POST to anhttp://preview URL stays a POST. The answer isCache-Control: no-store, because browsers cache permanent redirects indefinitely and a cached one would keep pointing at the old port after the listener moves. The host echoed intoLocationis restricted to names under the preview domain whose labels are plain DNS characters, so the redirect cannot be turned into an open redirect or carry userinfo, escapes or brackets. It requires--preview-addrand a different address. - The CA is downloadable only behind lab's auth.
GET /api/v1/preview/caon the main listener, underrequireAuth, returns the CA certificate; 404 with an operator certificate or with previews off. The trust anchor is never served from the preview or redirect listeners. TheContent-Typeisapplication/x-x509-ca-certwithContent-Disposition: attachment; filename="lab-preview-ca.crt". The issue allowedapplication/x-pem-filetoo.application/x-x509-ca-certis the type iOS Safari treats as a certificate to install (it is what turns the download into the install-profile prompt), so it is the one chosen; the generic PEM type gives no such hint. The Settings link is a plain navigation without adownloadattribute for the same reason: iOS would otherwise save to Files instead of offering installation. - Status and health:
GET /api/v1/previewand Settings → Previews. Authenticated, always 200:{"enabled": false}, or the domain, port, whether the redirect listener is on,cert(sourcelab-caoroperator,not_after,dns_names, anderrorwhen the last renewal or reload failed) and whether the CA download is offered. The startup log line names the source and the leaf's expiry. The Settings card shows the domain, the URL shape, the source, the expiry (flagged under 30 days) and, with the lab CA, the download link plus one line of trust steps per platform. - URLs are
https://<name>.<domain>, with the port unless it is 443. labctl's printed URL, the registration answer, the chat chip and Run details all use it. Upstream requests carryX-Forwarded-Proto: https. preview.enablereplaceslistenAddras the NixOS switch. The module's options areenable(defaultfalse),listenAddr(default":443"),redirectAddr(default":80",nulldisables),domain(default"lab.internal"),certFileandkeyFile. All flags render only whileenableis true. ADR-0084'slistenAddr = nullswitch would have made a leftover config silently turn previews off, so setting any preview option whileenableis false fails evaluation with a message naming the option and saying to setenable.certFilewithoutkeyFilealso fails evaluation, ahead of lab's own refusal.openFirewallopens both ports. The explicit-set check keys on option priorities (above the option default), so it does not read the values.AmbientCapabilities=CAP_NET_BIND_SERVICEwhen a configured port is below 1024, and noCapabilityBoundingSet. The issue said to widen the bounding set. The unit has none, and adding one would break rootless podman, whose setuidnewuidmap/newgidmapwrappers rely on capabilities a bounding set strips; the user-manager arrangement of ADR-0060 is untouched. Ambient capabilities surviveexecveinto children, which would handCAP_NET_BIND_SERVICEto the tmux server and every agent session, host runs included, so lab clears its ambient capability set immediately after binding the preview listeners. Ports of 1024 and up add nothing to the unit.- Out of scope, unchanged. No authentication on previews (ADR-0084's decision stands), no ACME issuance by lab itself (operators with a domain bring a certificate; the ops guide shows the Hetzner DNS-01 route), nothing but a redirect on the redirect listener, and no TLS on lab's main or dashboard listeners.
Status¶
Accepted 2026-10-18. Resolves issue #106. Decided with the maintainer in a grill session on 2026-10-18; the issue's agent brief is the contract. Amends ADR-0084: its "Plain HTTP for now" decision is replaced by this one, and with it the http:// URL shape and the NixOS listenAddr-as-switch. ADR-0084's tunnel, routing and no-authentication decisions stand.
- ADR-0084: amended as above; unchanged otherwise.
- ADR-0052: unchanged. A container run still reaches lab only through the agent socket; it does not mount
<state>/preview/. - ADR-0060: unchanged. The ambient capability is dropped before any session is started, and no bounding set is added.
- ADR-0080: the one-page Settings gains a Previews section; no change to the page's save model (the section is read-only).
Considered options¶
- A single long-lived self-signed leaf. Rejected: iOS cannot trust it through a profile (CA:true required) and Apple rejects server certificates valid over 825 days.
- A reverse proxy in front (nginx/Caddy terminating TLS). Rejected on the maintainer's call: previews should work on any lab install, and lab can mint what it needs.
- ACME issued by lab. Rejected as out of scope: it would need DNS-provider credentials and a renewal state machine lab does not otherwise have. Operators who own a domain use the two flags.
- Ed25519 keys. Rejected for compatibility (above).
- Replace a damaged CA automatically. Rejected: silent, per-device breakage far from its cause.
- Name constraints on the CA (
permittedDNSDomainslimited to the preview domain, which would bound the damage of a leaked CA key to previews). Considered and not adopted. The issue requires a domain change to re-mint only the leaf; a name-constrained CA would be tied to the domain it was minted for, so changing--preview-domainwould mean a new CA that must be trusted again on every device. Constraint enforcement is also uneven across the platforms that matter. Recorded as a possible future hardening: if lab ever adds a way to confine the CA to a domain, it can ship with an explicit "re-trust on every device" step. - Serve the CA from the redirect listener, or a status page there. Rejected: unauthenticated plain HTTP must not host the trust anchor.
- Widen
CapabilityBoundingSet. Rejected for rootless podman (above). - A file capability on the lab binary (
setcap, a NixOS security wrapper), or systemd socket activation, for the low ports. Rejected: a wrapper or file capability puts a second binary or a filesystem attribute between systemd and lab and has to follow every package update, and socket activation would move two listeners' binding out of lab.AmbientCapabilitiesis the unit-level mechanism, and lab clears it itself. - Pin ALPN to HTTP/1.1 always. Rejected: it gives up multiplexing for no gain while Go does not advertise extended CONNECT.
- A 301 redirect, or a redirect to a fixed host. Rejected: 301 turns a POST into a GET, and a fixed host cannot serve a wildcard.
Consequences¶
- Previews are HTTPS origins. Service workers, secure cookies,
crypto.subtleand clipboard writes work on a preview page, and an embedded https resource no longer trips mixed-content blocking. - Each device must trust the CA once (or the operator brings a certificate its devices already trust). The ops guide gives the iPhone, macOS and Linux steps and states the Apple limits it relies on (CA:true for profile trust, at most 825 days of server-certificate validity), checked against Apple's iOS 18 / macOS 15-era documentation (HT210176 and HT204477). A future OS release could change them; a leaf that stops being trusted after a clean install is where to look first.
- The CA key on the lab host can mint a certificate for any name that devices trusting the CA accept. The state directory is lab-owned and 0700, and host-runner runs execute as the lab user without a sandbox, so an agent in a
hostrun can readca.key. The operator owns this: keep the state directory private, prefer the container runner, or use an operator certificate and never trust a lab CA. Name constraints (above) are the future hardening. - Losing or replacing
ca.crt/ca.keycosts a re-trust on every device. They are in the recommended backup set. ca.keyandleaf.keysit unencrypted at 0600 under<state>/preview/; the vault's master key is not involved, because the leaf has to be usable by the process at startup without an operator step.- A lab-minted leaf needs no operator action for years; an operator certificate is the operator's to renew, and lab shows its expiry and any failed reload in Settings.
- The module defaults bind
:443and:80: opting in withenable = trueis a behavior change for a #105 deployment that usedlistenAddr(the old config now fails evaluation, and the same port now speaks HTTPS). - A new
internal/previewsurface (CertManager,RedirectHandler), a fifthhttp.Serverincmd/labfor the redirect listener, aGET /api/v1/previewandGET /api/v1/preview/capair ininternal/httpapi, and a Settings section in the SPA.