Skip to content

Commit

Permalink
Preventing prototype pollution vulnerabilities (#2115)
Browse files Browse the repository at this point in the history
* Preventing prototype pollution vulnerabilities

* added changelog.md

* changed changelog

* added each in tests

* fixed test description

* fixed vulnerability for add method

* fixed tests and changelog as suggested
  • Loading branch information
AnastasiiaSvietlova authored Apr 15, 2024
1 parent be1ea1d commit f1a2167
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Changed

- (metadata-delegate) Preventing prototype pollution vulnerabilities [#2115](https://github.com/bugsnag/bugsnag-js/pull/2115)
- (plugin-interaction-breadcrumbs) Improved performance of click event breadcrumbs [#2094](https://github.com/bugsnag/bugsnag-js/pull/2094)
- (react-native) Rename Bugsnag.framework to BugsnagReactNative.framework [#2117](https://github.com/bugsnag/bugsnag-js/pull/2117)

Expand Down
10 changes: 10 additions & 0 deletions packages/core/lib/metadata-delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ const add = (state, section, keyOrObj, maybeVal) => {
// exit if we don't have an updates object at this point
if (!updates) return

// preventing the __proto__ property from being used as a key
if (section === '__proto__' || section === 'constructor' || section === 'prototype') {
return
}

// ensure a section with this name exists
if (!state[section]) state[section] = {}

Expand Down Expand Up @@ -41,6 +46,11 @@ const clear = (state, section, key) => {
return
}

// preventing the __proto__ property from being used as a key
if (section === '__proto__' || section === 'constructor' || section === 'prototype') {
return
}

// clear a single value from a section
if (state[section]) {
delete state[section][key]
Expand Down
58 changes: 58 additions & 0 deletions packages/core/test/metadata-delegate.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { add, clear } from '../lib/metadata-delegate'

// it doesn't seem easy or even impossible to check whether __proto__ keys can be overwritten
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/proto
// so tests are only for prototype and constructor

describe('metadata delegate', () => {
describe('add', () => {
it.each([
{
key: 'constructor',
expected: {}
},
{
key: 'prototype',
expected: {}
}
])('should not add $key keys', ({ key, expected }) => {
const state = {}
add(state, key, 'foo', 'bar')
expect(state).toEqual(expected)
})
})

describe('clear', () => {
it.each([
{
key: 'constructor',
state: {
constructor: {
foo: 'bar'
}
},
expected: {
constructor: {
foo: 'bar'
}
}
},
{
key: 'prototype',
state: {
prototype: {
foo: 'bar'
}
},
expected: {
prototype: {
foo: 'bar'
}
}
}
])('should not overwrite $key keys', ({ key, state, expected }) => {
clear(state, key, 'foo')
expect(state).toEqual(expected)
})
})
})

0 comments on commit f1a2167

Please sign in to comment.