Skip to content

Commit e57a8e4

Browse files
author
kauanAfonso
committed
fix: increase the default from qs's 20-item limit to 100
Signed-off-by: kauanAfonso <kauan.afonso@ibm.com>
1 parent 04f4cf4 commit e57a8e4

2 files changed

Lines changed: 22 additions & 37 deletions

File tree

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

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

22-
context('with default arrayLimit (20)', () => {
22+
context('with default arrayLimit (100)', () => {
2323
beforeEach(async () => {
2424
app = givenApplication();
2525
await app.start();
@@ -34,16 +34,29 @@ describe('Query parameter array limit', () => {
3434
expect(response.body.ids).to.eql(ids);
3535
});
3636

37-
it('converts arrays with 21+ items to objects (qs default behavior)', async () => {
37+
it('parses arrays with 21 items correctly', async () => {
3838
const ids = Array.from({length: 21}, (_, i) => (i + 1).toString());
3939
const query = ids.map(id => `ids=${id}`).join('&');
4040

4141
const response = await client.get(`/test?${query}`).expect(200);
42+
expect(response.body.ids).to.eql(ids);
43+
expect(Array.isArray(response.body.ids)).to.be.true();
44+
});
45+
46+
it('parses arrays with 100 items correctly', async () => {
47+
const ids = Array.from({length: 100}, (_, i) => (i + 1).toString());
48+
const query = ids.map(id => `ids=${id}`).join('&');
4249

43-
expect(response.body.ids).to.be.Object();
44-
expect(Array.isArray(response.body.ids)).to.be.false();
45-
expect(response.body.ids).to.have.property('0', '1');
46-
expect(response.body.ids).to.have.property('20', '21');
50+
const response = await client.get(`/test?${query}`).expect(200);
51+
expect(response.body.ids).to.eql(ids);
52+
expect(Array.isArray(response.body.ids)).to.be.true();
53+
});
54+
55+
it('converts arrays with 101+ items to objects (exceeds default limit)', async () => {
56+
const ids = Array.from({length: 101}, (_, i) => (i + 1).toString());
57+
const query = ids.map(id => `ids=${id}`).join('&');
58+
59+
await client.get(`/test?${query}`).expect(400);
4760
});
4861
});
4962

@@ -91,35 +104,7 @@ describe('Query parameter array limit', () => {
91104
const ids = Array.from({length: 101}, (_, i) => (i + 1).toString());
92105
const query = ids.map(id => `ids=${id}`).join('&');
93106

94-
const response = await client.get(`/test?${query}`).expect(200);
95-
96-
expect(response.body.ids).to.be.Object();
97-
expect(Array.isArray(response.body.ids)).to.be.false();
98-
expect(response.body.ids).to.have.property('0', '1');
99-
expect(response.body.ids).to.have.property('100', '101');
100-
});
101-
});
102-
103-
context('with arrayLimit set to 1000', () => {
104-
beforeEach(async () => {
105-
app = givenApplication({
106-
rest: {
107-
queryParser: {
108-
arrayLimit: 1000,
109-
},
110-
},
111-
});
112-
await app.start();
113-
client = createRestAppClient(app);
114-
});
115-
116-
it('parses arrays with 500 items correctly', async () => {
117-
const ids = Array.from({length: 500}, (_, i) => (i + 1).toString());
118-
const query = ids.map(id => `ids=${id}`).join('&');
119-
120-
const response = await client.get(`/test?${query}`).expect(200);
121-
expect(response.body.ids).to.eql(ids);
122-
expect(Array.isArray(response.body.ids)).to.be.true();
107+
await client.get(`/test?${query}`).expect(400);
123108
});
124109
});
125110

packages/rest/src/rest.server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ export class RestServer
330330
}
331331

332332
// Configure query parser with custom arrayLimit if provided
333-
const arrayLimit = this.config.queryParser?.arrayLimit ?? 20;
333+
const arrayLimit = this.config.queryParser?.arrayLimit ?? 100;
334334
this._expressApp.set('query parser', (str: string) => {
335335
return qs.parse(str, {
336336
arrayLimit,
@@ -1201,7 +1201,7 @@ export interface RestServerResolvedOptions {
12011201
* The qs library defaults to 20 to prevent DoS attacks with large array indices.
12021202
* Set this to a higher value if your API needs to handle more than 20 array items.
12031203
*
1204-
* @default 20
1204+
* @default 100
12051205
* @see https://github.com/ljharb/qs#parsing-arrays
12061206
*/
12071207
arrayLimit?: number;

0 commit comments

Comments
 (0)