From b50037affc4039285ad80856f30e20b9239557b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Thu, 27 Apr 2023 18:53:42 +0200 Subject: [PATCH] events: Implement sanitize on MessageType --- crates/ruma-common/CHANGELOG.md | 1 + crates/ruma-common/src/events/room/message.rs | 52 ++++++++++++++----- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/crates/ruma-common/CHANGELOG.md b/crates/ruma-common/CHANGELOG.md index 002ef3b6..392a0e00 100644 --- a/crates/ruma-common/CHANGELOG.md +++ b/crates/ruma-common/CHANGELOG.md @@ -31,6 +31,7 @@ Improvements: - `user_can_send_message` - `user_can_send_state` - `user_can_trigger_room_notification` +- Add `MessageType::sanitize` behind the `unstable-sanitize` feature # 0.11.3 diff --git a/crates/ruma-common/src/events/room/message.rs b/crates/ruma-common/src/events/room/message.rs index 64d09994..26bb8293 100644 --- a/crates/ruma-common/src/events/room/message.rs +++ b/crates/ruma-common/src/events/room/message.rs @@ -326,19 +326,13 @@ impl RoomMessageEventContent { mode: HtmlSanitizerMode, remove_reply_fallback: RemoveReplyFallback, ) { - if let MessageType::Emote(EmoteMessageEventContent { body, formatted, .. }) - | MessageType::Notice(NoticeMessageEventContent { body, formatted, .. }) - | MessageType::Text(TextMessageEventContent { body, formatted, .. }) = &mut self.msgtype - { - if let Some(formatted) = formatted { - formatted.sanitize_html(mode, remove_reply_fallback); - } - if remove_reply_fallback == RemoveReplyFallback::Yes - && matches!(self.relates_to, Some(Relation::Reply { .. })) - { - *body = remove_plain_reply_fallback(body).to_owned(); - } - } + let remove_reply_fallback = if matches!(self.relates_to, Some(Relation::Reply { .. })) { + remove_reply_fallback + } else { + RemoveReplyFallback::No + }; + + self.msgtype.sanitize(mode, remove_reply_fallback); } } @@ -558,6 +552,38 @@ impl MessageType { Self::_Custom(c) => Cow::Borrowed(&c.data), } } + + /// Sanitize this message. + /// + /// If this message contains HTML, this removes the [tags and attributes] that are not listed in + /// the Matrix specification. + /// + /// It can also optionally remove the [rich reply fallback] from the plain text and HTML + /// message. Note that you should be sure that the message is a reply, as there is no way to + /// differentiate plain text reply fallbacks and markdown quotes. + /// + /// This method is only effective on text, notice and emote messages. + /// + /// [tags and attributes]: https://spec.matrix.org/latest/client-server-api/#mroommessage-msgtypes + /// [rich reply fallback]: https://spec.matrix.org/latest/client-server-api/#fallbacks-for-rich-replies + #[cfg(feature = "unstable-sanitize")] + pub fn sanitize( + &mut self, + mode: HtmlSanitizerMode, + remove_reply_fallback: RemoveReplyFallback, + ) { + if let MessageType::Emote(EmoteMessageEventContent { body, formatted, .. }) + | MessageType::Notice(NoticeMessageEventContent { body, formatted, .. }) + | MessageType::Text(TextMessageEventContent { body, formatted, .. }) = self + { + if let Some(formatted) = formatted { + formatted.sanitize_html(mode, remove_reply_fallback); + } + if remove_reply_fallback == RemoveReplyFallback::Yes { + *body = remove_plain_reply_fallback(body).to_owned(); + } + } + } } impl From for RoomMessageEventContent {