= Lazy::new(|| {
parser
});
+/// Replace special HTML characters in API parameters to prevent XSS attacks.
+///
+/// Taken from https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.md#output-encoding-for-html-contexts
+///
+/// `>` is left in place because it is interpreted as markdown quote.
+pub fn sanitize_html(text: &str) -> String {
+ text
+ .replace('&', "&")
+ .replace('<', "<")
+ .replace('\"', """)
+ .replace('\'', "'")
+}
+
+/// Converts text from markdown to HTML, while escaping special characters.
pub fn markdown_to_html(text: &str) -> String {
MARKDOWN_PARSER.parse(text).xrender()
}
@@ -21,7 +35,7 @@ mod tests {
#![allow(clippy::unwrap_used)]
#![allow(clippy::indexing_slicing)]
- use crate::utils::markdown::markdown_to_html;
+ use super::*;
#[test]
fn test_basic_markdown() {
@@ -71,6 +85,11 @@ mod tests {
"::: spoiler click to see more\nhow spicy!\n:::\n",
"click to see more
how spicy!\n
\n"
),
+ (
+ "escape html special chars",
+ " hello &\"",
+ "<script>alert(‘xss’);</script> hello &"
\n"
+ )
];
tests.iter().for_each(|&(msg, input, expected)| {
@@ -83,4 +102,11 @@ mod tests {
);
});
}
+
+ #[test]
+ fn test_sanitize_html() {
+ let sanitized = sanitize_html(" hello &\"'");
+ let expected = "<script>alert('xss');</script> hello &"'";
+ assert_eq!(expected, sanitized)
+ }
}
diff --git a/crates/utils/translations b/crates/utils/translations
index 18da10858..e943f97fe 160000
--- a/crates/utils/translations
+++ b/crates/utils/translations
@@ -1 +1 @@
-Subproject commit 18da10858d8c63750beb06247947f25d91944741
+Subproject commit e943f97fe481dc425acdebc8872bf1fdcabaf875