Sanitize HTML
HTML written by users made safe to show: scripts, event handlers and javascript: links removed, the formatting kept.
Version 1.0.0 · 3 functions · 1 crate
Functions
What use sanitize.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use sanitize_html, sanitize_html_only from sanitize (Quillo 0.6).
sanitize_html(html: str) → strhtml with only safe tags and attributes left (links, lists, tables, images, emphasis); links get rel="noopener noreferrer".
sanitize_html_only(html: str, tags: list) → strhtml with only the tags named left, and none of their attributes but href on links.
strip_tags(html: str) → strThe text of html, every tag taken out (the text stays escaped, ready for a page).
Get it
$ quillo install sanitize # sanitize.qwer into this folder$ quillo install sanitize --example # and its example, sanitize.qweOr with curl:
$ curl -O https://quillolang.org/plugins/files/sanitize.qwer$ curl -O https://quillolang.org/plugins/files/sanitize.qweThen, in your program, all of it or only what you call:
use sanitize.qweruse sanitize_html, sanitize_html_only from sanitize // Quillo 0.6: only theseIt uses this crate, which cargo fetches the first time the plugin is built:
ammonia = "4"Example
// A comment written in HTML, shown safely.//// quillo run sanitize.qwe then open http://localhost:8080/use "sanitize.qwer"let comment = `<p onclick="steal()">Nice post! <b>Bold</b> <a href="javascript:alert(1)">click</a><a href="https://quillolang.org">Quillo</a><script>alert("x")</script><img src=x onerror="steal()"></p>`route GET / = <html><body> <h1>Comments</h1> <div>{raw(sanitize_html(comment))}</div> <div>{raw(sanitize_html_only(comment, ["b", "a"]))}</div> <p>{strip_tags(comment)}</p></body></html>$ quillo run sanitize.qwe$ curl 'localhost:8080/'Source
//! HTML from users made safe to show: scripts, event handlers and dangerous links removed, formatting kept.//! version: 1.0.0//!//! [dependencies]//! ammonia = "4"use std::collections::HashSet;/// `html` with only safe tags and attributes left (links, lists, tables, images, emphasis); links get `rel="noopener noreferrer"`.pub fn sanitize_html(html: &str) -> String { ammonia::clean(html)}/// `html` with only the `tags` named left, and none of their attributes but `href` on links.pub fn sanitize_html_only(html: &str, tags: Vec<String>) -> String { let set: HashSet<&str> = tags.iter().map(|t| t.as_str()).collect(); let mut b = ammonia::Builder::empty(); b.tags(set); if tags.iter().any(|t| t == "a") { b.add_tag_attributes("a", &["href"]).link_rel(Some("noopener noreferrer nofollow")).url_schemes(["http", "https", "mailto"].into()); } b.clean(html).to_string()}/// The text of `html`, every tag taken out (the text stays escaped, ready for a page).pub fn strip_tags(html: &str) -> String { ammonia::Builder::empty().clean(html).to_string()}Related
csp.qwer · 1.0.0Content-Security-Policy headers built from a map and read back, a nonce per page, the policy graded for the holes that let scripts in, and whether a URL would load under it.7 views · 7 downloadsLink finder link_extract.qwer · 1.0.0URLs and emails found in plain text, with trailing punctuation handled, and the text turned into safe HTML with clickable, nofollow links.7 views · 6 downloadsThread 0 posts
On Sanitize HTML: questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.