From b7f37fcc78de4986b6016c3130b93117a455d883 Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Wed, 7 Oct 2026 10:06:40 -0600 Subject: [PATCH] fix: parse Matrix env files safely --- infra/matrix-server/bootstrap.sh | 35 +++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/infra/matrix-server/bootstrap.sh b/infra/matrix-server/bootstrap.sh index 518bf7f..4087040 100644 --- a/infra/matrix-server/bootstrap.sh +++ b/infra/matrix-server/bootstrap.sh @@ -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 \ No newline at end of file +EOF