ammonia

Repair and secure untrusted HTML

Latest version: 4.1.0 registry icon
Maintenance score
87
Safety score
96
Popularity score
79
Check your open source dependency risks. Get immediate insight about security, stability and licensing risks.
Security
  Vulnerabilities
Version Suggest Low Medium High Critical
4.1.0 0 0 0 0 0
4.0.0 0 0 0 0 0
3.3.0 0 0 0 0 0
3.2.1 0 0 0 0 0
3.2.0 0 0 0 0 0
3.1.4 0 0 0 0 0
3.1.3 0 0 0 0 0
3.1.2 0 0 1 0 0
3.1.1 0 0 1 0 0
3.1.0 0 0 1 0 0
3.0.0 0 0 2 0 0
2.1.4 0 0 1 0 0
2.1.3 0 0 1 0 0
2.1.2 0 0 1 0 0
2.1.1 0 0 1 0 0
2.1.0 0 0 1 0 0
2.0.0 0 0 2 0 0
1.2.0 0 0 2 0 0
1.1.0 0 0 2 0 0
1.0.1 0 0 2 0 0
1.0.0 0 0 2 0 0
0.7.0 0 0 2 0 0
0.6.1 0 0 2 0 0
0.6.0 0 0 2 0 0
0.5.0 0 0 2 0 0
0.4.0 0 0 2 0 0
0.3.0 0 0 2 0 0
0.2.0 0 0 2 0 0
0.1.3 0 0 2 0 0
0.1.2 0 0 2 0 0
0.1.1 0 0 2 0 0
0.1.0 0 0 2 0 0

Stability
Latest release:

4.1.0 - This version is safe to use because it has no known security vulnerabilities at this time. Find out if your coding project uses this component and get notified of any reported security vulnerabilities with Meterian-X Open Source Security Platform

Licensing

Maintain your licence declarations and avoid unwanted licences to protect your IP the way you intended.

Apache-2.0   -   Apache License 2.0

Not a wildcard

Not proprietary

OSI Compliant


MIT   -   MIT License

Not a wildcard

Not proprietary

OSI Compliant



HTML Sanitization

Crates.IO Requires rustc 1.80.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.

Installation

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

[dependencies]
ammonia = "4.1"

Changes

Please see the CHANGELOG for a release history.

Example

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

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

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

let mut options = Options::empty();
options.insert(Options::ENABLE_TABLES);

let mut md_parse = Parser::new_ext(text, options);
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>");

Performance

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.')

License

Licensed under either of these:

Thanks

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, to ssokolow, whose review and experience were also very helpful, to securityMB, for finding a very obscure namespace-related injection bug, and xfix for finding a DoS bug in a recursive destructor.

And finally, thanks to the contributors.