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.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c7e4ae00d7d7e31a826ff43d6e80f6988b6518199a96d2fef9a0a9ec28b1a19 |
|
MD5 | 0f1e539f2854c13da97a547f0d6be4f2 |
|
BLAKE2b-256 | c24d66aa313dcb26bff57acae5fc62000c3274b5ffcfa89bb0c8b2702d259567 |
Hashes for django_cidr_allowed_hosts-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25f6e43dad3c6d26010767dd0936e3e1489d05f1c703ec61c1a4a1537aed7182 |
|
MD5 | 8638e5318e895eba94b0e09dffbfe56b |
|
BLAKE2b-256 | 71d190f2c2925d703fa77e65f9352513ab7fb6ed753c43a5cb58c4da29907074 |