|
| 1 | +// Copyright 2025 Gnuxie <[email protected]> |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: AFL-3.0 |
| 4 | + |
| 5 | +import { Type } from "@sinclair/typebox"; |
| 6 | +import { |
| 7 | + AbstractProtection, |
| 8 | + describeProtection, |
| 9 | + EDStatic, |
| 10 | + EventConsequences, |
| 11 | + EventWithMixins, |
| 12 | + isError, |
| 13 | + Logger, |
| 14 | + Ok, |
| 15 | + ProtectedRoomsSet, |
| 16 | + Protection, |
| 17 | + ProtectionDescription, |
| 18 | + RoomMessageSender, |
| 19 | + Task, |
| 20 | + UserConsequences, |
| 21 | +} from "matrix-protection-suite"; |
| 22 | +import { Draupnir } from "../Draupnir"; |
| 23 | +import { |
| 24 | + MatrixRoomID, |
| 25 | + StringRoomID, |
| 26 | + StringUserID, |
| 27 | +} from "@the-draupnir-project/matrix-basic-types"; |
| 28 | +import { LazyLeakyBucket } from "../queues/LeakyBucket"; |
| 29 | +import { DeadDocumentJSX } from "@the-draupnir-project/interface-manager"; |
| 30 | +import { |
| 31 | + renderMentionPill, |
| 32 | + sendMatrixEventsFromDeadDocument, |
| 33 | +} from "@the-draupnir-project/mps-interface-adaptor"; |
| 34 | + |
| 35 | +const log = new Logger("InvalidEventProtection"); |
| 36 | + |
| 37 | +const InvalidEventProtectionSettings = Type.Object( |
| 38 | + { |
| 39 | + warningText: Type.String({ |
| 40 | + description: |
| 41 | + "The reason to use to notify the user after redacting their infringing message.", |
| 42 | + default: |
| 43 | + "You have sent an invalid event that could cause problems in some Matrix clients, so we have had to redact it.", |
| 44 | + }), |
| 45 | + }, |
| 46 | + { |
| 47 | + title: "InvalidEventProtectionSettings", |
| 48 | + } |
| 49 | +); |
| 50 | + |
| 51 | +type InvalidEventProtectionSettings = EDStatic< |
| 52 | + typeof InvalidEventProtectionSettings |
| 53 | +>; |
| 54 | + |
| 55 | +export type InvalidEventProtectionDescription = ProtectionDescription< |
| 56 | + unknown, |
| 57 | + typeof InvalidEventProtectionSettings, |
| 58 | + InvalidEventProtectionCapabilities |
| 59 | +>; |
| 60 | + |
| 61 | +export type InvalidEventProtectionCapabilities = { |
| 62 | + eventConsequences: EventConsequences; |
| 63 | + userConsequences: UserConsequences; |
| 64 | +}; |
| 65 | + |
| 66 | +export class InvalidEventProtection |
| 67 | + extends AbstractProtection<InvalidEventProtectionDescription> |
| 68 | + implements Protection<InvalidEventProtectionDescription> |
| 69 | +{ |
| 70 | + private readonly eventConsequences: EventConsequences; |
| 71 | + private readonly userConsequences: UserConsequences; |
| 72 | + private readonly consequenceBucket = new LazyLeakyBucket<StringUserID>( |
| 73 | + 1, |
| 74 | + 30 * 60_000 // half an hour will do |
| 75 | + ); |
| 76 | + public constructor( |
| 77 | + description: InvalidEventProtectionDescription, |
| 78 | + capabilities: InvalidEventProtectionCapabilities, |
| 79 | + protectedRoomsSet: ProtectedRoomsSet, |
| 80 | + private readonly warningText: string, |
| 81 | + private readonly roomMessageSender: RoomMessageSender, |
| 82 | + private readonly managentRoomID: StringRoomID |
| 83 | + ) { |
| 84 | + super(description, capabilities, protectedRoomsSet, {}); |
| 85 | + this.eventConsequences = capabilities.eventConsequences; |
| 86 | + this.userConsequences = capabilities.userConsequences; |
| 87 | + } |
| 88 | + |
| 89 | + private async redactEventWithMixin(event: EventWithMixins): Promise<void> { |
| 90 | + const redactResult = await this.eventConsequences.consequenceForEvent( |
| 91 | + event.sourceEvent.room_id, |
| 92 | + event.sourceEvent.event_id, |
| 93 | + "invalid event mixin" |
| 94 | + ); |
| 95 | + if (isError(redactResult)) { |
| 96 | + log.error( |
| 97 | + `Failed to redact and event sent by ${event.sourceEvent.sender} in ${event.sourceEvent.room_id}`, |
| 98 | + redactResult.error |
| 99 | + ); |
| 100 | + } |
| 101 | + const managementRoomSendResult = await sendMatrixEventsFromDeadDocument( |
| 102 | + this.roomMessageSender, |
| 103 | + this.managentRoomID, |
| 104 | + <root> |
| 105 | + <details> |
| 106 | + <summary> |
| 107 | + Copy of invalid event content from {event.sourceEvent.sender} |
| 108 | + </summary> |
| 109 | + <pre>{JSON.stringify(event.sourceEvent.content)}</pre> |
| 110 | + </details> |
| 111 | + </root>, |
| 112 | + {} |
| 113 | + ); |
| 114 | + if (isError(managementRoomSendResult)) { |
| 115 | + log.error( |
| 116 | + "Failed to send redacted event details to the management room", |
| 117 | + managementRoomSendResult.error |
| 118 | + ); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private async sendWarning(event: EventWithMixins): Promise<void> { |
| 123 | + const result = await sendMatrixEventsFromDeadDocument( |
| 124 | + this.roomMessageSender, |
| 125 | + event.sourceEvent.room_id, |
| 126 | + <root> |
| 127 | + {renderMentionPill(event.sourceEvent.sender, event.sourceEvent.sender)}{" "} |
| 128 | + {this.warningText} |
| 129 | + </root>, |
| 130 | + { replyToEvent: event.sourceEvent } |
| 131 | + ); |
| 132 | + if (isError(result)) { |
| 133 | + log.error( |
| 134 | + "Unable to warn the user", |
| 135 | + event.sourceEvent.sender, |
| 136 | + result.error |
| 137 | + ); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private async banUser(event: EventWithMixins): Promise<void> { |
| 142 | + const banResult = await this.userConsequences.consequenceForUserInRoom( |
| 143 | + event.sourceEvent.room_id, |
| 144 | + event.sourceEvent.sender, |
| 145 | + "Sending invalid events" |
| 146 | + ); |
| 147 | + if (isError(banResult)) { |
| 148 | + log.error( |
| 149 | + "Unable to ban the sender of invalid events", |
| 150 | + event.sourceEvent.sender, |
| 151 | + event.sourceEvent.room_id, |
| 152 | + banResult.error |
| 153 | + ); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + public handleProtectionDisable(): void { |
| 158 | + this.consequenceBucket.stop(); |
| 159 | + } |
| 160 | + |
| 161 | + public handleTimelineEventMixins( |
| 162 | + _room: MatrixRoomID, |
| 163 | + event: EventWithMixins |
| 164 | + ): void { |
| 165 | + if (!event.mixins.some((mixin) => mixin.isErroneous)) { |
| 166 | + return; |
| 167 | + } |
| 168 | + const infractions = this.consequenceBucket.getTokenCount( |
| 169 | + event.sourceEvent.sender |
| 170 | + ); |
| 171 | + if (infractions > 0) { |
| 172 | + void Task(this.banUser(event), { log }); |
| 173 | + } else { |
| 174 | + void Task(this.sendWarning(event), { log }); |
| 175 | + } |
| 176 | + this.consequenceBucket.addToken(event.sourceEvent.sender); |
| 177 | + void Task(this.redactEventWithMixin(event), { log }); |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +describeProtection< |
| 182 | + InvalidEventProtectionCapabilities, |
| 183 | + Draupnir, |
| 184 | + typeof InvalidEventProtectionSettings |
| 185 | +>({ |
| 186 | + name: "InvalidEventProtection", |
| 187 | + description: `Protect the room against malicious events or evasion of other protections.`, |
| 188 | + capabilityInterfaces: { |
| 189 | + eventConsequences: "EventConsequences", |
| 190 | + userConsequences: "UserConsequences", |
| 191 | + }, |
| 192 | + defaultCapabilities: { |
| 193 | + eventConsequences: "StandardEventConsequences", |
| 194 | + userConsequences: "StandardUserConsequences", |
| 195 | + }, |
| 196 | + configSchema: InvalidEventProtectionSettings, |
| 197 | + factory: async ( |
| 198 | + decription, |
| 199 | + protectedRoomsSet, |
| 200 | + draupnir, |
| 201 | + capabilitySet, |
| 202 | + settings |
| 203 | + ) => |
| 204 | + Ok( |
| 205 | + new InvalidEventProtection( |
| 206 | + decription, |
| 207 | + capabilitySet, |
| 208 | + protectedRoomsSet, |
| 209 | + settings.warningText, |
| 210 | + draupnir.clientPlatform.toRoomMessageSender(), |
| 211 | + draupnir.managementRoomID |
| 212 | + ) |
| 213 | + ), |
| 214 | +}); |
0 commit comments