-
Notifications
You must be signed in to change notification settings - Fork 57
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
Add protection for mention spam #524
Merged
Merged
Changes from 13 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
ea58b0b
Add mention spam protection.
Half-Shot 923c00a
Add test for mention spam.
Half-Shot 02de47e
fix imports
Half-Shot fa9a17c
fix log line
Half-Shot 4ee523e
increase to 10
Half-Shot 38580c7
fix
Half-Shot b9d4f21
add a minimum
Half-Shot c77d57b
fix tests
Half-Shot 1486c4a
Merge remote-tracking branch 'origin/main' into hs/mention-spam-prote…
Half-Shot fadc16b
Update deps
Half-Shot 5d497b4
plus one
Half-Shot ec0c8b7
Move operator
Half-Shot 0c37599
fix typo
Half-Shot b61f39d
Test HTML messages
Half-Shot 19e07a6
Test sigil instead.
Half-Shot 4ec3f30
Short circuit on short messages
Half-Shot a51cd63
Optimise further
Half-Shot 0c75c17
Reduce logic further
Half-Shot 7689475
Merge remote-tracking branch 'origin/main' into hs/mention-spam-prote…
Half-Shot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
Copyright 2024 The Matrix.org Foundation C.I.C. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
import { Protection } from "./IProtection"; | ||
import { Mjolnir } from "../Mjolnir"; | ||
import { LogLevel, Permalinks, UserID } from "@vector-im/matrix-bot-sdk"; | ||
import { NumberProtectionSetting } from "./ProtectionSettings"; | ||
|
||
export const DEFAULT_MAX_MENTIONS = 10; | ||
const USER_ID_REGEX = /@[^:]*:.+/; | ||
|
||
export class MentionSpam extends Protection { | ||
|
||
settings = { | ||
maxMentions: new NumberProtectionSetting(DEFAULT_MAX_MENTIONS, 1), | ||
}; | ||
|
||
constructor() { | ||
super(); | ||
} | ||
|
||
public get name(): string { | ||
return 'MentionSpam'; | ||
} | ||
public get description(): string { | ||
return "If a user posts many mentions, that message is redacted. No bans are issued."; | ||
} | ||
|
||
public checkMentions(body: unknown|undefined, htmlBody: unknown|undefined, mentionArray: unknown|undefined): boolean { | ||
const max = this.settings.maxMentions.value; | ||
if (Array.isArray(mentionArray)) { | ||
if (mentionArray.length > this.settings.maxMentions.value) { | ||
return true; | ||
} | ||
} | ||
if (typeof body === "string") { | ||
let found = 0; | ||
for (const word of body.split(/\s/)) { | ||
if (USER_ID_REGEX.test(word.trim())) { | ||
if (++found > max) { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
if (typeof htmlBody === "string") { | ||
let found = 0; | ||
for (const word of htmlBody.split(/\s/)) { | ||
if (USER_ID_REGEX.test(word.trim())) { | ||
if (++found > max) { | ||
return true; | ||
} | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
public async handleEvent(mjolnir: Mjolnir, roomId: string, event: any): Promise<any> { | ||
if (event['type'] === 'm.room.message') { | ||
let content = event['content'] || {}; | ||
const explicitMentions = content["m.mentions"]?.user_ids; | ||
const hitLimit = this.checkMentions(content.body, content.formatted_body, explicitMentions); | ||
if (hitLimit) { | ||
await mjolnir.managementRoomOutput.logMessage(LogLevel.WARN, "MentionSpam", `Redacting event from ${event['sender']} for spamming mentions. ${Permalinks.forEvent(roomId, event['event_id'], [new UserID(await mjolnir.client.getUserId()).domain])}`); | ||
// Redact the event | ||
if (!mjolnir.config.noop) { | ||
await mjolnir.client.redactEvent(roomId, event['event_id'], "Message was detected as spam."); | ||
} else { | ||
await mjolnir.managementRoomOutput.logMessage(LogLevel.WARN, "MentionSpam", `Tried to redact ${event['event_id']} in ${roomId} but Mjolnir is running in no-op mode`, roomId); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import {newTestUser} from "./clientHelper"; | ||
|
||
import {MatrixClient} from "@vector-im/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, 2, "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, { | ||
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, 2, "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+1}, (_, i) => `@user${i}:example.org`); | ||
const messageWithTextMentions = await client.sendText(room, mentionUsers.join(' ')); | ||
const messageWithMMentions = await client.sendMessage(room, { | ||
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"); | ||
}); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we just limit this to number of
@
symbols? (plus HTML encoded varient for htmlBody)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think there are concrete chances of that reacting on somebody posting a snippet of code with some delimiter:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"don't do that in my rooms" :p