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: bad word filtering not working #32810

Merged
merged 7 commits into from
Jul 18, 2024
Merged
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/funny-wolves-tie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@rocket.chat/meteor': patch
---

Fixed issue where bad word filtering was not working in the UI for messages
2 changes: 1 addition & 1 deletion apps/meteor/server/services/messages/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ export class MessageService extends ServiceClassInternal implements IMessageServ

message = await mentionServer.execute(message);
message = await this.cannedResponse.replacePlaceholders({ message, room, user });
message = await this.markdownParser.parseMarkdown({ message, config: this.getMarkdownConfig() });
message = await this.badWords.filterBadWords({ message });
message = await this.markdownParser.parseMarkdown({ message, config: this.getMarkdownConfig() });
message = await this.spotify.convertSpotifyLinks({ message });
message = await this.jumpToMessage.createAttachmentForMessageURLs({
message,
Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/tests/e2e/permissions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ test.describe.serial('permissions', () => {
});
});

test.describe.skip('Filter words', () => {
test.describe.serial('Filter words', () => {
test.beforeAll(async ({ api }) => {
const statusCode1 = (await api.post('/settings/Message_AllowBadWordsFilter', { value: true })).status();
const statusCode2 = (await api.post('/settings/Message_BadWordsFilterList', { value: 'badword' })).status();
Expand Down
67 changes: 67 additions & 0 deletions apps/meteor/tests/end-to-end/api/chat.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { Credentials } from '@rocket.chat/api-client';
import type { IMessage, IRoom, IThreadMessage, IUser } from '@rocket.chat/core-typings';
import { Random } from '@rocket.chat/random';
import { expect } from 'chai';
import { after, before, beforeEach, describe, it } from 'mocha';
import type { Response } from 'supertest';
Expand Down Expand Up @@ -768,6 +769,40 @@ describe('[Chat]', () => {
.end(done);
});

describe('Bad words filter', () => {
before(() =>
Promise.all([updateSetting('Message_AllowBadWordsFilter', true), updateSetting('Message_BadWordsFilterList', 'badword,badword2')]),
);

after(() => Promise.all([updateSetting('Message_AllowBadWordsFilter', false), updateSetting('Message_BadWordsFilterList', '')]));

abhinavkrin marked this conversation as resolved.
Show resolved Hide resolved
it('should censor bad words on send', async () => {
const badMessage = {
_id: Random.id(),
rid: testChannel._id,
msg: 'This message has badword badword2',
};

await request
.post(api('chat.sendMessage'))
.set(credentials)
.send({ message: badMessage })
.expect(200)
.expect('Content-Type', 'application/json')
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('message');
const { message } = res.body;
expect(message).to.have.property('msg', 'This message has ******* ********');
expect(message).to.have.property('md').to.be.an('array').that.has.lengthOf(1);
const para = message.md[0];
expect(para).to.have.property('value').to.be.an('array').that.has.lengthOf(1);
const text = para.value[0];
expect(text).to.have.property('value', 'This message has ******* ********');
});
});
});

describe('oembed', () => {
let ytEmbedMsgId: IMessage['_id'];
let imgUrlMsgId: IMessage['_id'];
Expand Down Expand Up @@ -1460,6 +1495,38 @@ describe('[Chat]', () => {
expect(res.body.message).to.have.property('attachments').that.is.an('array').that.has.lengthOf(0);
});
});

describe('Bad words filter', () => {
before(() =>
Promise.all([updateSetting('Message_AllowBadWordsFilter', true), updateSetting('Message_BadWordsFilterList', 'badword,badword2')]),
);

after(() => Promise.all([updateSetting('Message_AllowBadWordsFilter', false), updateSetting('Message_BadWordsFilterList', '')]));

it('should censor bad words on update', async () => {
await request
.post(api('chat.update'))
.set(credentials)
.send({
roomId: testChannel._id,
msgId: message._id,
text: 'This message has badword badword2',
})
.expect(200)
.expect('Content-Type', 'application/json')
.expect((res) => {
expect(res.body).to.have.property('success', true);
expect(res.body).to.have.property('message');
const { message } = res.body;
expect(message).to.have.property('msg', 'This message has ******* ********');
expect(message).to.have.property('md').to.be.an('array').that.has.lengthOf(1);
const para = message.md[0];
expect(para).to.have.property('value').to.be.an('array').that.has.lengthOf(1);
const text = para.value[0];
expect(text).to.have.property('value', 'This message has ******* ********');
});
});
});
});

describe('[/chat.delete]', () => {
Expand Down
Loading