Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Filtering non ascii bad words #35418

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/witty-ads-visit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': patch
---

Adds support for filtering bad words from messages for languages other than English
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,18 @@ import type BadWordsFilter from 'bad-words';
export class BeforeSaveBadWords {
badWords: BadWordsFilter | null = null;

badWordsRegex: RegExp | null = null;

async configure(badWordsList?: string, goodWordsList?: string) {
const { default: Filter } = await import('bad-words');
const badWords =
badWordsList
?.split(',')
.map((word) => word.trim())
.filter(Boolean) || [];

const options = {
list:
badWordsList
?.split(',')
.map((word) => word.trim())
.filter(Boolean) || [],
list: badWords,
// library definition does not allow optional definition
exclude: undefined,
splitRegex: undefined,
Expand All @@ -24,13 +27,20 @@ export class BeforeSaveBadWords {

this.badWords = new Filter(options);

try {
this.badWordsRegex = new RegExp(`(?<=^|[\\p{P}\\p{Z}])(${badWords.join('|')})(?=$|[\\p{P}\\p{Z}])`, 'gmiu');
} catch (error) {
this.badWordsRegex = null;
Comment on lines +32 to +33
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

silently consuming the error. can we log this pls?

}

if (goodWordsList?.length) {
this.badWords.removeWords(...goodWordsList.split(',').map((word) => word.trim()));
}
}

disable() {
this.badWords = null;
this.badWordsRegex = null;
}

async filterBadWords({ message }: { message: IMessage }): Promise<IMessage> {
Expand All @@ -42,6 +52,10 @@ export class BeforeSaveBadWords {
message.msg = this.badWords.clean(message.msg);
} catch (error) {
// ignore
} finally {
if (this.badWordsRegex) {
message.msg = message.msg.replace(this.badWordsRegex, (match) => '*'.repeat(match.length));
}
}

return message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,48 @@ describe('Filter bad words before saving message', () => {

return expect(result.msg).to.be.equal('hell');
});

it('should filter non ascii bad words', async () => {
const badWords = new BeforeSaveBadWords();
await badWords.configure('バカ');

const message = createMessage('hell is バカ');

const result = await badWords.filterBadWords({ message });

return expect(result.msg).to.be.equal('**** is **');
});

it('should filter just the non ascii bad words', async () => {
const badWords = new BeforeSaveBadWords();
await badWords.configure('バカ');

const message = createMessage('バカ');

const result = await badWords.filterBadWords({ message });

return expect(result.msg).to.be.equal('**');
});

it('should filter non ascii bad words with punctuation', async () => {
const badWords = new BeforeSaveBadWords();
await badWords.configure('バカ');

const message = createMessage('バカ.');

const result = await badWords.filterBadWords({ message });

return expect(result.msg).to.be.equal('**.');
});

it('should not filter non ascii bad words, if part of another word ', async () => {
const badWords = new BeforeSaveBadWords();
await badWords.configure('バカ');

const message = createMessage('TESTバカTEST');

const result = await badWords.filterBadWords({ message });

return expect(result.msg).to.be.equal('TESTバカTEST');
});
});
Loading