23 releases (9 stable)

✓ Uses Rust 2018 edition

3.0.0 Jul 25, 2019
2.1.2 Jun 28, 2019
2.1.1 May 8, 2019
2.0.0 Dec 28, 2018
0.1.0 Sep 4, 2015

#3 in Web programming

Download history 11293/week @ 2019-06-12 17956/week @ 2019-06-19 13453/week @ 2019-06-26 12353/week @ 2019-07-03 9962/week @ 2019-07-10 6705/week @ 2019-07-17 7005/week @ 2019-07-24 5836/week @ 2019-07-31 6425/week @ 2019-08-07 6884/week @ 2019-08-14 5794/week @ 2019-08-21 6316/week @ 2019-08-28 6203/week @ 2019-09-04 5647/week @ 2019-09-11 6625/week @ 2019-09-18

43,013 downloads per month
Used in 8 crates (7 directly)


1.5K SLoC

HTML Sanitization

Crates.IO Requires rustc 1.30.0

Ammonia is a whitelist-based HTML sanitization library. It is designed to prevent cross-site scripting, layout breaking, and clickjacking caused by untrusted user-provided HTML being mixed into a larger web page.

Ammonia uses html5ever to parse and serialize document fragments the same way browsers do, so it is extremely resilient to syntactic obfuscation.

Ammonia parses its input exactly according to the HTML5 specification; it will not linkify bare URLs, insert line or paragraph breaks, or convert (C) into ©. If you want that, use a markup processor before running the sanitizer, like pulldown-cmark.


To use ammonia, add it to your project's Cargo.toml file:

ammonia = "3"


Please see the CHANGELOG for a release history.


Using pulldown-cmark together with Ammonia for a friendly user-facing comment site.

use ammonia::clean;
use pulldown_cmark::{push_html, Parser};

let text = "[a link](http://www.notriddle.com/)";

let mut md_parse = Parser::new_ext(text, OPTION_ENABLE_TABLES);
let mut unsafe_html = String::new();
push_html(&mut unsafe_html, md_parse);

let safe_html = clean(&*unsafe_html);
assert_eq!(safe_html, "<a href=\"http://www.notriddle.com/\">a link</a>");


Ammonia builds a DOM, traverses it (replacing unwanted nodes along the way), and serializes it again. It could be faster for what it does, and if you don't want to allow any HTML it is possible to be even faster than that.

However, it takes about fifteen times longer to sanitize an HTML string using bleach-2.0.0 with html5lib-0.999999999 than it does using Ammonia 1.0.

$ cd benchmarks
$ cargo run --release
    Running `target/release/ammonia_bench`
87539 nanoseconds to clean up the intro to the Ammonia docs.
$ python bleach_bench.py
(1498800.015449524, 'nanoseconds to clean up the intro to the Ammonia docs.')


Licensed under either of these:


Thanks to the other sanitizer libraries, particularly Bleach for Python and sanitize-html for Node, which we blatantly copied most of our API from.

Thanks to ChALkeR, whose Improper Markup Sanitization document helped us find high-level semantic holes in Ammonia, and to ssokolow, whose review and experience were also very helpful.

And finally, thanks to the contributors.


~59K SLoC