Free, Open Source, Self-Hosted WhatsApp API Gateway
TypeScript apibotgatewayself-hostedwhatsappwhatsapp-apiwhatsapp-automationwhatsapp-botwhatsapp-gatewaywhatsapp-gateway-apiwhatsapp-webwhatsapp-web-api
fix(ingress): send only the media type of a declared ack Content-Type
Yudhi Armyndharisaeba622e581a
diff --git a/src/modules/integration/ingress-ack.spec.ts b/src/modules/integration/ingress-ack.spec.tsindex ed90dca42..c6a987f4d 100644--- a/src/modules/integration/ingress-ack.spec.ts+++ b/src/modules/integration/ingress-ack.spec.ts@@ -37,15 +37,20 @@ describe('ackContentType', () => {expect(ackContentType({ 'x-ack': '1' })).toBe('text/plain');});- it('honors an allowlisted type, keeping the declared value verbatim', () => {+ it('honors an allowlisted type, returning the bare media type', () => {expect(ackContentType({ 'content-type': 'application/json' })).toBe('application/json');- expect(ackContentType({ 'content-type': 'application/json; charset=utf-8' })).toBe(- 'application/json; charset=utf-8',- );+ expect(ackContentType({ 'content-type': 'application/json; charset=utf-8' })).toBe('application/json');+ });++ it('drops a parameter list Express could not parse', () => {+ expect(ackContentType({ 'content-type': 'application/json;' })).toBe('application/json');+ expect(ackContentType({ 'content-type': 'application/json; charset=utf-8;' })).toBe('application/json');+ expect(ackContentType({ 'content-type': 'application/json; foo' })).toBe('application/json');+ expect(ackContentType({ 'content-type': 'text/plain;' })).toBe('text/plain');});it('matches the header name and the media type case-insensitively', () => {- expect(ackContentType({ 'Content-Type': 'Application/JSON' })).toBe('Application/JSON');+ expect(ackContentType({ 'Content-Type': 'Application/JSON' })).toBe('application/json');});it('forces text/plain for anything a browser could execute', () => {diff --git a/src/modules/integration/ingress-ack.ts b/src/modules/integration/ingress-ack.tsindex ff06f189d..dce3a492d 100644--- a/src/modules/integration/ingress-ack.ts+++ b/src/modules/integration/ingress-ack.ts@@ -82,14 +82,19 @@ export function safeAckHeaders(headers: Record<string, string> | undefined): Rec*/const HONORED_ACK_MEDIA_TYPES = new Set(['application/json', 'text/plain']);-/** The Content-Type to emit for an ack: the declared value when allowlisted, else text/plain. Total. */+/**+ * The Content-Type to emit for an ack: the declared media type when allowlisted, else text/plain. Total.+ * Only the bare media type is returned. Express rewrites the charset of a string body to utf-8 anyway,+ * and a declared parameter list it cannot parse (`application/json;`) makes send() throw, which+ * answered 500 to a delivery that had already been persisted.+ */export function ackContentType(headers: Record<string, string> | undefined): string {const declared = headers? Object.entries(headers).find(([name]) => name.toLowerCase() === 'content-type')?.[1]: undefined;if (typeof declared !== 'string' || !declared) return 'text/plain';const mediaType = declared.split(';', 1)[0].trim().toLowerCase();- return HONORED_ACK_MEDIA_TYPES.has(mediaType) ? declared : 'text/plain';+ return HONORED_ACK_MEDIA_TYPES.has(mediaType) ? mediaType : 'text/plain';}function substitute(template: string, ctx: AckRenderCtx): string {diff --git a/src/modules/integration/ingress.controller.http.spec.ts b/src/modules/integration/ingress.controller.http.spec.tsnew file mode 100644index 000000000..007a72fc6--- /dev/null+++ b/src/modules/integration/ingress.controller.http.spec.ts@@ -0,0 +1,45 @@+import type { INestApplication } from '@nestjs/common';+import { Test } from '@nestjs/testing';+import request from 'supertest';+import type { App } from 'supertest/types';+import { IngressController } from './ingress.controller';+import { IngressService } from './ingress.service';+import { InstanceThrottlerGuard } from './instance-throttler.guard';++// The controller spec's fake Response never runs Express's send(), so it cannot see a Content-Type that+// Express rejects while writing. This one answers through a real Nest/Express app.+describe('IngressController over HTTP: declared ack Content-Type', () => {+ let app: INestApplication<App>;+ let declared = '';+ const handle = jest.fn(() => Promise.resolve({ status: 200, body: '{}', headers: { 'Content-Type': declared } }));++ beforeAll(async () => {+ const mod = await Test.createTestingModule({+ controllers: [IngressController],+ providers: [{ provide: IngressService, useValue: { handle } }],+ })+ .overrideGuard(InstanceThrottlerGuard)+ .useValue({ canActivate: () => true })+ .compile();+ app = mod.createNestApplication({ logger: false });+ await app.init();+ });++ afterAll(async () => {+ await app.close();+ });++ it.each([+ ['application/json', 'application/json; charset=utf-8'],+ ['application/json;', 'application/json; charset=utf-8'],+ ['application/json; charset=utf-8;', 'application/json; charset=utf-8'],+ ['application/json; foo', 'application/json; charset=utf-8'],+ ['text/plain;', 'text/plain; charset=utf-8'],+ ])('answers the ack for a declared %p', async (value, wire) => {+ declared = value;+ const res = await request(app.getHttpServer()).post('/ingress/p/i/hook').send('{}');+ expect(res.status).toBe(200);+ expect(res.headers['content-type']).toBe(wire);+ expect(res.text).toBe('{}');+ });+});