From fa418d0441b78fa54e56acbda1a719fae617d9ce Mon Sep 17 00:00:00 2001 From: David Teller Date: Thu, 15 Apr 2021 14:52:09 +0200 Subject: [PATCH] Bot-side: Banning registration by ip, username, email --- src/commands/UnbanBanCommand.ts | 24 +++++++++++----- src/models/BanList.ts | 41 ++++++++++++++++++++------- src/models/ListRule.ts | 2 +- synapse_antispam/mjolnir/antispam.py | 2 +- synapse_antispam/mjolnir/ban_list.py | 2 +- synapse_antispam/mjolnir/list_rule.py | 2 +- 6 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/commands/UnbanBanCommand.ts b/src/commands/UnbanBanCommand.ts index d28145d..3a1d7a0 100644 --- a/src/commands/UnbanBanCommand.ts +++ b/src/commands/UnbanBanCommand.ts @@ -15,7 +15,7 @@ limitations under the License. */ import { Mjolnir } from "../Mjolnir"; -import BanList, { RULE_ROOM, RULE_SERVER, RULE_USER, USER_RULE_TYPES } from "../models/BanList"; +import BanList, { RULE_REGISTRATION_EMAIL, RULE_REGISTRATION_IP, RULE_ROOM, RULE_SERVER, RULE_USER, USER_RULE_TYPES } from "../models/BanList"; import { LogLevel, LogService, MatrixGlob, RichReply } from "matrix-bot-sdk"; import { RECOMMENDATION_BAN, recommendationToStable } from "../models/ListRule"; import config from "../config"; @@ -50,10 +50,20 @@ export async function parseArguments(roomId: string, event: any, mjolnir: Mjolni while (argumentIndex < 7 && argumentIndex < parts.length) { const arg = parts[argumentIndex++]; if (!arg) break; - if (["user", "room", "server"].includes(arg.toLowerCase())) { - if (arg.toLowerCase() === 'user') ruleType = RULE_USER; - if (arg.toLowerCase() === 'room') ruleType = RULE_ROOM; - if (arg.toLowerCase() === 'server') ruleType = RULE_SERVER; + if (["user", "room", "server", "registration_ip", "registration_email"].includes(arg.toLowerCase())) { + let lowercase = arg.toLowerCase(); + for (let {value, type} of [ + {value: "user", type: RULE_USER}, + {value: "room", type: RULE_ROOM}, + {value: "server", type: RULE_SERVER}, + {value: "registration_ip", type: RULE_REGISTRATION_IP}, + {value: "registration_email", type: RULE_REGISTRATION_EMAIL}, + ]) { + if (lowercase === value) { + ruleType = type; + break; + } + } } else if (!entity && (arg[0] === '@' || arg[0] === '!' || arg[0] === '#' || arg.includes("*"))) { entity = arg; if (arg.startsWith("@") && !ruleType) ruleType = RULE_USER; @@ -115,7 +125,7 @@ export async function parseArguments(roomId: string, event: any, mjolnir: Mjolni }; } -// !mjolnir ban [reason] [--force] +// !mjolnir ban [reason] [--force] export async function execBanCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) { const bits = await parseArguments(roomId, event, mjolnir, parts); if (!bits) return; // error already handled @@ -132,7 +142,7 @@ export async function execBanCommand(roomId: string, event: any, mjolnir: Mjolni await mjolnir.client.unstableApis.addReactionToEvent(roomId, event['event_id'], '✅'); } -// !mjolnir unban [apply:t/f] +// !mjolnir unban [apply:t/f] export async function execUnbanCommand(roomId: string, event: any, mjolnir: Mjolnir, parts: string[]) { const bits = await parseArguments(roomId, event, mjolnir, parts); if (!bits) return; // error already handled diff --git a/src/models/BanList.ts b/src/models/BanList.ts index 71c6f46..c34dee7 100644 --- a/src/models/BanList.ts +++ b/src/models/BanList.ts @@ -20,18 +20,37 @@ import { ListRule } from "./ListRule"; export const RULE_USER = "m.room.rule.user"; export const RULE_ROOM = "m.room.rule.room"; export const RULE_SERVER = "m.room.rule.server"; +export const RULE_REGISTRATION_EMAIL = "m.room.rule.registration.email"; +export const RULE_REGISTRATION_IP = "m.room.rule.registration.ip"; export const USER_RULE_TYPES = [RULE_USER, "org.matrix.mjolnir.rule.user"]; export const ROOM_RULE_TYPES = [RULE_ROOM, "org.matrix.mjolnir.rule.room"]; export const SERVER_RULE_TYPES = [RULE_SERVER, "org.matrix.mjolnir.rule.server"]; -export const ALL_RULE_TYPES = [...USER_RULE_TYPES, ...ROOM_RULE_TYPES, ...SERVER_RULE_TYPES]; +export const REGISTRATION_EMAIL_RULE_TYPES = [RULE_REGISTRATION_EMAIL, "org.matrix.mjolnir.rule.registration.email"]; +export const REGISTRATION_IP_RULE_TYPES = [RULE_REGISTRATION_IP, "org.matrix.mjolnir.rule.registration.ip"]; + +export const ALL_RULE_TYPES = [...USER_RULE_TYPES, ...ROOM_RULE_TYPES, ...SERVER_RULE_TYPES, ...REGISTRATION_EMAIL_RULE_TYPES, ...REGISTRATION_IP_RULE_TYPES]; +const LABELED_RULE_TYPES = Object.freeze([ + {types: USER_RULE_TYPES, key: RULE_USER}, + {types: ROOM_RULE_TYPES, key: RULE_ROOM}, + {types: SERVER_RULE_TYPES, key: RULE_SERVER}, + {types: REGISTRATION_EMAIL_RULE_TYPES, key: RULE_REGISTRATION_EMAIL}, + {types: REGISTRATION_IP_RULE_TYPES, key: RULE_REGISTRATION_IP}, +]); export const SHORTCODE_EVENT_TYPE = "org.matrix.mjolnir.shortcode"; + export function ruleTypeToStable(rule: string, unstable = true): string { - if (USER_RULE_TYPES.includes(rule)) return unstable ? USER_RULE_TYPES[USER_RULE_TYPES.length - 1] : RULE_USER; - if (ROOM_RULE_TYPES.includes(rule)) return unstable ? ROOM_RULE_TYPES[ROOM_RULE_TYPES.length - 1] : RULE_ROOM; - if (SERVER_RULE_TYPES.includes(rule)) return unstable ? SERVER_RULE_TYPES[SERVER_RULE_TYPES.length - 1] : RULE_SERVER; + for (let {types, key} of LABELED_RULE_TYPES) { + if (types.includes(rule)) { + if (unstable) { + return types[types.length - 1]; + } else { + return key; + } + } + } return null; } @@ -86,13 +105,13 @@ export default class BanList { } let kind: string = null; - if (USER_RULE_TYPES.includes(event['type'])) { - kind = RULE_USER; - } else if (ROOM_RULE_TYPES.includes(event['type'])) { - kind = RULE_ROOM; - } else if (SERVER_RULE_TYPES.includes(event['type'])) { - kind = RULE_SERVER; - } else { + for (let {types, key} of LABELED_RULE_TYPES) { + if (types.includes(event['type'])) { + kind = key; + break; + } + } + if (kind == null) { continue; // invalid/unknown } diff --git a/src/models/ListRule.ts b/src/models/ListRule.ts index 5e69ed2..f512fcf 100644 --- a/src/models/ListRule.ts +++ b/src/models/ListRule.ts @@ -1,5 +1,5 @@ /* -Copyright 2019 The Matrix.org Foundation C.I.C. +Copyright 2019-2021 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. diff --git a/synapse_antispam/mjolnir/antispam.py b/synapse_antispam/mjolnir/antispam.py index bf0b6f6..b807f2e 100644 --- a/synapse_antispam/mjolnir/antispam.py +++ b/synapse_antispam/mjolnir/antispam.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2019 The Matrix.org Foundation C.I.C. +# Copyright 2019-2021 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. diff --git a/synapse_antispam/mjolnir/ban_list.py b/synapse_antispam/mjolnir/ban_list.py index 1d6a52d..aea162c 100644 --- a/synapse_antispam/mjolnir/ban_list.py +++ b/synapse_antispam/mjolnir/ban_list.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2019 The Matrix.org Foundation C.I.C. +# Copyright 2019-2021 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. diff --git a/synapse_antispam/mjolnir/list_rule.py b/synapse_antispam/mjolnir/list_rule.py index 75961d1..d8366a3 100644 --- a/synapse_antispam/mjolnir/list_rule.py +++ b/synapse_antispam/mjolnir/list_rule.py @@ -1,5 +1,5 @@ # -*- coding: utf-8 -*- -# Copyright 2019 The Matrix.org Foundation C.I.C. +# Copyright 2019-2021 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.