Skip to main content

Sanitizes input data to prevent XSS i.e. cross site scripting attacks.

Project description

A tool for removing malicious content from input data before saving data into database. It takes input containing HTML with XSS scripts and returns valid HTML in the output. It is a wrapper around Python’s bleach library to easily integrate with Django framework.

Setup

  1. Install input-sanitizer via pip:

    pip install input-sanitizer
  2. Add input-sanitizer to your INSTALLED_APPS:

    INSTALLED_APPS = [
        # ...
        'input_sanitizer',
        # ...
    ]
  3. Add default configurations for allowed tags, etc in settings.py. These configurations are optional and will defaults to using the bleach defaults. Refer to bleach documentation for their use:

    # tags which are allowed
    BLEACH_ALLOWED_TAGS = ["div", "section", "a", "i"]
    
    # remove all tags from input
    BLEACH_STRIP_TAGS = True
    
    # remove comments from input
    BLEACH_STRIP_COMMENTS = True

Usage

In Django Models

input-sanitizer provides two custom model fields SanitizedCharField and SanitizedTextField to automatically remove malicious content from input before saving data into database, but beware it won’t work with bulk update, bulk create, etc as these operations are done at the database level. You can still manually sanitize input data to use for bulk update, bulk create, etc.

# in models.py
from django import models
from input_sanitizer import sanitized_fields

class User(models.Model):
    username = sanitized_fields.SanitizedCharField()
    info = sanitized_fields.SanitizedTextField()

SanitizedCharField and SanitizedTextField may take following arguments to alter cleaning behaviour. Please, refer bleach documentation for their use:

  • allowed_tags

  • strip_comments

  • strip_tags

SanitizedCharField is a extension of Django’s CharField and therefore, it will accept all normal CharField arguments.

SanitizedTextField is a extension of Django’s TextField and therefore, it will accept all normal TextField arguments.

In Views

To manually sanitize data, you can use get_sanitized_data function. It can be used to sanitize data to be used for bulk update, bulk create, etc.

from input_sanitizer import sanitizers
cleaned_data = sanitizers.get_sanitized_data(data, bleach_kwargs)

bleach_kwargs arguments are optional and will default to using the bleach defaults. You may pass following arguments to alter cleaned output as per your requirement.

  • allowed_tags

  • strip_comments

  • strip_tags

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

input_sanitizer-0.1.6-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

Details for the file input_sanitizer-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: input_sanitizer-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 3.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.5

File hashes

Hashes for input_sanitizer-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 495ec664dae9a5b4e4909d61775b270d903487ce0301eae6b550801e8a8e2c68
MD5 2283ce0ba41e300a2417605d48ecd00c
BLAKE2b-256 e31667ffe79fe7381f0db1d86dd9af112a21333e2969cf2bc8df29137160a77d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page