Skip to content

Commit

Permalink
Merge pull request #313 from bugsnag/ip-option
Browse files Browse the repository at this point in the history
Add option to prevent IP collection
  • Loading branch information
bengourley committed Jan 23, 2018
2 parents 8cfa050 + 5a03f7d commit 3a45199
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 76 deletions.
5 changes: 5 additions & 0 deletions browser/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,10 @@ module.exports = {
},
message: '(string) releaseStage should be set',
validate: value => typeof value === 'string' && value.length
},
collectUserIp: {
defaultValue: () => true,
message: '(boolean) collectUserIp should true/false',
validate: value => value === true || value === false
}
}
4 changes: 3 additions & 1 deletion browser/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const plugins = {
'navigation breadcrumbs': require('./plugins/navigation-breadcrumbs'),
'interaction breadcrumbs': require('./plugins/interaction-breadcrumbs'),
'inline script content': require('./plugins/inline-script-content'),
'sessions': require('./plugins/sessions')
'sessions': require('./plugins/sessions'),
'ip': require('./plugins/ip')
}

const transports = {
Expand Down Expand Up @@ -72,6 +73,7 @@ module.exports = (opts, userPlugins = []) => {
bugsnag.use(plugins['inline script content'])
bugsnag.use(plugins['throttle'])
bugsnag.use(plugins['sessions'])
bugsnag.use(plugins['ip'])

// optional browser-specific plugins

Expand Down
12 changes: 12 additions & 0 deletions browser/plugins/ip.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Prevent collection of user IPs
*/
module.exports = {
init: (client) => {
if (client.config.collectUserIp) return
client.config.beforeSend.push(report => {
report.user = { id: '[NOT COLLECTED]', ...report.user }
report.request = { clientIp: '[NOT COLLECTED]', ...report.request }
})
}
}
55 changes: 55 additions & 0 deletions browser/plugins/test/ip.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// magical jasmine globals
const { describe, it, expect } = global

const plugin = require('../ip')

const Client = require('../../../base/client')
const schema = { ...require('../../../base/config').schema, ...require('../../config') }
const VALID_NOTIFIER = { name: 't', version: '0', url: 'http://' }

describe('plugin: ip', () => {
it('does nothing when collectUserIp=true', () => {
const client = new Client(VALID_NOTIFIER, schema)
const payloads = []
client.configure({ apiKey: 'API_KEY_YEAH' })
client.use(plugin)

client.transport({ sendReport: (logger, config, payload) => payloads.push(payload) })
client.notify(new Error('noooo'), {
beforeSend: report => { report.request = { 'some': 'detail' } }
})

expect(payloads.length).toEqual(1)
expect(payloads[0].events[0].request).toEqual({ 'some': 'detail' })
})

it('doesn’t overwrite an existing user id', () => {
const client = new Client(VALID_NOTIFIER, schema)
const payloads = []
client.configure({ apiKey: 'API_KEY_YEAH', collectUserIp: false })
client.use(plugin)

client.user = { id: 'foobar' }

client.transport({ sendReport: (logger, config, payload) => payloads.push(payload) })
client.notify(new Error('noooo'))

expect(payloads.length).toEqual(1)
expect(payloads[0].events[0].user).toEqual({ id: 'foobar' })
expect(payloads[0].events[0].request).toEqual({ clientIp: '[NOT COLLECTED]' })
})

it('redacts user IP if none is provided', () => {
const client = new Client(VALID_NOTIFIER, schema)
const payloads = []
client.configure({ apiKey: 'API_KEY_YEAH', collectUserIp: false })
client.use(plugin)

client.transport({ sendReport: (logger, config, payload) => payloads.push(payload) })
client.notify(new Error('noooo'))

expect(payloads.length).toEqual(1)
expect(payloads[0].events[0].user).toEqual({ id: '[NOT COLLECTED]' })
expect(payloads[0].events[0].request).toEqual({ clientIp: '[NOT COLLECTED]' })
})
})

0 comments on commit 3a45199

Please sign in to comment.