Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a blacklist option to the browser side [Fixes #283] #319

Merged
merged 1 commit into from
Jun 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 31 additions & 10 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 All @@ -84,7 +104,7 @@ function messageIsIgnored(item, settings) {
try {
messageIsIgnored = false;
ignoredMessages = settings.ignoredMessages;

if (!ignoredMessages || ignoredMessages.length === 0) {
return true;
}
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