Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(api): treat a blank message cursor as absent, and declare its 400

Yudhi Armyndharis550c2b1e0c91
diff --git a/docs/06-api-specification.md b/docs/06-api-specification.md
index 227500e20..33e4f7b60 100644
--- a/docs/06-api-specification.md
+++ b/docs/06-api-specification.md
@@ -1245,7 +1245,10 @@ Each `Message`: `{ id (uuid), sessionId, waMessageId (string|null), chatId, from
> **Inline media is carried up to a budget, then omitted.** `MESSAGE_LIST_INLINE_MEDIA_BUDGET_BYTES` (8 MiB of encoded base64 by default) bounds how much inline media one response may hold across its rows. A row is not a bounded object — `limit` is clamped to `[1,100]` but each row can carry its base64 in `metadata.media.data`, so a page of media rows could otherwise reach hundreds of megabytes and fail the read outright. The budget is spent newest-first, matching the `createdAt` DESC order above, so a page that cannot carry everything keeps the most recent media. Past it a payload is replaced with `{ mimetype, filename?, omitted: true, sizeBytes }` — the same marker the engine emits for inbound media over `MEDIA_DOWNLOAD_MAX_BYTES` — and the bytes remain available from [`GET /messages/:chatId/:messageId/media`](#get-apisessionssessionidmessageschatidmessageidmedia). Two rules bound the edges: the newest payload is always inlined even when it alone exceeds the budget (otherwise a single large photo would be permanently unreadable through this route), and a budget of `0` means "never inline" and grants no such allowance. The knob is validated at boot — `8MiB` would parse to 8 bytes — and is forwarded by both compose files. The MCP `MessageList` tool shares this path and the same budget.
-**Errors:** `401` missing/invalid API key
+**Errors:** `400` `after` names no message in this session · `401` missing/invalid API key
+
+A blank `after` is treated as absent, the way a blank `limit` or `offset` already is, so a client
+templating a cursor it has not got yet keeps the unfiltered first page rather than a `400`.
#### GET /api/sessions/:sessionId/messages/:chatId/history
diff --git a/openapi.json b/openapi.json
index dd4728327..ef560b565 100644
--- a/openapi.json
+++ b/openapi.json
@@ -2327,6 +2327,9 @@
}
}
}
+ },
+ "400": {
+ "description": "`after` names no message in this session. The keyset comparison would otherwise return an empty page, which reads exactly like the end of the history, so a walk resumed from a stale or foreign cursor would stop silently instead of reporting the cursor."
}
},
"summary": "Get message history for a session",
diff --git a/src/modules/message/message.controller.spec.ts b/src/modules/message/message.controller.spec.ts
index 4cb3575ba..b924712e6 100644
--- a/src/modules/message/message.controller.spec.ts
+++ b/src/modules/message/message.controller.spec.ts
@@ -90,4 +90,25 @@ describe('MessageController - inlineMedia is opt-out', () => {
it.each(['false', '0'])('omits media for %p', async raw => {
expect(await inlineMediaFor(raw)).toBe(false);
});
+
+ const afterFor = async (raw?: string): Promise<string | undefined> => {
+ getMessages.mockClear();
+ await controller.getMessages('session-1', undefined, undefined, undefined, undefined, raw, undefined);
+ const [, options] = getMessages.mock.calls[0] as [string, { after?: string }];
+ return options.after;
+ };
+
+ /**
+ * The service only skips the keyset branch on `undefined`. A blank reached the anchor lookup,
+ * matched no row, and answered 400 for what is an ordinary unfiltered first page: a client
+ * templating a cursor it has not got yet sends exactly that.
+ */
+ it.each([undefined, '', ' '])('treats a blank after as absent for %p', async raw => {
+ expect(await afterFor(raw)).toBeUndefined();
+ });
+
+ it('passes a real cursor through, trimmed', async () => {
+ expect(await afterFor('db-42')).toBe('db-42');
+ expect(await afterFor(' db-42 ')).toBe('db-42');
+ });
});
diff --git a/src/modules/message/message.controller.ts b/src/modules/message/message.controller.ts
index 7a50c11ea..fdbf99498 100644
--- a/src/modules/message/message.controller.ts
+++ b/src/modules/message/message.controller.ts
@@ -98,6 +98,13 @@ export class MessageController {
description: 'Message history',
type: MessageListResponseDto,
})
+ @ApiResponse({
+ status: 400,
+ description:
+ '`after` names no message in this session. The keyset comparison would otherwise return an ' +
+ 'empty page, which reads exactly like the end of the history, so a walk resumed from a stale ' +
+ 'or foreign cursor would stop silently instead of reporting the cursor.',
+ })
async getMessages(
@Param('sessionId') sessionId: string,
@Query('chatId') chatId?: string,
@@ -112,7 +119,11 @@ export class MessageController {
from,
limit: limit ? parseInt(limit, 10) : undefined,
offset: offset ? parseInt(offset, 10) : undefined,
- after,
+ // Blank means absent, as it already does for `limit` and `offset` above. A client templating
+ // a cursor it has not got yet sends `?after=`, and the service only skips the keyset branch
+ // on `undefined`: the empty string reached the anchor lookup, matched no row, and turned a
+ // working list request into a 400.
+ after: after?.trim() || undefined,
// Opt-out, so anything but an explicit false keeps today's behaviour. Same string pair the
// opt-in flags on this controller accept, read the other way round.
inlineMedia: inlineMedia !== 'false' && inlineMedia !== '0',