Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(config): reject a SQLite data path that collides with the main database file (#399)
Yudhi Armyndharis47db1e2fe5b3
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex c2867b7f9..2f751a5c2 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -20,6 +20,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0also the resource id on unrelated routes (e.g. `auth/api-keys/:id`, `plugins/:id`) — so asession-scoped key got a spurious `401` there. Session scoping is now applied only where `:id`actually denotes a session; enforcement on the real `sessions/:id/...` routes is unchanged. (#398)+- **Boot is now rejected when the SQLite `DATABASE_NAME` collides with the internal main database+ file.** The auth/audit ("main") and application ("data") connections must be separate SQLite files;+ pointing `DATABASE_NAME` at `./data/main.sqlite` ran two connections — each with its own migration+ ledger and synchronize policy — against one file, risking schema divergence and lock contention.+ Startup validation now fails fast with a clear message (paths are normalized, so relative spellings+ of the same file are caught). Postgres is unaffected (its `DATABASE_NAME` is a bare db name). (#399)### Securitydiff --git a/src/config/env.validation.spec.ts b/src/config/env.validation.spec.tsindex 4ff1818df..f925fbe5c 100644--- a/src/config/env.validation.spec.ts+++ b/src/config/env.validation.spec.ts@@ -35,4 +35,28 @@ describe('validateEnv', () => {expect(() => validateEnv({ STORAGE_TYPE: 'local' })).not.toThrow();expect(() => validateEnv({ STORAGE_TYPE: 's3' })).not.toThrow();});++ it('rejects a sqlite data DB path that collides with the internal main database file', () => {+ // The 'main' (auth/audit) and 'data' connections must be separate SQLite files; sharing one+ // file means two migration ledgers + synchronize policies on the same tables.+ expect(() => validateEnv({ DATABASE_TYPE: 'sqlite', DATABASE_NAME: './data/main.sqlite' })).toThrow(+ /DATABASE_NAME/,+ );+ // Relative spellings of the same file are caught (path normalization).+ expect(() => validateEnv({ DATABASE_TYPE: 'sqlite', DATABASE_NAME: './data/../data/main.sqlite' })).toThrow(+ /DATABASE_NAME/,+ );+ // The default data path is fine.+ expect(() => validateEnv({ DATABASE_TYPE: 'sqlite', DATABASE_NAME: './data/openwa.sqlite' })).not.toThrow();+ // Postgres uses a bare DB name, never a file path — must not false-positive.+ expect(() =>+ validateEnv({+ DATABASE_TYPE: 'postgres',+ DATABASE_HOST: 'db',+ DATABASE_USERNAME: 'u',+ DATABASE_PASSWORD: 'p',+ DATABASE_NAME: 'main.sqlite',+ }),+ ).not.toThrow();+ });});diff --git a/src/config/env.validation.ts b/src/config/env.validation.tsindex 6d98f2ef4..497b458ed 100644--- a/src/config/env.validation.ts+++ b/src/config/env.validation.ts@@ -1,5 +1,10 @@+import { resolve } from 'path';+type EnvConfig = Record<string, unknown>;+// The 'main' (auth/audit) connection is always this fixed SQLite file (not env-overridable).+const MAIN_DB_PATH = './data/main.sqlite';+/*** Fail-fast environment validation. Wired as ConfigModule's `validate`* callback so a misconfigured deployment is rejected at BOOT instead of silently@@ -40,6 +45,15 @@ export function validateEnv(config: EnvConfig): EnvConfig {errors.push(`${key} is required when DATABASE_TYPE=postgres`);}}+ } 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+ // migration ledgers + synchronize policies against the same tables, risking schema divergence and+ // lock contention. (Postgres DATABASE_NAME is a bare db name, so this never applies there.)+ const dataDbName = str('DATABASE_NAME');+ if (dataDbName && resolve(dataDbName) === resolve(MAIN_DB_PATH)) {+ errors.push(`DATABASE_NAME must not point at the main database file (${MAIN_DB_PATH}); use a separate file`);+ }}const checkPort = (key: string): void => {