Skip to content

Commit f9c7082

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 4a862fe commit f9c7082

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 !== 'number'
6464
? bytes.parse(options?.limit || '100kb')
6565
: 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
@@ -85,6 +85,28 @@ describe('normalizeOptions(options, defaultType)', () => {
8585
const result = normalizeOptions({ limit: 'invalid' }, 'application/json')
8686
assert.strictEqual(result.limit, null)
8787
})
88+
89+
it('should throw an error for negative string limit', () => {
90+
assert.throws(() => {
91+
normalizeOptions({ limit: '-100kb' }, 'application/json')
92+
}, /option limit must be a non-negative number/)
93+
})
94+
95+
it('should throw an error for negative number limit', () => {
96+
assert.throws(() => {
97+
normalizeOptions({ limit: -1024 }, 'application/json')
98+
}, /option limit must be a non-negative number/)
99+
})
100+
101+
it('should accept zero limit', () => {
102+
const result = normalizeOptions({ limit: 0 }, 'application/json')
103+
assert.strictEqual(result.limit, 0)
104+
})
105+
106+
it('should accept zero string limit', () => {
107+
const result = normalizeOptions({ limit: '0kb' }, 'application/json')
108+
assert.strictEqual(result.limit, 0)
109+
})
88110
})
89111

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

0 commit comments

Comments
 (0)