From 8860d4fb22e175a543332c6b880427508e09411f Mon Sep 17 00:00:00 2001 From: Tiago R Date: Thu, 28 Dec 2023 14:41:32 +0000 Subject: [PATCH] Timeouts: better validations (#438) * better timeout validations Signed-off-by: GitHub * almeida reviews Signed-off-by: GitHub * better error handling Signed-off-by: GitHub * add missing catch noops Signed-off-by: GitHub --------- Signed-off-by: GitHub --- backend/src/RecoverablePluginError.ts | 4 ++++ .../events/ReapplyActiveMuteOnJoinEvt.ts | 12 ++++++++-- .../src/plugins/Mutes/functions/muteUser.ts | 22 +++++++++++++++++-- .../Mutes/functions/renewTimeoutMute.ts | 12 ++++++++-- .../src/plugins/Mutes/functions/unmuteUser.ts | 8 ++++--- 5 files changed, 49 insertions(+), 9 deletions(-) diff --git a/backend/src/RecoverablePluginError.ts b/backend/src/RecoverablePluginError.ts index b621fd41..46a86ae5 100644 --- a/backend/src/RecoverablePluginError.ts +++ b/backend/src/RecoverablePluginError.ts @@ -9,6 +9,8 @@ export enum ERRORS { INVALID_USER, INVALID_MUTE_ROLE_ID, MUTE_ROLE_ABOVE_ZEP, + USER_ABOVE_ZEP, + USER_NOT_MODERATABLE, } export const RECOVERABLE_PLUGIN_ERROR_MESSAGES = { @@ -20,6 +22,8 @@ export const RECOVERABLE_PLUGIN_ERROR_MESSAGES = { [ERRORS.INVALID_USER]: "Invalid user", [ERRORS.INVALID_MUTE_ROLE_ID]: "Specified mute role is not valid", [ERRORS.MUTE_ROLE_ABOVE_ZEP]: "Specified mute role is above Zeppelin in the role hierarchy", + [ERRORS.USER_ABOVE_ZEP]: "Cannot mute user, specified user is above Zeppelin in the role hierarchy", + [ERRORS.USER_NOT_MODERATABLE]: "Cannot mute user, specified user is not moderatable", }; export class RecoverablePluginError extends Error { diff --git a/backend/src/plugins/Mutes/events/ReapplyActiveMuteOnJoinEvt.ts b/backend/src/plugins/Mutes/events/ReapplyActiveMuteOnJoinEvt.ts index a859f7ad..6db4f27c 100644 --- a/backend/src/plugins/Mutes/events/ReapplyActiveMuteOnJoinEvt.ts +++ b/backend/src/plugins/Mutes/events/ReapplyActiveMuteOnJoinEvt.ts @@ -1,5 +1,6 @@ import moment from "moment-timezone"; import { MuteTypes } from "../../../data/MuteTypes"; +import { noop } from "../../../utils.js"; import { LogsPlugin } from "../../Logs/LogsPlugin"; import { RoleManagerPlugin } from "../../RoleManager/RoleManagerPlugin"; import { getTimeoutExpiryTime } from "../functions/getTimeoutExpiryTime"; @@ -12,6 +13,7 @@ export const ReapplyActiveMuteOnJoinEvt = mutesEvt({ event: "guildMemberAdd", async listener({ pluginData, args: { member } }) { const mute = await pluginData.state.mutes.findExistingMuteForUserId(member.id); + const logs = pluginData.getPlugin(LogsPlugin); if (!mute) { return; } @@ -25,11 +27,17 @@ export const ReapplyActiveMuteOnJoinEvt = mutesEvt({ if (!member.isCommunicationDisabled()) { const expiresAt = mute.expires_at ? moment.utc(mute.expires_at).valueOf() : null; const timeoutExpiresAt = getTimeoutExpiryTime(expiresAt); - await member.disableCommunicationUntil(timeoutExpiresAt); + if (member.moderatable) { + await member.disableCommunicationUntil(timeoutExpiresAt).catch(noop); + } else { + logs.logBotAlert({ + body: `Cannot mute user, specified user is not moderatable`, + }); + } } } - pluginData.getPlugin(LogsPlugin).logMemberMuteRejoin({ + logs.logMemberMuteRejoin({ member, }); }, diff --git a/backend/src/plugins/Mutes/functions/muteUser.ts b/backend/src/plugins/Mutes/functions/muteUser.ts index fc864ada..575f96fb 100644 --- a/backend/src/plugins/Mutes/functions/muteUser.ts +++ b/backend/src/plugins/Mutes/functions/muteUser.ts @@ -13,6 +13,7 @@ import { TemplateSafeValueContainer, renderTemplate } from "../../../templateFor import { UserNotificationMethod, UserNotificationResult, + noop, notifyUser, resolveMember, resolveUser, @@ -108,7 +109,7 @@ export async function muteUser( if (zepRoles.size === 0 || !zepRoles.some((zepRole) => zepRole.position > actualMuteRole.position)) { lock.unlock(); logs.logBotAlert({ - body: `Cannot mute users, specified mute role is above Zeppelin in the role hierarchy`, + body: `Cannot mute user, specified mute role is above Zeppelin in the role hierarchy`, }); throw new RecoverablePluginError(ERRORS.MUTE_ROLE_ABOVE_ZEP, pluginData.guild); } @@ -117,7 +118,24 @@ export async function muteUser( pluginData.getPlugin(RoleManagerPlugin).addPriorityRole(member.id, muteRole!); } } else { - await member.disableCommunicationUntil(timeoutUntil); + if (!member.manageable) { + lock.unlock(); + logs.logBotAlert({ + body: `Cannot mute user, specified user is above Zeppelin in the role hierarchy`, + }); + throw new RecoverablePluginError(ERRORS.USER_ABOVE_ZEP, pluginData.guild); + } + + if (!member.moderatable) { + // redundant safety, since canActOn already checks this + lock.unlock(); + logs.logBotAlert({ + body: `Cannot mute user, specified user is not moderatable`, + }); + throw new RecoverablePluginError(ERRORS.USER_NOT_MODERATABLE, pluginData.guild); + } + + await member.disableCommunicationUntil(timeoutUntil).catch(noop); } // If enabled, move the user to the mute voice channel (e.g. afk - just to apply the voice perms from the mute role) diff --git a/backend/src/plugins/Mutes/functions/renewTimeoutMute.ts b/backend/src/plugins/Mutes/functions/renewTimeoutMute.ts index 57def080..8363cf9f 100644 --- a/backend/src/plugins/Mutes/functions/renewTimeoutMute.ts +++ b/backend/src/plugins/Mutes/functions/renewTimeoutMute.ts @@ -3,7 +3,8 @@ import { GuildPluginData } from "knub"; import moment from "moment-timezone"; import { MAX_TIMEOUT_DURATION } from "../../../data/Mutes"; import { Mute } from "../../../data/entities/Mute"; -import { DBDateFormat, resolveMember } from "../../../utils"; +import { DBDateFormat, noop, resolveMember } from "../../../utils"; +import { LogsPlugin } from "../../Logs/LogsPlugin.js"; import { MutesPluginType } from "../types"; export async function renewTimeoutMute(pluginData: GuildPluginData, mute: Mute) { @@ -24,6 +25,13 @@ export async function renewTimeoutMute(pluginData: GuildPluginData