diff --git a/src/browser/predicates.js b/src/browser/predicates.js index 0e4501d65..cc8a439dd 100644 --- a/src/browser/predicates.js +++ b/src/browser/predicates.js @@ -32,16 +32,31 @@ function userCheckIgnore(item, settings) { return true; } +function urlIsBlacklisted(item, settings) { + return urlIsOnAList(item, settings, 'blacklist'); +} + function urlIsWhitelisted(item, settings) { - var whitelist, trace, frame, filename, frameLength, url, listLength, urlRegex; + return urlIsOnAList(item, settings, 'whitelist'); +} + +function urlIsOnAList(item, settings, whiteOrBlack) { + // whitelist is the default + var black = false; + if (whiteOrBlack === 'blacklist') { + black = true; + } + var list, trace, frame, filename, frameLength, url, listLength, urlRegex; var i, j; try { - whitelist = settings.hostWhiteList; - listLength = whitelist && whitelist.length; + list = black ? settings.hostBlackList : settings.hostWhiteList; + listLength = list && list.length; trace = _.get(item, 'body.trace'); - if (!whitelist || listLength === 0) { + // These two checks are important to come first as they are defaults + // in case the list is missing or the trace is missing or not well-formed + if (!list || listLength === 0) { return true; } if (!trace || !trace.frames) { @@ -58,22 +73,27 @@ function urlIsWhitelisted(item, settings) { } for (j = 0; j < listLength; j++) { - url = whitelist[j]; + url = list[j]; urlRegex = new RegExp(url); if (urlRegex.test(filename)){ - return true; + return !black; } } } } catch (e) /* istanbul ignore next */ { - settings.hostWhiteList = null; - logger.error('Error while reading your configuration\'s hostWhiteList option. Removing custom hostWhiteList.', e); + if (black) { + settings.hostBlackList = null; + } else { + settings.hostWhiteList = null; + } + var listName = black ? 'hostBlackList' : 'hostWhiteList'; + logger.error('Error while reading your configuration\'s ' + listName + ' option. Removing custom ' + listName + '.', e); return true; } - return false; + return black; } function messageIsIgnored(item, settings) { @@ -84,7 +104,7 @@ function messageIsIgnored(item, settings) { try { messageIsIgnored = false; ignoredMessages = settings.ignoredMessages; - + if (!ignoredMessages || ignoredMessages.length === 0) { return true; } @@ -121,6 +141,7 @@ function messageIsIgnored(item, settings) { module.exports = { checkIgnore: checkIgnore, userCheckIgnore: userCheckIgnore, + urlIsBlacklisted: urlIsBlacklisted, urlIsWhitelisted: urlIsWhitelisted, messageIsIgnored: messageIsIgnored }; diff --git a/test/browser.predicates.test.js b/test/browser.predicates.test.js index b83b819b8..bd7c29ab6 100644 --- a/test/browser.predicates.test.js +++ b/test/browser.predicates.test.js @@ -211,6 +211,101 @@ describe('urlIsWhitelisted', function() { }); }); +describe('urlIsBlacklisted', function() { + it('should return true with no blacklist', function() { + var item = { + level: 'critical', + body: {trace: {frames: [ + {filename: 'http://api.fake.com/v1/something'}, + {filename: 'http://api.example.com/v1/something'}, + {filename: 'http://api.fake.com/v2/something'} + ]}} + }; + var settings = { + reportLevel: 'debug' + }; + expect(p.urlIsBlacklisted(item, settings)).to.be.ok(); + }); + it('should return true with no trace', function() { + var item = { + level: 'critical', + body: {message: 'hey'} + }; + var settings = { + reportLevel: 'debug', + hostBlackList: ['fake.com', 'other.com'] + }; + expect(p.urlIsBlacklisted(item, settings)).to.be.ok(); + }); + it('should return false if any regex matches at least one filename in the trace', function() { + var item = { + level: 'critical', + body: {trace: {frames: [ + {filename: 'http://api.fake.com/v1/something'}, + {filename: 'http://api.example.com/v1/something'}, + {filename: 'http://api.fake.com/v2/something'} + ]}} + }; + var settings = { + reportLevel: 'debug', + hostBlackList: ['example.com', 'other.com'] + }; + expect(p.urlIsBlacklisted(item, settings)).to.not.be.ok(); + }); + it('should return true if the filename is not a string', function() { + var item = { + level: 'critical', + body: {trace: {frames: [ + {filename: {url: 'http://api.fake.com/v1/something'}}, + {filename: {url: 'http://api.example.com/v1/something'}}, + {filename: {url: 'http://api.fake.com/v2/something'}}, + ]}} + }; + var settings = { + reportLevel: 'debug', + hostBlackList: ['example.com', 'other.com'] + }; + expect(p.urlIsBlacklisted(item, settings)).to.be.ok(); + }); + it('should return true if there is no frames key', function() { + var item = { + level: 'critical', + body: {trace: {notframes: []}} + }; + var settings = { + reportLevel: 'debug', + hostBlackList: ['nope.com'] + }; + expect(p.urlIsBlacklisted(item, settings)).to.be.ok(); + }); + it('should return true if there are no frames', function() { + var item = { + level: 'critical', + body: {trace: {frames: []}} + }; + var settings = { + reportLevel: 'debug', + hostBlackList: ['nope.com'] + }; + expect(p.urlIsBlacklisted(item, settings)).to.be.ok(); + }); + it('should return true if nothing in the blacklist matches', function() { + var item = { + level: 'critical', + body: {trace: {frames: [ + {filename: 'http://api.fake.com/v1/something'}, + {filename: 'http://api.example.com/v1/something'}, + {filename: 'http://api.fake.com/v2/something'} + ]}} + }; + var settings = { + reportLevel: 'debug', + hostBlackList: ['baz\.com', 'foo\.com'] + }; + expect(p.urlIsBlacklisted(item, settings)).to.be.ok(); + }); +}); + describe('messageIsIgnored', function() { it('true if no ignoredMessages setting', function() { var item = {