73 lines
2.4 KiB
Rust
73 lines
2.4 KiB
Rust
//! Convenience methods and types to sanitize HTML messages.
|
|
|
|
use crate::{Html, SanitizerConfig};
|
|
|
|
/// Sanitize the given HTML string.
|
|
///
|
|
/// This removes the [tags and attributes] that are not listed in the Matrix specification.
|
|
///
|
|
/// It can also optionally remove the [rich reply fallback].
|
|
///
|
|
/// [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
|
|
pub fn sanitize_html(
|
|
s: &str,
|
|
mode: HtmlSanitizerMode,
|
|
remove_reply_fallback: RemoveReplyFallback,
|
|
) -> String {
|
|
let mut config = match mode {
|
|
HtmlSanitizerMode::Strict => SanitizerConfig::strict(),
|
|
HtmlSanitizerMode::Compat => SanitizerConfig::compat(),
|
|
};
|
|
|
|
if remove_reply_fallback == RemoveReplyFallback::Yes {
|
|
config = config.remove_reply_fallback();
|
|
}
|
|
|
|
sanitize_inner(s, config)
|
|
}
|
|
|
|
/// What HTML [tags and attributes] should be kept by the sanitizer.
|
|
///
|
|
/// [tags and attributes]: https://spec.matrix.org/latest/client-server-api/#mroommessage-msgtypes
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
#[allow(clippy::exhaustive_enums)]
|
|
pub enum HtmlSanitizerMode {
|
|
/// Keep only the tags and attributes listed in the Matrix specification.
|
|
Strict,
|
|
|
|
/// Like `Strict` mode, with additional tags and attributes that are not yet included in
|
|
/// the spec, but are reasonable to keep.
|
|
Compat,
|
|
}
|
|
|
|
/// Whether to remove the [rich reply fallback] while sanitizing.
|
|
///
|
|
/// [rich reply fallback]: https://spec.matrix.org/latest/client-server-api/#fallbacks-for-rich-replies
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
#[allow(clippy::exhaustive_enums)]
|
|
pub enum RemoveReplyFallback {
|
|
/// Remove the rich reply fallback.
|
|
Yes,
|
|
|
|
/// Don't remove the rich reply fallback.
|
|
No,
|
|
}
|
|
|
|
/// Remove the [rich reply fallback] of the given HTML string.
|
|
///
|
|
/// Due to the fact that the HTML is parsed, note that malformed HTML and comments will be stripped
|
|
/// from the output.
|
|
///
|
|
/// [rich reply fallback]: https://spec.matrix.org/latest/client-server-api/#fallbacks-for-rich-replies
|
|
pub fn remove_html_reply_fallback(s: &str) -> String {
|
|
let config = SanitizerConfig::new().remove_reply_fallback();
|
|
sanitize_inner(s, config)
|
|
}
|
|
|
|
fn sanitize_inner(s: &str, config: SanitizerConfig) -> String {
|
|
let mut html = Html::parse(s);
|
|
html.sanitize_with(config);
|
|
html.to_string()
|
|
}
|