html: Expose sanitize API on Html
This commit is contained in:
committed by
Kévin Commaille
parent
0120861951
commit
ba973c98f1
93
crates/ruma-html/tests/it/helpers.rs
Normal file
93
crates/ruma-html/tests/it/helpers.rs
Normal file
@@ -0,0 +1,93 @@
|
||||
use ruma_html::{
|
||||
remove_html_reply_fallback, sanitize_html, HtmlSanitizerMode, RemoveReplyFallback,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn sanitize() {
|
||||
let sanitized = sanitize_html(
|
||||
"\
|
||||
<mx-reply>\
|
||||
<blockquote>\
|
||||
<a href=\"https://matrix.to/#/!n8f893n9:example.com/$1598361704261elfgc:localhost\">In reply to</a> \
|
||||
<a href=\"https://matrix.to/#/@alice:example.com\">@alice:example.com</a>\
|
||||
<br>\
|
||||
Previous message\
|
||||
</blockquote>\
|
||||
</mx-reply>\
|
||||
<removed>This has no tag</removed>\
|
||||
<p>But this is inside a tag</p>\
|
||||
",
|
||||
HtmlSanitizerMode::Strict,
|
||||
RemoveReplyFallback::No,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
sanitized,
|
||||
"\
|
||||
<mx-reply>\
|
||||
<blockquote>\
|
||||
<a href=\"https://matrix.to/#/!n8f893n9:example.com/$1598361704261elfgc:localhost\">In reply to</a> \
|
||||
<a href=\"https://matrix.to/#/@alice:example.com\">@alice:example.com</a>\
|
||||
<br>\
|
||||
Previous message\
|
||||
</blockquote>\
|
||||
</mx-reply>\
|
||||
This has no tag\
|
||||
<p>But this is inside a tag</p>\
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sanitize_without_reply() {
|
||||
let sanitized = sanitize_html(
|
||||
"\
|
||||
<mx-reply>\
|
||||
<blockquote>\
|
||||
<a href=\"https://matrix.to/#/!n8f893n9:example.com/$1598361704261elfgc:localhost\">In reply to</a> \
|
||||
<a href=\"https://matrix.to/#/@alice:example.com\">@alice:example.com</a>\
|
||||
<br>\
|
||||
Previous message\
|
||||
</blockquote>\
|
||||
</mx-reply>\
|
||||
<removed>This has no tag</removed>\
|
||||
<p>But this is inside a tag</p>\
|
||||
",
|
||||
HtmlSanitizerMode::Strict,
|
||||
RemoveReplyFallback::Yes,
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
sanitized,
|
||||
"\
|
||||
This has no tag\
|
||||
<p>But this is inside a tag</p>\
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn remove_html_reply() {
|
||||
let without_reply = remove_html_reply_fallback(
|
||||
"\
|
||||
<mx-reply>\
|
||||
<blockquote>\
|
||||
<a href=\"https://matrix.to/#/!n8f893n9:example.com/$1598361704261elfgc:localhost\">In reply to</a> \
|
||||
<a href=\"https://matrix.to/#/@alice:example.com\">@alice:example.com</a>\
|
||||
<br>\
|
||||
Previous message\
|
||||
</blockquote>\
|
||||
</mx-reply>\
|
||||
<keep-me>This keeps its tag</keep-me>\
|
||||
<p>But this is inside a tag</p>\
|
||||
",
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
without_reply,
|
||||
"\
|
||||
<keep-me>This keeps its tag</keep-me>\
|
||||
<p>But this is inside a tag</p>\
|
||||
"
|
||||
);
|
||||
}
|
||||
1
crates/ruma-html/tests/it/html.rs
Normal file
1
crates/ruma-html/tests/it/html.rs
Normal file
@@ -0,0 +1 @@
|
||||
mod sanitize;
|
||||
248
crates/ruma-html/tests/it/html/sanitize.rs
Normal file
248
crates/ruma-html/tests/it/html/sanitize.rs
Normal file
@@ -0,0 +1,248 @@
|
||||
use ruma_html::{Html, SanitizerConfig};
|
||||
|
||||
#[test]
|
||||
fn valid_input() {
|
||||
let config = SanitizerConfig::strict().remove_reply_fallback();
|
||||
let mut html = Html::parse(
|
||||
"\
|
||||
<ul><li>This</li><li>has</li><li>no</li><li>tag</li></ul>\
|
||||
<p>This is a paragraph <span data-mx-color=\"green\">with some color</span></p>\
|
||||
<img src=\"mxc://notareal.hs/abcdef\">\
|
||||
<code class=\"language-html\"><mx-reply>This is a fake reply</mx-reply></code>\
|
||||
",
|
||||
);
|
||||
html.sanitize_with(config);
|
||||
|
||||
assert_eq!(
|
||||
html.to_string(),
|
||||
"\
|
||||
<ul><li>This</li><li>has</li><li>no</li><li>tag</li></ul>\
|
||||
<p>This is a paragraph <span data-mx-color=\"green\">with some color</span></p>\
|
||||
<img src=\"mxc://notareal.hs/abcdef\">\
|
||||
<code class=\"language-html\"><mx-reply>This is a fake reply</mx-reply></code>\
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tags_remove() {
|
||||
let config = SanitizerConfig::strict();
|
||||
let mut html = Html::parse(
|
||||
"\
|
||||
<mx-reply>\
|
||||
<blockquote>\
|
||||
<a href=\"https://matrix.to/#/!n8f893n9:example.com/$1598361704261elfgc:localhost\">In reply to</a> \
|
||||
<a href=\"https://matrix.to/#/@alice:example.com\">@alice:example.com</a>\
|
||||
<br>\
|
||||
Previous message\
|
||||
</blockquote>\
|
||||
</mx-reply>\
|
||||
<removed>This has no tag</removed>\
|
||||
<p>But this is inside a tag</p>\
|
||||
",
|
||||
);
|
||||
html.sanitize_with(config);
|
||||
|
||||
assert_eq!(
|
||||
html.to_string(),
|
||||
"\
|
||||
<mx-reply>\
|
||||
<blockquote>\
|
||||
<a href=\"https://matrix.to/#/!n8f893n9:example.com/$1598361704261elfgc:localhost\">In reply to</a> \
|
||||
<a href=\"https://matrix.to/#/@alice:example.com\">@alice:example.com</a>\
|
||||
<br>\
|
||||
Previous message\
|
||||
</blockquote>\
|
||||
</mx-reply>\
|
||||
This has no tag\
|
||||
<p>But this is inside a tag</p>\
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tags_remove_without_reply() {
|
||||
let config = SanitizerConfig::strict().remove_reply_fallback();
|
||||
let mut html = Html::parse(
|
||||
"\
|
||||
<mx-reply>\
|
||||
<blockquote>\
|
||||
<a href=\"https://matrix.to/#/!n8f893n9:example.com/$1598361704261elfgc:localhost\">In reply to</a> \
|
||||
<a href=\"https://matrix.to/#/@alice:example.com\">@alice:example.com</a>\
|
||||
<br>\
|
||||
Previous message\
|
||||
</blockquote>\
|
||||
</mx-reply>\
|
||||
<removed>This has no tag</removed>\
|
||||
<p>But this is inside a tag</p>\
|
||||
",
|
||||
);
|
||||
html.sanitize_with(config);
|
||||
|
||||
assert_eq!(
|
||||
html.to_string(),
|
||||
"\
|
||||
This has no tag\
|
||||
<p>But this is inside a tag</p>\
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tags_remove_only_reply_fallback() {
|
||||
let config = SanitizerConfig::new().remove_reply_fallback();
|
||||
let mut html = Html::parse(
|
||||
"\
|
||||
<mx-reply>\
|
||||
<blockquote>\
|
||||
<a href=\"https://matrix.to/#/!n8f893n9:example.com/$1598361704261elfgc:localhost\">In reply to</a> \
|
||||
<a href=\"https://matrix.to/#/@alice:example.com\">@alice:example.com</a>\
|
||||
<br>\
|
||||
Previous message\
|
||||
</blockquote>\
|
||||
</mx-reply>\
|
||||
<keep-me>This keeps its tag</keep-me>\
|
||||
<p>But this is inside a tag</p>\
|
||||
",
|
||||
);
|
||||
html.sanitize_with(config);
|
||||
|
||||
assert_eq!(
|
||||
html.to_string(),
|
||||
"\
|
||||
<keep-me>This keeps its tag</keep-me>\
|
||||
<p>But this is inside a tag</p>\
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn attrs_remove() {
|
||||
let config = SanitizerConfig::strict();
|
||||
let mut html = Html::parse(
|
||||
"\
|
||||
<h1 id=\"anchor1\">Title for important stuff</h1>\
|
||||
<p class=\"important\">Look at <font color=\"blue\" size=20>me!</font></p>\
|
||||
",
|
||||
);
|
||||
html.sanitize_with(config);
|
||||
|
||||
assert_eq!(
|
||||
html.to_string(),
|
||||
"\
|
||||
<h1>Title for important stuff</h1>\
|
||||
<p>Look at <font color=\"blue\">me!</font></p>\
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn img_remove_scheme() {
|
||||
let config = SanitizerConfig::strict();
|
||||
let mut html = Html::parse(
|
||||
"\
|
||||
<p>Look at that picture:</p>\
|
||||
<img src=\"https://notareal.hs/abcdef\">\
|
||||
",
|
||||
);
|
||||
html.sanitize_with(config);
|
||||
|
||||
assert_eq!(html.to_string(), "<p>Look at that picture:</p>");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn link_remove_scheme() {
|
||||
let config = SanitizerConfig::strict();
|
||||
let mut html = Html::parse(
|
||||
"\
|
||||
<p>Go see <a href=\"file://local/file.html\">my local website</a></p>\
|
||||
",
|
||||
);
|
||||
html.sanitize_with(config);
|
||||
|
||||
assert_eq!(
|
||||
html.to_string(),
|
||||
"\
|
||||
<p>Go see my local website</p>\
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn link_compat_scheme() {
|
||||
let config = SanitizerConfig::strict();
|
||||
let mut html = Html::parse(
|
||||
"\
|
||||
<p>Join <a href=\"matrix:r/myroom:notareal.hs\">my room</a></p>\
|
||||
<p>To talk about <a href=\"https://mycat.org\">my cat</a></p>\
|
||||
",
|
||||
);
|
||||
html.sanitize_with(config);
|
||||
assert_eq!(
|
||||
html.to_string(),
|
||||
"\
|
||||
<p>Join my room</p>\
|
||||
<p>To talk about <a href=\"https://mycat.org\">my cat</a></p>\
|
||||
"
|
||||
);
|
||||
|
||||
let config = SanitizerConfig::compat();
|
||||
let mut html = Html::parse(
|
||||
"\
|
||||
<p>Join <a href=\"matrix:r/myroom:notareal.hs\">my room</a></p>\
|
||||
<p>To talk about <a href=\"https://mycat.org\">my cat</a></p>\
|
||||
",
|
||||
);
|
||||
html.sanitize_with(config);
|
||||
assert_eq!(
|
||||
html.to_string(),
|
||||
"\
|
||||
<p>Join <a href=\"matrix:r/myroom:notareal.hs\">my room</a></p>\
|
||||
<p>To talk about <a href=\"https://mycat.org\">my cat</a></p>\
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn class_remove() {
|
||||
let config = SanitizerConfig::strict();
|
||||
let mut html = Html::parse(
|
||||
"\
|
||||
<pre><code class=\"language-rust custom-class\">
|
||||
type StringList = Vec<String>;
|
||||
</code></pre>\
|
||||
<p>What do you think of the name <code class=\"fake-language-rust\">StringList</code>?</p>\
|
||||
",
|
||||
);
|
||||
html.sanitize_with(config);
|
||||
|
||||
assert_eq!(
|
||||
html.to_string(),
|
||||
"\
|
||||
<pre><code class=\"language-rust\">
|
||||
type StringList = Vec<String>;
|
||||
</code></pre>\
|
||||
<p>What do you think of the name <code>StringList</code>?</p>\
|
||||
"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn depth_remove() {
|
||||
let config = SanitizerConfig::strict();
|
||||
let deeply_nested_html: String = std::iter::repeat("<div>")
|
||||
.take(100)
|
||||
.chain(Some(
|
||||
"<span>I am in too deep!</span>\
|
||||
I should be fine.",
|
||||
))
|
||||
.chain(std::iter::repeat("</div>").take(100))
|
||||
.collect();
|
||||
|
||||
let mut html = Html::parse(&deeply_nested_html);
|
||||
html.sanitize_with(config);
|
||||
|
||||
let res = html.to_string();
|
||||
assert!(res.contains("I should be fine."));
|
||||
assert!(!res.contains("I am in too deep!"));
|
||||
}
|
||||
2
crates/ruma-html/tests/it/main.rs
Normal file
2
crates/ruma-html/tests/it/main.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
mod helpers;
|
||||
mod html;
|
||||
Reference in New Issue
Block a user