Skip to content

Commit

Permalink
Add test for mention spam.
Browse files Browse the repository at this point in the history
  • Loading branch information
Half-Shot committed Sep 19, 2024
1 parent ea58b0b commit 923c00a
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/protections/MentionSpam.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ import { Mjolnir } from "../Mjolnir";
import { LogLevel, Permalinks, UserID } from "@vector-im/matrix-bot-sdk";
import { NumberProtectionSetting } from "./ProtectionSettings";

const MAX_MENTIONS = 8;
export const DEFAULT_MAX_MENTIONS = 8;
const USER_ID_REGEX = /@[^:]*:.+/;

export class MentionSpam extends Protection {

settings = {
maxMentions: new NumberProtectionSetting(MAX_MENTIONS),
maxMentions: new NumberProtectionSetting(DEFAULT_MAX_MENTIONS),
};

constructor() {
Expand Down
93 changes: 93 additions & 0 deletions test/integration/mentionSpamProtectionTest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import {newTestUser} from "./clientHelper";

import {MatrixClient} from "matrix-bot-sdk";
import {getFirstReaction} from "./commands/commandUtils";
import {strict as assert} from "assert";
import { DEFAULT_MAX_MENTIONS } from "../../src/protections/MentionSpam";

describe("Test: Mention spam protection", function () {
let client: MatrixClient;
let room: string;
this.beforeEach(async function () {
client = await newTestUser(this.config.homeserverUrl, {name: {contains: "mention-spam-protection"}});
await client.start();
const mjolnirId = await this.mjolnir.client.getUserId();
room = await client.createRoom({ invite: [mjolnirId] });
await client.joinRoom(room);
await client.joinRoom(this.config.managementRoom);
await client.setUserPowerLevel(mjolnirId, room, 100);
})
this.afterEach(async function () {
await client.stop();
})

function delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}


it("does not redact a normal message", async function() {
await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir rooms add ${room}` });
await getFirstReaction(client, this.mjolnir.managementRoomId, '✅', async () => {
return await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: "!mjolnir enable MentionSpam" });
});
const testMessage = await client.sendText(room, 'Hello world');

await delay(500);
const fetchedEvent = await client.getEvent(room, testMessage);
assert.equal(Object.keys(fetchedEvent.content).length, 3, "This event should not have been redacted");
});

it("does not redact a message with some mentions", async function() {
await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir rooms add ${room}` });
await getFirstReaction(client, this.mjolnir.managementRoomId, '✅', async () => {
return await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: "!mjolnir enable MentionSpam" });
});
// Also covers HTML mentions
const messageWithTextMentions = await client.sendText(room, 'Hello world @foo:bar @beep:boop @test:here');
const messageWithMMentions = await client.sendMessage(room, {
content: {
msgtype: 'm.text',
body: 'Hello world',
['m.mentions']: {
user_ids: [
"@foo:bar",
"@beep:boop",
"@test:here"
]
}
}
});

await delay(500);
const fetchedTextEvent = await client.getEvent(room, messageWithTextMentions);
assert.equal(Object.keys(fetchedTextEvent.content).length, 3, "This event should not have been redacted");
const fetchedMentionsEvent = await client.getEvent(room, messageWithMMentions);
assert.equal(Object.keys(fetchedMentionsEvent.content).length, 3, "This event should not have been redacted");
});

it("does redact a message with too many mentions", async function() {
await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: `!mjolnir rooms add ${room}` });
await getFirstReaction(client, this.mjolnir.managementRoomId, '✅', async () => {
return await client.sendMessage(this.mjolnir.managementRoomId, { msgtype: 'm.text', body: "!mjolnir enable MentionSpam" });
});
// Also covers HTML mentions
const mentionUsers = Array.from({length: DEFAULT_MAX_MENTIONS}, (_, i) => `@user${i}:example.org`);
const messageWithTextMentions = await client.sendText(room, 'Hello world ' + mentionUsers.join(' '));
const messageWithMMentions = await client.sendMessage(room, {
content: {
msgtype: 'm.text',
body: 'Hello world',
['m.mentions']: {
user_ids: mentionUsers
}
}
});

await delay(500);
const fetchedTextEvent = await client.getEvent(room, messageWithTextMentions);
assert.equal(Object.keys(fetchedTextEvent.content).length, 0, "This event should have been redacted");
const fetchedMentionsEvent = await client.getEvent(room, messageWithMMentions);
assert.equal(Object.keys(fetchedMentionsEvent.content).length, 0, "This event should have been redacted");
});
});

0 comments on commit 923c00a

Please sign in to comment.