Skip to content

Commit 31126d6

Browse files
committed
fix(isProfaneLike): abort trying to match every instance of profane words.
This has caused several issues should be up to the implementer to augment the list for their specific use-case. From this point on, only exact (case-insensitive) matches will be filtered. RocketChat/Rocket.Chat#9880, #33, #25, #29
1 parent 04ac4ae commit 31126d6

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

lib/badwords.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,20 @@ var Filter = (function() {
3939
* @param {string} word - String to evaluate for profanity.
4040
*/
4141
Filter.prototype.isProfaneLike = function profaneLike(word) {
42-
return !!~this.exclude.indexOf(word) ? false : !!~this.list.indexOf(word) || this.list
42+
if (!!~this.exclude.indexOf(word)) {
43+
return false;
44+
}
45+
46+
if (!!~this.list.indexOf(word)) {
47+
return true;
48+
}
49+
50+
return this.list
4351
.map(function(w) {
44-
return new RegExp(w.replace(/(\W)/g, '\\$1'), 'gi');
52+
return new RegExp('^' + w.replace(/(\W)/g, '\\$1') + '$', 'gi');
4553
}, this)
4654
.reduce(function(outcome, wordExp) {
47-
if (wordExp.test(word)) {
48-
return true;
49-
}
50-
return outcome;
55+
return outcome || wordExp.test(word);
5156
}, false);
5257
};
5358

test/filter.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ var Filter = require('../lib/badwords.js'),
66
describe('filter', function(){
77
describe('clean',function(){
88
it('Should replace a bad word within a sentence asterisks (******)',function(){
9+
console.log(filter.clean('Don\'t be an ash0le'));
910
assert(filter.clean('Don\'t be an ash0le') === 'Don\'t be an ******');
11+
1012
});
1113

1214
it('Should replace multiple instances of any bad words within a sentence asterisks (******)',function(){
@@ -34,8 +36,12 @@ describe('filter', function(){
3436
assert(filter.clean('<p>Don\'t be an asshole</p>') === '<p>Don\'t be an *******</p>');
3537
});
3638

37-
it('Should filter words that are derivatives of words from the filter blacklist', function() {
39+
xit('Should filter words that are derivatives of words from the filter blacklist', function() {
3840
assert(filter.clean('shitshit') === '********');
3941
});
42+
43+
it('Shouldn\'t filter words that aren\'t profane.', function() {
44+
assert(filter.clean('hello there') === 'hello there');
45+
});
4046
});
4147
});

0 commit comments

Comments
 (0)