fix(docker): run backend as non-root, keep env files out, never silently rotate auth keys #93

Merged
joshuafhiggins merged 1 commits from t3/harden-docker into main 2026-11-16 21:43:58 -05:00
Owner

Three problems in the Docker image:

  • The Convex backend and its Node actions ran as root. Only the web server dropped privileges.
  • .dockerignore only excluded .env* files at the context root, so apps/web/.env.local and packages/convex/.env.local were copied into the build. Next.js inlines NEXT_PUBLIC_* values, and the Convex CLI reads .env.local on deploy.
  • env get JWKS ... || true treated any CLI failure as "no keys yet", so a transient error regenerated the signing keys and signed every user out.

Fix

  • New convex system user runs run_backend.sh, generate_admin_key.sh and the Convex CLI. On boot the entrypoint chowns anything in /convex/data not owned by convex, so volumes from older root-run images upgrade in place (without this, read_credentials.sh would mint a new instance secret). convex is in the otunes group, and /tmp/otunes is setgid and group-writable so the HLS cleanup cron can still delete the web server's streams.
  • .dockerignore now uses **/.env and **/.env.* (with !**/.env.example).
  • Keys are generated only when env get JWKS exits 0 with empty output. Any failure stops the container with an error.

Verification

  • Full docker build of main and this branch (all stages).
  • With canary .env.local files in both workspaces: main's build context includes them, this branch's does not; no canary in the image.
  • Upgrade: ran main's image on a fresh volume, signed up, then started this image on the same volume. Data volume re-owned by convex, instance secret unchanged, no key regeneration, old access token still valid, refresh token works. ps shows backend as convex, web as otunes; only tini and the entrypoint supervisor stay root.
  • Fresh volume generates keys once; restart keeps the session.
  • Forcing env get to fail (bad admin key) exits 1 with "Could not read Convex Auth signing keys"; the next normal boot still accepts the existing session.

Changes made by Claude Opus 5.5 in Claude Code (via T3 Code).

🤖 Generated with Claude Code

Three problems in the Docker image: - The Convex backend and its Node actions ran as root. Only the web server dropped privileges. - `.dockerignore` only excluded `.env*` files at the context root, so `apps/web/.env.local` and `packages/convex/.env.local` were copied into the build. Next.js inlines `NEXT_PUBLIC_*` values, and the Convex CLI reads `.env.local` on deploy. - `env get JWKS ... || true` treated any CLI failure as "no keys yet", so a transient error regenerated the signing keys and signed every user out. ## Fix - New `convex` system user runs `run_backend.sh`, `generate_admin_key.sh` and the Convex CLI. On boot the entrypoint chowns anything in `/convex/data` not owned by `convex`, so volumes from older root-run images upgrade in place (without this, `read_credentials.sh` would mint a new instance secret). `convex` is in the `otunes` group, and `/tmp/otunes` is setgid and group-writable so the HLS cleanup cron can still delete the web server's streams. - `.dockerignore` now uses `**/.env` and `**/.env.*` (with `!**/.env.example`). - Keys are generated only when `env get JWKS` exits 0 with empty output. Any failure stops the container with an error. ## Verification - Full `docker build` of main and this branch (all stages). - With canary `.env.local` files in both workspaces: main's build context includes them, this branch's does not; no canary in the image. - Upgrade: ran main's image on a fresh volume, signed up, then started this image on the same volume. Data volume re-owned by `convex`, instance secret unchanged, no key regeneration, old access token still valid, refresh token works. `ps` shows backend as `convex`, web as `otunes`; only tini and the entrypoint supervisor stay root. - Fresh volume generates keys once; restart keeps the session. - Forcing `env get` to fail (bad admin key) exits 1 with "Could not read Convex Auth signing keys"; the next normal boot still accepts the existing session. Changes made by Claude Opus 5.5 in Claude Code (via T3 Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
joshuafhiggins added 1 commit 2026-11-16 13:52:44 -05:00
- Run the Convex backend, admin key generation and CLI as a dedicated convex
  user; chown volumes left root-owned by older images on boot.
- Exclude .env files in every workspace from the build context.
- Only generate Convex Auth keys when 'env get JWKS' succeeds and is empty;
  fail startup on any other error instead of signing everyone out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
joshuafhiggins merged commit 2427ca1cda into main 2026-11-16 21:43:58 -05:00
Sign in to join this conversation.
No Reviewers
No labels
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: joshuafhiggins/otunes#93