Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(scripts): honour NODE_EXTRA_CA_CERTS and TLS settings across backup and restore (#1826)

Yudhi Armyndharis5022f6aa0d4a
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d9fd98337..41d1a4341 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -29,6 +29,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Drop whatsapp-web.js inbound media over `MEDIA_DOWNLOAD_MAX_BYTES` in the page when the sender understates its size.
+- Trust `NODE_EXTRA_CA_CERTS` in `backup.sh`'s PostgreSQL TLS check on Node older than 22.15.
+- Set `PGSSLMODE` in the `psql` import command `restore.sh` prints when `DATABASE_SSL=true`.
- Use the `OPENWA_DATA_DIR` volume's `media/`, even before it exists, for a leftover `STORAGE_LOCAL_PATH=./uploads` in host-side `backup.sh` and `restore.sh` runs, instead of the working directory's `./uploads`.
- Read the `./data` database defaults and `./data/...` paths in `./.env` under `OPENWA_DATA_DIR` in host-side `backup.sh` and `restore.sh` runs, instead of the working directory's `./data`.
- Probe the configured `PORT` in the Docker image healthcheck instead of a fixed 2785.
diff --git a/docs/11-operational-runbooks.md b/docs/11-operational-runbooks.md
index d9d8cfa80..b7b4ca602 100644
--- a/docs/11-operational-runbooks.md
+++ b/docs/11-operational-runbooks.md
@@ -624,7 +624,11 @@ tar -xzOf "$BACKUP_DIR/openwa-backup-<timestamp>.tar.gz" ./database.sql | sed '/
# An external PostgreSQL server: rename the upgraded database and create an empty one under the
# DATABASE_NAME the app uses in the same way, then load the dump into it. DATABASE_URL is not an
# OpenWA setting: fill in your own URL for that database, such as
-# postgres://<user>@<host>:5432/<database>, with the password in PGPASSWORD
+# postgres://<user>@<host>:5432/<database>, with the password in PGPASSWORD. With DATABASE_SSL=true,
+# run psql under PGSSLMODE=verify-full (require when DATABASE_SSL_REJECT_UNAUTHORIZED=false), as the
+# command restore.sh prints does; libpq's default sslmode=prefer accepts any certificate. verify-full
+# also needs PGSSLROOTCERT set to the server's CA file: libpq otherwise reads ~/.postgresql/root.crt,
+# and sslrootcert=system needs libpq 16+ and a system CA store, which the image lacks
tar -xzOf "$BACKUP_DIR/openwa-backup-<timestamp>.tar.gz" ./database.sql | sed '/^SET transaction_timeout = 0;$/d' |
psql -v ON_ERROR_STOP=1 "$DATABASE_URL"
@@ -914,7 +918,12 @@ tar -xzOf ./backups/openwa-backup-<timestamp>.tar.gz ./database.sql | sed '/^SET
# An external PostgreSQL server: rename the current database and create an empty one under the
# DATABASE_NAME the app uses in the same way, then load the dump into it. DATABASE_URL is not an
# OpenWA setting: fill in your own URL for that database, such as
-# postgres://<user>@<host>:5432/<database>, with the password in PGPASSWORD
+# postgres://<user>@<host>:5432/<database>, with the password in PGPASSWORD. With
+# DATABASE_SSL=true, run psql under PGSSLMODE=verify-full (require when
+# DATABASE_SSL_REJECT_UNAUTHORIZED=false), as the command restore.sh prints does; libpq's default
+# sslmode=prefer accepts any certificate. verify-full also needs PGSSLROOTCERT set to the server's
+# CA file: libpq otherwise reads ~/.postgresql/root.crt, and sslrootcert=system needs libpq 16+
+# and a system CA store, which the image lacks
tar -xzOf ./backups/openwa-backup-<timestamp>.tar.gz ./database.sql | sed '/^SET transaction_timeout = 0;$/d' |
psql -v ON_ERROR_STOP=1 "$DATABASE_URL"
diff --git a/scripts/backup.sh b/scripts/backup.sh
index 87f0c5003..7555effbe 100755
--- a/scripts/backup.sh
+++ b/scripts/backup.sh
@@ -239,8 +239,15 @@ if [ "$DATABASE_TYPE" = "postgres" ]; then
if [ -z "${PGSSLROOTCERT:-}" ] && command -v node >/dev/null 2>&1; then
PG_ROOT_CERTS="$(mktemp)"
trap 'rm -rf "$STAGE" "$PG_ROOT_CERTS"' EXIT
+ # Before Node 22.15 there is no getCACertificates, and rootCertificates holds only the bundled
+ # roots, so NODE_EXTRA_CA_CERTS is added from its file. An unreadable file is skipped, as Node
+ # itself skips it after the warning it prints at startup.
node -e 'const tls = require("tls");
- console.log((tls.getCACertificates ? tls.getCACertificates("default") : tls.rootCertificates).join("\n"))' \
+ let certs = tls.getCACertificates ? tls.getCACertificates("default") : tls.rootCertificates;
+ if (!tls.getCACertificates && process.env.NODE_EXTRA_CA_CERTS) {
+ try { certs = certs.concat(require("fs").readFileSync(process.env.NODE_EXTRA_CA_CERTS, "utf8")); } catch {}
+ }
+ console.log(certs.join("\n"))' \
>"$PG_ROOT_CERTS"
elif [ -z "${PGSSLROOTCERT:-}" ]; then
log "node not found: verifying the server against the system CA store (sslrootcert=system," \
diff --git a/scripts/restore.sh b/scripts/restore.sh
index c391acec8..94f805ba9 100755
--- a/scripts/restore.sh
+++ b/scripts/restore.sh
@@ -479,7 +479,31 @@ if [ -f "$STAGE/database.sql" ]; then
log "Postgres dump present: load it into an EMPTY database, as docs/11-operational-runbooks.md"
log "(Restore from Backup, step 3) shows for the built-in openwa-postgres container. For an external"
log "server, with DATABASE_URL set to your own URL for that database:"
- log " sed '/^SET transaction_timeout = 0;\$/d' $DATA_DIR/database.sql | psql -v ON_ERROR_STOP=1 \"\$DATABASE_URL\""
+ # psql defaults to sslmode=prefer, which falls back to plaintext and takes any certificate, so the
+ # printed command asks for the TLS mode the app uses. The restore is already done: a TLS line the
+ # scripts cannot parse leaves the check on rather than stopping it.
+ PSQL_SSL=""
+ PSQL_UNREAD=""
+ if [ -z "${PGSSLMODE:-}" ]; then
+ PSQL_TLS="$(openwa_resolve DATABASE_SSL false 2>/dev/null)" || { PSQL_TLS=true && PSQL_UNREAD=DATABASE_SSL; }
+ if [ "$PSQL_TLS" = true ]; then
+ PSQL_SSL="PGSSLMODE=verify-full "
+ PSQL_REJECT="$(openwa_resolve DATABASE_SSL_REJECT_UNAUTHORIZED true 2>/dev/null)" ||
+ PSQL_UNREAD="${PSQL_UNREAD:+$PSQL_UNREAD and }DATABASE_SSL_REJECT_UNAUTHORIZED"
+ if [ "$PSQL_REJECT" = false ]; then
+ PSQL_SSL="PGSSLMODE=require "
+ fi
+ fi
+ fi
+ log " sed '/^SET transaction_timeout = 0;\$/d' $DATA_DIR/database.sql | ${PSQL_SSL}psql -v ON_ERROR_STOP=1 \"\$DATABASE_URL\""
+ if [ -n "$PSQL_UNREAD" ]; then
+ log "($PSQL_UNREAD could not be read from the configuration, so the command takes the stricter setting)"
+ fi
+ if [ "$PSQL_SSL" = "PGSSLMODE=verify-full " ] && [ -z "${PGSSLROOTCERT:-}" ]; then
+ log "(verify-full needs the server's CA: set PGSSLROOTCERT to its CA file, such as the NODE_EXTRA_CA_CERTS"
+ log "file for a private CA; libpq otherwise reads ~/.postgresql/root.crt, and sslrootcert=system needs libpq 16+"
+ log "and a system CA store, which the image lacks)"
+ fi
log "(sed drops a setting this image's pg_dump 17 writes and PostgreSQL 16 rejects)"
fi
diff --git a/scripts/smoke-test-backup-restore.sh b/scripts/smoke-test-backup-restore.sh
index e0c330f8d..bc767f576 100755
--- a/scripts/smoke-test-backup-restore.sh
+++ b/scripts/smoke-test-backup-restore.sh
@@ -51,9 +51,11 @@
# another line does not hide the key, and a line the scripts cannot parse fails the lookup, so
# backup and restore stop before archiving or writing anything instead of using a default the app
# may not read
-# (ah) pg_dump makes the app's TLS check when DATABASE_SSL=true (falling back to the system CA store
-# without node), connects as before when it is not, and never starts past a DATABASE_SSL or
-# DATABASE_SSL_REJECT_UNAUTHORIZED line the scripts cannot parse
+# (ah) pg_dump makes the app's TLS check when DATABASE_SSL=true (trusting NODE_EXTRA_CA_CERTS on a
+# Node without tls.getCACertificates, falling back to the system CA store without node), connects
+# as before when it is not, and never starts past a DATABASE_SSL or DATABASE_SSL_REJECT_UNAUTHORIZED
+# line the scripts cannot parse; the psql command restore prints asks for the same TLS mode and
+# says where verify-full takes its CA from
# (ai) the ./data defaults and ./data paths in ./.env and .env.generated follow OPENWA_DATA_DIR in a
# run on the host, in both scripts, as does a leftover ./uploads, also into an empty volume, while a
# bare-metal ./data named by another spelling or a symlink keeps ./uploads, non-path settings and
@@ -71,7 +73,8 @@ set -euo pipefail
unset OPENWA_DATA_DIR BACKUP_DIR DATABASE_TYPE MAIN_DATABASE_NAME DATABASE_NAME SESSION_DATA_PATH \
BAILEYS_AUTH_DIR STORAGE_LOCAL_PATH PLUGINS_DIR PLUGIN_STATE_DIR OPENWA_RESTORE_SNAPSHOT_DIR BOOTSTRAP_KEY_FILE \
ENGINE_TYPE DATABASE_URL DATABASE_HOST DATABASE_PORT DATABASE_USERNAME DATABASE_PASSWORD \
- DATABASE_SSL DATABASE_SSL_REJECT_UNAUTHORIZED PGSSLMODE PGSSLROOTCERT
+ DATABASE_SSL DATABASE_SSL_REJECT_UNAUTHORIZED PGSSLMODE PGSSLROOTCERT \
+ NODE_EXTRA_CA_CERTS
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
BACKUP="$REPO_ROOT/scripts/backup.sh"
@@ -1891,10 +1894,47 @@ fi
if [ "${BASH_REMATCH[2]}" -lt 100 ] || [ -e "${BASH_REMATCH[1]}" ]; then
fail "(ah) the root certificates were not Node's CA set, or were left behind: $GOT_AH"
fi
+# A private CA in NODE_EXTRA_CA_CERTS is trusted too. Before Node 22.15 tls.getCACertificates is
+# missing and tls.rootCertificates leaves that file out; the preload stands in for such a Node.
+node -p 'require("tls").rootCertificates[0]' >"$AH/extra-ca.pem"
+printf 'delete require("tls").getCACertificates;\n' >"$AH/old-node.js"
+for preload in "" "--require=$AH/old-node.js"; do
+ GOT_AH="$(backup_ah "${PG_AH}DATABASE_SSL=true\n" NODE_OPTIONS="$preload")"
+ GOT_AH_EXTRA="$(backup_ah "${PG_AH}DATABASE_SSL=true\n" NODE_OPTIONS="$preload" NODE_EXTRA_CA_CERTS="$AH/extra-ca.pem")"
+ if [ "${GOT_AH_EXTRA##*certs=}" != "$((${GOT_AH##*certs=} + 1))" ]; then
+ fail "(ah) NODE_EXTRA_CA_CERTS was left out of the root certificates (NODE_OPTIONS=$preload): $GOT_AH, then $GOT_AH_EXTRA"
+ fi
+done
if [ "$(backup_ah "${PG_AH}DATABASE_SSL=true\nDATABASE_SSL_REJECT_UNAUTHORIZED=false\n" \
DATABASE_URL=postgres://openwa@db/openwa)" != "mode=require root= certs=none" ]; then
fail "(ah) DATABASE_SSL_REJECT_UNAUTHORIZED=false did not encrypt without verifying: $(cat "$AH/log")"
fi
+# psql defaults to sslmode=prefer as well, so the import command restore.sh prints asks for the
+# app's TLS mode. restore_ah [./.env content] [VAR=value...]: what that command sets before psql, for
+# the archive in out/, then +ca when the CA hint follows it, +unread when the note on a TLS line the
+# scripts cannot parse does, and +error when the restore printed a configuration error.
+restore_ah() {
+ rm -rf "$AH/dst" && mkdir -p "$AH/dst"
+ printf '%b' "${1:-}" >"$AH/dst/.env"
+ OUT_AH="$(cd "$AH/dst" && env "${@:2}" "$RESTORE" "$(ls "$AH"/out/openwa-backup-*.tar.gz)" 2>&1)" ||
+ fail "(ah) the restore failed: $OUT_AH"
+ printf '%s' "$(sed -n 's/.*database\.sql | \(.*\)psql -v ON_ERROR_STOP=1.*/\1/p' <<<"$OUT_AH")"
+ if grep -q "verify-full needs the server's CA: set PGSSLROOTCERT" <<<"$OUT_AH"; then printf '+ca'; fi
+ if grep -q 'could not be read from the configuration' <<<"$OUT_AH"; then printf '+unread'; fi
+ if grep -q '\[config\] ERROR' <<<"$OUT_AH"; then printf '+error'; fi
+}
+GOT_AH="$(restore_ah)"
+backup_ah "${PG_AH}DATABASE_SSL=true\n" >/dev/null
+# An operator's PGSSLMODE is left alone, and the CA hint is needed only without PGSSLROOTCERT.
+GOT_AH="$GOT_AH|$(restore_ah)|$(restore_ah '' PGSSLROOTCERT=/ca.pem)|$(restore_ah '' PGSSLMODE=disable)"
+backup_ah "$PG_AH" >/dev/null
+# A TLS line the scripts cannot parse leaves the check on: the restore is done by then.
+GOT_AH="$GOT_AH|$(restore_ah)|$(restore_ah 'DATABASE_SSL="true" # tls on\n')"
+GOT_AH="$GOT_AH|$(restore_ah 'DATABASE_SSL=true\nDATABASE_SSL_REJECT_UNAUTHORIZED="false" # x\n')"
+if [ "$GOT_AH" != "PGSSLMODE=require |PGSSLMODE=verify-full +ca|PGSSLMODE=verify-full ||\
+|PGSSLMODE=verify-full +ca+unread|PGSSLMODE=verify-full +ca+unread" ]; then
+ fail "(ah) the printed psql command did not follow DATABASE_SSL: '$GOT_AH'"
+fi
# The app reads a TLS line the scripts cannot parse, so the run stops before pg_dump connects without it.
for check in 'DATABASE_SSL|DATABASE_SSL="true" # tls on\n' \
'DATABASE_SSL_REJECT_UNAUTHORIZED|DATABASE_SSL=true\nDATABASE_SSL_REJECT_UNAUTHORIZED="false" # x\n'; do
@@ -1930,7 +1970,7 @@ if [ "$(PATH="$AH/nonode" backup_ah "${PG_AH}DATABASE_SSL=true\n")" != "mode=ver
! grep -q 'node not found.*sslrootcert=system.*libpq 16+' "$AH/out.log"; then
fail "(ah) without node the system CA store was not used, or the log gave no hint: $(cat "$AH/out.log")"
fi
-pass "(ah) pg_dump verifies the server under DATABASE_SSL=true and is unchanged without it"
+pass "(ah) pg_dump and the printed psql command verify the server under DATABASE_SSL=true and are unchanged without it"
echo "==> (ai) ./data defaults and paths follow OPENWA_DATA_DIR in a run on the host"
# The app writes STORAGE_LOCAL_PATH=./data/media on first run, and a dashboard save adds
# SESSION_DATA_PATH=./data/sessions, both relative to /app in the image, where the databases default to