mirror of
https://github.com/matrix-org/mjolnir.git
synced 2024-10-01 01:36:06 -04:00
Add test for mention spam.
This commit is contained in:
parent
ea58b0b5b5
commit
923c00a1dd
@ -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() {
|
||||
|
93
test/integration/mentionSpamProtectionTest.ts
Normal file
93
test/integration/mentionSpamProtectionTest.ts
Normal file
@ -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");
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
Block a user