From 3014017976faf95454b12444b833df4d8691fa0c Mon Sep 17 00:00:00 2001 From: Hector Nieva Date: Fri, 7 Feb 2020 21:28:08 -0300 Subject: [PATCH] Fix regexp mathing of banned word list by exchanging .test() with .match() (#2828) * [Fix] Use .match instead of .test to avoid false positives when matching regex returned groups * Add test to assure memoized regexp for banned words will match properly * fix: fixes bug with wordlist matching Co-authored-by: Wyatt Johnson --- .../comments/pipeline/wordList.spec.ts | 22 ++++++++++++++++++- .../services/comments/pipeline/wordList.ts | 2 +- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/core/server/services/comments/pipeline/wordList.spec.ts b/src/core/server/services/comments/pipeline/wordList.spec.ts index a56533671..967e6d1ee 100644 --- a/src/core/server/services/comments/pipeline/wordList.spec.ts +++ b/src/core/server/services/comments/pipeline/wordList.spec.ts @@ -1,4 +1,7 @@ -import { containsMatchingPhrase } from "coral-server/services/comments/pipeline/wordList"; +import { + containsMatchingPhrase, + containsMatchingPhraseMemoized, +} from "coral-server/services/comments/pipeline/wordList"; const phrases = [ "cookies", @@ -44,3 +47,20 @@ describe("containsMatchingPhrase", () => { expect(containsMatchingPhrase([], "test")).toEqual(false); }); }); + +describe("containsMatchingPhraseMemoized", () => { + it("return true for all cases after memoizing the first result", () => { + [ + "cookies 1", + "cookies 2", + "cookies 4", + "cookies 5", + "this is for cookies 6", + "this is for cookies 7", + "this is for cookies 8", + "this is for cookies 9", + ].forEach(word => { + expect(containsMatchingPhraseMemoized(phrases, word)).toEqual(true); + }); + }); +}); diff --git a/src/core/server/services/comments/pipeline/wordList.ts b/src/core/server/services/comments/pipeline/wordList.ts index 418086054..2ad61869b 100644 --- a/src/core/server/services/comments/pipeline/wordList.ts +++ b/src/core/server/services/comments/pipeline/wordList.ts @@ -22,7 +22,7 @@ export function generateRegExp(phrases: string[]) { .join('[\\s"?!.]+') ) .join("|"); - return new RegExp(`(^|[^\\w])(${inner})(?=[^\\w]|$)`, "gmiu"); + return new RegExp(`(^|[^\\w])(${inner})(?=[^\\w]|$)`, "miu"); } export const generateRegExpMemoized = memoize(generateRegExp);