Bleach is an allowed-list-based HTML sanitizing library for Python (current version 6.3.0) that escapes or strips markup and attributes based on a configurable safelist. It also provides functionality to safely linkify text, including setting `rel` attributes. Designed for sanitizing text from untrusted sources, Bleach is built upon html5lib, making it robust against malformed HTML fragments. Note that the project was deprecated in January 2023, citing upstream dependency `html5lib`'s lack of active maintenance, and is now in a minimum-maintenance mode, with new projects discouraged.
pip install bleachVerified import paths — ran on the pinned version, not inferred.
This example demonstrates basic HTML sanitization using `bleach.clean()` and URL linkification with `bleach.linkify()`. It also shows how to use a `Cleaner` instance for more advanced or repeated sanitization tasks with custom allowed tags and attributes.
Consider alternative HTML sanitization libraries, or fork/maintain `bleach` and `html5lib` at your own risk for existing projects. Do not use for new development.
Update argument values from lists to Python `set` objects (e.g., `['b', 'i']` becomes `{'b', 'i'}`).Install the `css` extra (`pip install 'bleach[css]'`) and review the updated documentation for CSS sanitization in `style` attributes.
Update custom attribute callable functions to accept the `tag` argument as the first parameter.
Always pass `bleach.clean()` output through an additional escaping mechanism (like `django.utils.html.escape` or Jinja2's `escape`) if it's going into an HTML attribute or any non-HTML context.
Ensure your project runs on Python 3.10 or newer.
Install the 'bleach' module using pip: 'pip install bleach'.
Install the 'bleach_whitelist' module using pip: 'pip install bleach_whitelist'.
Reinstall the 'bleach_allowlist' module using pip: 'pip uninstall bleach_allowlist' followed by 'pip install bleach_allowlist'.
Ensure you are using a compatible version of bleach (e.g., current versions provide `bleach.clean` directly). Verify the import statement is `import bleach` and call `bleach.clean(text)`.
Ensure that the input text passed to `bleach.clean()` or `bleach.linkify()` is always a string and not `None`. You might need to add a check for `None` or provide a default empty string.