Skip to main content

bleached

This is a small HTML checker. It can validate that HTML code is safe.

It does not aim to support the entire HTML spec, rather it focuses on checking HTML that has been run through a sanitizer (such as nh3 or the older bleach).

How to use?

$ pip install bleached
$ python3
>>> import bleached
>>> bleached.is_html_bleached('<p>Hello world</p>')
True
>>> bleached.is_html_bleached('<script>alert("Hello world");</script>')
False
>>> bleached.check_html('<p>Hello world</p>')
>>> bleached.check_html('<script>alert("Hello world");</script>')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
bleached.UnsafeInput: Line 1 character 8 (input index 7): Found forbidden opening tag 'script'

Why use this?

nh3 is a great library for sanitizing untrusted HTML. You should use it instead of this where possible.

However, it offers no way to check that a piece of HTML has been sanitized. Running the HTML through nh3 again will only work if you have the exact same version, as nh3 makes no guarantee of stability of their input. This is where bleached is useful.

Warnings

  • No validation of attribute valuess is performed. If you choose to allow an attribute, it is up to you to validate the values.
  • This accepts a much smaller subset of HTML than web browsers. Be ready for false negatives if you use this to validate HTML documents.

Release files for bleached 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bleached 1.0.1
File Size Uploaded
bleached-1.0.1.tar.gz 4.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bleached 1.0.1
File Interpreter ABI Platform
bleached-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 9.7 kB

Release files / bleached-1.0.1.tar.gz

Download URL bleached-1.0.1.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a0946a7fb8b56eb087d93cfbf28f17866a7a1eaad27e176146096fabe507c66a
BLAKE2b-256 checksum
How to use checksums
ce581e1bd07e8670270fe833680e49ec5d408ccdfc4dc217838ed3b36b997c58
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release files / bleached-1.0.1-py3-none-any.whl

Download URL bleached-1.0.1-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e520c0efc94c4682028b03b3df6f2cbf515f3fe517f1ee89c0e6f14b2c870d56
BLAKE2b-256 checksum
How to use checksums
441e15b854c82f2e75b580b9cc5ddfa89aa1fb232dcb8fde8021a05779e3586f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.12

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page