Skip to content

Commit

Permalink
run cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Kapu1178 committed Apr 21, 2024
1 parent cbef232 commit 0aa9284
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/sanitize.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::error::Result;
use std::collections::HashSet;
use maplit::hashset;
use std::collections::HashSet;

byond_fn!(fn sanitize_html(text, attribute_whitelist_json, tag_whitelist_json) {
match seriously_sanitize_html(text, attribute_whitelist_json, tag_whitelist_json) {
Expand All @@ -9,22 +9,25 @@ byond_fn!(fn sanitize_html(text, attribute_whitelist_json, tag_whitelist_json) {
}
});

fn seriously_sanitize_html(text: &str, attribute_whitelist_json: &str, tag_whitelist_json: &str) -> Result<String> {
fn seriously_sanitize_html(
text: &str,
attribute_whitelist_json: &str,
tag_whitelist_json: &str,
) -> Result<String> {
let attribute_whitelist: HashSet<&str> = serde_json::from_str(attribute_whitelist_json)?;
let tag_whitelist: HashSet<&str> = serde_json::from_str(tag_whitelist_json)?;

let mut prune_url_schemes = ammonia::Builder::default().clone_url_schemes();
prune_url_schemes.insert("byond");

let sanitized = ammonia::Builder::empty()
.clean_content_tags(hashset!["script", "style"]) // Completely forbid script and style attributes.
.link_rel(Some("noopener")) // https://mathiasbynens.github.io/rel-noopener/
.url_schemes(prune_url_schemes)
.generic_attributes(attribute_whitelist)
.tags(tag_whitelist)
.clean(text)
.to_string();

.clean_content_tags(hashset!["script", "style"]) // Completely forbid script and style attributes.
.link_rel(Some("noopener")) // https://mathiasbynens.github.io/rel-noopener/
.url_schemes(prune_url_schemes)
.generic_attributes(attribute_whitelist)
.tags(tag_whitelist)
.clean(text)
.to_string();

Ok(sanitized)
}

0 comments on commit 0aa9284

Please sign in to comment.