LibrePortal/scripts/config/password/password_generate.sh
librelad 5706498565 fix(secrets): move app credentials into <app>.config, fix slot collision
Five apps (mastodon, owncloud, mattermost, matrix, stoat) took their generated
secrets from the compose-side generator tags PASSWORD_TAG_<n>/RANDOM_TAG_<n>/
HEX_TAG_<n>/VAPID_TAG_<n>. Those mint a fresh secret on every templating run, so
a reinstall handed the app a new database password while its data volume kept the
one initdb was given, and the app came back up unable to open its own database.

Move them to <app>.config as RANDOMIZED* placeholders, reaching the compose via
the #LIBREPORTAL|<APP>_<KEY>_TAG| mechanism tags_processor_app_config_values
already provides. No new handler: the tag name is derived from the config key, so
this is a config line plus a tag per secret. Generation is unchanged — still
random on first install; the value is now remembered instead of re-rolled.

Also fixes two things this exposed:

- The RANDOMIZED* replacers matched unanchored. `sort -u` orders slots lexically
  (1, 10, 11, 2), so slot 1's pattern rewrote the prefix inside slot 10's
  placeholder and slots 10+ ended up holding slot 1's secret with a digit glued
  on — derivable, and invisible because the values weren't byte-identical.
  Anchoring with \b makes match order irrelevant. Verified at 20 slots across
  all four placeholder types: 64 keys, 64 distinct values, no prefix collisions.

- generateRandomPassword drew from base64 without constraining the mix; measured
  over 2000 draws, 1 in 40 contained no digit at all. Retry until the result has
  both a digit and a letter, bounded so a pathological length can't spin.

owncloud gains a fix in passing: its compose seeded the admin account from
PASSWORD_TAG_2 while the WebUI displayed CFG_OWNCLOUD_ADMIN_PASSWORD, which was
generated separately and never used. Both now read the same value.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-18 19:33:40 +01:00

30 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
generateRandomPassword()
{
local password=""
local length=${CFG_GENERATED_PASS_LENGTH:-20} # Default to 20 if not set
local attempt
# Letters and numbers only, no special characters: these values are embedded
# in connection URLs (Mattermost's Postgres DSN), interpolated into container
# entrypoints and written into YAML, and a symbol would need different
# escaping in each.
#
# Retry until the result carries at least one digit AND one letter. base64 of
# urandom is alphanumeric but says nothing about the mix — measured over 2000
# draws at the default length, 1 in 40 came back with no digit at all, which
# trips any policy that requires one. At that hit rate the retry is free.
# Bounded rather than `while true` so a pathological length (or an empty
# /dev/urandom read) can't spin forever; a sub-2 length can't hold both
# classes at all, so it is accepted as-is.
for attempt in {1..50}; do
password=$(dd if=/dev/urandom bs=64 count=1 2>/dev/null | base64 | tr -d '+/=' | head -c $length)
[[ ${#password} -eq $length ]] || continue
[[ $length -lt 2 ]] && break
[[ "$password" == *[0-9]* && "$password" == *[A-Za-z]* ]] && break
done
echo "$password"
}