From 98651ecbf8fbbc77eb79f0a1a55de34ccca76227 Mon Sep 17 00:00:00 2001 From: Georges-Antoine Assi Date: Mon, 27 Jul 2026 08:07:50 -0400 Subject: [PATCH] Fix ROMM_AUTH_SECRET_KEY auto-generation under nounset The init script runs with `set -o nounset`, so testing `${ROMM_AUTH_SECRET_KEY}` aborted with "unbound variable" before reaching the generation branch, leaving the container in a restart loop. This only triggered when the variable was omitted entirely; shipping it with an empty value took the working path. Use `${ROMM_AUTH_SECRET_KEY-}` so the unset case is testable, and warn instead of info since a generated key changes on every restart. Co-Authored-By: Claude Opus 5 (1M context) --- docker/init_scripts/init | 4 ++-- docs/BACKEND_ARCHITECTURE.md | 22 +++++++++++----------- entrypoint.sh | 2 +- env.template | 2 +- examples/docker-compose.example.yml | 2 +- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/docker/init_scripts/init b/docker/init_scripts/init index 43a3cfd85..0ec739f86 100755 --- a/docker/init_scripts/init +++ b/docker/init_scripts/init @@ -332,9 +332,9 @@ if ! printenv | grep -q '^OTEL_'; then fi # Set ROMM_AUTH_SECRET_KEY if not already set -if [[ -z ${ROMM_AUTH_SECRET_KEY} ]]; then +if [[ -z ${ROMM_AUTH_SECRET_KEY-} ]]; then ROMM_AUTH_SECRET_KEY=$(python3 -c "import secrets; print(secrets.token_hex(32))") - info_log "ROMM_AUTH_SECRET_KEY not set, generating random secret key" + warn_log "ROMM_AUTH_SECRET_KEY not set, generating a random secret key. It changes on every restart, invalidating API bearer tokens and pending invite/password-reset links. Set it explicitly to keep those valid, and always set it when running more than one replica." export ROMM_AUTH_SECRET_KEY fi diff --git a/docs/BACKEND_ARCHITECTURE.md b/docs/BACKEND_ARCHITECTURE.md index 45db82747..2938aaf20 100644 --- a/docs/BACKEND_ARCHITECTURE.md +++ b/docs/BACKEND_ARCHITECTURE.md @@ -1541,17 +1541,17 @@ Falls back to `FakeRedis` in test mode. #### Authentication -| Variable | Default | Description | -| ------------------------------------ | --------- | ------------------------------------- | -| `ROMM_AUTH_SECRET_KEY` | | **Required.** JWT/session signing key | -| `OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS` | `1800` | 30 minutes | -| `OAUTH_REFRESH_TOKEN_EXPIRE_SECONDS` | `604800` | 7 days | -| `SESSION_MAX_AGE_SECONDS` | `1209600` | 14 days | -| `DISABLE_CSRF_PROTECTION` | `false` | Disable CSRF | -| `DISABLE_DOWNLOAD_ENDPOINT_AUTH` | `false` | Allow unauthenticated downloads | -| `DISABLE_USERPASS_LOGIN` | `false` | Disable password login | -| `DISABLE_LOGS_VIEWER` | `false` | Disable backend log viewer + endpoint | -| `KIOSK_MODE` | `false` | Read-only anonymous access | +| Variable | Default | Description | +| ------------------------------------ | --------- | ----------------------------------------- | +| `ROMM_AUTH_SECRET_KEY` | | JWT/session signing key (random if unset) | +| `OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS` | `1800` | 30 minutes | +| `OAUTH_REFRESH_TOKEN_EXPIRE_SECONDS` | `604800` | 7 days | +| `SESSION_MAX_AGE_SECONDS` | `1209600` | 14 days | +| `DISABLE_CSRF_PROTECTION` | `false` | Disable CSRF | +| `DISABLE_DOWNLOAD_ENDPOINT_AUTH` | `false` | Allow unauthenticated downloads | +| `DISABLE_USERPASS_LOGIN` | `false` | Disable password login | +| `DISABLE_LOGS_VIEWER` | `false` | Disable backend log viewer + endpoint | +| `KIOSK_MODE` | `false` | Read-only anonymous access | #### OIDC diff --git a/entrypoint.sh b/entrypoint.sh index d14bfa181..74ecd13d5 100644 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -31,7 +31,7 @@ function handle_termination() { trap handle_termination SIGTERM SIGINT # Set ROMM_AUTH_SECRET_KEY if not already set -if [[ -z ${ROMM_AUTH_SECRET_KEY} ]]; then +if [[ -z ${ROMM_AUTH_SECRET_KEY-} ]]; then ROMM_AUTH_SECRET_KEY=$(python3 -c "import secrets; print(secrets.token_hex(32))") export ROMM_AUTH_SECRET_KEY fi diff --git a/env.template b/env.template index 143bc314e..5d2a64e97 100644 --- a/env.template +++ b/env.template @@ -24,7 +24,7 @@ REDIS_DB=0 # Database number for the Redis/Valkey instance REDIS_SSL=false # Enable SSL (rediss://) for the Redis/Valkey connection # Authentication -ROMM_AUTH_SECRET_KEY= # App secret, generate with `openssl rand -hex 32` [REQUIRED] +ROMM_AUTH_SECRET_KEY= # App secret, generate with `openssl rand -hex 32`. Randomly generated at startup if left blank, which invalidates API tokens and invite/reset links on every restart OAUTH_ACCESS_TOKEN_EXPIRE_SECONDS=1800 # Access token lifetime in seconds OAUTH_REFRESH_TOKEN_EXPIRE_SECONDS=604800 # Refresh token lifetime in seconds SESSION_MAX_AGE_SECONDS=1209600 # Maximum age of a session in seconds diff --git a/examples/docker-compose.example.yml b/examples/docker-compose.example.yml index 827444b48..20b0fc127 100644 --- a/examples/docker-compose.example.yml +++ b/examples/docker-compose.example.yml @@ -13,7 +13,7 @@ services: - DB_NAME=romm # Should match MARIADB_DATABASE in mariadb - DB_USER=romm-user # Should match MARIADB_USER in mariadb - DB_PASSWD= # Should match MARIADB_PASSWORD in mariadb - - ROMM_AUTH_SECRET_KEY= # Generate a key with `openssl rand -hex 32` + - ROMM_AUTH_SECRET_KEY= # Generate a key with `openssl rand -hex 32`, otherwise a random one is generated on each restart (invalidating API tokens) - SCREENSCRAPER_USER= # These are the recommended metadata providers - SCREENSCRAPER_PASSWORD= # https://docs.romm.app/latest/Getting-Started/Metadata-Providers/#screenscraper - RETROACHIEVEMENTS_API_KEY= # https://docs.romm.app/latest/Getting-Started/Metadata-Providers/#retroachievements