Skip to main content

Blacklist users and hosts in Django. Automatically blacklist rate-limited clients.

Project description

Django Blacklist

Blacklist users and hosts in Django. Automatically blacklist rate-limited clients.

Overview

Django Blacklist allows you to block specific users and IP addresses/networks from accessing your application. Clients can be blocked manually from the admin interface, or automatically after exceeding a request rate limit. The blacklist rules are applied for a specific duration. The blacklist is very scalable and is applied without noticeable overhead for large numbers of rules.

Installation

To install the package, run:

$ pip install django-blacklist

Add the blacklist application to INSTALLED_APPS:

INSTALLED_APPS = [
    ...
    'blacklist'
]

Add the blacklist_middleware middleware after AuthenticationMiddleware:

MIDDLEWARE = [
    ...
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'blacklist.middleware.blacklist_middleware',
    ...
]

Apply the blacklist database migrations:

$ python manage.py migrate blacklist

Usage

You can manage the blacklist rules from the admin. Changes take effect after a configurable time, or when the server is restarted. A rule can target a user or an IP address. You can also target IP networks (ranges) by specifying the optional prefixlen field (number of network prefix bits). Each rule has a specific duration. After that duration passes, rules expire automatically. When a request is blocked due to a matching rule:

  • Status 400 (bad request) is returned.
  • An error template is rendered. You can specify a custom one (see below), or use the one for status 400.
  • A message is logged (warning from logger blacklist.middleware for custom templates, or error from logger django.security otherwise).

Removing Expired Rules

Expired rules are not automatically removed from the database. They can be cleaned up with the included management command trim_blacklist:

$ python manage.py trim_blacklist [-c <created_days>] [-e <expired_days>]

The options -c and -e specify the minimum ages of creation and expiry, respectively.

Automatic Blacklisting

Clients can be blacklisted automatically, after exceeding a specified request rate limit. This feature requires django-ratelimit.

First, rate-limit a view by applying the @ratelimit decorator. Make sure to set block=False. Then, blacklist rate-limited clients by adding the @blacklist_ratelimited decorator. Specify the blacklist duration. For example:

from datetime import timedelta
from ratelimit.decorators import ratelimit
from blacklist.ratelimit import blacklist_ratelimited

@ratelimit(key='user_or_ip', rate='50/m', block=False)
@blacklist_ratelimited(timedelta(minutes=30))
def index(request):
    ...

Automatic rules take effect immediately. If the request comes from an authenticated user, the rule will target that user. Otherwise, it will target their IP address.


Note: The client IP address is taken from the REMOTE_ADDR value of request.META. If your application is behind one or more reverse proxies, this will, by default, always be the address of the nearest proxy. To avoid blacklisting all clients, you can set REMOTE_ADDR from the X-Forwarded-For header in middleware. However, keep in mind that this header can be forged to bypass the rate limits. To counter that, you can use the last address in that header. If you are behind two proxies, use the second to last, etc.


@blacklist_ratelimited accepts two arguments: (duration, block=True).

  • duration can be a timedelta object, or a tuple of two separate durations (for user-based and IP-based rules).
  • block specifies if the request should be blocked immediately, or passed to the view.

Automatic rules will have a comment that contains the ID of the request, which triggered the creation of the rule, and the "request line". The request ID is added only if available. Django does not generate request IDs. For that purpose, you can install django-log-request-id.

Settings

  • BLACKLIST_ENABLE - whether blacklisted clients should be blocked, and rate-limited clients should be blacklisted; default: True
  • BLACKLIST_RELOAD_PERIOD - how often to reload the blacklist, in seconds; default: 60
  • BLACKLIST_RATELIMITED_ENABLE - whether rate-limited clients should be automatically blacklisted; requires BLACKLIST_ENABLE; default: True
  • BLACKLIST_TEMPLATE - name of a custom error template to render to blocked clients; its context will contain request and exception; set to None to use the template for status 400; default: None
  • BLACKLIST_LOGGING_ENABLE - whether blocked requests should be logged (honored only if a custom error template is configured); default: True

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

django-blacklist-0.5.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_blacklist-0.5.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file django-blacklist-0.5.0.tar.gz.

File metadata

  • Download URL: django-blacklist-0.5.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.8

File hashes

Hashes for django-blacklist-0.5.0.tar.gz
Algorithm Hash digest
SHA256 c231617fd4ceff8478d1cab30f8c60513ff71178cf6aed7dc09ef2ddb0747a2d
MD5 96b3954151347bdf306e7a8e097b40ba
BLAKE2b-256 8cb6328510843477bb9c892bda8ed3d3cf96661296b9eb21f5431af5954ed694

See more details on using hashes here.

File details

Details for the file django_blacklist-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: django_blacklist-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.8

File hashes

Hashes for django_blacklist-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 15242e5c1392ef5e463700242789fe3a06e67201ee931c81926a77395a12e25a
MD5 33593ce6a102194d92c943a5b208137b
BLAKE2b-256 de256101081fbe854ab87499dd005055d023c7b465520e5710b003e3d9924ed9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page