All Projects → ColdHeat → pybluemonday

ColdHeat / pybluemonday

Licence: BSD-3-Clause License
pybluemonday is a library for sanitizing HTML very quickly via bluemonday.

Programming Languages

python
139335 projects - #7 most used programming language
go
31211 projects - #10 most used programming language
shell
77523 projects
Dockerfile
14818 projects
Makefile
30231 projects

Projects that are alternatives of or similar to pybluemonday

deno bindgen
Simplified glue code generation for Deno FFI libraries written in Rust.
Stars: ✭ 142 (+468%)
Mutual labels:  ffi
wasm
Utilities for loading and running WASM modules from Dart code
Stars: ✭ 252 (+908%)
Mutual labels:  ffi
windigo
Windows API and GUI in idiomatic Go.
Stars: ✭ 187 (+648%)
Mutual labels:  ffi
haskell-libui
Haskell bindings to the libui C library.
Stars: ✭ 45 (+80%)
Mutual labels:  ffi
ol
Otus Lisp (Ol in short) is a purely* functional dialect of Lisp.
Stars: ✭ 157 (+528%)
Mutual labels:  ffi
cargo-valgrind
A cargo subcommand, that runs valgrind and displays its output in a helpful manner.
Stars: ✭ 66 (+164%)
Mutual labels:  ffi
asmdot
[Unstable] Fast, zero-copy and lightweight (Arm | Mips | x86) assembler in (C | C++ | C# | Go | Haskell | Javascript | Nim | OCaml | Python | Rust).
Stars: ✭ 23 (-8%)
Mutual labels:  ffi
php-rdkafka-ffi
PHP Kafka client - binding librdkafka via FFI
Stars: ✭ 49 (+96%)
Mutual labels:  ffi
clr-loader
Loader for different .NET runtimes
Stars: ✭ 16 (-36%)
Mutual labels:  ffi
buke
full text search manpages
Stars: ✭ 27 (+8%)
Mutual labels:  ffi
lua-resty-openssl
FFI-based OpenSSL binding for OpenResty
Stars: ✭ 76 (+204%)
Mutual labels:  ffi
ffi-clang
Ruby FFI bindings for libclang 3.4+.
Stars: ✭ 41 (+64%)
Mutual labels:  ffi
pony-sodium
Safe Pony FFI wrapper for the libsodium cryptography library. 🐴 🔐
Stars: ✭ 24 (-4%)
Mutual labels:  ffi
Compiler-Principle
词法分析,LL(1) 文法分析,LR(1) 文法分析
Stars: ✭ 18 (-28%)
Mutual labels:  ffi
lonlat bng
A multithreaded Rust library with FFI for converting WGS84 longitude and latitude coordinates into BNG (OSGB36) Eastings and Northings and vice versa (using OSTN15)
Stars: ✭ 20 (-20%)
Mutual labels:  ffi
denoffi
Deno Foreign Function Interface.
Stars: ✭ 37 (+48%)
Mutual labels:  ffi
opencl-ruby
OpenCL bindings for Ruby
Stars: ✭ 38 (+52%)
Mutual labels:  ffi
winsafe
Windows API and GUI in safe, idiomatic Rust.
Stars: ✭ 110 (+340%)
Mutual labels:  ffi
PointerScript
Scripting language with pointers and native library access.
Stars: ✭ 26 (+4%)
Mutual labels:  ffi
idris-ffi-example
A minimal example of the Idris C FFI
Stars: ✭ 14 (-44%)
Mutual labels:  ffi

pybluemonday

PyPI

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 is still experimental. It may not have feature parity with the actual bluemonday and is likely to sanitize HTML slightly differently than other libraries. 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. However, 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 when you try to call a method.

Essentially you want to create a Policy with the provided pybluemonday.UGCPolicy, pybluemonday.StrictPolicy, and pybluemonday.NewPolicy classes and then call methods that map to the appropriate bluemonday struct method.

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): 37.613802053
html_sanitizer (20000 sanitizations): 17.645683948
lxml Cleaner (20000 sanitizations): 10.500760227999997
pybluemonday (20000 sanitizations): 0.6188559669999876

Benchmarks taken on a MacBook Pro 15-inch, 2016 (2.7 GHz Intel Core i7, 16 GB RAM)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].