Compare commits

...

7 Commits

Author SHA1 Message Date
Half-Shot
768947529b Merge remote-tracking branch 'origin/main' into hs/mention-spam-protection 2024-09-20 09:27:39 +01:00
Half-Shot
0c75c17920 Reduce logic further 2024-09-20 09:26:12 +01:00
Half-Shot
a51cd63dde Optimise further 2024-09-20 09:22:43 +01:00
Travis Ralston
f66ac77a79
Merge pull request #525 from matrix-org/hs/name-that-protection
Log the name of the failed protection to the moderation room when it fails.
2024-09-20 09:15:44 +02:00
Travis Ralston
d3f8c033b1
Merge pull request #526 from matrix-org/hs/await-redact
Fix NSFW protection not awaiting redaction
2024-09-20 00:46:28 +02:00
Will Hunt
4f73da38b8 await redact in nsfw protection 2024-09-19 22:41:51 +01:00
Will Hunt
35f59771a9 Name the protection that failed. 2024-09-19 16:06:20 +01:00
3 changed files with 8 additions and 27 deletions

View File

@ -20,7 +20,6 @@ import { LogLevel, Permalinks, UserID } from "@vector-im/matrix-bot-sdk";
import { NumberProtectionSetting } from "./ProtectionSettings";
export const DEFAULT_MAX_MENTIONS = 10;
const USER_ID_SIGIL_REGEX = /(@|%40)/;
export class MentionSpam extends Protection {
@ -41,31 +40,14 @@ export class MentionSpam extends Protection {
public checkMentions(body: unknown|undefined, htmlBody: unknown|undefined, mentionArray: unknown|undefined): boolean {
const max = this.settings.maxMentions.value;
const minMessageLength = max * 3; // "@:a"
if (Array.isArray(mentionArray)) {
if (mentionArray.length > this.settings.maxMentions.value) {
return true;
}
if (Array.isArray(mentionArray) && mentionArray.length > max) {
return true;
}
if (typeof body === "string" && body.length > minMessageLength) {
let found = 0;
for (const word of body.split(/\s/)) {
if (USER_ID_SIGIL_REGEX.test(word.trim())) {
if (++found > max) {
return true;
}
}
}
if (typeof body === "string" && body.split('@').length - 1 > max) {
return true;
}
if (typeof htmlBody === "string" && htmlBody.length > minMessageLength) {
let found = 0;
for (const word of htmlBody.split(/\s/)) {
if (USER_ID_SIGIL_REGEX.test(word.trim())) {
if (++found > max) {
return true;
}
}
}
if (typeof htmlBody === "string" && htmlBody.split('%40').length - 1 > max) {
return true;
}
return false;
}

View File

@ -60,10 +60,9 @@ export class NsfwProtection extends Protection {
if (prediction["probability"] > mjolnir.config.nsfwSensitivity) {
await mjolnir.managementRoomOutput.logMessage(LogLevel.INFO, "NSFWProtection", `Redacting ${event["event_id"]} for inappropriate content.`);
try {
mjolnir.client.redactEvent(roomId, event["event_id"])
await mjolnir.client.redactEvent(roomId, event["event_id"]);
} catch (err) {
await mjolnir.managementRoomOutput.logMessage(LogLevel.ERROR, "NSFWProtection", `There was an error redacting ${event["event_id"]}: ${err}`);
}
}
}

View File

@ -310,7 +310,7 @@ export class ProtectionManager {
LogService.error("ProtectionManager", "Error handling protection: " + protection.name);
LogService.error("ProtectionManager", "Failed event: " + eventPermalink);
LogService.error("ProtectionManager", extractRequestError(e));
await this.mjolnir.client.sendNotice(this.mjolnir.managementRoomId, "There was an error processing an event through a protection - see log for details. Event: " + eventPermalink);
await this.mjolnir.client.sendNotice(this.mjolnir.managementRoomId, `There was an error processing an event through a protection (${protection.name}) - see log for details. Event: ${eventPermalink}`);
continue;
}