Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(infra): drop NUL from template and rule text on data import
Yudhi Armyndharisd0217f3712be
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex 141762db7..d84ec461f 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -257,7 +257,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- An MCP tool input holding a NUL character gets a tool error saying so instead of `Internal error` on PostgreSQL.- On PostgreSQL, a NUL character in received message text, a status, an archived media type or a dead-letter error is dropped when stored instead of failing the write, so a history sync no longer loses the rest of its batch.- On PostgreSQL, a NUL character in the account's own profile name is dropped when a session turns ready, instead of failing the write that binds its phone number.-- `POST /api/infra/import-data` drops a NUL character from the message, status, profile-name and dead-letter text it restores, so a SQLite backup with one in that text restores on PostgreSQL instead of rolling back.+- `POST /api/infra/import-data` drops a NUL character from the message, status, profile-name, dead-letter, template-content and automation-rule text it restores, so a SQLite backup with one in that text restores on PostgreSQL instead of rolling back.- An ingress delivery whose plugin error holds a NUL character is dead-lettered on PostgreSQL instead of being re-queued or replayed without end.- A replica that starts while Redis is unreachable subscribes to cross-replica WebSocket events once Redis returns, instead of missing them until a restart.- Status media received without a type is served as `application/octet-stream` instead of answering `404` on the `mediaUrl` it was advertised with.@@ -488,7 +488,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0- ⚠️ **Breaking (API).** Media conversion answers `503` instead of `400` when ffmpeg cannot be started, and on Baileys a rate-limited or timed-out group, channel or catalog call answers `503` instead of `403` (`404` for `GET /api/sessions/:sessionId/groups/join-info`, `400` for `POST /api/sessions/:sessionId/groups/join`), except a group or channel create that WhatsApp times out (code 408), which may have succeeded and answers `500` instead of `403`; a profile-picture lookup whose connection drops, that WhatsApp rate-limits or times out (code 429 or 408), or that WhatsApp answers with a server error (code 500 or above), answers `503` instead of `200` with a null `url`; a client that branched on the old code needs updating.- Media conversion on a source install needs ffmpeg 4.4 or newer: an older binary fails every video conversion with `400`.- A request with `%00` in its path or query, or with a NUL character in its body, now answers `400` on SQLite too, and an MCP tool input holding one gets a tool error; only a backup sent to `POST /api/infra/import-data` and an ingress delivery may still carry one in the body.-- On SQLite too, a NUL character is now dropped when stored from received message text, statuses, contact names, archived media types, the account's own profile name and dead-letter errors, and from the same text in a restored backup.+- On SQLite too, a NUL character is now dropped when stored from received message text, statuses, contact names, archived media types, the account's own profile name and dead-letter errors, and from that text, template content and automation-rule text in a restored backup.- ⚠️ **Breaking (API).** `POST /api/sessions` refuses an out-of-range or mistyped `config.maxReconnectAttempts`, `config.reconnectBaseDelay` or `config.autoRejectCalls` with `400` instead of storing it; a string such as `"true"` or `"5"` is still accepted and stored typed.- `PATCH /api/sessions/:sessionId/config` answers `409` when concurrent updates to the same session keep conflicting; retry it.- ⚠️ **Breaking (API).** API key create and update refuse with `400` an `expiresAt` that is valid ISO 8601 but not a date the gateway can read, such as the week form `2026-W40-1`, and a key already stored with such an expiry is treated as expired; give it a new expiry with `PUT /api/auth/api-keys/:id`.diff --git a/docs/06-api-specification.md b/docs/06-api-specification.mdindex b9f2b802c..f7cc73d91 100644--- a/docs/06-api-specification.md+++ b/docs/06-api-specification.md@@ -107,7 +107,7 @@ A few refusals add a stable machine-readable `code`, so a client can branch with| `IMPORT_NESTED_TRANSACTION` | `409` | Another database transaction is open on the connection; retry with no other data operation in flight || `EXPORT_IN_PROGRESS` | `409` | A data export is in progress; retry the import after it finishes |-Validation failures (`statusCode: 400`) return `message` as an **array** of field-level strings, with `error: "Bad Request"`, when field detail is enabled: by default outside production, or anywhere with `VALIDATION_ERROR_DETAIL=true`. Under `NODE_ENV=production` (the Docker image, compose and Helm default) detail is off unless that variable is set, and the body is only `{ "statusCode": 400, "message": "Bad Request" }`. Clients should accept `message` as either a string or an array of strings. A global `ValidationPipe` runs with `whitelist` + `forbidNonWhitelisted`, so any request-body field not declared on the DTO is rejected with `400`. A request whose path or query string contains `%00`, or whose body holds a NUL character (U+0000) in any string, is refused with `400` before the route runs. The body check does not cover three routes: `POST /api/infra/import-data` accepts a backup holding one and drops it from the stored text listed below, so a SQLite backup with a NUL there restores on PostgreSQL, the ingress route (`/api/ingress/:pluginId/:instanceId/:path`) reads its body raw and hands it to its plugin as delivered, and `POST /mcp` runs outside the Nest pipe chain, so a NUL in its tool input gets an in-band `BadRequestException` tool error instead of a `400`. Message text, captions, chat and contact names, media types and the account's own profile name received from WhatsApp, and dead-letter errors, are stored without NUL characters.+Validation failures (`statusCode: 400`) return `message` as an **array** of field-level strings, with `error: "Bad Request"`, when field detail is enabled: by default outside production, or anywhere with `VALIDATION_ERROR_DETAIL=true`. Under `NODE_ENV=production` (the Docker image, compose and Helm default) detail is off unless that variable is set, and the body is only `{ "statusCode": 400, "message": "Bad Request" }`. Clients should accept `message` as either a string or an array of strings. A global `ValidationPipe` runs with `whitelist` + `forbidNonWhitelisted`, so any request-body field not declared on the DTO is rejected with `400`. A request whose path or query string contains `%00`, or whose body holds a NUL character (U+0000) in any string, is refused with `400` before the route runs. The body check does not cover three routes: `POST /api/infra/import-data` accepts a backup holding one and drops it from the stored text listed below and from template bodies, headers and footers and automation-rule names and replies, so a SQLite backup with a NUL there restores on PostgreSQL, the ingress route (`/api/ingress/:pluginId/:instanceId/:path`) reads its body raw and hands it to its plugin as delivered, and `POST /mcp` runs outside the Nest pipe chain, so a NUL in its tool input gets an in-band `BadRequestException` tool error instead of a `400`. Message text, captions, chat and contact names, media types and the account's own profile name received from WhatsApp, and dead-letter errors, are stored without NUL characters.### General Error Codesdiff --git a/src/modules/infra/table-importers.spec.ts b/src/modules/infra/table-importers.spec.tsindex 5f6d9898d..296323915 100644--- a/src/modules/infra/table-importers.spec.ts+++ b/src/modules/infra/table-importers.spec.ts@@ -108,8 +108,9 @@ describe('automationRules table importer', () => {/*** A restore writes with raw SQL, so the NUL drop the entities apply to their free-text columns never- * runs. PostgreSQL rejects U+0000 in a bound parameter, and an older SQLite backup may hold one, so- * the importers drop it from exactly those columns and leave ids and lookup keys as they are.+ * runs. PostgreSQL rejects U+0000 in a bound parameter, and an older SQLite backup may hold one, also+ * in template and rule text written before the DTOs refused it, so the importers drop it from those+ * columns and leave ids and lookup keys (a template name is unique per session) as they are.*/describe('table importers: NUL in free text', () => {const nul = 'a\u0000b';@@ -126,6 +127,8 @@ describe('table importers: NUL in free text', () => {],['webhookDeliveryFailures', { id: 'wf1', lastError: nul }, [9]],['integrationDeliveryFailures', { id: 'df1', lastError: nul }, [7]],+ ['templates', { id: 't1', name: 'greet', body: nul, header: nul, footer: nul }, [3, 4, 5]],+ ['automationRules', { id: 'r1', name: nul, replyText: nul }, [2, 5]],])('drops it from the %s text columns', (key, row, columns) => {const params = mapped(key, row);columns.forEach(index => expect(params[index]).toBe('ab'));@@ -134,5 +137,6 @@ describe('table importers: NUL in free text', () => {it('keeps it in a lookup column', () => {expect(mapped('messages', { id: 'm1', chatId: nul })[3]).toBe(nul);expect(mapped('statusUpdates', { id: 'su1', contactJid: nul })[2]).toBe(nul);+ expect(mapped('templates', { id: 't1', name: nul })[2]).toBe(nul);});});diff --git a/src/modules/infra/table-importers.ts b/src/modules/infra/table-importers.tsindex 8aa2ab200..32f88d864 100644--- a/src/modules/infra/table-importers.ts+++ b/src/modules/infra/table-importers.ts@@ -69,7 +69,8 @@ function decodeJsonColumn(value: unknown): unknown {// The entity columns that drop U+0000 do it in a TypeORM transformer, which raw SQL never runs. An// older SQLite backup may still hold one, and PostgreSQL refuses it in a bound parameter, failing the-// whole restore. Apply the same drop here, to those same free-text columns only, never to an id.+// whole restore. Apply the same drop here, to those same free-text columns and to the template and+// rule text the DTOs only refuse from 0.24.0 on; never to an id or a lookup key such as a template name.function nulFree(value: unknown): unknown {return NulFreeTransformer.to(value);}@@ -230,9 +231,9 @@ export const TABLE_IMPORTERS: AnyTableImporter[] = [tpl.id,tpl.sessionId,tpl.name,- tpl.body,- tpl.header ?? null,- tpl.footer ?? null,+ nulFree(tpl.body),+ nulFree(tpl.header ?? null),+ nulFree(tpl.footer ?? null),tpl.createdAt,tpl.updatedAt,],@@ -464,10 +465,10 @@ export const TABLE_IMPORTERS: AnyTableImporter[] = [map: (rule: AutomationRuleRow) => [rule.id,rule.sessionId,- rule.name,+ nulFree(rule.name),rule.enabled ?? true,rule.conditions ?? null,- rule.replyText,+ nulFree(rule.replyText),rule.cooldownSeconds ?? 60,rule.createdAt,rule.updatedAt,