Bleach is an allowed-list-based HTML sanitizing library that escapes or strips markup and attributes.
Bleach can also linkify text safely, applying filters that Django's urlize filter cannot, and optionally setting rel attributes, even on links already in the text.
Bleach is intended for sanitizing text from untrusted sources. If you find yourself jumping through hoops to allow your site administrators to do lots of things, you're probably outside the use cases. Either trust those users, or don't.
Because it relies on html5lib, Bleach is as good as modern browsers at dealing with weird, quirky HTML fragments. And any of Bleach's methods will fix unbalanced or mis-nested tags.
The version on GitHub is the most up-to-date and contains the latest bug fixes. You can find full documentation on ReadTheDocs.
Code: | https://github.com/mozilla/bleach |
---|---|
Documentation: | https://bleach.readthedocs.io/ |
Issue tracker: | https://github.com/mozilla/bleach/issues |
IRC: | #bleach on irc.mozilla.org |
License: | Apache License v2; see LICENSE file |
For regular bugs, please report them in our issue tracker.
If you believe that you've found a security vulnerability, please file a secure bug report in our bug tracker or send an email to security AT mozilla DOT org.
For more information on security-related bug disclosure and the PGP key to use for sending encrypted mail or to verify responses received from that address, please read our wiki page at https://www.mozilla.org/en-US/security/#For_Developers.
Bleach is a security-focused library.
We have a responsible security vulnerability reporting process. Please use that if you're reporting a security issue.
Security issues are fixed in private. After we land such a fix, we'll do a release.
For every release, we mark security issues we've fixed in the CHANGES in the Security issues section. We include any relevant CVE links.
Warning
Before doing any upgrades, read through Bleach Changes for backwards incompatible changes, newer versions, etc.
The simplest way to use Bleach is:
>>> import bleach >>> bleach.clean('an <script>evil()</script> example') u'an <script>evil()</script> example' >>> bleach.linkify('an http://example.com url') u'an <a href="http://example.com" rel="nofollow">http://example.com</a> url
This project and repository is governed by Mozilla's code of conduct and etiquette guidelines. For more details please see the Mozilla Community Participation Guidelines and Developer Etiquette Guidelines.
Security fixes
None
Backwards incompatible changes
None
Features
Bug fixes
Security fixes
None
Backwards incompatible changes
None
Features
None
Bug fixes
Security fixes
None
Backwards incompatible changes
None
Features
Bug fixes
Security fixes
None
Backwards incompatible changes
A bunch of functions were moved from one module to another.
These were moved from bleach.sanitizer to bleach.html5lib_shim:
These functions and classes weren't documented and aren't part of the public API, but people read code and might be using them so we're considering it an incompatible API change.
If you're using them, you'll need to update your code.
Features
Bleach no longer depends on html5lib. html5lib==1.0.1 is now vendored into Bleach. You can remove it from your requirements file if none of your other requirements require html5lib.
This means Bleach will now work fine with other libraries that depend on html5lib regardless of what version of html5lib they require. (#386)
Bug fixes
Security fixes
None
Backwards incompatible changes
Features
None
Bug fixes
Security fixes
Attributes that have URI values weren't properly sanitized if the values contained character entities. Using character entities, it was possible to construct a URI value with a scheme that was not allowed that would slide through unsanitized.
This security issue was introduced in Bleach 2.1. Anyone using Bleach 2.1 is highly encouraged to upgrade.
Backwards incompatible changes
None
Features
None
Bug fixes
Security fixes
None
Backwards incompatible changes
None
Features
None
Bug fixes
Security fixes
None
Backwards incompatible changes
None
Features
None
Bug fixes
Security fixes
Convert control characters (backspace particularly) to "?" preventing malicious copy-and-paste situations. (#298)
See https://github.com/mozilla/bleach/issues/298 for more details.
This affects all previous versions of Bleach. Check the comments on that issue for ways to alleviate the issue if you can't upgrade to Bleach 2.1.
Backwards incompatible changes
Features
Bug fixes
Security fixes
Backwards incompatible changes
Removed support for Python 2.6. #206
Removed support for Python 3.2. #224
Bleach no longer supports html5lib < 0.99999999 (8 9s).
This version is a rewrite to use the new sanitizing API since the old one was dropped in html5lib 0.99999999 (8 9s).
If you're using 0.9999999 (7 9s) upgrade to 0.99999999 (8 9s) or higher.
If you're using 1.0b8 (equivalent to 0.9999999 (7 9s)), upgrade to 1.0b9 (equivalent to 0.99999999 (8 9s)) or higher.
bleach.clean and friends were rewritten
clean was reimplemented as an html5lib filter and happens at a different step in the HTML parsing -> traversing -> serializing process. Because of that, there are some differences in clean's output as compared with previous versions.
Amongst other things, this version will add end tags even if the tag in question is to be escaped.
bleach.clean and friends attribute callables now take three arguments: tag, attribute name and attribute value. Previously they only took attribute name and attribute value.
All attribute callables will need to be updated.
bleach.linkify was rewritten
linkify was reimplemented as an html5lib Filter. As such, it no longer accepts a tokenizer argument.
The callback functions for adjusting link attributes now takes a namespaced attribute.
Previously you'd do something like this:
def check_protocol(attrs, is_new): if not attrs.get('href', '').startswith('http:', 'https:')): return None return attrs
Now it's more like this:
def check_protocol(attrs, is_new): if not attrs.get((None, u'href'), u'').startswith(('http:', 'https:')): # ^^^^^^^^^^^^^^^ return None return attrs
Further, you need to make sure you're always using unicode values. If you don't then html5lib will raise an assertion error that the value is not unicode.
All linkify filters will need to be updated.
bleach.linkify and friends had a skip_pre argument--that's been replaced with a more general skip_tags argument.
Before, you might do:
bleach.linkify(some_text, skip_pre=True)
The equivalent with Bleach 2.0 is:
bleach.linkify(some_text, skip_tags=['pre'])
You can skip other tags, too, like style or script or other places where you don't want linkification happening.
All uses of linkify that use skip_pre will need to be updated.
Changes
Security fixes
Backwards incompatible changes
clean: The list of ALLOWED_PROTOCOLS now defaults to http, https and mailto.
Previously it was a long list of protocols something like ed2k, ftp, http, https, irc, mailto, news, gopher, nntp, telnet, webcal, xmpp, callto, feed, urn, aim, rsync, tag, ssh, sftp, rtsp, afs, data. #149
Changes
Security fixes
Changes
Changes
Changes
Changes