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.
Metadata
Release files for django-image-sanitizer 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| django_image_sanitizer-0.1.1.tar.gz | 5.1 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_image_sanitizer-0.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.1 kB
Release files / django_image_sanitizer-0.1.1.tar.gz
| Download URL | django_image_sanitizer-0.1.1.tar.gz |
|---|---|
| Size | 5.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
32bb93d2c563e575e68aa9b6e482aac1edd8d99515d2f899599b329af658ee54
|
|
BLAKE2b-256 checksum How to use checksums |
6660315af7d4d5562268b7e3a4828e9f767d86b71088ceb1b7cbb4d5bc3a53cd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.0
|
Release files / django_image_sanitizer-0.1.1-py3-none-any.whl
| Download URL | django_image_sanitizer-0.1.1-py3-none-any.whl |
|---|---|
| Size | 6.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
6f62a03588ce0fe635bd2dc3c510f0a3d5f0fa28afb6f8bebb40ba6fbd314cd2
|
|
BLAKE2b-256 checksum How to use checksums |
468a5652a4bc8a75c62446b194f0df6b5ea99cccb62877c03cf9f4ad90d30aaa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.0
|
Security for django-image-sanitizer 0.1.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.