Skip to content

Commit

Permalink
Merge pull request #319 from rollbar/hostBlacklist
Browse files Browse the repository at this point in the history
add a blacklist option to the browser side [Fixes #283]
  • Loading branch information
rokob authored Jun 29, 2017
2 parents 350af47 + 5e976d7 commit ad69fa5
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 9 deletions.
39 changes: 30 additions & 9 deletions src/browser/predicates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -121,6 +141,7 @@ function messageIsIgnored(item, settings) {
module.exports = {
checkIgnore: checkIgnore,
userCheckIgnore: userCheckIgnore,
urlIsBlacklisted: urlIsBlacklisted,
urlIsWhitelisted: urlIsWhitelisted,
messageIsIgnored: messageIsIgnored
};
Expand Down
95 changes: 95 additions & 0 deletions test/browser.predicates.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit ad69fa5

Please sign in to comment.