Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): turn TLS off for the built-in database

Yudhi Armyndharis1fbb127fea08
diff --git a/dashboard/src/hooks/useInfraConfigForm.test.ts b/dashboard/src/hooks/useInfraConfigForm.test.ts
index 556eba9f7..f5a98f53e 100644
--- a/dashboard/src/hooks/useInfraConfigForm.test.ts
+++ b/dashboard/src/hooks/useInfraConfigForm.test.ts
@@ -297,6 +297,23 @@ test('a refetch after the form is seeded does not overwrite an edit in progress'
assert.equal(result.current.buildSavePayload().database?.host, 'edited-host');
});
+// The bundled container runs without TLS, so turning Use Built-in on over an external database saved with SSL sends
+// SSL off; off again, the card's own setting goes out. Beside a host the environment pins elsewhere the next boot
+// keeps dialling that server, whose setting stays as shown.
+test('a built-in database is saved with SSL off', () => {
+ const { result } = renderForm({ status: RUNNING, saved: SAVED });
+ rtl.act(() => result.current.updateDbConfig('builtIn', true));
+ assert.equal(result.current.buildSavePayload().database?.sslEnabled, false);
+ rtl.act(() => result.current.updateDbConfig('builtIn', false));
+ assert.equal(result.current.buildSavePayload().database?.sslEnabled, true);
+});
+
+test('beside a host pinned elsewhere a built-in database keeps the SSL setting shown', () => {
+ const { result } = renderForm({ status: { ...RUNNING, envPinned: ['DATABASE_HOST'] }, saved: SAVED });
+ rtl.act(() => result.current.updateDbConfig('builtIn', true));
+ assert.equal(result.current.buildSavePayload().database?.sslEnabled, true);
+});
+
// parseInt('') is NaN, which JSON writes as null, and the gateway refuses a null pool size.
test('an emptied pool size is left out of the save instead of going out as null', () => {
const { result } = renderForm({ status: RUNNING, saved: SAVED });
diff --git a/dashboard/src/hooks/useInfraConfigForm.ts b/dashboard/src/hooks/useInfraConfigForm.ts
index 34205ef8b..4fc5d9a74 100644
--- a/dashboard/src/hooks/useInfraConfigForm.ts
+++ b/dashboard/src/hooks/useInfraConfigForm.ts
@@ -281,6 +281,16 @@ export function useInfraConfigForm(
? undefined
: dbConfig.builtIn,
password: dbConfig.builtIn ? '' : dbConfig.password,
+ // The bundled container runs without TLS, so a built-in database is saved with SSL off (the gateway writes that
+ // too); beside a host pinned elsewhere the next boot dials that server, and the setting shown stays.
+ sslEnabled:
+ dbConfig.builtIn &&
+ !(
+ infraStatus?.envPinned?.includes('DATABASE_HOST') &&
+ !namesBundledContainer('postgres', savedConfig?.database.host)
+ )
+ ? false
+ : dbConfig.sslEnabled,
poolSize: Number.isInteger(dbConfig.poolSize) ? dbConfig.poolSize : undefined,
},
// `connected` is runtime-only status, not persisted configuration. Keep it out of the
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.md
index f49134059..d500575ba 100644
--- a/docs/06-api-specification.md
+++ b/docs/06-api-specification.md
@@ -5696,8 +5696,8 @@ Merge-save infrastructure config to `data/.env.generated` (a `0600` secret file)
| `database.schema` | string | No | lower-case, no `pg_` prefix (else 400) | Postgres schema, saved as `POSTGRES_SCHEMA`; an empty value writes `public` (also forced to `public` when switching to the built-in DB) |
| `database.password` | string | No | secret | Empty/omitted keeps the existing stored secret; with built-in postgres, the stored password of the built-in database, else `openwa` |
| `database.poolSize` | number | No | — | Default 10 |
-| `database.sslEnabled` | boolean | No | — | Default false |
-| `database.sslRejectUnauthorized` | boolean | No | — | Only written when `sslEnabled` is true; default true |
+| `database.sslEnabled` | boolean | No | — | Default false; the built-in database writes `false` (the bundled container has no TLS) |
+| `database.sslRejectUnauthorized` | boolean | No | — | Only written when `sslEnabled` is true, and dropped for the built-in database; default true |
| `redis.enabled` / `.builtIn` | boolean | No | — | `builtIn` writes host `redis`; the `redis` profile follows only while something uses Redis (see below) |
| `redis.host` / `.port` | string | No | `port` is a string | Defaults `localhost`/`6379` |
| `redis.password` | string | No | secret | Empty keeps existing |
@@ -5712,7 +5712,7 @@ Merge-save infrastructure config to `data/.env.generated` (a `0600` secret file)
| `engine.sessionDataPath` | string | No | — | Default `./data/sessions` |
| `engine.browserArgs` | string | No | — | Saved as `PUPPETEER_ARGS`. Empty saves the default `--no-sandbox --disable-setuid-sandbox --disable-dev-shm-usage --disable-gpu`; a non-empty value replaces that list in full |
-Built-in flags: a section sent without `builtIn` stays in the stored mode. A stored `POSTGRES_BUILTIN`, `REDIS_BUILTIN` or `MINIO_BUILTIN` of `true` writes the bundled connection values again, and the fields sent apply only to an external service; send `builtIn: false` with them to switch it, as the dashboard does once a card beside a host the environment pins elsewhere is edited. A built-in Redis asks for the `redis` profile only while something the next boot runs uses Redis (`REDIS_ENABLED`, `QUEUE_ENABLED` or `CACHE_ENABLED`, pinned, sent or stored); a save where nothing uses it, in the file either, writes `REDIS_BUILTIN=false` whatever `builtIn` says and drops a `REDIS_HOST` that names the bundled container. A built-in save keeps the stored database password and S3 keys (`S3_ACCESS_KEY_ID`/`S3_SECRET_ACCESS_KEY`, else the legacy `S3_ACCESS_KEY`/`S3_SECRET_KEY`) when the flag was already `true` or the next boot's host already reaches the bundled container, and writes `openwa` or `minioadmin` otherwise. It keeps the stored user, database name and schema (`DATABASE_USERNAME`, `DATABASE_NAME`, `POSTGRES_SCHEMA`), or the stored `S3_BUCKET` and `S3_REGION`, when the next boot already runs PostgreSQL or S3 storage on a host that reaches the bundled container (a file that names that container beside a database or bucket of its own), and writes `openwa`, `openwa` and `public`, or `openwa` and `us-east-1`, otherwise. The bundled container counts in any spelling: its alias (`postgres`, `redis`, `minio`) or its container name (`openwa-postgres`, `openwa-redis`, `openwa-minio`), in any letter case, with or without a trailing dot, and for storage as the host of the endpoint URL.
+Built-in flags: a section sent without `builtIn` stays in the stored mode. A stored `POSTGRES_BUILTIN`, `REDIS_BUILTIN` or `MINIO_BUILTIN` of `true` writes the bundled connection values again, and the fields sent apply only to an external service; send `builtIn: false` with them to switch it, as the dashboard does once a card beside a host the environment pins elsewhere is edited. A built-in Redis asks for the `redis` profile only while something the next boot runs uses Redis (`REDIS_ENABLED`, `QUEUE_ENABLED` or `CACHE_ENABLED`, pinned, sent or stored); a save where nothing uses it, in the file either, writes `REDIS_BUILTIN=false` whatever `builtIn` says and drops a `REDIS_HOST` that names the bundled container. A built-in save keeps the stored database password and S3 keys (`S3_ACCESS_KEY_ID`/`S3_SECRET_ACCESS_KEY`, else the legacy `S3_ACCESS_KEY`/`S3_SECRET_KEY`) when the flag was already `true` or the next boot's host already reaches the bundled container, and writes `openwa` or `minioadmin` otherwise. It keeps the stored user, database name and schema (`DATABASE_USERNAME`, `DATABASE_NAME`, `POSTGRES_SCHEMA`), or the stored `S3_BUCKET` and `S3_REGION`, when the next boot already runs PostgreSQL or S3 storage on a host that reaches the bundled container (a file that names that container beside a database or bucket of its own), and writes `openwa`, `openwa` and `public`, or `openwa` and `us-east-1`, otherwise. The bundled container counts in any spelling: its alias (`postgres`, `redis`, `minio`) or its container name (`openwa-postgres`, `openwa-redis`, `openwa-minio`), in any letter case, with or without a trailing dot, and for storage as the host of the endpoint URL. A built-in database writes `DATABASE_SSL=false` and drops `DATABASE_SSL_REJECT_UNAUTHORIZED`, since the bundled container has no TLS, except beside a `DATABASE_HOST` the environment pins elsewhere: the next boot dials that server, and `sslEnabled` is written as sent. A boot with `POSTGRES_BUILTIN=true`, `DATABASE_SSL=true` and a `DATABASE_HOST` that reaches the bundled container is refused with a message naming `DATABASE_SSL`.
```json
{
diff --git a/src/config/env.validation.spec.ts b/src/config/env.validation.spec.ts
index 54adefdb0..8ef4be316 100644
--- a/src/config/env.validation.spec.ts
+++ b/src/config/env.validation.spec.ts
@@ -522,6 +522,29 @@ describe('validateEnv', () => {
);
});
+ // The bundled PostgreSQL container runs without TLS: a built-in database with DATABASE_SSL=true would retry its
+ // connection until the boot gives up, so the boot refuses it at once and names the key.
+ describe('DATABASE_SSL with the built-in PostgreSQL container', () => {
+ const builtIn = {
+ DATABASE_TYPE: 'postgres',
+ POSTGRES_BUILTIN: 'true',
+ DATABASE_HOST: 'postgres',
+ DATABASE_USERNAME: 'openwa',
+ DATABASE_PASSWORD: 'openwa',
+ };
+ it.each(['postgres', 'Postgres', 'postgres.', 'openwa-postgres'])('refuses it beside the host %j', host => {
+ expect(() => validateEnv({ ...builtIn, DATABASE_HOST: host, DATABASE_SSL: 'true' })).toThrow(
+ /DATABASE_SSL=true .*built-in PostgreSQL container/,
+ );
+ });
+ it('accepts it off, and on for another host or without the built-in flag', () => {
+ expect(() => validateEnv({ ...builtIn, DATABASE_SSL: 'false' })).not.toThrow();
+ expect(() => validateEnv({ ...builtIn, DATABASE_HOST: 'db.ext.example', DATABASE_SSL: 'true' })).not.toThrow();
+ // A server of its own named postgres (a Kubernetes Service, a compose service with TLS set up) may serve TLS.
+ expect(() => validateEnv({ ...builtIn, POSTGRES_BUILTIN: 'false', DATABASE_SSL: 'true' })).not.toThrow();
+ });
+ });
+
it('rejects DATABASE_SYNCHRONIZE=true with DATABASE_TYPE=postgres (drops body_ts → /search 501)', () => {
// The Postgres data connection hardcodes migrationsRun=true; an opted-in synchronize=true makes
// TypeORM re-sync from entities on every boot, dropping the migration-created `body_ts` generated
diff --git a/src/config/env.validation.ts b/src/config/env.validation.ts
index 0c4b0b572..3a42a5872 100644
--- a/src/config/env.validation.ts
+++ b/src/config/env.validation.ts
@@ -1,5 +1,6 @@
import { resolve } from 'path';
import { normalizeS3KeyPrefix } from '../common/storage/s3-key-prefix';
+import { namesBundledContainer } from '../modules/docker/bundled-host';
import { resolveBodyLimit } from './bootstrap-security';
import { parseBodyLimitBytes } from './inflight-body-budget';
@@ -174,6 +175,20 @@ export function validateEnv(config: EnvConfig): EnvConfig {
if (pgSchemaError) {
errors.push(pgSchemaError);
}
+ // The bundled PostgreSQL container runs without TLS (DockerService.getContainerSpec sets none up), so the built-in
+ // database with DATABASE_SSL=true retries its connection until the boot gives up, and the dashboard that could undo
+ // it stays down. Both flags are read raw, as configuration.ts and the boot guard compare them.
+ const dbHost = str('DATABASE_HOST');
+ if (
+ config.POSTGRES_BUILTIN === 'true' &&
+ config.DATABASE_SSL === 'true' &&
+ namesBundledContainer('postgres', dbHost)
+ ) {
+ errors.push(
+ `DATABASE_SSL=true cannot reach the built-in PostgreSQL container (POSTGRES_BUILTIN=true, DATABASE_HOST=${dbHost}), ` +
+ 'which runs without TLS: set DATABASE_SSL=false where it is set (data/.env.generated, the project .env or the environment)',
+ );
+ }
} else {
// SQLite (explicit or default): DATABASE_NAME is a file path for the 'data' connection. It must
// not resolve to the 'main' DB file — two TypeORM connections on one SQLite file run separate
diff --git a/src/modules/infra/config-sections.ts b/src/modules/infra/config-sections.ts
index bd24b461c..e8f997ced 100644
--- a/src/modules/infra/config-sections.ts
+++ b/src/modules/infra/config-sections.ts
@@ -3,7 +3,7 @@ import { DEFAULT_PUPPETEER_ARGS } from '../../config/configuration';
import { isEnvPinned } from '../../config/env-precedence';
import { postgresSchemaError } from '../../config/env.validation';
import type { EngineFactory } from '../../engine/engine.factory';
-import { datastoreInUse, namesBundledContainer } from '../docker';
+import { datastoreInUse, hostPinnedElsewhere, namesBundledContainer } from '../docker';
import { DatabaseConfigDto, EngineConfigDto, RedisConfigDto, StorageConfigDto } from './dto/save-config.dto';
// Mutable accumulator threaded through the pipeline stages and per-section appliers extracted
@@ -144,7 +144,13 @@ export function applyDatabaseSection(
}
updates.DATABASE_POOL_SIZE = String(database.poolSize || 10);
}
- if (database.sslEnabled !== undefined) {
+ // The bundled container runs without TLS (getContainerSpec sets none up), so a built-in database the next boot
+ // dials there connects without it: SSL kept from an external server would refuse every connection. Beside a host
+ // pinned elsewhere the next boot dials that server, and the setting sent stays.
+ if (dbBuiltIn && !hostPinnedElsewhere('postgres')) {
+ updates.DATABASE_SSL = 'false';
+ staleKeys.add('DATABASE_SSL_REJECT_UNAUTHORIZED');
+ } else if (database.sslEnabled !== undefined) {
updates.DATABASE_SSL = database.sslEnabled ? 'true' : 'false';
if (database.sslEnabled) {
// Default to certificate verification; only relax it when the operator opts out
diff --git a/src/modules/infra/infra-config.controller.spec.ts b/src/modules/infra/infra-config.controller.spec.ts
index 1548f16c0..2c3ea538d 100644
--- a/src/modules/infra/infra-config.controller.spec.ts
+++ b/src/modules/infra/infra-config.controller.spec.ts
@@ -95,6 +95,59 @@ describe('InfraConfigController.saveConfig SSL reject-unauthorized', () => {
const env = writtenEnv({ database: { type: 'postgres', sslEnabled: false, password: 'unit-test-pw' } });
expect(env).not.toContain('DATABASE_SSL_REJECT_UNAUTHORIZED');
});
+
+ // The bundled PostgreSQL container runs without TLS, so the built-in database connects without it: SSL carried
+ // over from an external server would refuse every connection of the next boot.
+ describe('with the built-in database', () => {
+ const EXTERNAL_TLS =
+ 'DATABASE_TYPE=postgres\nPOSTGRES_BUILTIN=false\nDATABASE_HOST=db.ext.example\nDATABASE_PASSWORD=ExtPw12345!\n' +
+ 'DATABASE_SSL=true\nDATABASE_SSL_REJECT_UNAUTHORIZED=false\n';
+ const parsed = (text: string) => jest.requireActual<typeof import('dotenv')>('dotenv').parse(text);
+ beforeEach(() => {
+ (fs.existsSync as jest.Mock).mockReturnValue(true);
+ (fs.readFileSync as jest.Mock).mockReturnValue(EXTERNAL_TLS);
+ });
+ afterEach(() => {
+ delete process.env.DATABASE_HOST;
+ delete process.env.DATABASE_PASSWORD;
+ recordPinnedEnvKeys({});
+ (fs.existsSync as jest.Mock).mockReturnValue(false);
+ (fs.readFileSync as jest.Mock).mockReturnValue('');
+ });
+
+ it('writes DATABASE_SSL=false and drops the verify setting over a file with SSL on', () => {
+ const env = parsed(
+ writtenEnv({
+ database: { type: 'postgres', builtIn: true, sslEnabled: true, sslRejectUnauthorized: false },
+ }),
+ );
+ expect([env.DATABASE_HOST, env.DATABASE_SSL, env.DATABASE_SSL_REJECT_UNAUTHORIZED]).toEqual([
+ 'postgres',
+ 'false',
+ undefined,
+ ]);
+ });
+
+ it('writes it for a stored built-in database the section keeps without a flag', () => {
+ (fs.readFileSync as jest.Mock).mockReturnValue(
+ 'DATABASE_TYPE=postgres\nPOSTGRES_BUILTIN=true\nDATABASE_HOST=postgres\nDATABASE_SSL=true\n',
+ );
+ const env = parsed(writtenEnv({ database: { type: 'postgres', sslEnabled: true } }));
+ expect([env.DATABASE_SSL, env.DATABASE_SSL_REJECT_UNAUTHORIZED]).toEqual(['false', undefined]);
+ });
+
+ it('keeps the setting sent beside a host pinned elsewhere, which the next boot dials', () => {
+ process.env.DATABASE_HOST = 'db.ext.example';
+ process.env.DATABASE_PASSWORD = 'PinnedPw456!';
+ recordPinnedEnvKeys({ DATABASE_HOST: 'db.ext.example', DATABASE_PASSWORD: 'PinnedPw456!' });
+ const env = parsed(
+ writtenEnv({
+ database: { type: 'postgres', builtIn: true, sslEnabled: true, sslRejectUnauthorized: false },
+ }),
+ );
+ expect([env.DATABASE_SSL, env.DATABASE_SSL_REJECT_UNAUTHORIZED]).toEqual(['true', 'false']);
+ });
+ });
});
// The saved file is loaded by the next boot, whose validation refuses these values and crash-loops the