Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(integration): read a conversation pointer by RFC 6901 tokens
Yudhi Armyndharis54fa7171cc3c
diff --git a/src/modules/integration/ingress.service.spec.ts b/src/modules/integration/ingress.service.spec.tsindex 4d11f0b8e..7e7928da1 100644--- a/src/modules/integration/ingress.service.spec.ts+++ b/src/modules/integration/ingress.service.spec.ts@@ -643,6 +643,32 @@ describe('extractConversationId', () => {it('returns undefined on a malformed body without throwing', () => {expect(extractConversationId({ jsonPointer: '/a/b' }, {}, 'not json')).toBeUndefined();});++ it.each([+ ['/a~1b/id', { 'a/b': { id: 'slash' } }, 'slash'],+ ['/a~0b/id', { 'a~b': { id: 'tilde' } }, 'tilde'],+ ['/a~01b/id', { 'a~1b': { id: 'once' } }, 'once'],+ ['/box//id', { box: { '': { id: 'empty' }, id: 'wrong' } }, 'empty'],+ ['/', { '': 'empty-root-key' }, 'empty-root-key'],+ ['/items/0/id', { items: [{ id: 42 }] }, '42'],+ ['conversation/id', { conversation: { id: 'no-leading-slash' } }, 'no-leading-slash'],+ ])('resolves the pointer %s by its RFC 6901 tokens', (jsonPointer, body, expected) => {+ expect(extractConversationId({ jsonPointer }, {}, JSON.stringify(body))).toBe(expected);+ });++ it.each(['/a~2b', '/a~', '/items/01/id', '/items/length', '/toString', '/name/length'])(+ 'finds nothing for the pointer %s',+ jsonPointer => {+ const body = { 'a~2b': 'wrong', 'a~': 'wrong', items: [{ id: 1 }], name: 'abc' };+ expect(extractConversationId({ jsonPointer }, {}, JSON.stringify(body))).toBeUndefined();+ },+ );++ it('keeps the declared header ahead of a body pointer', () => {+ expect(extractConversationId({ header: 'X-Conv', jsonPointer: '/' }, { 'x-conv': 'header' }, '{"":"body"}')).toBe(+ 'header',+ );+ });});describe('IngressService.handle — response contract', () => {diff --git a/src/modules/integration/ingress.service.ts b/src/modules/integration/ingress.service.tsindex 5d012fa25..c3eb1535b 100644--- a/src/modules/integration/ingress.service.ts+++ b/src/modules/integration/ingress.service.ts@@ -344,8 +344,15 @@ export function extractConversationId(if (spec.jsonPointer) {try {let node: unknown = JSON.parse(rawBody);- for (const seg of spec.jsonPointer.split('/').filter(Boolean)) {- node = (node as Record<string, unknown>)?.[seg];+ // RFC 6901: an empty token is a key, `~1` and `~0` stand for `/` and `~`, and an array takes only an+ // index. A pointer written without its leading slash keeps resolving as it did.+ const path = spec.jsonPointer.startsWith('/') ? spec.jsonPointer.slice(1) : spec.jsonPointer;+ for (const token of path.split('/')) {+ if (/~(?:[^01]|$)/.test(token)) return undefined;+ const key = token.replace(/~1/g, '/').replace(/~0/g, '~');+ if (node === null || typeof node !== 'object' || !Object.hasOwn(node, key)) return undefined;+ if (Array.isArray(node) && !/^(0|[1-9][0-9]*)$/.test(key)) return undefined;+ node = (node as Record<string, unknown>)[key];}// Only a scalar is a usable conversation key — an object/array would stringify to junk.if (typeof node === 'string') return node;