Skip to main content

Adds middleware and context processors to give some protection against the BREACH attack in Django.

Project description

Basic mitigation against the BREACH attack for Django projects.

When combined with rate limiting in your web-server, or by using something like django-ratelimit, the techniques here should provide at least some protection against the BREACH attack.

https://badge.fury.io/py/django-debreach.png https://travis-ci.org/lpomfrey/django-debreach.png?branch=master https://coveralls.io/repos/lpomfrey/django-debreach/badge.png?branch=master https://pypip.in/d/django-debreach/badge.png

Installation

Install from PyPI using:

$ pip install django-debreach

If installing from git you’ll also need to install the PyCrypto library.

Add to your INSTALLED_APPS:

INSTALLED_APPS = (
    ...
    'debreach',
    ...
)

Configuration

CSRF token masking

To mask CSRF tokens in the template add the debreach.context_processors.csrf context processor to the end of your TEMPLATE_CONTEXT_PROCESSORS:

TEMPLATE_CONTEXT_PROCESSORS = (
    ...
    'debreach.context_processors.csrf',
)

And add the debreach.middleware.CSRFCryptMiddleware to your middleware, before django.middleware.csrf.CSRFMiddleware:

MIDDLEWARE_CLASSES = (
    'debreach.middleware.CSRFCryptMiddleware',
    ...
    'django.middleware.csrf.CSRFMiddleware',
    ...
)

This works by AES encrypting the CSRF token when it is added to the template, so that {% csrf_token %} now produces a hidden field with a value that is "<random-crypt-text>$<actual-csrf-token-encrypted-with-random-crypt-text>". Then, when the form is POSTed, the middleware decrypts the CSRF token back into it’s original form. This ensures that the CSRF content is never the same between requests. If you are passing the token using the X-CSRFToken header (e.g. using XHR) that header will also be processed in the same way.

Note that values that are unencrypted, or rather, don’t contain a delimiting $, will be left unmodified.

Content length modification

django-debreach also enables you to counter the BREACH attack by randomising the content length of each response. This is acheived by adding a random string of between 12 and 25 characters as a comment to the end of the HTML content. Note that this will only be applied to responses with a content type of text/html.

To enable content length modification for all responses, add the debreach.middleware.RandomCommentMiddleware to the start of your middleware, but after the GzipMiddleware if you are using that.:

MIDDLEWARE_CLASSES = (
    'debreach.middleware.RandomCommentMiddleware',
    ...
)

or:

MIDDLEWARE_CLASSES = (
    'django.middleware.gzip.GzipMiddleware',
    'debreach.middleware.RandomCommentMiddleware',
    ...
)

If you wish to disable this feature for selected views, simply apply the debreach.decorators.random_comment_exempt decorator to the view.

If you only want to protect a subset of views with content length modification then it may be easier to not use the middleware, but to selectively apply the debreach.decorators.append_random_comment decorator to the views you want protected.

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-debreach-1.0.0.tar.gz (10.0 kB view details)

Uploaded Source

File details

Details for the file django-debreach-1.0.0.tar.gz.

File metadata

File hashes

Hashes for django-debreach-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e0240729d38c52b5455d24508bf6b98b336e16451f94591f4654ad9d66fef84d
MD5 a9c53392f4d0b8354663645aa718b3be
BLAKE2b-256 f03f2ca9e122e15463b95ae5995d544cb9dd4540474fb4052b6f6b0686416c16

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