Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): let a blank .env line end the config lookup

Yudhi Armyndharis8fd70611fc40
diff --git a/scripts/lib-env.sh b/scripts/lib-env.sh
index 252a450b5..a7afdebc4 100644
--- a/scripts/lib-env.sh
+++ b/scripts/lib-env.sh
@@ -22,13 +22,14 @@
# 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 <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() {
local file="$1" key="$2" line value
- [ -f "$file" ] || return 0
+ [ -f "$file" ] || return 1
# 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
+ [ -n "$line" ] || return 1
value="${line#*"$key"}"
value="${value#"${value%%[![:space:]]*}"}"
case "$value" in
@@ -45,7 +46,7 @@ openwa_env_file_value() {
*\"* | *\'* | *'#'*)
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 0
+ return 1
;;
esac
printf '%s' "$value"
@@ -97,10 +98,11 @@ openwa_resolve() {
printf '%s' "$current"
return 0
fi
+ # 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.
for layer in "./.env" "$OPENWA_GENERATED_ENV"; do
- value="$(openwa_env_file_value "$layer" "$key")"
- if [ -n "$value" ]; then
- printf '%s' "$value"
+ if value="$(openwa_env_file_value "$layer" "$key")"; then
+ printf '%s' "${value:-$fallback}"
return 0
fi
done
diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.sh
index e3b482c43..385d1c593 100755
--- a/scripts/smoke-test-backup-restore.sh
+++ b/scripts/smoke-test-backup-restore.sh
@@ -41,6 +41,7 @@
# both scripts whatever the uid
# (aa) engine auth state copied from a running app (an open whatsapp-web.js profile, Baileys state)
# is noted in the archive and printed by restore, which does not refuse it even with --strict
+# (ab) a blank ./.env line keeps data/.env.generated from supplying the key, so the default applies
#
# Usage: ./scripts/smoke-test-backup-restore.sh
# Requires: bash, tar, node (restore.sh path resolution). sqlite3 is optional (see (c) and (k)).
@@ -1228,5 +1229,24 @@ if tar -tzf "$(ls "$AA"/quiet-out/openwa-backup-*.tar.gz)" | grep -q 'ENGINE-STA
fi
pass "(aa) live engine auth state is noted in the archive and printed, never refused"
+echo ""
+echo "==> (ab) a blank line in ./.env hides the key from data/.env.generated, as it does in the app"
+# dotenv sets a blank ./.env line to '' and never overwrites a key that is already set, so the app
+# reads its built-in default and never sees .env.generated's value. Falling through to that value
+# archived a database the app does not use, and the run exited 0.
+AB="$WORK/ab"
+mkdir -p "$AB/data" "$AB/elsewhere" "$AB/extract"
+make_fixture "$AB/data/main.sqlite" "bravo2-main"
+make_fixture "$AB/data/openwa.sqlite" "bravo2-default"
+make_fixture "$AB/elsewhere/openwa.sqlite" "WRONG-data"
+printf 'DATABASE_NAME=\n' >"$AB/.env"
+printf 'DATABASE_NAME=%s\n' "$AB/elsewhere/openwa.sqlite" >"$AB/data/.env.generated"
+(cd "$AB" && BACKUP_DIR="$AB/out" "$BACKUP" >/dev/null 2>&1)
+tar -xzf "$(ls "$AB"/out/openwa-backup-*.tar.gz)" -C "$AB/extract"
+if [ "$(db_fingerprint "$AB/extract/openwa.sqlite")" != "bravo2-default" ]; then
+ fail "(ab) a blank DATABASE_NAME in ./.env fell through to data/.env.generated"
+fi
+pass "(ab) a blank ./.env line stops the lookup and the built-in default applies"
+
echo ""
echo "All smoke tests passed!"