Skip to content

Commit 40e1c71

Browse files
committed
fix: prevent prototype pollution via multipart field names
1 parent 3017053 commit 40e1c71

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/form_fields.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export class FormFields {
1717
/**
1818
* Internal storage for form fields
1919
*/
20-
#fields: any = {}
20+
#fields: any = Object.create(null)
2121

2222
#normalizer?: (value: string) => string | null
2323

tests/form_fields.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,4 +94,20 @@ test.group('Form Fields Parser', () => {
9494
formFields.add('username', ' ')
9595
assert.deepEqual(formFields.get(), { username: null })
9696
})
97+
98+
test('should not pollute Object.prototype via __proto__', ({ assert }) => {
99+
const formFields = new FormFields()
100+
formFields.add('__proto__.polluted', 'yes')
101+
102+
const clean: Record<string, any> = {}
103+
assert.isUndefined(clean['polluted'])
104+
})
105+
106+
test('should not pollute Object.prototype via constructor.prototype', ({ assert }) => {
107+
const formFields = new FormFields()
108+
formFields.add('constructor.prototype.polluted2', 'yes')
109+
110+
const clean: Record<string, any> = {}
111+
assert.isUndefined(clean['polluted2'])
112+
})
97113
})

0 commit comments

Comments
 (0)