Skip to content

Commit 2f833c2

Browse files
author
kauanAfonso
committed
fix(rest): apply queryParser.arrayLimit as opt-in to preserve default behavior
Signed-off-by: kauanAfonso <kauan.afonso@ibm.com>
1 parent fa0d18a commit 2f833c2

2 files changed

Lines changed: 20 additions & 13 deletions

File tree

packages/rest/src/__tests__/acceptance/request-parsing/array-limit.acceptance.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,15 @@ describe('Query parameter array limit', () => {
1919
if (app) await app.stop();
2020
});
2121

22-
context('with default arrayLimit (30)', () => {
22+
context('with custom arrayLimit (30)', () => {
2323
beforeEach(async () => {
24-
app = givenApplication();
24+
app = givenApplication({
25+
rest: {
26+
queryParser: {
27+
arrayLimit: 30,
28+
},
29+
},
30+
});
2531
await app.start();
2632
client = createRestAppClient(app);
2733
});
@@ -43,7 +49,7 @@ describe('Query parameter array limit', () => {
4349
expect(Array.isArray(response.body.ids)).to.be.true();
4450
});
4551

46-
it('converts arrays with 31+ items to objects (exceeds default limit)', async () => {
52+
it('converts arrays with 31+ items to objects (exceeds limit)', async () => {
4753
const ids = Array.from({length: 31}, (_, i) => (i + 1).toString());
4854
const query = ids.map(id => `ids=${id}`).join('&');
4955

packages/rest/src/rest.server.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -329,16 +329,17 @@ export class RestServer
329329
this._expressApp.set('strict routing', this.config.router.strict);
330330
}
331331

332-
// Configure query parser with custom arrayLimit if provided
333-
const arrayLimit = this.config.queryParser?.arrayLimit ?? 30;
334-
this._expressApp.set('query parser', (str: string) => {
335-
return qs.parse(str, {
336-
arrayLimit,
337-
// Use extended mode (same as body-parser urlencoded)
338-
allowPrototypes: false,
339-
depth: 20,
332+
if (this.config.queryParser?.arrayLimit !== undefined) {
333+
const arrayLimit = this.config.queryParser.arrayLimit;
334+
335+
this._expressApp.set('query parser', (str: string) => {
336+
return qs.parse(str, {
337+
arrayLimit,
338+
allowPrototypes: false,
339+
depth: 20,
340+
});
340341
});
341-
});
342+
}
342343
}
343344

344345
/**
@@ -1198,10 +1199,10 @@ export interface RestServerResolvedOptions {
11981199
queryParser?: {
11991200
/**
12001201
* Maximum number of array elements to parse in query parameters.
1202+
* When not configured, Express uses its default query parser.
12011203
* The qs library defaults to 20 to prevent DoS attacks with large array indices.
12021204
* Set this to a higher value if your API needs to handle more than 20 array items.
12031205
*
1204-
* @default 30
12051206
* @see https://github.com/ljharb/qs#parsing-arrays
12061207
*/
12071208
arrayLimit?: number;

0 commit comments

Comments
 (0)