Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(integration): preserve literal acknowledgment values
Yudhi Armyndharisd9c649da8a18
diff --git a/src/modules/integration/ingress-ack.spec.ts b/src/modules/integration/ingress-ack.spec.tsindex c6a987f4d..216bf62bd 100644--- a/src/modules/integration/ingress-ack.spec.ts+++ b/src/modules/integration/ingress-ack.spec.ts@@ -25,6 +25,18 @@ describe('renderAck', () => {expect(renderAck({ body: '{rawBody}' }, { ...ctx, rawBody: '$&$`$1' }).body).toBe('$&$`$1');});+ it('substitutes only the original template tokens and preserves inserted values literally', () => {+ const literal = {+ rawBody: '{timestamp}:{id}:$&:$1:{rawBody}',+ timestamp: '{id}:$&:$1',+ id: '{rawBody}:{timestamp}:$&:$1',+ };+ expect(renderAck({ body: '{rawBody}|{timestamp}|{id}|{rawBody}|{unknown}' }, literal).body).toBe(+ '{timestamp}:{id}:$&:$1:{rawBody}|{id}:$&:$1|{rawBody}:{timestamp}:$&:$1|{timestamp}:{id}:$&:$1:{rawBody}|{unknown}',+ );+ expect(renderAck({ body: '{rawBody}{timestamp}{id}' }, { rawBody: '', timestamp: '', id: '' }).body).toBe('');+ });+it('omits body and headers when not declared', () => {expect(renderAck({ status: 204 }, ctx)).toEqual({ status: 204 });});diff --git a/src/modules/integration/ingress-ack.ts b/src/modules/integration/ingress-ack.tsindex dce3a492d..14ff0e44e 100644--- a/src/modules/integration/ingress-ack.ts+++ b/src/modules/integration/ingress-ack.ts@@ -11,9 +11,8 @@ export type AckResult = { status: number; body?: string; headers?: Record<string/*** Renders the synchronous ack for an inbound route, computed entirely host-side. `spec` is the route's* `response.ack` (undefined → the default 202 'accepted'). The body may interpolate `{rawBody}`,- * `{timestamp}`, `{id}` from the VERIFIED request. Uses split/join (never String.replace with a string- * pattern, which would interpret `$&`/`$1` in provider-controlled bytes). Total: never throws — on any- * unexpected input it falls back to the declared literal.+ * `{timestamp}`, `{id}` from the VERIFIED request. Substitutes only original template tokens, with+ * inserted values kept literal. Total: never throws; unexpected input falls back to the declared literal.*/export function renderAck(spec: IngressResponseContract['ack'] | undefined, ctx: AckRenderCtx): AckResult {if (!spec) return { status: 202, body: 'accepted' };@@ -98,12 +97,5 @@ export function ackContentType(headers: Record<string, string> | undefined): str}function substitute(template: string, ctx: AckRenderCtx): string {- // split/join avoids `$`-interpretation that String.replace applies to the replacement string.- return template- .split('{rawBody}')- .join(ctx.rawBody)- .split('{timestamp}')- .join(ctx.timestamp)- .split('{id}')- .join(ctx.id);+ return template.replace(/\{(rawBody|timestamp|id)\}/g, (_, key: keyof AckRenderCtx) => ctx[key]);}