Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(infra): answer 400, not 500, for a malformed import archive

Yudhi Armyndharisbd17d31017d5
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 3b5f529f7..59acdae9c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- `POST /api/infra/import-data` now answers 400 for a malformed archive instead of 500. A table value that is not an array — a hand-edited or truncated backup — was dereferenced with `.map()` before anything checked it, so the operator was told the server had broken when their file was simply wrong. Checked for every table, not only `sessions`: the rest are read inside the transaction, where the same mistake would fail mid-restore rather than before it opens.
+
- README no longer inverts the shipped MCP posture. It described `MCP_ENABLED=true` as exposing 51 tools including messaging, when the surface is read-only unless `MCP_READONLY=false` — a secure default the code chose deliberately — so an operator wired a client, saw no send or group tools, and had nothing in README pointing at the knob. It now states the 25 read-only tools mounted by default and names the opt-in for all 51. A gate derives both counts from the tool sources, so a tier change fails instead of making the prose wrong again.
- README's Ports table no longer publishes Swagger unconditionally. The documented production command produces a deployment where `/api/docs` returns 404, and SECURITY.md separately tells operators to review `ENABLE_SWAGGER`, so the two published documents disagreed. The row now states the condition.
diff --git a/src/modules/infra/infra-data.controller.spec.ts b/src/modules/infra/infra-data.controller.spec.ts
index 295abe71f..70501be90 100644
--- a/src/modules/infra/infra-data.controller.spec.ts
+++ b/src/modules/infra/infra-data.controller.spec.ts
@@ -38,6 +38,7 @@ import { IntegrationDeliveryFailure } from '../integration/entities/integration-
import { StatusUpdate } from '../status-store/entities/status-update.entity';
import { AutomationRule } from '../automation/entities/automation-rule.entity';
import { AuditAction } from '../audit/entities/audit-log.entity';
+import { BadRequestException } from '@nestjs/common';
describe('InfraDataController.importData round-trips export-data (no silent message/batch loss)', () => {
let ds: DataSource;
@@ -1997,3 +1998,33 @@ describe('restoreSessionOwnership', () => {
expect(await bindLease(null, readAt, commitAt)).toBeNull();
});
});
+
+/**
+ * `data.tables.sessions` was dereferenced with `.map()` before anything checked it was an array, so a
+ * hand-edited or truncated archive whose table value is a string (or an array of nulls) produced a
+ * TypeError — a 500 that tells the operator the server broke, when what actually happened is that
+ * their file is malformed. It fires before the transaction opens, so nothing was written; only the
+ * answer was wrong.
+ */
+describe('InfraDataController.importData rejects a malformed table value', () => {
+ const controller = () => new InfraDataController({ get: () => undefined } as never, {} as never);
+
+ it.each([
+ ['a string', 'not-an-array'],
+ ['a number', 7],
+ ['an object', { id: 'x' }],
+ ])('answers 400 when tables.sessions is %s', async (_label, value) => {
+ await expect(controller().importData({ tables: { sessions: value } } as never)).rejects.toThrow(
+ BadRequestException,
+ );
+ });
+
+ it('names the offending table so the operator can fix the file', async () => {
+ await expect(controller().importData({ tables: { messages: 'nope' } } as never)).rejects.toThrow(/messages/);
+ });
+
+ // Negative twin: an absent table is legal (a partial archive), and must not be rejected.
+ it('accepts an archive that simply omits a table', async () => {
+ await expect(controller().importData({ tables: {} })).rejects.not.toThrow(/must be an array/);
+ });
+});
diff --git a/src/modules/infra/infra-data.controller.ts b/src/modules/infra/infra-data.controller.ts
index f6877a0e6..e71892bda 100644
--- a/src/modules/infra/infra-data.controller.ts
+++ b/src/modules/infra/infra-data.controller.ts
@@ -1,4 +1,14 @@
-import { Controller, Get, Post, Body, ConflictException, HttpCode, HttpStatus, Optional } from '@nestjs/common';
+import {
+ BadRequestException,
+ Body,
+ ConflictException,
+ Controller,
+ Get,
+ HttpCode,
+ HttpStatus,
+ Optional,
+ Post,
+} from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse, ApiBody } from '@nestjs/swagger';
import { InfraExportDataResponseDto, InfraImportDataResponseDto } from './dto/infra-response.dto';
import { ImportDataDto } from './dto/import-data.dto';
@@ -556,6 +566,16 @@ export class InfraDataController {
// engines writing into the freshly replaced tables for an unbounded time is the corruption this
// gate exists to prevent, so the explicit-stop path closes that window instead of relying on the
// operator to restart promptly.
+ // Every present table must be an array of rows before anything dereferences one. `.map()` on a
+ // string or a number is a TypeError — a 500 telling the operator the SERVER broke, when their
+ // archive is simply malformed. Checked for all tables, not just `sessions`: the rest are read
+ // inside the transaction, where the same mistake would fail mid-restore instead of before it.
+ for (const [table, rows] of Object.entries(data.tables ?? {})) {
+ if (rows !== undefined && !Array.isArray(rows)) {
+ throw new BadRequestException(`tables.${table} must be an array of rows`);
+ }
+ }
+
const importedSessionIds = new Set((data.tables.sessions ?? []).map(s => s.id));
const orphanedEngines = (this.sessionService?.getActiveSessionIds() ?? []).filter(
id => !importedSessionIds.has(id),