Security

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

#html#xss#comments#user-content

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) → str

html with only safe tags and attributes left (links, lists, tables, images, emphasis); links get rel="noopener noreferrer".

sanitize_html_only(html: str, tags: list) → str

html with only the tags named left, and none of their attributes but href on links.

strip_tags(html: str) → str

The 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.qwe

Or with curl:

$ curl -O https://quillolang.org/plugins/files/sanitize.qwer$ curl -O https://quillolang.org/plugins/files/sanitize.qwe

Then, in your program, all of it or only what you call:

main.qwe
use sanitize.qweruse sanitize_html, sanitize_html_only from sanitize      // Quillo 0.6: only these

It uses this crate, which cargo fetches the first time the plugin is built:

[dependencies]
ammonia = "4"

Example

sanitize.qwe
// 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

sanitize.qwer
//! 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()}
Thread
77Useful? Like it, or add to its thread below.

Thread 0 posts

On Sanitize HTML: questions, tips, corrections, and code that uses it.

No posts yet. The first one opens the thread.