bleach
An easy safelist-based HTML-sanitizing tool.
Downloads: 0 (30 days)
Description
======
Bleach
======
.. image:: https://github.com/mozilla/bleach/workflows/Test/badge.svg
:target: https://github.com/mozilla/bleach/actions?query=workflow%3ATest
.. image:: https://github.com/mozilla/bleach/workflows/Lint/badge.svg
:target: https://github.com/mozilla/bleach/actions?query=workflow%3ALint
.. image:: https://badge.fury.io/py/bleach.svg
:target: http://badge.fury.io/py/bleach
**NOTE: 2023-01-23: Bleach is deprecated.** See issue:
`<https://github.com/mozilla/bleach/issues/698>`__
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
:License: Apache License v2; see LICENSE file
Reporting Bugs
==============
For regular bugs, please report them `in our issue tracker
<https://github.com/mozilla/bleach/issues>`_.
If you believe that you've found a security vulnerability, please `file a secure
bug report in our bug tracker
<https://bugzilla.mozilla.org/enter_bug.cgi?assigned_to=nobody%40mozilla.org&product=Webtools&component=Bleach-security&groups=webtools-security>`_
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>`_.
Security
========
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.
Installing Bleach
=================
Bleach is available on PyPI_, so you can install it with ``pip``::
$ pip install bleach
Upgrading Bleach
================
.. warning::
Before doing any upgrades, read through `Bleach Changes
<https://bleach.readthedocs.io/en/latest/changes.html>`_ for backwards
incompatible changes, newer versions, etc.
Bleach follows `semver 2`_ versioning. Vendored libraries will not
be changed in patch releases.
Basic use
=========
The simplest way to use Bleach is:
.. code-block:: python
>>> 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'
Code of Conduct
===============
This project and repository is governed by Mozilla's code of conduct and
etiquette guidelines. For more details please see the `CODE_OF_CONDUCT.md
</CODE_OF_CONDUCT.md>`_
.. _html5lib: https://github.com/html5lib/html5lib-python
.. _GitHub: https://github.com/mozilla/bleach
.. _ReadTheDocs: https://bleach.readthedocs.io/
.. _PyPI: https://pypi.org/project/bleach/
.. _semver 2: https://semver.org/
Bleach changes
==============
Version 6.3.0 (October 27th, 2025)
----------------------------------
**Backwards incompatible changes**
* Dropped support for Python 3.9. (#756)
**Security fixes**
None
**Bug fixes**
* Add support for Python 3.14. (#758)
* Fix wbr handling. (#488)
Version 6.2.0 (October 29th, 2024)
----------------------------------
**Backwards incompatible changes**
* Dropped support for Python 3.8. (#737)
**Security fixes**
None
**Bug fixes**
* Add support for Python 3.13. (#736)
* Remove six depdenncy. (#618)
* Update known-good versions for tinycss2. (#732)
* Fix additional < followed by characters and EOF issues. (#728)
Version 6.1.0 (October 6th, 2023)
---------------------------------
**Backwards incompatible changes**
* Dropped support for Python 3.7. (#709)
**Security fixes**
None
**Bug fixes**
* Add support for Python 3.12. (#710)
* Fix linkify with arrays in querystring (#436)
* Handle more cases with < followed by character data (#705)
* Fix entities inside a tags in linkification (#704)
* Update cap for tinycss2 to <1.3 (#702)
* Updated Sphinx requirement
* Add dependabot for github actions and update github actions
Version 6.0.0 (January 23rd, 2023)
----------------------------------
**Backwards incompatible changes**
* ``bleach.clean``, ``bleach.sanitizer.Cleaner``,
``bleach.html5lib_shim.BleachHTMLParser``: the ``tags`` and ``protocols``
arguments were changed from lists to sets.
Old pre-6.0.0:
.. code-block:: python
bleach.clean(
"some text",
tags=["a", "p", "img"],
# ^ ^ list
protocols=["http", "https"],
# ^ ^ list
)
New 6.0.0 and later:
.. code-block:: python
bleach.clean(
"some text",
tags={"a", "p", "img"},
# ^ ^ set
protocols={"http", "https"},
# ^ ^ set
)
* ``bleach.linkify``, ``bleach.linkifier.Linker``: the ``skip_tags`` and
``recognized_tags`` arguments were changed from lists to sets.
Old pre-6.0.0:
.. code-block:: python
bleach.linkify(
"some text",
skip_tags=["pre"],
# ^ ^ list
)
linker = Linker(
skip_tags=["pre"],
# ^ ^ list
recognized_tags=html5lib_shim.HTML_TAGS + ["custom-element"],
# ^ ^ ^ list
# |
# | list concatenation
)
New 6.0.0 and later:
.. code-block:: python
bleach.linkify(
"some text",
skip_tags={"pre"},
# ^ ^ set
)
linker = Linker(
skip_tags={"pre"},
# ^ ^ set
recognized_tags=html5lib_shim.HTML_TAGS | {"custom-element"},
# ^ ^ ^ set
# |
# | union operator
)
* ``bleach.sanitizer.BleachSanitizerFilter``: ``strip_allowed_elements`` is now
``strip_allowed_tags``. We now use "tags" everywhere rather than a mishmash
of "tags" in some places and "elements" in others.
**Security fixes**
None
**Bug fixes**
* Add support for Python 3.11. (#675)
* Fix API weirness in ``BleachSanitizerFilter``. (#649)
We're using "tags" instead of "elements" everywhere--no more weird
overloading of "elements" anymore.
Also, it no longer calls the superclass constructor.
* Add warning when ``css_sanitizer`` isn't set, but the ``style``
attribute is allowed. (#676)
* Fix linkify handling of character entities. (#501)
* Rework dev dependencies to use ``requirements-dev.txt`` and
``requirements-flake8.txt`` instead of extras.
* Fix project infrastructure to be tox-based so it's easier to have CI
run the same things we're running in development and with flake8
in an isolated environment.
* Update action versions in CI.
* Switch to f-strings where possible. Make tests parametrized to be
easier to read/maintain.
Version 5.0.1 (June 27th, 2022)
-------------------------------
**Security fixes**
None
**Bug fixes**
* Add missing comma to tinycss2 require. Thank you, @shadchin!
* Add url parse tests based on wpt url tests. (#688)
* Support scheme-less urls if "https" is in allow list. (#662)
* Handle escaping ``<`` in edge cases where it doesn't start a tag. (#544)
* Fix reference warnings in docs. (#660)
* Correctly urlencode email address parts. Thank you, @larseggert! (#659)
Version 5.0.0 (April 7th, 2022)
-------------------------------
**Backwards incompatible changes**
* ``clean`` and ``linkify`` now preserve the order of HTML attributes. Thank
you, @askoretskly! (#566)
* Drop support for Python 3.6. Thank you, @hugovk! (#629)
* CSS sanitization in style tags is completely different now. If you're using
Bleach ``clean`` to sanitize css in style tags, you'll need to update your
code and you'll need to install the ``css`` extras::
pip install 'bleach[css]'
See `the documentation on sanitizing CSS for how to do it
<https://bleach.readthedocs.io/en/latest/clean.html#sanitizing-css>`_. (#633)
**Security fixes**
None
**Bug fixes**
* Rework dev dependencies. We no longer have
``requirements-dev.in``/``requirements-dev.txt``. Instead, we're using
``dev`` extras.
See `development docs <https://bleach.readthedocs.io/en/latest/dev.html>`_
for more details. (#620)
* Add newline when dropping block-level tags. Thank you, @jvanasco! (#369)
Version 4.1.0 (August 25th, 2021)
---------------------------------
**Features**
* Python 3.9 support
**Security fixes**
None
**Bug fixes**
* Update sanitizer clean to use vendored 3.6.14 stdlib urllib.parse to
fix test failures on Python 3.9. (#536)
Version 4.0.0 (August 3rd, 2021)
--------------------------------
**Backwards incompatible changes**
* Drop support for unsup