Skip to content

Commit 44768be

Browse files
authored
fix: set maxFiles default to 1000 to match maxFields (#1065)
maxFiles defaulted to Infinity, making _setUpMaxFiles() a no-op. An attacker could send thousands of tiny files in a single multipart request, each creating a temp file via fs.createWriteStream(), crashing the process with EMFILE on default ulimit -n (1024). Closes #1064
1 parent 960f0a3 commit 44768be

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

src/Formidable.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const createId = cuid2init({ length: 25, fingerprint: CUID2_FINGERPRINT.toLowerC
2222
const DEFAULT_OPTIONS = {
2323
maxFields: 1000,
2424
maxFieldsSize: 20 * 1024 * 1024,
25-
maxFiles: Infinity,
25+
maxFiles: 1000,
2626
maxFileSize: 200 * 1024 * 1024,
2727
maxTotalFileSize: undefined,
2828
minFileSize: 1,

test/unit/formidable.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,32 @@ function makeHeader(originalFilename) {
302302
});
303303
});
304304

305+
test(`${name}: maxFiles exceeded emits error`, (done) => {
306+
const form = getForm(name, { maxFiles: 1 });
307+
form.req = requestStub();
308+
309+
form.on('error', (error) => {
310+
expect(error.message.includes('maxFiles')).toBe(true);
311+
done();
312+
});
313+
314+
const part1 = new Stream();
315+
part1.mimetype = 'text/plain';
316+
const part2 = new Stream();
317+
part2.mimetype = 'text/plain';
318+
319+
form.onPart(part1).then(() => {
320+
part1.emit('data', Buffer.alloc(1));
321+
part1.emit('end');
322+
form.onPart(part2);
323+
});
324+
});
325+
326+
test(`${name}: maxFiles defaults to 1000`, () => {
327+
const form = getForm(name);
328+
expect(form.options.maxFiles).toBe(1000);
329+
});
330+
305331
// test(`${name}: use custom options.originalFilename instead of form._uploadPath`, () => {
306332
// const form = getForm(name, {
307333
// originalFilename: (_) => path.join(__dirname, 'sasa'),

0 commit comments

Comments
 (0)