Skip to content

Commit

Permalink
fix: prevent prototype polluting
Browse files Browse the repository at this point in the history
  • Loading branch information
pionxzh committed Jan 16, 2024
1 parent ec741f1 commit 5474c87
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export function deleteValue (input: any, path: (string | number)[], value: any)

const [key, ...restPath] = path
if (key !== undefined) {
if (key === '__proto__') {
throw new TypeError('Modification of prototype is not allowed')
}
if (restPath.length > 0) {
input[key] = deleteValue(input[key], restPath, value)
} else {
Expand Down
14 changes: 14 additions & 0 deletions tests/util.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ describe('function applyValue', () => {
}).toThrow()
})

test('prototype polluting', () => {
const original = {}
expect(() => {
applyValue(original, ['__proto__', 'polluted'], 1)
}).toThrow()
})

test('undefined', () => {
patches.forEach(patch => {
const newValue = applyValue(undefined, [], patch)
Expand Down Expand Up @@ -101,6 +108,13 @@ describe('function deleteValue', () => {
}).toThrow()
})

test('prototype polluting', () => {
const original = {}
expect(() => {
deleteValue(original, ['__proto__', 'polluted'], 1)
}).toThrow()
})

test('undefined', () => {
patches.forEach(patch => {
const newValue = deleteValue(undefined, [], patch)
Expand Down

0 comments on commit 5474c87

Please sign in to comment.