Skip to content

Commit c5a46a7

Browse files
committed
Add --max-body
Fixes danihodovic#50
1 parent cde0ec1 commit c5a46a7

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,8 @@ X-Powered-By: Express
3838
"html": "\n <!doctype html>\n ..."
3939
}
4040
```
41+
42+
#### Configuration
43+
44+
A list of available configuration options can be found in
45+
[./lib/parse_args.js](./lib/parse_args.js).

lib/parse_args.js

+6
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,10 @@ module.exports.argv = yargs
3838
type: 'string',
3939
choices: ['strict', 'soft', 'skip']
4040
})
41+
.option('max-body', {
42+
demandOption: false,
43+
default: '1mb',
44+
describe: 'Max size of the http body',
45+
type: 'string'
46+
})
4147
.argv;

lib/server.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ module.exports.create = (argv) => {
1919

2020
const app = express();
2121
app.use(loggingMiddleware);
22-
app.use(bodyParser.text({ type: () => true }));
22+
app.use(bodyParser.text({
23+
type: () => true,
24+
limit: argv.maxBody
25+
}));
2326
app.post(renderEndpoint, (req, res) => {
2427
let html, errors;
2528

test/test.js

+20
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ describe('server', function () {
6565
});
6666
});
6767

68+
describe('with --max-body', function () {
69+
let server;
70+
let url;
71+
72+
before(async () => {
73+
server = await create({ maxBody: '10' }).listen();
74+
url = `http://localhost:${server.address().port}`;
75+
});
76+
77+
after(async () => {
78+
await server.close();
79+
});
80+
81+
it('returns 413 for payloads larger than --max-body', async () => {
82+
const data = 'o'.repeat(10);
83+
const res = await makeReq(url, { data: `<mj-text>${data}</mj-text>` });
84+
expect(res.status).to.eql(413);
85+
});
86+
});
87+
6888
const makeReq = (url, { method = 'POST', path = '/v1/render', data = '' } = {}) => {
6989
return axios({
7090
method: 'POST',

0 commit comments

Comments
 (0)