Skip to main content

Django HTML Sanitizer provides a set of utilities to easily sanitize/escape/clean HTML inputs in django. This app is built on top of bleach, the excellent Python HTML sanitizer.

Dependencies

Installation

You’ll first need to install the package (or download manually from pypi):

pip install django-html_sanitizer

And then add sanitizer to your INSTALLED_APPS in django’s settings.py:

INSTALLED_APPS = (
    # other apps
    "sanitizer",
)

Model Usage

Similar to bleach, django sanitizer is a whitelist (only allows specified tags and attributes) based HTML sanitizer. Django sanitizer provides two model fields that automatically sanitizes text values; SanitizedCharField and SanitizedTextField.

These fields accept extra arguments:

  • allowed_tags: a list of allowed HTML tags

  • allowed_attributes: a list of allowed HTML attributes, or a dictionary of tag keys with atttribute list for each key

  • allowed_styles: a list of allowed styles if “style” is one of the allowed attributes

  • strip: a boolean indicating whether offending tags/attributes should be escaped or stripped

Here’s how to use it in django models:

from django.db import models
from sanitizer.models import SanitizedCharField, SanitizedTextField

class MyModel(models.Model):
    # Allow only <a>, <p>, <img> tags and "href" and "src" attributes
    foo = SanitizedCharField(max_length=255, allowed_tags=['a', 'p', 'img'],
                             allowed_attributes=['href', 'src'], strip=False)
    bar = SanitizedTextField(max_length=255, allowed_tags=['a', 'p', 'img'],
                             allowed_attributes=['href', 'src'], strip=False)
    foo2 = SanitizedCharField(max_length=255, allowed_tags=['a', 'p', 'img'],
                             allowed_attributes={'img':['src', 'style']},
                             allowed_styles=['width', 'height'], strip=False)

Form Usage

Using django HTML sanitizer in django forms is very similar to model usage:

from django import forms
from sanitizer.forms import SanitizedCharField

class MyForm(forms.Form):
    # Allow only <a>, <p>, <img> tags and "href" and "src" attributes
    foo = SanitizedCharField(max_length=255, allowed_tags=['a', 'p', 'img'],
                             allowed_attributes=['href', 'src'], strip=False)
    bar = SanitizedCharField(max_length=255, allowed_tags=['a', 'p', 'img'],
                             allowed_attributes=['href', 'src'], strip=False, widget=forms.Textarea)
    foo2 = SanitizedCharField(max_length=255, allowed_tags=['a', 'p', 'img'],
                             allowed_attributes={'img':['src', 'style']},
                             allowed_styles=['width', 'height'], strip=False)

Template Usage

Django sanitizer provides a few differents ways of cleaning HTML in templates.

escape_html Template Tag

Example usage:

{% load sanitizer %}
{% escape_html post.content "a, p, img" "href, src, style" "width"%}

Assuming post.content contains the string ‘<a href =”#” style=”width:200px; height=”400px”>Example</a><script>alert(“x”)</script>’, the above tag will output:

'<a href ="#" style="width:200px;">Example</a>&lt;script&gt;alert("x")&lt;/script&gt;'

On django 1.4 you could also use keyword arguments:

{% escape_html '<a href="">bar</a>' allowed_tags="a,img" allowed_attributes="href,src" allowed_styles="width" %}

strip_html Template Tag

Example usage:

{% load sanitizer %}
{% strip_html post.content "a, p, img" "href, src" %}

If post.content contains the string ‘<a href =”#”>Example</a><script>alert(“x”)</script>’, this will give you:

'<a href ="#">Example</a>alert("x")'

escape_html Filter

Escapes HTML tags from string based on settings. To use this filter you need to put these variables on settings.py:

  • SANITIZER_ALLOWED_TAGS - a list of allowed tags (defaults to an empty list)

  • SANITIZER_ALLOWED_ATTRIBUTES - a list of allowed attributes (defaults to an empty list)

  • SANITIZER_ALLOWED_STYLES - a list of allowed styles if the style attribute is set (defaults to an empty list)

For example if we have SANITIZER_ALLOWED_TAGS = ['a'], SANITIZER_ALLOWED_ATTRIBUTES = ['href'], SANITIZER_ALLOWED_STYLES = ['width'] in settings.py, doing:

{% load sanitizer %}
{{ post.content|escape_html }}

If post.content contains the string ‘<a href =”#” style=”width:200px; height:400px”>Example</a><script>alert(“x”)</script>’, it will give you:

'<a href ="#" style="width=200px;">Example</a>&lt;script&gt;alert("x")&lt;/script&gt;'

strip_html Filter

Similar to escape_html filter, except it strips out offending HTML tags.

For example if we have SANITIZER_ALLOWED_TAGS = ['a'], SANITIZER_ALLOWED_ATTRIBUTES = ['href'] in settings.py, doing:

{% load sanitizer %}
{{ post.content|strip_html }}

If post.content contains the string ‘<a href =”#”>Example</a><script>alert(“x”)</script>’, we will get:

'<a href ="#">Example</a>alert("x")'

Changelog

Version 0.1.5

  • Fixes for smart_unicode and basestring (python 3.x support)

Version 0.1.4

  • CharField, TextField, strip_html and escape_html now support allowed_styles (thanks cltrudeau,

  • Added an example of template tag usage using kwargs now that Django 1.4 is out

Version 0.1.2

  • allowed_tags and allowed_attributes in CharField and TextField now default to []

Metadata

Release files for django-html_sanitizer 0.1.5

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

Source distribution (sdist)

Source distribution for django-html_sanitizer 0.1.5
File Size Uploaded
django-html_sanitizer-0.1.5.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-html_sanitizer 0.1.5
File Interpreter ABI Platform
django_html_sanitizer-0.1.5-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 16.6 kB

Release files / django-html_sanitizer-0.1.5.tar.gz

Download URL django-html_sanitizer-0.1.5.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
407079629e472bd4d9998b6a050c18949d3e88e38ceb985d30ea2b05128e32d4
BLAKE2b-256 checksum
How to use checksums
3a0fa877623d3692ee9e0b1e2be9c460e21f88028b1ffe7e49de2912cc946490
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_html_sanitizer-0.1.5-py2.py3-none-any.whl

Download URL django_html_sanitizer-0.1.5-py2.py3-none-any.whl
Size 10.6 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
5820ad3ac6bebdc8dc95d8401194a921248852e4ba32ba1d22d0ddaefd649873
BLAKE2b-256 checksum
How to use checksums
0a4cf4c7364126273ddb86bdf52a0824bb49273e2d4f2ce2968b766359256654
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1.5 This release

2 release files

0.1.4

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

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