An easy safelist-based HTML-sanitizing tool.
Bleach is a 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.
|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 available on PyPI, so you can install it with pip:
$ pip install bleach
Or with easy_install:
$ easy_install bleach
Or by cloning the repo from GitHub:
$ git clone git://github.com/mozilla/bleach.git
Then install it by running:
$ python setup.py install
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
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).
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:
The equivalent with Bleach 2.0 is:
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.
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
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|File Name & Checksum SHA256 Checksum Help||Version||File Type||Upload Date|
|bleach-2.0.0-py2.py3-none-any.whl (23.6 kB) Copy SHA256 Checksum SHA256||py2.py3||Wheel||Mar 8, 2017|
|bleach-2.0.0.tar.gz (46.1 kB) Copy SHA256 Checksum SHA256||–||Source||Mar 8, 2017|