Skip to content

Commit 531e9c5

Browse files
authored
Merge commit from fork
1 parent 3a67f7f commit 531e9c5

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

‎src/utils/body.test.ts‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ describe('Parse Body Util', () => {
2222
})
2323
}
2424

25+
const createNestedKey = (depth: number) =>
26+
Array(depth + 1)
27+
.fill('a')
28+
.join('.')
29+
2530
it('should parse `multipart/form-data`', async () => {
2631
const data = new FormData()
2732
data.append('message', 'hello')
@@ -179,6 +184,49 @@ describe('Parse Body Util', () => {
179184
})
180185
})
181186

187+
it('should allow 32 nesting levels by default', async () => {
188+
const data = new FormData()
189+
data.append(createNestedKey(32), 'value')
190+
191+
const req = createRequest(FORM_URL, 'POST', data)
192+
let nested: unknown = await parseBody(req, { dot: true })
193+
194+
for (let i = 0; i <= 32; i++) {
195+
nested = (nested as Record<string, unknown>).a
196+
}
197+
expect(nested).toBe('value')
198+
})
199+
200+
it('should reject dot notation deeper than the default limit', async () => {
201+
const data = new FormData()
202+
data.append(createNestedKey(33), 'value')
203+
204+
const req = createRequest(FORM_URL, 'POST', data)
205+
206+
await expect(parseBody(req, { dot: true })).rejects.toThrow('Nesting limit exceeded')
207+
})
208+
209+
it('should reject deeply nested keys without splitting every segment', async () => {
210+
const req = createRequest(SEARCH_URL, 'POST', `${'.'.repeat(65_530)}=value`, {
211+
'Content-Type': 'application/x-www-form-urlencoded',
212+
})
213+
214+
await expect(parseBody(req, { dot: true })).rejects.toThrow('Nesting limit exceeded')
215+
})
216+
217+
it('should reject too many nested objects across multiple fields', async () => {
218+
const searchParams = new URLSearchParams()
219+
for (let i = 0; i <= 10_000; i++) {
220+
searchParams.append(`field${i}.value`, 'value')
221+
}
222+
223+
const req = createRequest(SEARCH_URL, 'POST', searchParams, {
224+
'Content-Type': 'application/x-www-form-urlencoded',
225+
})
226+
227+
await expect(parseBody(req, { dot: true })).rejects.toThrow('Nesting limit exceeded')
228+
})
229+
182230
it('should skip keys starting with __proto__. to prevent prototype pollution', async () => {
183231
const data = new FormData()
184232
data.append('__proto__.polluted', 'malicious')

‎src/utils/body.ts‎

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
import type { HonoRequest } from '../request'
77
import { bufferToFormData } from './buffer'
88

9+
const MAX_NESTING_DEPTH = 32
10+
const MAX_NESTED_OBJECTS = 10_000
11+
912
type BodyDataValueDot = { [x: string]: string | File | BodyDataValueDot }
1013
type BodyDataValueDotAll = {
1114
[x: string]: string | File | (string | File)[] | BodyDataValueDotAll
@@ -159,6 +162,7 @@ function convertFormDataToBodyData<T extends BodyData = BodyData>(
159162
options: ParseBodyOptions
160163
): T {
161164
const form: BodyData = Object.create(null)
165+
const nestingState = { count: 0 }
162166

163167
formData.forEach((value, key) => {
164168
const shouldParseAllValues = options.all || key.endsWith('[]')
@@ -175,7 +179,7 @@ function convertFormDataToBodyData<T extends BodyData = BodyData>(
175179
const shouldParseDotValues = key.includes('.')
176180

177181
if (shouldParseDotValues) {
178-
handleParsingNestedValues(form, key, value)
182+
handleParsingNestedValues(form, key, value, nestingState)
179183
delete form[key]
180184
}
181185
})
@@ -221,14 +225,18 @@ const handleParsingAllValues = (
221225
const handleParsingNestedValues = (
222226
form: BodyData,
223227
key: string,
224-
value: BodyDataValue<Partial<ParseBodyOptions>>
228+
value: BodyDataValue<Partial<ParseBodyOptions>>,
229+
state: { count: number }
225230
): void => {
226231
if (/(?:^|\.)__proto__\./.test(key)) {
227232
return
228233
}
229234

230235
let nestedForm = form
231-
const keys = key.split('.')
236+
const keys = key.split('.', MAX_NESTING_DEPTH + 2)
237+
if (keys.length > MAX_NESTING_DEPTH + 1) {
238+
throwNestingLimitExceeded()
239+
}
232240

233241
keys.forEach((key, index) => {
234242
if (index === keys.length - 1) {
@@ -240,9 +248,16 @@ const handleParsingNestedValues = (
240248
Array.isArray(nestedForm[key]) ||
241249
nestedForm[key] instanceof File
242250
) {
251+
if (state.count++ >= MAX_NESTED_OBJECTS) {
252+
throwNestingLimitExceeded()
253+
}
243254
nestedForm[key] = Object.create(null)
244255
}
245256
nestedForm = nestedForm[key] as unknown as BodyData
246257
}
247258
})
248259
}
260+
261+
const throwNestingLimitExceeded = (): never => {
262+
throw new Error('Nesting limit exceeded')
263+
}

0 commit comments

Comments
 (0)