Python bindings for the bluemonday HTML sanitizer
Project description
pybluemonday
pybluemonday is a library for sanitizing HTML very quickly via bluemonday.
pybluemonday takes untrusted user generated content as an input, and will return HTML that has been sanitised against a whitelist of approved HTML elements and attributes so that you can safely include the content in your web page.
Note: This library is in a useable state but may not have feature parity with the actual bluemonday. PRs and feedback are welcome on improving this library.
Installation
pip install pybluemonday
Examples
from pybluemonday import UGCPolicy
s = UGCPolicy()
print(s.sanitize("<script>alert(1)</script><b class='stuff'>testing</b>")) # <b>testing</b>
s.AllowAttrs("class", "style").Globally()
print(s.sanitize("<script>alert(1)</script><b class='stuff' style='color: red;'>testing</b>") # <b class="stuff" style="color: red;">testing</b>
from pybluemonday import StrictPolicy
s = StrictPolicy()
s.sanitize("<center><b>Blog Post Title</b></center>") # Blog Post Title
How does this work?
pybluemonday is a binding to bluemonday through a shared library built through cgo.
Instead of replicating the entire API, pybluemonday uses reflection on the Go side and some type checking on the Python side to call the right bluemonday function. This is an open area of improvement but gets reasonable coverage of the original bluemonday interface.
Also because it's difficult to share Go structs over to Python, pybluemonday keeps an ID reference to the struct in the Go side and passes the reference for every Go call. This means that if you corrupt or change the ID for some nonsensical reason you may likely end up with a memory leak. This is also an open area of improvement.
Performance
Most Python based HTML sanitizing libraries will need to rely on html5lib for parsing HTML in a reasoanble way. Because of this you will likely see performance hits when using these libraries.
Since pybluemonday is just bindings for bluemonday it has very good performance because all parsing and processing is done in Go by bluemonday. Go also ships an HTML5 parser which means we avoid html5lib but still process HTML pretty well.
Always take benchmarks with a grain of salt but when compared to other similar Python sanitizing libraries pybluemonday executes far faster:
❯ python benchmarks.py
bleach (20000 sanitizations): 36.573391182
html_sanitizer (20000 sanitizations): 17.280117869
bluemonday (20000 sanitizations): 0.6063146659999958
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for pybluemonday-0.0.2-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5679d1433e102b1c05e985e382b4d5dca9418df71898491af748deceba735094 |
|
MD5 | bf97a8ce8a300a034e1574f4989642db |
|
BLAKE2b-256 | dc4137e29312a28d5c5a3c24ebdd72752c892652bd34bc918e7ee5b1c392213e |
Hashes for pybluemonday-0.0.2-cp39-cp39-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d937b0bf0b33f3035208ec4dfb67657bac0482b46b3d12f8eec38388c3433e50 |
|
MD5 | 3a66c14d7faf15fd6af08ffc6145e25d |
|
BLAKE2b-256 | f976d73eb421f0147ca62f7b204a522ddc955ef02739fdc6f7b43b7473fdba6b |
Hashes for pybluemonday-0.0.2-cp39-cp39-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23e61776beb09eb48e5e8177c931fee2874c8e4941acf3ed2e0fcb700e958148 |
|
MD5 | f353a323718cf4860e42794f37568a51 |
|
BLAKE2b-256 | 1308fdf591908706b136e71999812941007658eee0ab70e1dae9fc358a5360fd |
Hashes for pybluemonday-0.0.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad52c3dcff9607b62552b9b6df299f83efe91c676c78daaef21040f5da51e010 |
|
MD5 | aa46fa743536a562883f34750086bbf8 |
|
BLAKE2b-256 | 4c8f8068e10b30b164d3cf908b0605859bbd3c5261d186e0a0a08db198f2647c |
Hashes for pybluemonday-0.0.2-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc1ffa85e2ab4f649a4df4fab359d6ba75f001657dc716256f552e58aef5aa19 |
|
MD5 | 9a00cc5a6e6cefdc239f5d7cacc0414a |
|
BLAKE2b-256 | 716f991c1f86e088d1ba3270e295688e41f6741c3b045eb45477b8e1871fc178 |
Hashes for pybluemonday-0.0.2-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 32c051457ac699616ec6a00bfd14fbcb0b079083b15ace249bda3ef3ad3e6a40 |
|
MD5 | 5bd381008ddb5bd5166bc073db5ad71b |
|
BLAKE2b-256 | c813fc53870c997d35ecdfce1fdabe8bc3e217347a377db2966758195089540d |
Hashes for pybluemonday-0.0.2-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 195171821228af26fa72029402e9a4a99bdcd11efd6ef87b7359f7059636378c |
|
MD5 | 40b68251d9aff962f33cc8a1618869f5 |
|
BLAKE2b-256 | c0df12ae5cc699311e8733bc7d038add8ca64892e8412f07cb6a40a4c87d9cf1 |
Hashes for pybluemonday-0.0.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | efa761246e645130552a6d8d32835c766439755d28ce6e528509b9d0448c5f8a |
|
MD5 | 9c19f9d15dc4167a2829fe5bd22bc948 |
|
BLAKE2b-256 | 1af190c5161d7cf47f09b1569a0b74fec8955c04851497410e20678807b21902 |
Hashes for pybluemonday-0.0.2-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9995e923b945478ed17a086e0e65dfbf598bafa2d67e0ee1aff77af091692a12 |
|
MD5 | 88855bca7539072018143fe0a135a18d |
|
BLAKE2b-256 | d93f2e7c2cdcb31488df9e2afa4fc084096643ae7fe9d24cc278b6f9875369c4 |
Hashes for pybluemonday-0.0.2-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e275e3e8c29dcb9536350de1295d5f93f6eed1c305e624fc6ded428a34650e29 |
|
MD5 | 770d86a7a9d3024949324110a67e7117 |
|
BLAKE2b-256 | 83170ef45e89024df2327572c7377139fdaa1fd78386f69ee39377dab972d849 |
Hashes for pybluemonday-0.0.2-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c06eaa965dc73c336928d2d2ebfcc736b040d3b45edfe497b5fe2c7a1dccdc75 |
|
MD5 | 76f4edd5062a7cd7edee82e0f3276ae4 |
|
BLAKE2b-256 | 4eea301ece5b820aa7b6d7558d367a61ec4585feb1a7b8759f5818a79fa2dc39 |
Hashes for pybluemonday-0.0.2-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5dae5fc1a98cc8f41b16bb8056a007c90dcf4941f431962377353a6ed3445175 |
|
MD5 | 7294c7f036102775983dde2cbbe6d46f |
|
BLAKE2b-256 | c959c674d764ccdb47315b1259cf17b593723bf0c2736d678a8699189f5da47a |
Hashes for pybluemonday-0.0.2-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d99774a997f95fe4bf2708c094c8a6440659c9146d987cbb6b1c4706c8eb0cdc |
|
MD5 | 04a75964a500bbb1109da4b2d66b9d62 |
|
BLAKE2b-256 | 83d281a94ab4f96fd680054db40a5bc915617195c4594637a9c7bf9501a6c997 |
Hashes for pybluemonday-0.0.2-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1aa3a32bbb736579218c74d339c25f828453b820b69d68e981b9f2d8026c78a3 |
|
MD5 | 55299c44e3a2d0d1a4a3c3690c06632e |
|
BLAKE2b-256 | bd4fe02b66f4f1f3460c47333637277632f737069501188c4eb94f5e68bd5c3b |
Hashes for pybluemonday-0.0.2-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 91b88838a51186f1038202e299b19b1fbca703b110ca3114839061fbb2b9bb6c |
|
MD5 | a0e8fd5cbb38016f569fa04af26b388f |
|
BLAKE2b-256 | ba7fba3732da450a9e5177e6ea2e7e5b99f322e64632100115e0a439f327d5df |
Hashes for pybluemonday-0.0.2-cp36-cp36m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ab23f00e814c259b2901ae9eee066ad902832bf466767875a30af047c00a107a |
|
MD5 | 5a0da997cb8a0d88a28d366bce80f27a |
|
BLAKE2b-256 | 7669046669e609097f1b9d5593737ed80098d5a38e008561311ed73525b4e1f6 |