-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds sanitize_html
, a whitelist based HTML sanitizer.
#171
Changes from 6 commits
79558d1
cbef232
0aa9284
6d241ae
a3db447
0473992
fb9674b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Takes in a string and json_encode()"d lists to produce a sanitized string. | ||
* This function operates on whitelists, there is currently no way to blacklist. | ||
* Args: | ||
* * text: the string to sanitize. | ||
* * attribute_whitelist_json: a json_encode()'d list of HTML attributes to allow in the final string. | ||
* * tag_whitelist_json: a json_encode()'d list of HTML tags to allow in the final string. | ||
*/ | ||
#define rustg_sanitize_html(text, attribute_whitelist_json, tag_whitelist_json) RUSTG_CALL(RUST_G, "sanitize_html")(text, attribute_whitelist_json, tag_whitelist_json) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Semicolon? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand, am I missing something here? |
||
|
||
/* | ||
* Here is a recommended default tag whitelist | ||
list( | ||
Kapu1178 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"b","br", | ||
"center", "code", | ||
"dd", "del", "div", "dl", "dt", | ||
"em", | ||
"font", | ||
"h1", "h2", "h3", "h4", "h5", "h6", "hr", | ||
"i", "ins", | ||
"li", | ||
"menu", | ||
"ol", | ||
"p", "pre", | ||
"span", "strong", | ||
"table", | ||
"tbody", | ||
"td", | ||
"th", | ||
"thead", | ||
"tfoot", | ||
"tr", | ||
"u", | ||
"ul", | ||
) | ||
*/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use crate::error::Result; | ||
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) { | ||
Ok(r) => return Some(r), | ||
Err(e) => return Some(e.to_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::from_iter(["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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wouldn't it make sense to keep this around rather than build it anew on every invocation There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd have to hash the arguments and such and that's out of my skill set presently. |
||
.clean(text) | ||
.to_string(); | ||
|
||
Ok(sanitized) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The interface should take a list and json_encode in itself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't do this so that you can store pre-encoded global lists to save on perf.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't that mean it's encoding on every call? The thing is that this will likely be called many times with only one or a few lists, so this introduces extra overhead.