Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(integration): refuse an ingress body no parser captured

Yudhi Armyndharis0b2d24fdc145
diff --git a/src/modules/integration/ingress.controller.spec.ts b/src/modules/integration/ingress.controller.spec.ts
index 94446fe88..c666050a3 100644
--- a/src/modules/integration/ingress.controller.spec.ts
+++ b/src/modules/integration/ingress.controller.spec.ts
@@ -139,6 +139,27 @@ describe('IngressController', () => {
expect(arg.rawBody).toBe('');
});
+ it('flags a body that no parser captured, and only that', async () => {
+ const handle = jest.fn().mockResolvedValue({ status: 202, body: 'accepted' });
+ const controller = new IngressController({ handle } as unknown as IngressService);
+ const send = (headers: Record<string, string>, rawBody?: Buffer) =>
+ controller.receive(
+ 'p',
+ 'i',
+ {},
+ { method: 'POST', params: { path: ['hook'] }, headers, rawBody } as unknown as Request & { rawBody?: Buffer },
+ fakeRes().res,
+ );
+
+ await send({ 'content-type': 'text/plain', 'content-length': '5' });
+ await send({ 'content-type': 'application/xml', 'transfer-encoding': 'chunked' });
+ await send({ 'content-type': 'application/json', 'content-length': '2' }, Buffer.from('{}'));
+ await send({ 'content-length': '0' });
+
+ const flags = (handle.mock.calls as Array<[{ unparsedBody?: boolean }]>).map(([arg]) => arg.unparsedBody);
+ expect(flags).toEqual([true, true, false, false]);
+ });
+
it('forwards response headers from the pipeline', async () => {
const handle = jest.fn().mockResolvedValue({
status: 200,
diff --git a/src/modules/integration/ingress.controller.ts b/src/modules/integration/ingress.controller.ts
index e165e4b6c..e3524cb8d 100644
--- a/src/modules/integration/ingress.controller.ts
+++ b/src/modules/integration/ingress.controller.ts
@@ -63,6 +63,10 @@ export class IngressController {
@ApiResponse({ status: 403, description: 'GET verification challenge failed (verifyToken mismatch).' })
@ApiResponse({ status: 404, description: 'Unknown pluginId/instanceId, or no route claimed by the plugin.' })
@ApiResponse({ status: 413, description: 'Request body exceeds the route maxBodyBytes limit.' })
+ @ApiResponse({
+ status: 415,
+ description: 'Request body is not `application/json` or `application/x-www-form-urlencoded`.',
+ })
@ApiResponse({
status: 429,
description:
@@ -104,6 +108,11 @@ export class IngressController {
]),
);
const rawBody = req.rawBody?.toString('utf8') ?? '';
+ // Only json() and urlencoded() capture req.rawBody, so a body in any other content type reaches
+ // here unread; the service refuses it rather than handling it as the empty body.
+ const unparsedBody =
+ req.rawBody === undefined &&
+ (Number(headers['content-length'] || 0) > 0 || headers['transfer-encoding'] !== undefined);
const result = await this.ingress.handle({
pluginId,
instanceId,
@@ -112,6 +121,7 @@ export class IngressController {
headers,
query: flatQuery,
rawBody,
+ unparsedBody,
});
if (result.headers) res.set(safeAckHeaders(result.headers));
// Both reflections echo provider-controlled strings (hub.challenge, the ack template). Express
diff --git a/src/modules/integration/ingress.service.spec.ts b/src/modules/integration/ingress.service.spec.ts
index 70ca27a98..059277cf3 100644
--- a/src/modules/integration/ingress.service.spec.ts
+++ b/src/modules/integration/ingress.service.spec.ts
@@ -149,6 +149,16 @@ describe('IngressService.handle', () => {
expect(res.status).toBe(202);
});
+ // A body no parser read would be handled as the empty body: it passes a header-only scheme, and every
+ // such delivery hashes to the same dedup key, so all but the first are acked and dropped.
+ it('refuses a body in a content type no parser read (415), before anything is persisted', async () => {
+ const d = deps();
+ const res = await new IngressService(d).handle({ ...req, rawBody: '', unparsedBody: true });
+ expect(res).toMatchObject({ status: 415 });
+ expect(d.events.recordOrSkip).not.toHaveBeenCalled();
+ expect(d.enqueue).not.toHaveBeenCalled();
+ });
+
it('uses the signed webhook-id as the default Standard Webhooks dedup key', async () => {
const rawKey = Buffer.from('0123456789abcdef0123456789abcdef', 'hex');
const secret = `v1,whsec_${rawKey.toString('base64')}`;
diff --git a/src/modules/integration/ingress.service.ts b/src/modules/integration/ingress.service.ts
index 392680e9f..1a722978a 100644
--- a/src/modules/integration/ingress.service.ts
+++ b/src/modules/integration/ingress.service.ts
@@ -19,6 +19,11 @@ export interface IngressRequest {
headers: Record<string, string>; // lower-cased keys
query: Record<string, string>;
rawBody: string;
+ /**
+ * A body arrived in a content type no parser captured (only JSON and form bodies are), so `rawBody`
+ * is empty rather than the bytes that were sent.
+ */
+ unparsedBody?: boolean;
}
export interface ResolvedInstance {
@@ -114,6 +119,11 @@ export class IngressService {
return { status: 403, body: 'challenge failed' };
}
+ // Handled as the empty body, it would pass a scheme that signs only a header, and every such delivery
+ // would hash to one dedup key: the first stored without its body, the rest acked and dropped. It
+ // would also slip past the size cap below.
+ if (req.unparsedBody) return { status: 415, body: 'unsupported ingress content type' };
+
// `n > undefined` is always false, so a manifest that omits maxBodyBytes — or carries a
// non-numeric or non-positive value — left this check inert: the 413 the published contract
// promises never fired, and every accepted delivery is persisted with the body stored twice