Skip to content

Commit 284d6e0

Browse files
committed
fix: validate that limit option is non-negative
Add validation to reject negative limit values like '-100kb' that were previously silently accepted. This prevents configuration errors from going unnoticed. Fixes #705
1 parent 2322e11 commit 284d6e0

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

lib/utils.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ function normalizeOptions (options, defaultType) {
6363
var limit = typeof options?.limit === 'undefined' || options?.limit === null
6464
? 102400 // 100kb default
6565
: bytes.parse(options.limit)
66+
67+
if (limit !== null && limit < 0) {
68+
throw new TypeError('option limit must be a non-negative number')
69+
}
70+
6671
var type = options?.type || defaultType
6772
var verify = options?.verify || false
6873
var defaultCharset = options?.defaultCharset || 'utf-8'

test/utils.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,28 @@ describe('normalizeOptions(options, defaultType)', () => {
130130
normalizeOptions({ limit: { foo: 'bar' } }, 'application/json')
131131
}, /option limit "\[object Object\]" is invalid/)
132132
})
133+
134+
it('should throw an error for negative string limit', () => {
135+
assert.throws(() => {
136+
normalizeOptions({ limit: '-100kb' }, 'application/json')
137+
}, /option limit must be a non-negative number/)
138+
})
139+
140+
it('should throw an error for negative number limit', () => {
141+
assert.throws(() => {
142+
normalizeOptions({ limit: -1024 }, 'application/json')
143+
}, /option limit must be a non-negative number/)
144+
})
145+
146+
it('should accept zero limit', () => {
147+
const result = normalizeOptions({ limit: 0 }, 'application/json')
148+
assert.strictEqual(result.limit, 0)
149+
})
150+
151+
it('should accept zero string limit', () => {
152+
const result = normalizeOptions({ limit: '0kb' }, 'application/json')
153+
assert.strictEqual(result.limit, 0)
154+
})
133155
})
134156

135157
describe('type', () => {

0 commit comments

Comments
 (0)