From 18d6f14860f6985226fff562269eb3a3840a0559 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Fri, 13 Aug 2021 19:33:51 -0300 Subject: [PATCH 1/6] bad words for empty messages --- app/lib/server/methods/filterBadWords.js | 29 ---------------- app/lib/server/methods/filterBadWords.ts | 44 ++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 29 deletions(-) delete mode 100644 app/lib/server/methods/filterBadWords.js create mode 100644 app/lib/server/methods/filterBadWords.ts diff --git a/app/lib/server/methods/filterBadWords.js b/app/lib/server/methods/filterBadWords.js deleted file mode 100644 index 0e09a31d1777..000000000000 --- a/app/lib/server/methods/filterBadWords.js +++ /dev/null @@ -1,29 +0,0 @@ -import Filter from 'bad-words'; - -import { settings } from '../../../settings'; -import { callbacks } from '../../../callbacks'; - -callbacks.add('beforeSaveMessage', function(message) { - if (settings.get('Message_AllowBadWordsFilter')) { - const badWordsList = settings.get('Message_BadWordsFilterList'); - let whiteList = settings.get('Message_BadWordsWhitelist'); - let options; - - // Add words to the blacklist - if (!!badWordsList && badWordsList.length) { - options = { - list: badWordsList.split(','), - }; - } - const filter = new Filter(options); - - if (whiteList?.length) { - whiteList = whiteList.split(',').map((word) => word.trim()); - filter.removeWords(...whiteList); - } - - message.msg = filter.clean(message.msg); - } - - return message; -}, 1, 'filterBadWords'); diff --git a/app/lib/server/methods/filterBadWords.ts b/app/lib/server/methods/filterBadWords.ts new file mode 100644 index 000000000000..c8ec9d2b4147 --- /dev/null +++ b/app/lib/server/methods/filterBadWords.ts @@ -0,0 +1,44 @@ +import { Meteor } from 'meteor/meteor'; +import { Tracker } from 'meteor/tracker'; +import Filter from 'bad-words'; + +import { settings } from '../../../settings/server'; +import { callbacks } from '../../../callbacks/server'; + + +Meteor.startup(() => { + Tracker.autorun(() => { + const allowBadWordsFilter = settings.get('AllowBadWordsFilter'); + + if (!allowBadWordsFilter) { + return callbacks.remove('beforeSaveMessage', 'filterBadWords'); + } + + const badWordsList = settings.get('Message_BadWordsFilterList') as string | undefined; + const whiteList = settings.get('Message_BadWordsWhitelist') as string | undefined; + + const options = { + list: badWordsList?.split(',').map((word) => word.trim()) || [], + // library definition does not allow optional definition + exclude: undefined, + splitRegex: undefined, + placeHolder: undefined, + regex: undefined, + replaceRegex: undefined, + emptyList: undefined, + }; + + const filter = new Filter(options); + + if (whiteList?.length) { + filter.removeWords(...whiteList.split(',').map((word) => word.trim())); + } + + callbacks.add('beforeSaveMessage', function(message) { + if (!message.msg) { + return message; + } + message.msg = filter.clean(message.msg); + }, callbacks.priority.HIGH, 'filterBadWords'); + }); +}); From ec0b259673e19c3543a10c1d427c3cc69bb2d565 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Mon, 16 Aug 2021 10:55:32 -0300 Subject: [PATCH 2/6] definition --- app/lib/server/methods/filterBadWords.ts | 3 ++- package-lock.json | 5 +++++ package.json | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/app/lib/server/methods/filterBadWords.ts b/app/lib/server/methods/filterBadWords.ts index c8ec9d2b4147..a90176129d1a 100644 --- a/app/lib/server/methods/filterBadWords.ts +++ b/app/lib/server/methods/filterBadWords.ts @@ -4,6 +4,7 @@ import Filter from 'bad-words'; import { settings } from '../../../settings/server'; import { callbacks } from '../../../callbacks/server'; +import { IMessage } from '../../../../definition/IMessage'; Meteor.startup(() => { @@ -34,7 +35,7 @@ Meteor.startup(() => { filter.removeWords(...whiteList.split(',').map((word) => word.trim())); } - callbacks.add('beforeSaveMessage', function(message) { + callbacks.add('beforeSaveMessage', function(message: IMessage) { if (!message.msg) { return message; } diff --git a/package-lock.json b/package-lock.json index 0fd98600f251..3432c0db18f3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9247,6 +9247,11 @@ "@types/node": "*" } }, + "@types/bad-words": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/@types/bad-words/-/bad-words-3.0.1.tgz", + "integrity": "sha512-7la3ZDJG1tlRqySO+pnXycZpacaMEw/iLEm8kc4l+I+jN8KjBfoQVwO6jm98xzXVLrxV8vDrB5TaMoop8sKclQ==" + }, "@types/bcrypt": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/@types/bcrypt/-/bcrypt-5.0.0.tgz", diff --git a/package.json b/package.json index 3af3597c61e9..9f0fe711ce4b 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "@storybook/addons": "^6.3.6", "@storybook/react": "^6.3.6", "@types/agenda": "^2.0.9", + "@types/bad-words": "^3.0.1", "@types/bcrypt": "^5.0.0", "@types/body-parser": "^1.19.0", "@types/chai": "^4.2.19", From 04c67468acad765bfab00fd187d7a52eb2ba0817 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Mon, 16 Aug 2021 11:23:48 -0300 Subject: [PATCH 3/6] Fix Review --- app/lib/server/methods/filterBadWords.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/lib/server/methods/filterBadWords.ts b/app/lib/server/methods/filterBadWords.ts index a90176129d1a..4702cef45dab 100644 --- a/app/lib/server/methods/filterBadWords.ts +++ b/app/lib/server/methods/filterBadWords.ts @@ -11,8 +11,10 @@ Meteor.startup(() => { Tracker.autorun(() => { const allowBadWordsFilter = settings.get('AllowBadWordsFilter'); + callbacks.remove('beforeSaveMessage', 'filterBadWords'); + if (!allowBadWordsFilter) { - return callbacks.remove('beforeSaveMessage', 'filterBadWords'); + return; } const badWordsList = settings.get('Message_BadWordsFilterList') as string | undefined; From 5d8648fafbc8934ece38a2fb55a9d71a90e6b2b2 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Mon, 16 Aug 2021 13:33:47 -0300 Subject: [PATCH 4/6] Update app/lib/server/methods/filterBadWords.ts Co-authored-by: Diego Sampaio --- app/lib/server/methods/filterBadWords.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/lib/server/methods/filterBadWords.ts b/app/lib/server/methods/filterBadWords.ts index 4702cef45dab..3349332e878b 100644 --- a/app/lib/server/methods/filterBadWords.ts +++ b/app/lib/server/methods/filterBadWords.ts @@ -9,7 +9,7 @@ import { IMessage } from '../../../../definition/IMessage'; Meteor.startup(() => { Tracker.autorun(() => { - const allowBadWordsFilter = settings.get('AllowBadWordsFilter'); + const allowBadWordsFilter = settings.get('Message_AllowBadWordsFilter'); callbacks.remove('beforeSaveMessage', 'filterBadWords'); From 65fbf4cb2c77ef99492e96697fe2fb52bded1556 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Thu, 19 Aug 2021 13:10:21 -0300 Subject: [PATCH 5/6] Review --- app/lib/server/methods/filterBadWords.ts | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/lib/server/methods/filterBadWords.ts b/app/lib/server/methods/filterBadWords.ts index 3349332e878b..5d7b012e5f3b 100644 --- a/app/lib/server/methods/filterBadWords.ts +++ b/app/lib/server/methods/filterBadWords.ts @@ -6,9 +6,13 @@ import { settings } from '../../../settings/server'; import { callbacks } from '../../../callbacks/server'; import { IMessage } from '../../../../definition/IMessage'; - +const Dep = new Tracker.Dependency(); Meteor.startup(() => { + settings.get(/Message_AllowBadWordsFilter|Message_BadWordsFilterList|Message_BadWordsWhitelist/, () => { + Dep.changed(); + }); Tracker.autorun(() => { + Dep.depend(); const allowBadWordsFilter = settings.get('Message_AllowBadWordsFilter'); callbacks.remove('beforeSaveMessage', 'filterBadWords'); @@ -41,7 +45,12 @@ Meteor.startup(() => { if (!message.msg) { return message; } - message.msg = filter.clean(message.msg); + try { + message.msg = filter.clean(message.msg); + } finally { + // eslint-disable-next-line no-unsafe-finally + return message; + } }, callbacks.priority.HIGH, 'filterBadWords'); }); }); From 097edb127a39ccccdac38306f848020c3a253582 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Thu, 19 Aug 2021 13:16:28 -0300 Subject: [PATCH 6/6] Fix review --- app/lib/server/methods/filterBadWords.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/lib/server/methods/filterBadWords.ts b/app/lib/server/methods/filterBadWords.ts index 5d7b012e5f3b..57f89059fc7a 100644 --- a/app/lib/server/methods/filterBadWords.ts +++ b/app/lib/server/methods/filterBadWords.ts @@ -25,7 +25,7 @@ Meteor.startup(() => { const whiteList = settings.get('Message_BadWordsWhitelist') as string | undefined; const options = { - list: badWordsList?.split(',').map((word) => word.trim()) || [], + list: badWordsList?.split(',').map((word) => word.trim()).filter(Boolean) || [], // library definition does not allow optional definition exclude: undefined, splitRegex: undefined,