Skip to content

Commit

Permalink
Merge pull request #786 from bugsnag/v7-user-config
Browse files Browse the repository at this point in the history
fix(core): Stricter validation on user, copying over to prevent mutations
  • Loading branch information
bengourley committed Mar 27, 2020
2 parents 6ab18f3 + 934f535 commit 3906770
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/core/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,8 @@ class Client {
}

// update and elevate some options
this._metadata = config.metadata
this._user = config.user
this._metadata = assign({}, config.metadata)
this._user = assign({}, config.user)
this._context = config.context
if (config.logger) this._logger = config.logger

Expand Down
10 changes: 8 additions & 2 deletions packages/core/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,14 @@ module.exports.schema = {
},
user: {
defaultValue: () => ({}),
message: 'should be an object',
validate: (value) => typeof value === 'object' && value !== null
message: 'should be an object with { id, email, name } properties',
validate: value =>
(value === null) ||
(value && reduce(
keys(value),
(accum, key) => accum && includes(['id', 'email', 'name'], key),
true
))
},
metadata: {
defaultValue: () => ({}),
Expand Down
11 changes: 11 additions & 0 deletions packages/core/test/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ describe('@bugsnag/core/config', () => {
})
})

describe('user', () => {
it('should only allow id, name and email', () => {
const userValidator = config.schema.user.validate
expect(userValidator(null)).toBe(true)
expect(userValidator({ id: '123', email: 'bug@sn.ag', name: 'Bugsnag' })).toBe(true)
expect(userValidator({ id: '123', email: 'bug@sn.ag', name: 'Bugsnag', extra: 'aaa' })).toBe(false)
expect(userValidator({ id: '123' })).toBe(true)
expect(userValidator('123')).toBe(false)
})
})

describe('enabledBreadcrumbTypes', () => {
it('fails when a supplied value is not a valid breadcrumb type', () => {
const enabledBreadcrumbTypesValidator = config.schema.enabledBreadcrumbTypes.validate
Expand Down

0 comments on commit 3906770

Please sign in to comment.