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

Error on non-number limit rather than ignoring #7

Merged
merged 6 commits into from
Nov 27, 2021
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
23 changes: 7 additions & 16 deletions lib/types/multipart.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var Dicer = require('dicer');

var parseParams = require('../utils').parseParams,
decodeText = require('../utils').decodeText,
basename = require('../utils').basename;
basename = require('../utils').basename,
getLimit = require('../utils').getLimit;

var RE_BOUNDARY = /^boundary$/i,
RE_FIELD = /^form-data$/i,
Expand Down Expand Up @@ -57,21 +58,11 @@ function Multipart(boy, cfg) {
if (typeof boundary !== 'string')
throw new Error('Multipart: Boundary not found');

var fieldSizeLimit = (limits && typeof limits.fieldSize === 'number'
? limits.fieldSize
: 1 * 1024 * 1024),
fileSizeLimit = (limits && typeof limits.fileSize === 'number'
? limits.fileSize
: Infinity),
filesLimit = (limits && typeof limits.files === 'number'
? limits.files
: Infinity),
fieldsLimit = (limits && typeof limits.fields === 'number'
? limits.fields
: Infinity),
partsLimit = (limits && typeof limits.parts === 'number'
? limits.parts
: Infinity);
var fieldSizeLimit = getLimit(limits, 'fieldSize', 1 * 1024 * 1024),
fileSizeLimit = getLimit(limits, 'fileSize', Infinity),
filesLimit = getLimit(limits, 'files', Infinity),
fieldsLimit = getLimit(limits, 'fields', Infinity),
partsLimit = getLimit(limits, 'parts', Infinity);

var nfiles = 0,
nfields = 0,
Expand Down
15 changes: 5 additions & 10 deletions lib/types/urlencoded.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var Decoder = require('../utils').Decoder,
decodeText = require('../utils').decodeText;
decodeText = require('../utils').decodeText,
getLimit = require('../utils').getLimit;

var RE_CHARSET = /^charset$/i;

Expand All @@ -12,15 +13,9 @@ function UrlEncoded(boy, cfg) {
parsedConType = cfg.parsedConType;
this.boy = boy;

this.fieldSizeLimit = (limits && typeof limits.fieldSize === 'number'
? limits.fieldSize
: 1 * 1024 * 1024);
this.fieldNameSizeLimit = (limits && typeof limits.fieldNameSize === 'number'
? limits.fieldNameSize
: 100);
this.fieldsLimit = (limits && typeof limits.fields === 'number'
? limits.fields
: Infinity);
this.fieldSizeLimit = getLimit(limits, 'fieldSize', 1 * 1024 * 1024);
this.fieldNameSizeLimit = getLimit(limits, 'fieldNameSize', 100);
this.fieldsLimit = getLimit(limits, 'fields', Infinity);

var charset;
for (var i = 0, len = parsedConType.length; i < len; ++i) {
Expand Down
19 changes: 19 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,22 @@ function basename(path) {
return (path === '..' || path === '.' ? '' : path);
}
exports.basename = basename;


function getLimit(limits, name, defaultLimit) {
if (!limits)
return defaultLimit;

var limit = limits[name];
// Intentional double equals
if (limit == undefined)
return defaultLimit;

// Ensure limit is a number and is not NaN
if (typeof limit !== 'number' || limit !== limit) {
throw new Error('Limit ' + name + ' is not a valid number');
}

return limit;
}
exports.getLimit = getLimit;
22 changes: 22 additions & 0 deletions test/test-utils-get-limit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
var getLimit = require('../lib/utils').getLimit;

var assert = require('assert');

assert.strictEqual(getLimit(undefined, 'fieldSize', 1), 1);
assert.strictEqual(getLimit(undefined, 'fileSize', Infinity), Infinity);

assert.strictEqual(getLimit({}, 'fieldSize', 1), 1);
assert.strictEqual(getLimit({}, 'fileSize', Infinity), Infinity);
assert.strictEqual(getLimit({ fieldSize: null }, 'fieldSize', 1), 1);
assert.strictEqual(getLimit({ fileSize: null }, 'fileSize', Infinity), Infinity);

assert.strictEqual(getLimit({ fieldSize: 0 }, 'fieldSize', 1), 0);
assert.strictEqual(getLimit({ fileSize: 2 }, 'fileSize', 1), 2);

assert.throws(function() {
getLimit({ fieldSize: '1' }, 'fieldSize', 1);
}, /^Error: Limit fieldSize is not a valid number$/);

assert.throws(function() {
getLimit({ fieldSize: NaN }, 'fieldSize', 1);
}, /^Error: Limit fieldSize is not a valid number$/);