Django middleware to allow access from specific CIDR ranges
Project description
A Django middleware that allows you to specify a list of allowed hosts using CIDR notation.
Installation
Install using pip:
pip install django-cidr-allowed-hosts
Add the middleware at the top of your MIDDLEWARE settings:
MIDDLEWARE = [
'cidr.middleware.CIDRMiddleware',
...
]
Add the CIDR_ALLOWED_HOSTS setting to your settings:
CIDR_ALLOWED_HOSTS = ["0.0.0.0/0"] # allows any IPv4
And that should be it.
Features
ALLOWED_HOSTS will still work as expected. Since the middleware overrides the ALLOWED_HOSTS setting to "*", the value provided originally to ALLOWED_HOSTS will be stored in ORIGINAL_ALLOWED_HOSTS and used to check if the request should be allowed.
If CIDR_ALLOWED_HOSTS is not set, the middleware will not be used.
If ALLOWED_HOSTS contains "*" and CIDR_ALLOWED_HOSTS is set, the middleware will raise MiddlewareNotUsed exception.
CIDR_ALLOWED_HOSTS must follow the CIDR notation.
Only IPv4 is supported.
Development
python3 -m virtualenv venv source venv/bin/activate pip3 install tox tox
Credits
This project was inspired by django-allow-cidr
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
Hashes for django-cidr-allowed-hosts-1.0.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d29077cc98b4533d81a76a040468971361cd2a9bc0002b2c35f0cd06f8c5c780 |
|
MD5 | a1c25f66b02f9c0d38e443e6e4e2febb |
|
BLAKE2b-256 | 2ee4dd4a63b7a50e50d2d73ff60fd60b1f0bb70c47aa376c81d0db2d648656db |
Hashes for django_cidr_allowed_hosts-1.0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8528da7982683775e794ca572d31e2491187450122f76a6a180c15e863156d31 |
|
MD5 | cdfb85e414d540e380ac17b744aecdca |
|
BLAKE2b-256 | 6e9eb31a70b38a06e87e302f8c3c9d0b1ca984a9c7dfa56be7c2808e44c5a915 |