Bleach Allowlist

1.0.3 · maintenance · verified Thu Apr 16

Bleach Allowlist provides curated lists of HTML tags, attributes, and CSS styles, designed for sanitizing user-provided HTML using the `bleach` library. It offers ready-to-use allowlists for common scenarios like Markdown rendering or printing, as well as comprehensive CSS properties. The current version is 1.0.3, released on August 13, 2020. This library has had a single stable release since its inception and primarily serves as a data provider for `bleach`.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates using `bleach-allowlist` with the `bleach` library to sanitize an HTML string. It imports predefined tag, attribute, and style lists and applies them to `bleach.clean()` for secure content rendering. Note that `bleach` itself has deprecated direct `styles` argument in favor of `css_sanitizer` in recent versions; the example above demonstrates the older usage for `styles` or illustrates the values provided.

import bleach
from bleach_allowlist import print_tags, print_attrs, all_styles

raw_html = '<h1>Hello <script>alert("XSS")</script>World!</h1><p style="color: red;">This is a paragraph.</p><a href="javascript:alert(1)">Click me</a>'

# Bleach requires the css_sanitizer for style attributes
# You might need to install 'bleach[css]' if you use advanced CSS sanitization.
# For simple use, passing all_styles works with default bleach setup for allowed styles.
# Note: bleach itself is deprecated, consider alternatives for new projects.

# If you are using bleach >= 5.0, CSS sanitization is significantly different.
# You would typically use bleach.css_sanitizer.CSSSanitizer
# For this example, assuming a version of bleach where `all_styles` can be passed directly
# or for illustration of values. Always refer to bleach's current documentation.

sanitized_html = bleach.clean(
    raw_html,
    tags=print_tags,
    attributes=print_attrs,
    styles=all_styles, # Note: For modern bleach, use css_sanitizer=CSSSanitizer(allowed_css_properties=all_styles)
    strip=True
)

print(sanitized_html)

view raw JSON →