fix: parse Matrix env files safely
Homelab Main / validate (push) Waiting to run Details
Homelab Main / running-update (push) Blocked by required conditions Details
Homelab Main / debian-ops (push) Blocked by required conditions Details

This commit is contained in:
juvenal.diaz 2026-10-07 10:06:40 -06:00
parent 0977ad1a9b
commit b7f37fcc78
1 changed files with 30 additions and 5 deletions

View File

@ -15,10 +15,35 @@ if [[ ! -f "${ENV_FILE}" ]]; then
exit 1
fi
set -a
# shellcheck disable=SC1090
. "${ENV_FILE}"
set +a
load_env_file() {
local line key value
while IFS= read -r line || [[ -n "${line}" ]]; do
[[ -z "${line}" || "${line}" =~ ^[[:space:]]*# ]] && continue
if [[ "${line}" != *=* ]]; then
echo "Invalid env line without '=' in ${ENV_FILE}" >&2
exit 1
fi
key="${line%%=*}"
value="${line#*=}"
key="${key#"${key%%[![:space:]]*}"}"
key="${key%"${key##*[![:space:]]}"}"
if [[ ! "${key}" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then
echo "Invalid env key '${key}' in ${ENV_FILE}" >&2
exit 1
fi
# Do not source the file. Matrix secrets are generated and may contain
# shell metacharacters such as ';', '&', '$', or backticks. Exporting the
# literal value keeps this file compatible with docker-compose env-file
# syntax and prevents accidental command execution during bootstrap.
export "${key}=${value}"
done <"${ENV_FILE}"
}
load_env_file
MATRIX_SERVER_NAME="${MATRIX_SERVER_NAME:-matrix.lab2025.duckdns.org}"
MATRIX_POSTGRES_DB="${MATRIX_POSTGRES_DB:-synapse}"
@ -71,4 +96,4 @@ echo " server_name ${MATRIX_SERVER_NAME}"
cat <<'EOF'
Next: register users (hermes bot, and any humans) via the shared-secret CLI:
docker exec -u 991 matrix-synapse register_new_matrix_user -c /data/homeserver.yaml -u NAME -p PASS [--no-admin]
EOF
EOF