Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(config): reject a whitespace-padded PORT (#1834)
Yudhi Armyndharisab34cc4f43d9
diff --git a/.env.example b/.env.exampleindex b42388dc3..0677761a0 100644--- a/.env.example+++ b/.env.example@@ -27,7 +27,8 @@NODE_ENV=production# Port the app binds to when run directly (bare metal / `npm run start:prod`). In the bundled Docker# Compose the container always listens on 2785; the API_PORT below is only the HOST-published port.-# To run the image itself on another port, pass `-e PORT=...`: the image healthcheck never reads .env.+# To run the image itself on another port, pass `-e PORT=...`: the image healthcheck never reads .env+# or data/.env.generated.PORT=2785# Docker Compose only: the host-side port mapped to the container's 2785 (no effect on a bare-metal run).API_PORT=2785diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 6a602c7fe..25e7030e8 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -32,6 +32,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Fixed+- Refuse a `PORT` with surrounding whitespace at boot instead of binding a port the image healthcheck cannot probe.- Skip or cancel the Baileys version lookup of a session stopped while it connects, so no proxy dispatcher outlives it.- Stop a request left over from before logout from reloading the dashboard login form.- Stop a dashboard Logs export from downloading rows after sign-out when its last page was throttled.@@ -121,6 +122,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0### Upgrade notes (behavior changes)+- Boot fails on a `PORT` with surrounding whitespace or a whitespace-only `PORT`; remove the padding.- Aged undispatched ingress events are dead-lettered instead of deleted; some may already have been delivered, so check before redriving them.- Java SDK (next SDK release after 0.5.1): the request timeout (default 30 s) also bounds the response body; raise it for large media downloads.- With `OPENWA_DATA_DIR` set, `backup.sh` and `restore.sh` read the default SQLite databases and `./data/...` paths in `./.env` under it; pass a path in the environment to keep it in the working directory.diff --git a/Dockerfile b/Dockerfileindex 0f10d20df..6eaba802e 100644--- a/Dockerfile+++ b/Dockerfile@@ -303,8 +303,9 @@ RUN chmod +x /usr/local/bin/docker-entrypoint.shEXPOSE 2785# Probe the port the app binds: PORT from the container environment, else the app's own default.-# The probe shell sees only that environment, never a PORT set in a mounted .env file, so a-# non-default PORT must be passed with -e/--env. Shell form on purpose: exec form would not expand it.+# The probe shell sees only that environment, never a PORT set in a file the app loads (a mounted+# .env or data/.env.generated), so a non-default PORT must be passed with -e/--env. Shell form on+# purpose: exec form would not expand it.HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=3 \CMD curl -f "http://localhost:${PORT:-2785}/api/health/ready" || exit 1diff --git a/docs/10-devops-infrastructure.md b/docs/10-devops-infrastructure.mdindex 84ac25a0c..7c608b8b1 100644--- a/docs/10-devops-infrastructure.md+++ b/docs/10-devops-infrastructure.md@@ -71,8 +71,9 @@ and then drops privileges. Chromium comes from Chrome for Testing on amd64 and fThe healthcheck probes the `PORT` of the container environment, falling back to 2785. To run theimage on another port, pass `PORT` as an environment variable (`docker run -e PORT=8080 ...`): the-probe never reads a mounted `.env`, so a `PORT` set only there moves the app but not the probe, and-the container stays `unhealthy`.+probe never reads a mounted `.env` or `data/.env.generated`, so a `PORT` set only in one of those+files moves the app but not the probe, and the container stays `unhealthy`. Boot refuses a `PORT`+with surrounding whitespace, which the probe URL would carry verbatim.### Docker Compose (Development)diff --git a/src/config/env.validation.spec.ts b/src/config/env.validation.spec.tsindex 4b80e0ebb..e49790da7 100644--- a/src/config/env.validation.spec.ts+++ b/src/config/env.validation.spec.ts@@ -86,6 +86,15 @@ describe('validateEnv', () => {expect(() => validateEnv({ PORT: '2785' })).not.toThrow();});+ // main.ts listens on the raw PORT and the image healthcheck puts it in a URL verbatim, so a padded+ // value would boot on 2785 while every probe of `http://localhost:2785 /...` fails as malformed.+ it('rejects a PORT with surrounding whitespace and takes only an empty one as unset', () => {+ expect(() => validateEnv({ PORT: '2785 ' })).toThrow(/PORT/);+ expect(() => validateEnv({ PORT: ' 8080' })).toThrow(/PORT/);+ expect(() => validateEnv({ PORT: ' ' })).toThrow(/PORT/);+ expect(() => validateEnv({ PORT: '' })).not.toThrow();+ });+it('rejects a non-numeric database timeout knob (a typo would become NaN and break the pg pool)', () => {expect(() => validateEnv({ DATABASE_STATEMENT_TIMEOUT_MS: 'abc' })).toThrow(/DATABASE_STATEMENT_TIMEOUT_MS/);expect(() => validateEnv({ DATABASE_IDLE_TIMEOUT_MS: '30s' })).toThrow(/DATABASE_IDLE_TIMEOUT_MS/);diff --git a/src/config/env.validation.ts b/src/config/env.validation.tsindex 4a4ec980b..2106329f1 100644--- a/src/config/env.validation.ts+++ b/src/config/env.validation.ts@@ -197,15 +197,21 @@ export function validateEnv(config: EnvConfig): EnvConfig {// value identical to the parsed one.const DECIMAL_INTEGER = /^\d+$/;- const checkPort = (key: string): void => {- const raw = str(key);+ const checkPort = (key: string, read: (key: string) => string | undefined = str): void => {+ const raw = read(key);if (raw === undefined) return;const n = DECIMAL_INTEGER.test(raw) ? Number(raw) : NaN;if (!Number.isInteger(n) || n < 1 || n > 65535) {errors.push(`${key} must be an integer port in [1, 65535] (got "${raw}")`);}};- checkPort('PORT');+ // PORT is read untrimmed: main.ts listens on it, and the image healthcheck and the startup banner+ // splice it into a URL, where a padded "2785 " is malformed although Node still binds 2785. Only an+ // empty value takes the default there, so validate the exact value with only '' meaning unset.+ checkPort('PORT', key => {+ const value = config[key];+ return typeof value === 'string' && value !== '' ? value : undefined;+ });checkPort('DATABASE_PORT');checkPort('REDIS_PORT');