Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

test(http): pin the documented nesting limit as 100

Yudhi Armyndharisf23adc71f314
diff --git a/src/common/validation/body-nesting.spec.ts b/src/common/validation/body-nesting.spec.ts
index 1cccd507a..3b2e56822 100644
--- a/src/common/validation/body-nesting.spec.ts
+++ b/src/common/validation/body-nesting.spec.ts
@@ -21,7 +21,13 @@ const mixedText = (levels: number): string => {
};
const scan = (text: string): boolean => exceedsJsonNesting(Buffer.from(text));
-describe('the nesting limit on the raw bytes', () => {
+// The documented limit is written as a literal on purpose, the way field-limits.dto.spec.ts pins the other
+// documented limits: the 400 text and the release notes say 100, so moving the cap has to fail a test here.
+describe('the documented nesting limit', () => {
+ it('is 100 levels', () => {
+ expect(MAX_BODY_NESTING).toBe(100);
+ });
+
it.each([
['objects', objectText],
['arrays', arrayText],
@@ -30,17 +36,17 @@ describe('the nesting limit on the raw bytes', () => {
expect(scan(build(100))).toBe(false);
expect(scan(build(101))).toBe(true);
});
-});
-describe('exceedsBodyNesting', () => {
it.each([
['objects', objects],
['arrays', arrays],
- ])('passes %s nested to the cap and refuses one level more', (_kind, build) => {
- expect(exceedsBodyNesting(build(MAX_BODY_NESTING))).toBe(false);
- expect(exceedsBodyNesting(build(MAX_BODY_NESTING + 1))).toBe(true);
+ ])('admits a parsed value of 100 levels of %s and refuses 101', (_kind, build) => {
+ expect(exceedsBodyNesting(build(100))).toBe(false);
+ expect(exceedsBodyNesting(build(101))).toBe(true);
});
+});
+describe('exceedsBodyNesting', () => {
it('measures the deepest branch, not the breadth or the number of nodes', () => {
const wide = { items: Array.from({ length: 5000 }, () => ({ tags: ['a', 'b'], meta: { k: [1] } })) };
expect(exceedsBodyNesting(wide)).toBe(false);
diff --git a/test/configure-app.e2e-spec.ts b/test/configure-app.e2e-spec.ts
index 8aed557ac..3eef993ae 100644
--- a/test/configure-app.e2e-spec.ts
+++ b/test/configure-app.e2e-spec.ts
@@ -28,7 +28,6 @@ import { DASHBOARD_CSP_NONCE_PLACEHOLDER } from '../src/config/dashboard-csp';
import { ActiveKeyIndex } from '../src/modules/auth/active-key-index';
import { ShutdownService } from '../src/common/services/shutdown.service';
import { documentErrorResponses, ERROR_RESPONSE_SCHEMA } from '../src/config/swagger.config';
-import { MAX_BODY_NESTING } from '../src/common/validation/body-nesting';
import { SendTextMessageDto } from '../src/modules/message/dto/send-message.dto';
/**
@@ -223,19 +222,24 @@ describe('production HTTP surface (configureApp)', () => {
// '{"a":{}}' is two.
const nestedObjects = (levels: number): string => '{"a":'.repeat(levels - 1) + '{}' + '}'.repeat(levels - 1);
- it('admits a body nested to the cap and refuses one level more with 400', async () => {
+ // The documented limit is 100 levels, written as a literal here so that moving the cap fails a test.
+ it('admits a body nested 100 levels and refuses 101 with 400', async () => {
const post = (body: string) => request(app.getHttpServer()).post('/api/echo').type('json').send(body);
- expect((await post(nestedObjects(MAX_BODY_NESTING))).status).toBe(201);
+ expect((await post(nestedObjects(100))).status).toBe(201);
const res = await request(app.getHttpServer())
.post('/api/echo')
.type('json')
.set('Origin', 'https://allowed.example')
- .send(nestedObjects(MAX_BODY_NESTING + 1))
+ .send(nestedObjects(101))
.expect(400);
- expect(res.body).toMatchObject({ statusCode: 400, error: 'Bad Request' });
- expect(res.body).toHaveProperty('message', expect.stringContaining(String(MAX_BODY_NESTING)));
+ expect(res.status).toBe(400);
+ expect(res.body).toMatchObject({
+ statusCode: 400,
+ error: 'Bad Request',
+ message: 'Request body must not nest objects or arrays more than 100 levels deep',
+ });
expect(res.headers['x-request-id']).toBeDefined();
// A browser can read the refusal only if it carries the CORS headers.
expect(res.headers['access-control-allow-origin']).toBe('https://allowed.example');