Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(ingress): keep a usable maxBodyBytes cap the typed guard was discarding

Yudhi Armyndharis40bee2d47b4b
diff --git a/src/modules/integration/ingress.service.spec.ts b/src/modules/integration/ingress.service.spec.ts
index 66a5c6e71..abdf6dc2d 100644
--- a/src/modules/integration/ingress.service.spec.ts
+++ b/src/modules/integration/ingress.service.spec.ts
@@ -126,7 +126,6 @@ describe('IngressService.handle', () => {
it.each([
['omitted', undefined],
['non-numeric', 'lots' as unknown as number],
- ['zero', 0],
['negative', -1],
])('falls back to a real cap when maxBodyBytes is %s', async (_label, maxBodyBytes) => {
const d = deps({
@@ -147,6 +146,106 @@ describe('IngressService.handle', () => {
expect(d.events.recordOrSkip).not.toHaveBeenCalled();
});
+ /**
+ * The guard replaced `bodyLen > route.maxBodyBytes` with a typed check, and `>` COERCES: a manifest
+ * is third-party JSON with no runtime validation, so a quoted number (`"1024"`) enforced a real cap
+ * before and silently lost it to the much larger process-wide fallback after. The value is usable —
+ * the point of the guard was to catch values that are not.
+ */
+ it('honours a numeric string cap rather than falling back past it', async () => {
+ const d = deps({
+ manifestRoute: jest.fn().mockReturnValue({
+ route: 'chatwoot',
+ mode: 'async',
+ verify: 'core',
+ maxBodyBytes: '1024' as unknown as number,
+ signature: { scheme: 'none' },
+ dedupHeader: 'x-delivery',
+ }),
+ });
+ const res = await new IngressService(d).handle({ ...req, rawBody: 'x'.repeat(2048) });
+
+ expect(res.status).toBe(413);
+ expect(d.events.recordOrSkip).not.toHaveBeenCalled();
+ });
+
+ // Negative twin: the coercion must not swallow the cap it declares.
+ it('accepts a body inside a numeric string cap', async () => {
+ const d = deps({
+ manifestRoute: jest.fn().mockReturnValue({
+ route: 'chatwoot',
+ mode: 'async',
+ verify: 'core',
+ maxBodyBytes: '1024' as unknown as number,
+ signature: { scheme: 'none' },
+ dedupHeader: 'x-delivery',
+ }),
+ });
+ const res = await new IngressService(d).handle({ ...req, rawBody: 'x'.repeat(100) });
+
+ expect(res.status).not.toBe(413);
+ });
+
+ /**
+ * Zero is a cap, not a missing value: it admits only an empty body, which is what a verification
+ * callback sends. Treating it as "unusable" handed such a route the process-wide limit instead —
+ * a change in the permissive direction, on the one route shape that asked to accept nothing.
+ * A SMALL body is what discriminates: any body is over a cap of 0, but well under the fallback.
+ */
+ it('treats a zero cap as strict rather than missing', async () => {
+ const d = deps({
+ manifestRoute: jest.fn().mockReturnValue({
+ route: 'chatwoot',
+ mode: 'async',
+ verify: 'core',
+ maxBodyBytes: 0,
+ signature: { scheme: 'none' },
+ dedupHeader: 'x-delivery',
+ }),
+ });
+ const res = await new IngressService(d).handle({ ...req, rawBody: 'x' });
+
+ expect(res.status).toBe(413);
+ expect(d.events.recordOrSkip).not.toHaveBeenCalled();
+ });
+
+ /**
+ * The `negative` case above uses a huge body, which is over BOTH a -1 cap and the fallback — so it
+ * cannot tell "fell back" from "used -1 as the cap". An empty body separates them: it is over -1
+ * and inside any real cap. Without this, dropping the `>= 0` guard changes nothing observable.
+ */
+ it('does not use a negative cap as the limit', async () => {
+ const d = deps({
+ manifestRoute: jest.fn().mockReturnValue({
+ route: 'chatwoot',
+ mode: 'async',
+ verify: 'core',
+ maxBodyBytes: -1,
+ signature: { scheme: 'none' },
+ dedupHeader: 'x-delivery',
+ }),
+ });
+ const res = await new IngressService(d).handle({ ...req, rawBody: '' });
+
+ expect(res.status).not.toBe(413);
+ });
+
+ it('still admits the empty body a zero cap exists to allow', async () => {
+ const d = deps({
+ manifestRoute: jest.fn().mockReturnValue({
+ route: 'chatwoot',
+ mode: 'async',
+ verify: 'core',
+ maxBodyBytes: 0,
+ signature: { scheme: 'none' },
+ dedupHeader: 'x-delivery',
+ }),
+ });
+ const res = await new IngressService(d).handle({ ...req, rawBody: '' });
+
+ expect(res.status).not.toBe(413);
+ });
+
it('rejects a bad signature with 401 before any dedup or enqueue', async () => {
const d = deps({
manifestRoute: jest.fn().mockReturnValue({
diff --git a/src/modules/integration/ingress.service.ts b/src/modules/integration/ingress.service.ts
index e9861dda9..d1fa9b9e7 100644
--- a/src/modules/integration/ingress.service.ts
+++ b/src/modules/integration/ingress.service.ts
@@ -117,11 +117,20 @@ export class IngressService {
// The fallback is the process-wide body limit, which is what such a route was already bounded by
// in practice, so no delivery that is accepted today starts failing. What changes is that the
// check can no longer be vacuous, and the gap is now stated in the log instead of being silent.
- const declaredCap = route.maxBodyBytes;
- const effectiveCap =
- typeof declaredCap === 'number' && Number.isFinite(declaredCap) && declaredCap > 0
+ // A manifest is third-party JSON with no runtime validation, so the field is only a `number` by
+ // declaration. The `>` this replaced COERCED, which means a quoted number ("1024") enforced a real
+ // cap — rejecting it as "unusable" would swap that for the far larger process-wide fallback. And
+ // 0 is a cap, not a missing value: it admits the empty body a verification callback sends and
+ // nothing else. Only a value that cannot express a limit at all falls back.
+ const declaredCap: unknown = route.maxBodyBytes;
+ const parsedCap =
+ typeof declaredCap === 'number'
? declaredCap
- : this.fallbackMaxBodyBytes(req.pluginId, route.route);
+ : typeof declaredCap === 'string' && declaredCap.trim() !== ''
+ ? Number(declaredCap)
+ : Number.NaN;
+ const effectiveCap =
+ Number.isFinite(parsedCap) && parsedCap >= 0 ? parsedCap : this.fallbackMaxBodyBytes(req.pluginId, route.route);
if (Buffer.byteLength(req.rawBody, 'utf8') > effectiveCap) return { status: 413, body: 'payload too large' };
const verdict = verifyIngressSignature(route.signature, {