A Django middleware that sanitizes incoming request data to prevent XSS.
Project description
django-sanitizer
A lightweight, configurable Django middleware that automatically sanitizes all incoming request data (JSON, form-data, query params) to protect your application against XSS, HTML injection, and unsafe attributes.
Built with Bleach, easy to install, easy to extend, and safe by default.
🚀 Features
- 🔒 Sanitizes JSON bodies, form-data, and query parameters
- 🧼 Removes unsafe HTML tags, scripts, event handlers (e.g.,
onerror) - 🎯 Fully configurable via Django settings
- 📝 Optional HTML response sanitization
- 🛠 Zero configuration required — works out of the box
- 🧪 Comes with testing utilities and easy middleware integration
📦 Installation
pip install django-sanitizer
Or install your local dev version:
pip install -e .
⚙️ Setup
Add the middleware to your Django settings:
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django_sanitizer.middleware.SanitizerMiddleware",
"django.middleware.common.CommonMiddleware",
...
]
🔧 Configuration Options (Optional)
Add to settings.py only if you want customization:
SANITIZER_ENABLED = True
SANITIZER_ALLOWED_TAGS = [
"b", "i", "u", "a", "em", "strong", "p",
"ul", "ol", "li", "br", "img"
]
SANITIZER_ALLOWED_ATTRIBUTES = {
"a": ["href", "title", "rel"],
"img": ["src", "alt"],
}
SANITIZER_STRIP = True
SANITIZER_SANITIZE_RESPONSE_HTML = False
SANITIZER_DEBUG = False
🧪 Example
Request Body:
{
"bio": "<script>alert(1)</script><b>Hello</b>"
}
Sanitized Output:
{
"bio": "<b>Hello</b>"
}
🧪 Django Views Example
JSON Example Endpoint
# views.py
from django.http import JsonResponse
def echo_json(request):
return JsonResponse(request.sanitized_data)
Form Example Endpoint
def form_view(request):
return JsonResponse(request.sanitized_data)
🧪 Testing in Postman
For JSON:
- Method: POST
- URL:
/echo-json/ - Headers:
Content-Type: application/json - Body (raw JSON):
{"bio":"<img src=x onerror=alert(1)>hello"}
You should receive:
{"bio":"hello"}
🛡 How It Works
The middleware intercepts the request before it reaches your views:
- Extracts request data (JSON, form-data, GET params)
- Sanitizes all values using allowed tags + attributes
- Places sanitized result in
request.sanitized_data - Your view receives only safe data
This allows cleaning without modifying Django internals.
📁 Project Structure (Package Only)
django_sanitizer/
│
├── __init__.py
├── sanitizer.py
├── middleware.py
└── utils.py
🛠 Development
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
pip install -e .
Run tests:
pytest
📦 Publishing to PyPI
python -m build
twine upload dist/*
📄 License
MIT License © 2025 Free to use, modify, and integrate into commercial apps.
⭐ Support the Project
If this package helps you, please ⭐ star the repository on GitHub once published!
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_sanitizers-0.0.1.tar.gz.
File metadata
- Download URL: django_sanitizers-0.0.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5cad8fafd46b2be521a1fa4dc5b041aa2bed5942a7acec11ffe056c2cd17185b
|
|
| MD5 |
ee19cb66e407b68ff282dd8ae029b40e
|
|
| BLAKE2b-256 |
ab6f33addd37536bf3943d80f9e0cff3fd09cda1c55594d1d4c5decede4cb7d3
|
File details
Details for the file django_sanitizers-0.0.1-py3-none-any.whl.
File metadata
- Download URL: django_sanitizers-0.0.1-py3-none-any.whl
- Upload date:
- Size: 5.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb37e79adb9d93e216ca42a667f7026505b452908e007da4af6947d35d0b1dd6
|
|
| MD5 |
c96a5ded7c0b1a21b90e931c143231cf
|
|
| BLAKE2b-256 |
9a67963d527a5670990654d02ba0b86f3f0e4e1fc9c3e78794526464b27dc753
|