Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(scripts): read .env lines the way dotenv does
Yudhi Armyndharisc53ca5e7a3e9
diff --git a/docs/11-operational-runbooks.md b/docs/11-operational-runbooks.mdindex 4947738ba..004bec6cb 100644--- a/docs/11-operational-runbooks.md+++ b/docs/11-operational-runbooks.md@@ -659,7 +659,8 @@ OPENWA_DATA_DIR=/srv/openwa/data \> replaces the target's and is the one the restored app reads. Two caveats when> operating directly on the host mount: a path recorded inside the container (`/app/data/...`) is not> host-visible, so override it in the environment; and a value written with quotes or a trailing `#`-> comment is reported and skipped rather than guessed at, so pass those explicitly too.+> comment, or a `KEY: value` line, is reported and skipped rather than guessed at, so pass those+> explicitly too. Blanks around `=` and CRLF line endings are read as the app reads them.**Verification:**diff --git a/scripts/lib-env.sh b/scripts/lib-env.shindex a0a86bd0a..52e6f4e54 100644--- a/scripts/lib-env.sh+++ b/scripts/lib-env.sh@@ -15,25 +15,36 @@# database left at a default path from BEFORE the operator switched is archived instead, and the run# exits 0. A backup that captured an abandoned database only reveals itself during a restore.#-# Deliberately conservative: only a plain `KEY=value` line is honoured. A value carrying quotes or a-# `#` is reported and skipped rather than guessed at, because a silently mis-parsed path is the exact-# failure this exists to prevent. Nothing here exports anything — each key is looked up by name, so a-# stray entry in an operator's .env can never reach the script's own environment.+# Deliberately conservative: only a plain `KEY=value` line is honoured. Blanks around the `=` and the+# value, and CRLF line endings, are tolerated as dotenv tolerates them. A value carrying quotes or a+# `#`, and a `KEY: value` line, are reported and skipped rather than guessed at, because a silently+# mis-parsed path is the exact failure this exists to prevent. Nothing here exports anything — each+# key is looked up by name, so a stray entry in an operator's .env can never reach the script's own+# environment.# openwa_env_file_value <file> <key> — print the value from one env-file layer, or nothing.openwa_env_file_value() {local file="$1" key="$2" line value[ -f "$file" ] || return 0- line="$(grep -E "^[[:space:]]*(export[[:space:]]+)?${key}=" "$file" 2>/dev/null | tail -n 1)" || true+ # The last line naming the key wins, as in dotenv. `KEY: value` is matched only to be reported.+ line="$(grep -E "^[[:space:]]*(export[[:space:]]+)?${key}[[:space:]]*[=:]" "$file" 2>/dev/null | tail -n 1)" || true[ -n "$line" ] || return 0- value="${line#*=}"+ value="${line#*"$key"}"+ value="${value#"${value%%[![:space:]]*}"}"+ case "$value" in+ =*) value="${value#=}" ;;+ *) value='#' ;; # the colon form: fall into the report below+ esac+ # Trim both ends, which also drops the CR of a CRLF line.+ value="${value#"${value%%[![:space:]]*}"}"+ value="${value%"${value##*[![:space:]]}"}"case "$value" in'')return 0;;*\"* | *\'* | *'#'*)- echo "[config] WARN: $file sets $key in a form these scripts do not parse (quotes or a trailing" >&2- echo "[config] comment) — ignoring it. Pass $key in the environment if it matters here." >&2+ echo "[config] WARN: $file sets $key in a form these scripts do not parse (quotes, a trailing" >&2+ echo "[config] comment or KEY: value); ignoring it. Pass $key in the environment if it matters here." >&2return 0;;esacdiff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.shindex 4e4f8f355..28b394b9a 100755--- a/scripts/smoke-test-backup-restore.sh+++ b/scripts/smoke-test-backup-restore.sh@@ -27,6 +27,8 @@# (r) a symlinked state dir is archived by content and restored through the link, and an archive# member that is itself a symlink is refused# (s) restored state lands where the restored data/.env.generated points, below ./.env+# (t) ./.env lines with CRLF endings, blanks around = or trailing blanks resolve as dotenv reads them,+# and a `KEY: value` line is reported## Usage: ./scripts/smoke-test-backup-restore.sh# Requires: bash, tar, node (restore.sh path resolution). sqlite3 is optional (see (c) and (k)).@@ -827,5 +829,65 @@ if [ "$(cat "$S/dst-env/data/env-sessions/session-s1/marker" 2>/dev/null || truefipass "(s) restored state follows the restored data/.env.generated, and ./.env still wins"+echo ""+echo "==> (t) ./.env lines with CRLF endings, spaces around = or trailing blanks read as the app reads them"+# The app loads ./.env with dotenv, which drops a CR, trims the value and accepts `KEY = value`. The+# scripts kept the CR and the blanks in the path and skipped the spaced line without a word, so a+# backup fell back to a stale default and a restore wrote `custom.sqlite<CR>` beside the database the+# app opens, past a live-target guard that probed the wrong name.+T="$WORK/t"+mkdir -p "$T/src/data/sess/session-s1" "$T/src/live" "$T/dst/data"+make_fixture "$T/src/live/auth.sqlite" "tango-main"+make_fixture "$T/src/live/store.sqlite" "tango-data"+make_fixture "$T/src/data/main.sqlite" "STALE-main"+make_fixture "$T/src/data/openwa.sqlite" "STALE-data"+printf 'tango-session\n' >"$T/src/data/sess/session-s1/marker"+printf 'MAIN_DATABASE_NAME = %s\r\nDATABASE_NAME=%s\r\nSESSION_DATA_PATH=./data/sess \r\nBAILEYS_AUTH_DIR: ./data/bl\r\n' \+ "$T/src/live/auth.sqlite" "$T/src/live/store.sqlite" >"$T/src/.env"+set +e+OUT_T="$(cd "$T/src" && BACKUP_DIR="$T/out" "$BACKUP" 2>&1)"+RC_T=$?+set -e+if [ "$RC_T" -ne 0 ]; then+ fail "(t) backup failed on a CRLF ./.env: $OUT_T"+fi+mkdir -p "$T/extract"+tar -xzf "$(ls "$T"/out/openwa-backup-*.tar.gz)" -C "$T/extract"+if [ "$(db_fingerprint "$T/extract/main.sqlite")" != "tango-main" ]; then+ fail "(t) a \`MAIN_DATABASE_NAME = path\` line in ./.env was skipped and the stale default archived"+fi+if [ "$(db_fingerprint "$T/extract/openwa.sqlite")" != "tango-data" ]; then+ fail "(t) a CRLF DATABASE_NAME line in ./.env did not resolve to the database it names"+fi+if [ "$(cat "$T/extract/sessions/session-s1/marker" 2>/dev/null || true)" != "tango-session" ]; then+ fail "(t) a SESSION_DATA_PATH with trailing blanks did not resolve to the sessions dir"+fi+if ! printf '%s' "$OUT_T" | grep -q 'sets BAILEYS_AUTH_DIR in a form these scripts do not parse'; then+ fail "(t) a \`KEY: value\` line was skipped without the warning"+fi+printf 'DATABASE_NAME=./data/custom.sqlite\r\nMAIN_DATABASE_NAME = ./data/custom-main.sqlite \r\n' >"$T/dst/.env"+make_fixture "$T/dst/data/custom.sqlite" "tango-live"+set +e+OUT_T="$(cd "$T/dst" && "$RESTORE" "$(ls "$T"/out/openwa-backup-*.tar.gz)" 2>&1)"+RC_T=$?+set -e+if [ "$RC_T" -eq 0 ]; then+ fail "(t) restore without --force wrote past the live database a CRLF DATABASE_NAME names"+fi+(+ cd "$T/dst"+ "$RESTORE" "$(ls "$T"/out/openwa-backup-*.tar.gz)" --force >/dev/null+)+if [ "$(db_fingerprint "$T/dst/data/custom.sqlite")" != "tango-data" ]; then+ fail "(t) restore did not write the data store to the path the CRLF line names"+fi+if [ "$(db_fingerprint "$T/dst/data/custom-main.sqlite")" != "tango-main" ]; then+ fail "(t) restore did not write the main DB to the path the spaced line names"+fi+if [ -n "$(find "$T/dst/data" -name "*$(printf '\r')*" 2>/dev/null)" ]; then+ fail "(t) restore created a file whose name ends in a carriage return"+fi+pass "(t) CRLF, spaced and blank-padded ./.env lines resolve like dotenv, and \`KEY: value\` is reported"+echo ""echo "All smoke tests passed!"