Skip to content

Commit

Permalink
Improved solution to issue getsentry#179 (invalid options throwing er…
Browse files Browse the repository at this point in the history
…rors in joinRegExp)

This slightly more robust solution ignores options that are not strings
or regular expressions in the passed patterns array.

The prior solution still accepted non-string, non-regexp options (e.g.
[], {}, true, etc.).

Credit goes to @soundslocke for the fix.
  • Loading branch information
johnbacon committed Jan 11, 2014
1 parent fdac9de commit 0bd6221
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/raven.js
Original file line number Diff line number Diff line change
Expand Up @@ -644,14 +644,15 @@ function joinRegExp(patterns) {
// Be mad.
var sources = [], i = patterns.length;
while (i--) {
if (!isUndefined(patterns[i]) && patterns[i]) {
sources[i] = isString(patterns[i]) ?
// If it's a string, we need to escape it
// Taken from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
patterns[i].replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1") :
// If it's a regexp already, we want to extract the source
patterns[i].source;
if (isString(patterns[i])) {
// If it's a string, we need to escape it
// Taken from: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
sources.unshift(patterns[i].replace(/([.*+?^=!:${}()|\[\]\/\\])/g, "\\$1"));
} else if (!isUndefined(patterns[i]) && patterns[i] && patterns[i].source) {
// If it's a regexp already, we want to extract the source
sources.unshift(patterns[i].source);
}
// Intentionally skip other cases
}
return new RegExp(sources.join('|'), 'i');
}
Expand Down
6 changes: 6 additions & 0 deletions test/raven.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,12 @@ describe('globals', function() {
'a', 'b', null, undefined
]).source, 'a|b');
});

it('should skip entries that are not strings or regular expressions in the passed array of patterns', function() {
assert.equal(joinRegExp([
'a', 'b', null, 'a.b', undefined, true, /d/, 123, {}, /[0-9]/, []
]).source, 'a|b|a\\.b|d|[0-9]');
});
});
});

Expand Down

0 comments on commit 0bd6221

Please sign in to comment.