diff --git a/services/wordlist.js b/services/wordlist.js index 49b646bcb..96e0c3b1f 100644 --- a/services/wordlist.js +++ b/services/wordlist.js @@ -117,53 +117,52 @@ class Wordlist { }); } - filter(...fields) { - return (req, res, next) => { + /** + * Perform the filtering based on the loaded wordlists. + */ + filter(body, ...fields) { - // Start with the sensible default that the content does not contain - // profanity. - req.wordlist = { - matched: false - }; + // Start with the sensible default that the content does not contain + // profanity. + let errors = {}; - // Loop over all the fields from the body that we want to check. - for (let i = 0; i < fields.length; i++) { - let field = fields[i]; + // Loop over all the fields from the body that we want to check. + for (let i = 0; i < fields.length; i++) { + let field = fields[i]; - let phrase = _.get(req.body, field, false); + let phrase = _.get(body, field, false); - // If the field doesn't exist in the body, then it can't be profane! - if (!phrase) { + // If the field doesn't exist in the body, then it can't be profane! + if (!phrase) { - // Return that there wasn't a profane word here. - continue; - } - - // Check if the field contains a banned word. - if (this.match(this.lists.banned, phrase)) { - debug(`the field "${field}" contained a phrase "${phrase}" which contained a banned word/phrase`); - - req.wordlist.banned = ErrContainsProfanity; - - // Stop looping through the fields now, we discovered the worst possible - // situation (a banned word). - break; - } - - // Check if the field contains a banned word. - if (this.match(this.lists.suspect, phrase)) { - debug(`the field "${field}" contained a phrase "${phrase}" which contained a suspected word/phrase`); - - req.wordlist.suspect = ErrContainsProfanity; - - // Continue looping through the fields now, we discovered a possible bad - // word (suspect). - continue; - } + // Return that there wasn't a profane word here. + continue; } - next(); - }; + // Check if the field contains a banned word. + if (this.match(this.lists.banned, phrase)) { + debug(`the field "${field}" contained a phrase "${phrase}" which contained a banned word/phrase`); + + errors.banned = ErrContainsProfanity; + + // Stop looping through the fields now, we discovered the worst possible + // situation (a banned word). + break; + } + + // Check if the field contains a banned word. + if (this.match(this.lists.suspect, phrase)) { + debug(`the field "${field}" contained a phrase "${phrase}" which contained a suspected word/phrase`); + + errors.suspect = ErrContainsProfanity; + + // Continue looping through the fields now, we discovered a possible bad + // word (suspect). + continue; + } + } + + return errors; } /** @@ -186,7 +185,10 @@ class Wordlist { // Perform a filtering operation using the new instance of the // Wordlist. - wl.filter(...fields)(req, res, next); + req.wordlist = wl.filter(req.body, ...fields); + + // Call the next piece of middleware. + next(); }); }; } diff --git a/tests/services/wordlist.js b/tests/services/wordlist.js index dd3e5257e..4edfef0c7 100644 --- a/tests/services/wordlist.js +++ b/tests/services/wordlist.js @@ -6,13 +6,12 @@ describe('wordlist: services', () => { const wordlists = { banned: [ - 'BAD', - 'bad', - 'how to murder', - 'how to kill' + 'cookies', + 'how to do bad things', + 'how to do really bad things' ], suspect: [ - 'murder' + 'do bad things' ] }; @@ -35,12 +34,12 @@ describe('wordlist: services', () => { it('does match on a bad word', () => { [ - 'how to kill', - 'what is bad', - 'bad', - 'BAD.', - 'how to murder', - 'How To mUrDer' + 'how to do really bad things', + 'what is cookies', + 'cookies', + 'COOKIES.', + 'how to do bad things', + 'How To do bad things!' ].forEach((word) => { expect(wordlist.match(bannedList, word)).to.be.true; }); @@ -49,10 +48,9 @@ describe('wordlist: services', () => { it('does not match on a good word', () => { [ 'how to', - 'kill', - 'bads', + 'cookie', 'how to be a great person?', - 'how to not kill?' + 'how to not do really bad things?' ].forEach((word) => { expect(wordlist.match(bannedList, word)).to.be.false; }); @@ -64,62 +62,29 @@ describe('wordlist: services', () => { before(() => wordlist.upsert(wordlists)); - it('matches on bodies containing bad words', (done) => { - - let req = { - body: { - content: 'how to kill?' - } - }; - - wordlist.filter('content')(req, {}, (err) => { - expect(err).to.be.undefined; - expect(req).to.have.property('wordlist'); - expect(req.wordlist).to.have.property('matched'); - expect(req.wordlist.banned).to.be.equal(Wordlist.ErrContainsProfanity); - - done(); - }); + it('matches on bodies containing bad words', () => { + let errors = wordlist.filter({ + content: 'how to do really bad things?' + }, 'content'); + expect(errors).to.have.property('banned', Wordlist.ErrContainsProfanity); }); - it('does not match on bodies not containing bad words', (done) => { - - let req = { - body: { - content: 'how to be a great person?' - } - }; - - wordlist.filter('content')(req, {}, (err) => { - expect(err).to.be.undefined; - expect(req).to.have.property('wordlist'); - expect(req.wordlist).to.have.property('matched'); - expect(req.wordlist.matched).to.be.false; - - done(); - }); + it('does not match on bodies not containing bad words', () => { + let errors = wordlist.filter({ + content: 'how to not do really bad things?' + }, 'content'); + expect(errors).to.not.have.property('banned'); }); - it('does not match on bodies not containing the bad word field', (done) => { - - let req = { - body: { - author: 'how to kill?', - content: 'how to be a great person?' - } - }; - - wordlist.filter('content')(req, {}, (err) => { - expect(err).to.be.undefined; - expect(req).to.have.property('wordlist'); - expect(req.wordlist).to.have.property('matched'); - expect(req.wordlist.matched).to.be.false; - - done(); - }); + it('does not match on bodies not containing the bad word field', () => { + let errors = wordlist.filter({ + author: 'how to do really bad things?', + content: 'how to be a great person?' + }, 'content'); + expect(errors).to.not.have.property('banned'); }); });