Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(scripts): read quoted and commented .env values as dotenv does
Yudhi Armyndharis45c53baa8707
diff --git a/scripts/lib-env.sh b/scripts/lib-env.shindex a7afdebc4..533fee5c4 100644--- a/scripts/lib-env.sh+++ b/scripts/lib-env.sh@@ -15,15 +15,17 @@# 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. 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.+# Deliberately conservative: only the `KEY=value` forms dotenv reads plainly are honoured. Blanks+# around the `=` and the value, CRLF line endings, a value wrapped in one pair of quotes and a comment+# after an unquoted value are read as dotenv reads them. Anything else (a quoted value followed by a+# comment, a double-quoted value with escapes, a `KEY: value` line) is reported 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. Fails when the layer-# does not set the key (or sets it in a form reported below); a blank value succeeds and prints nothing.+# openwa_env_file_value <file> <key> - print the value from one env-file layer. Returns 1 when the layer+# does not set the key and 2 when it sets it in a form reported below; a blank value succeeds and prints+# nothing.openwa_env_file_value() {local file="$1" key="$2" line value[ -f "$file" ] || return 1@@ -33,23 +35,30 @@ openwa_env_file_value() {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, a trailing" >&2- echo "[config] comment or KEY: value); ignoring it. Pass $key in the environment if it matters here." >&2- return 1+ =*)+ value="${value#=}"+ # Trim both ends, which also drops the CR of a CRLF line.+ value="${value#"${value%%[![:space:]]*}"}"+ value="${value%"${value##*[![:space:]]}"}"+ case "$value" in+ \"*\\*) ;; # dotenv expands \n and \r inside double quotes+ \"*\" | \'*\' | \`*\`)+ printf '%s' "${value:1:${#value}-2}"+ return 0+ ;;+ \"* | \'* | \`*) ;; # the quote does not close the value, as with a trailing comment+ *)+ # An unquoted value ends at a `#`, as in dotenv.+ value="${value%%#*}"+ printf '%s' "${value%"${value##*[![:space:]]}"}"+ return 0+ ;;+ esac;;esac- printf '%s' "$value"+ echo "[config] WARN: $file sets $key in a form these scripts do not parse (a quoted value with a trailing" >&2+ echo "[config] comment or escapes, or KEY: value); using the default. Pass $key in the environment if it matters here." >&2+ return 2}# openwa_writable <path> - whether <path> can be written, or created when it does not exist yet (its@@ -92,19 +101,28 @@ OPENWA_GENERATED_ENV="${DATA_DIR:-./data}/.env.generated"# openwa_resolve <key> <default> - the application's precedence: environment, then ./.env, then# $OPENWA_GENERATED_ENV, then the built-in default. Requires DATA_DIR to be set before sourcing.openwa_resolve() {- local key="$1" fallback="$2" current value layer+ local key="$1" fallback="$2" current value layer rccurrent="$(printenv "$key" 2>/dev/null || true)"if [ -n "$current" ]; thenprintf '%s' "$current"return 0fi# The first layer that sets the key ends the lookup, even with a blank value: dotenv sets a blank- # line to '' and never overwrites a key already set, so the app reads its built-in default.+ # line to '' and never overwrites a key already set, so the app reads its built-in default. A line+ # these scripts cannot parse still sets the key for the app, so it ends the lookup too.for layer in "./.env" "$OPENWA_GENERATED_ENV"; do- if value="$(openwa_env_file_value "$layer" "$key")"; then- printf '%s' "${value:-$fallback}"- return 0- fi+ rc=0+ value="$(openwa_env_file_value "$layer" "$key")" || rc=$?+ case "$rc" in+ 0)+ printf '%s' "${value:-$fallback}"+ return 0+ ;;+ 2)+ printf '%s' "$fallback"+ return 0+ ;;+ esacdoneprintf '%s' "$fallback"}diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.shindex 58946119c..bec191802 100755--- a/scripts/smoke-test-backup-restore.sh+++ b/scripts/smoke-test-backup-restore.sh@@ -48,6 +48,8 @@# error still fails it, however long its output and whatever the host's locale# (ae) the min-content check passes an archive whose listing outgrows a pipe buffer# (af) the online SQLite backup waits out a writer holding the database lock (skipped without sqlite3)+# (ag) quoted values, inline comments and `KEY=""` in ./.env resolve as dotenv reads them, and a line+# the scripts cannot parse ends the lookup at the default instead of reading data/.env.generated## Usage: ./scripts/smoke-test-backup-restore.sh# Requires: bash, tar, node (restore.sh path resolution). sqlite3 is optional (see (c) and (k)).@@ -1401,5 +1403,38 @@ elseecho "SKIP: (af) sqlite3 not found on this host, so there is no database lock to wait on"fi+echo ""+echo "==> (ag) quoted, commented and empty-quoted ./.env values resolve as the app reads them"+# dotenv strips a value's quotes and an unquoted value's comment, and a key it has set keeps+# data/.env.generated from supplying it. The scripts skipped every such line and read the next layer,+# so .env.example's commented PLUGINS_DIR line and a `DATABASE_NAME=""` resolved to values the app+# never uses.+AG="$WORK/ag"+mkdir -p "$AG/data"+cat >"$AG/.env" <<'ENV'+PLUGINS_DIR=./data/plugins # Plugin directory (default: ./data/plugins)+DATABASE_NAME=""+MAIN_DATABASE_NAME='./data/quoted main.sqlite'+BAILEYS_AUTH_DIR=./data/bl#inline+SESSION_DATA_PATH="./data/sess" # quoted, then a comment+ENV+printf 'DATABASE_NAME=./elsewhere/openwa.sqlite\nSESSION_DATA_PATH=./elsewhere/sess\n' >"$AG/data/.env.generated"+resolve_ag() {+ (cd "$AG" && DATA_DIR=./data && . "$REPO_ROOT/scripts/lib-env.sh" && openwa_resolve "$1" "$2") 2>>"$AG/err"+}+# A blank value and an unparsed line both resolve to the default, never to data/.env.generated.+for check in 'PLUGINS_DIR|./data/plugins' 'DATABASE_NAME|DEFAULT' 'MAIN_DATABASE_NAME|./data/quoted main.sqlite' \+ 'BAILEYS_AUTH_DIR|./data/bl' 'SESSION_DATA_PATH|DEFAULT'; do+ key="${check%%|*}"+ got="$(resolve_ag "$key" DEFAULT)"+ if [ "$got" != "${check#*|}" ]; then+ fail "(ag) $key resolved to '$got', expected '${check#*|}'"+ fi+done+if [ "$(grep -c 'do not parse' "$AG/err")" -ne 1 ] || ! grep -q 'sets SESSION_DATA_PATH in a form' "$AG/err"; then+ fail "(ag) the parse warning did not name exactly the one unparsed line: $(cat "$AG/err")"+fi+pass "(ag) dotenv's quoted, commented and empty forms resolve like the app, and an unparsed line stops the lookup"+echo ""echo "All smoke tests passed!"