Skip to main content

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

Release files for django-cidr-allowed-hosts 1.0.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-cidr-allowed-hosts 1.0.3
File Size Uploaded
django-cidr-allowed-hosts-1.0.3.tar.gz 4.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-cidr-allowed-hosts 1.0.3
File Interpreter ABI Platform
django_cidr_allowed_hosts-1.0.3-py3-none-any.whl Python 3 none any Details

Total release size: 10.1 kB

Release files / django-cidr-allowed-hosts-1.0.3.tar.gz

Download URL django-cidr-allowed-hosts-1.0.3.tar.gz
Size 4.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d2109d0f5539bac7ea4b9529be102fc6fddd2483095bd001ab3b96f0a07311e3
BLAKE2b-256 checksum
How to use checksums
50ad669865ac1b1aabd385025504e14d4748213e2af5178dadfa888f516557c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release files / django_cidr_allowed_hosts-1.0.3-py3-none-any.whl

Download URL django_cidr_allowed_hosts-1.0.3-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
00f7a7e8122502a971bb749b5e236842cf1a763ed13c515d5a9c9dd2e6797db9
BLAKE2b-256 checksum
How to use checksums
25f2689dac0d5dedb34ecc178b4c58606e903d2f3f1f2a7c12e171b4475c955e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.2

Release history Release notifications | RSS feed

This release

1.0.3 This release

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page