Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(message): leave an impossible session id out of the key claim
Yudhi Armyndharis0da20ad4b718
diff --git a/src/modules/message/idempotency/send-idempotency.interceptor.spec.ts b/src/modules/message/idempotency/send-idempotency.interceptor.spec.tsindex 6b678632b..7008cecc1 100644--- a/src/modules/message/idempotency/send-idempotency.interceptor.spec.ts+++ b/src/modules/message/idempotency/send-idempotency.interceptor.spec.ts@@ -24,13 +24,14 @@ describe('SendIdempotencyInterceptor', () => {headers: Record<string, string | string[]>,body: unknown = { chatId: 'c', text: 't' },rawHeaders?: string[],+ sessionId = 's1',) => {setHeader = jest.fn();return {getType: () => 'http',getHandler: () => ({ name: 'sendText' }),switchToHttp: () => ({- getRequest: () => ({ headers, params: { sessionId: 's1' }, body, rawHeaders }),+ getRequest: () => ({ headers, params: { sessionId }, body, rawHeaders }),getResponse: () => ({ setHeader }),}),} as unknown as ExecutionContext;@@ -79,6 +80,19 @@ describe('SendIdempotencyInterceptor', () => {expect(store.complete).toHaveBeenCalledWith('row-1', { messageId: 'm1' });});+ it('leaves a path segment no session id can be to the handler, claiming nothing', async () => {+ const headers = { 'idempotency-key': 'k-1' };++ const out = await run(contextFor(headers, undefined, undefined, 'x'.repeat(256)), () =>+ of({ answered: 'by handler' }),+ );+ expect(out).toEqual({ answered: 'by handler' });+ expect(store.claim).not.toHaveBeenCalled();++ await run(contextFor(headers, undefined, undefined, 'x'.repeat(255)), () => of({}));+ expect(store.claim).toHaveBeenCalledTimes(1);+ });+it('replays a completed key without running the send, marking the response', async () => {store.claim.mockResolvedValue({ kind: 'replay', body: { messageId: 'm1' } });const handler = jest.fn(() => of({ messageId: 'NEW' }));diff --git a/src/modules/message/idempotency/send-idempotency.interceptor.ts b/src/modules/message/idempotency/send-idempotency.interceptor.tsindex 76d854fe5..235068360 100644--- a/src/modules/message/idempotency/send-idempotency.interceptor.ts+++ b/src/modules/message/idempotency/send-idempotency.interceptor.ts@@ -20,6 +20,8 @@ import { hashSendRequest, isValidIdempotencyKey, SendIdempotencyService } from 'export const IDEMPOTENCY_KEY_HEADER = 'idempotency-key';/** Response header set on an answer replayed from an earlier request with the same key. */export const IDEMPOTENT_REPLAYED_HEADER = 'idempotent-replayed';+/** The most characters of a session id worth a claim: session ids are UUIDs, and the handler refuses a longer segment. */+const CLAIMABLE_SESSION_ID_MAX_LENGTH = 255;/*** Whether a failed send proves nothing reached WhatsApp, so the key can be freed for a retry.@@ -76,6 +78,9 @@ export class SendIdempotencyInterceptor implements NestInterceptor {}const sessionId = String((request.params as Record<string, string | undefined>).sessionId ?? '');+ // Not claimed: the handler answers such a segment itself, where the claim would insert a value+ // PostgreSQL cannot index and answer 500.+ if (sessionId.length > CLAIMABLE_SESSION_ID_MAX_LENGTH) return next.handle();const route = context.getHandler().name;const claim = await this.store.claim({sessionId,