Skip to content
Myr

fix(validator): prevent type diff bug in form data parsing (#4753)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* fix(validator): prevent prototype pollution in form data parsing Use Object.create(null) instead of {} for the form data object and replace `key in form` with `Object.hasOwn(form, key)` to prevent __proto__ keys in FormData from polluting the object prototype chain. Add regression tests verifying null prototype and safe __proto__ handling. * chore: clean up comments in validator.test.ts Remove commented-out lines in the validator test.

authored by EdamAmex · committed by GitHub · parent df97e5f · Browse files · 2 files+22 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+22 added−2 removed

  • tests:+20 added−0 removed
  • other:+2 added−2 removed

2 files+22 added−2 removed

modified:src/validator/validator.test.ts+20 added
1416 unmodified lines
1417 expect(result.cloned).toBe(true)1418 expect(result.payload).toMatchObject(testData)1419 })1420})14211422describe('Form validator prototype pollution prevention', () => {1423 it('should store __proto__ as data and not misdetect inherited keys', async () => {1424 const app = new Hono()1425 app.post(1426 '/form',1427 validator('form', (value) => value),1428 (c) => c.json(c.req.valid('form'))1429 )14301431 const form = new FormData()1432 form.append('__proto__', 'evil')1433 form.append('toString', 'hello')14341435 const res = await app.request('/form', { method: 'POST', body: form })1436 const result = await res.json()1437 expect(result['__proto__']).toBe('evil')1438 expect(result['toString']).toBe('hello')1439 })1440})
modified:src/validator/validator.ts+2 added-2 removed