Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set body parser error to status 400 by default #5262

Merged
merged 2 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,8 +236,15 @@ module.exports = appInfo => {
depth: 5,
parameterLimit: 1000,
},
onerror(err) {
onerror(err, ctx) {
err.message += ', check bodyParser config';
if (ctx.status === 404) {
// set default status to 400, meaning client bad request
ctx.status = 400;
if (!err.status) {
err.status = 400;
}
}
throw err;
},
};
Expand Down
20 changes: 18 additions & 2 deletions test/app/middleware/body_parser.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const assert = require('assert');
const querystring = require('querystring');
const utils = require('../../utils');
Expand Down Expand Up @@ -82,6 +80,24 @@ describe('test/app/middleware/body_parser.test.js', () => {
.expect(413);
});

it('should 400 when GET with invalid body', async () => {
app.mockCsrf();
await app.httpRequest()
.get('/test/body_parser/user')
.set('content-type', 'application/json')
.set('content-encoding', 'gzip')
.expect(/unexpected end of file, check bodyParser config/)
.expect(400);

await app.httpRequest()
.get('/test/body_parser/user')
.set('content-type', 'application/json')
.set('content-encoding', 'gzip')
.send({ foo: 'a'.repeat(1024) })
.expect(/incorrect header check, check bodyParser config/)
.expect(400);
});

it('should disable body parser', async () => {
app1 = utils.app('apps/body_parser_testapp_disable');
await app1.ready();
Expand Down
Loading