django-xss-cleaner is a bleach-based Django XSSFilter toolkit that implements whitelist XSS filtering for GET and POST request parameters. The package ships with a set of built-in whitelisted HTML tags and attribute settings, and also supports custom extensions. Project address, https://github.com/shaowenchen/django-xss-cleaner
1. Installing and Configuring settings.py
- Add the middleware
xss_cleaner.middlewares.CleanXssMiddlewareto settings
| |
It is recommended to place CleanXssMiddleware as early as possible, preferably first. This ensures that all data the backend receives has passed through the XSS filter, so that no XSS vector can be injected.
- Configure the Clean XSS level [optional]
The default is ‘HIGHT’; available options: [‘LOW’, ‘HIGH’]
| |
If set to ‘HIGHT’, the allowed tags and attributes are
| |
If set to ‘LOW’, the allowed tags and attributes are
| |
The meaning of each parameter is described below.
- Add a custom whitelist [optional]
Incrementally add new tags and attributes to the whitelist.
| |
Parameter descriptions:
- tags (list) β allowed tags; tags not in the whitelist are escaped
- attributes (dict) β allowed attributes; attributes not in the whitelist are removed
- styles (list) β allowed styles; styles not in the whitelist are removed
- strip (bool) β whether to strip the escaped characters
- strip_comments (bool) β whether to strip HTML comments
The tags, attributes, and styles in BLEACH_WHITE_LIST are added incrementally on top of the whitelist allowed by the Clean XSS level. If strip or strip_comments is set, it overrides the default setting.
- Whether to print or log transfers [optional]
A switch is provided to make debugging easier by logging XSS Filter information:
| |
The default is True; available values: [True, False]
In local development, the transfer log is printed directly to the console. In production, it is printed as a warning log.
2. xss_cleaner Exemption Decorators
The xss_cleaner package provides two decorators for exempting requests from XSS Filter processing.
- escape_clean, provides View-level exemption.
| |
- escape_clean_param, provides parameter-level exemption.
| |
3. xss_cleaner Processing Examples
The examples below use the default configuration: XSS_LEVEL = ‘HIGH’
| |
The examples below use this configuration: XSS_LEVEL = ‘LOW’
| |
