A Django middleware to automatically sanitize and convert uploaded images to WebP to prevent XSS and save space.
Project description
Django Image Sanitizer
A lightweight, global Django middleware that automatically intercepts file uploads to sanitize images, prevent XSS attacks, and optimize storage by converting raster images to WebP.
🚀 Features
- Global Interception: Works at the middleware level, meaning it automatically applies to all image uploads across your entire Django project without needing to modify individual forms or models.
- XSS Prevention: Strictly blocks
.svgfiles andimage/svg+xmlcontent types to prevent malicious scripts from being uploaded. - Automatic WebP Conversion: Converts standard raster images (JPG, PNG, BMP, etc.) to optimized WebP format to save server space and improve page load speeds.
- Pixel Integrity Verification: Uses Pillow to verify the actual file contents, ensuring that disguised malicious files are caught even if their extension is changed.
- Configurable Size Limits: Easily set a maximum file size for image uploads directly in your Django settings.
- EXIF Data Stripping: Automatically removes metadata from images during the conversion process for enhanced user privacy.
📦 Installation
Install the package via pip:
pip install django-image-sanitizer
⚙️ Configuration
1. Add to Middleware
Open your Django settings.py and add the sanitizer to your MIDDLEWARE list. It is recommended to place it after security and authentication middlewares, but before any routing middlewares.
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
# Add the image sanitizer middleware
'django_image_sanitizer.middleware.ImageSanitizerMiddleware',
]
2. Configure Settings (Optional)
By default, the middleware blocks any image larger than 10MB. You can override this limit by adding IMAGE_SANITIZER_MAX_SIZE to your settings.py.
The value should be in bytes.
# settings.py
# Example: Set maximum upload size to 5MB
IMAGE_SANITIZER_MAX_SIZE = 5 * 1024 * 1024
🛠️ How It Works
Once installed and added to your middleware, django-image-sanitizer sits quietly in the background.
Whenever a user submits a POST request containing files (request.FILES), the middleware:
Checks if the file is an SVG and blocks it with an HttpResponseBadRequest if so.
Checks if the file is a raster image.
If it is a raster image, it verifies the file integrity, strips metadata, converts it to WebP (or PNG as a fallback), and updates the request.FILES object in memory.
Passes the sanitized, compressed image down to your views and forms as if the user uploaded the WebP file directly.
Non-image files (like PDFs or text documents) are ignored and pass through the middleware untouched.
📄 License
This project is licensed under the MIT License.
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 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_image_sanitizer-0.1.1.tar.gz.
File metadata
- Download URL: django_image_sanitizer-0.1.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
32bb93d2c563e575e68aa9b6e482aac1edd8d99515d2f899599b329af658ee54
|
|
| MD5 |
e4ab62152e5efbe2b719fac6a414c1dc
|
|
| BLAKE2b-256 |
6660315af7d4d5562268b7e3a4828e9f767d86b71088ceb1b7cbb4d5bc3a53cd
|
File details
Details for the file django_image_sanitizer-0.1.1-py3-none-any.whl.
File metadata
- Download URL: django_image_sanitizer-0.1.1-py3-none-any.whl
- Upload date:
- Size: 6.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f62a03588ce0fe635bd2dc3c510f0a3d5f0fa28afb6f8bebb40ba6fbd314cd2
|
|
| MD5 |
21c8db04bca15f36bc42008a9ed94a8f
|
|
| BLAKE2b-256 |
468a5652a4bc8a75c62446b194f0df6b5ea99cccb62877c03cf9f4ad90d30aaa
|