Django Cerber

CI Status Documentation Status Test coverage percentage

uv Ruff prek

PyPI Version Supported Python versions License


Documentation: https://django-cerber.readthedocs.io

Source Code: https://github.com/iddqd1/django-cerber


Login anomaly detection for Django: country changes, Tor and session revocation.

Installation

Install this via pip (or your favourite package manager):

pip install django-cerber

Add the app to your INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    "django_cerber",
]

MIDDLEWARE = [
    # ...
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    # ...
    "django_cerber.middleware.CerberMiddleware",
]

Then run python manage.py migrate. For the GeoIP country source install the extra: pip install "django-cerber[geoip]".

Configuration

All settings are optional. Add them to your Django settings module to override the defaults, for example:

from datetime import timedelta

CERBER_WINDOW = timedelta(minutes=30)
CERBER_COUNTRY_SOURCES = ("cloudflare", "geoip")
CERBER_GEOIP_DATABASE = "/var/lib/geoip/GeoLite2-Country.mmdb"
CERBER_BLOCKED_COUNTRIES = ("KP",)

Setting

Default

Description

CERBER_ENABLED

True

Whether login checks are performed at all.

CERBER_WINDOW

timedelta(hours=1)

A country change faster than this is treated as impossible travel.

CERBER_COUNTRY_SOURCES

('cloudflare',)

Where the client’s country comes from, tried in order until one knows it.

"cloudflare" reads the CF-IPCountry header, "geoip" looks the client IP up in CERBER_GEOIP_DATABASE. E.g. ("cloudflare", "geoip") uses GeoIP as a fallback when the request didn’t come through Cloudflare.

CERBER_GEOIP_DATABASE

None

Path to a MaxMind GeoIP2/GeoLite2 (or DB-IP) Country or City .mmdb file.

Required by the "geoip" source; needs pip install django-cerber[geoip].

CERBER_PROXY_IP_HEADER

None

Header set by your reverse proxy with the address it received the request from, e.g. "X-Forwarded-For" or "X-Real-IP". None = use REMOTE_ADDR.

Only set this when every request goes through that proxy, otherwise clients can forge it.

CERBER_PROXY_COUNT

1

Number of your own proxies that append to CERBER_PROXY_IP_HEADER.

For a comma separated list such as X-Forwarded-For: client, proxy1, the address is taken this many entries from the right; anything further left was sent by the client and is ignored.

CERBER_VERIFY_CLOUDFLARE_IP

True

Trust Cloudflare’s headers only when the peer address is a Cloudflare IP.

CERBER_BLOCKED_COUNTRIES

()

ISO-3166 alpha-2 codes whose logins are always blocked, e.g. ("RU", "KP").

Takes precedence over everything else, including impossible-travel exceptions. Use "T1" here to block Tor via Cloudflare as well.

CERBER_BLOCK_TOR

False

Block logins from Tor exit nodes (Cloudflare reports them as T1).

CERBER_LOGOUT_ALL_SESSIONS

True

On a blocked login, also end every other session of that user.

CERBER_REDIRECT_URL

None

Where blocked browser logins are redirected. Defaults to LOGIN_URL.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Tomasz Drobiszewski
Tomasz Drobiszewski

💻 📖 🤔 🚧 ⚠️

This project follows the all-contributors specification. Contributions of any kind welcome!

Credits

Copier

This package was created with Copier and the browniebroke/pypackage-template project template.