Skip to main content

Models, views, middlewares and forms to facilitate security hardening of Django applications.

Project description

Django-Security

Build Status

This package offers a number of models, views, middlewares and forms to facilitate security hardening of Django applications.

Full documentation

Automatically generated documentation of django-security is available on Read The Docs:

Requirements

  • Python >=3.12
  • Django ~4.2

Installation

Install from Python packages repository:

pip install django-security

If you prefer the latest development version, install from django-security repository on GitHub:

git clone https://github.com/sdelements/django-security.git
cd django-security
poetry install

Adding to Django application's settings.py file:

INSTALLED_APPS = (
    ...
    'security',
    ...
)

Middleware modules can be added to MIDDLEWARE list in settings file:

MIDDLEWARE = (
    ...
    'security.middleware.LoginRequiredMiddleware',
    ...
)

Unlike the modules listed above, some other modules require configuration settings, fully described in django-security documentation. Brief description is provided below.

Middleware

Provided middleware modules will modify web application's output and input and in most cases requires no or minimum configuration.

Middleware Description Configuration
ClearSiteDataMiddleware Send Clear-Site-Data header in HTTP response for any page that has been whitelisted. Recommended. Required.
ContentSecurityPolicyMiddleware Send Content Security Policy (CSP) header in HTTP response. Recommended, requires careful tuning. Required.
LoginRequiredMiddleware Requires a user to be authenticated to view any page on the site that hasn't been white listed. Required.
MandatoryPasswordChangeMiddleware Redirects any request from an authenticated user to the password change form if that user's password has expired. Required.
NoConfidentialCachingMiddleware Adds No-Cache and No-Store headers to confidential pages. Required.
ReferrerPolicyMiddleware Specify when the browser will set a `Referer` header. Optional.
SessionExpiryPolicyMiddleware Expire sessions on browser close, and on expiry times stored in the cookie itself. Required.
ProfilingMiddleware A simple middleware to capture useful profiling information in Django. Optional.

Views

csp_report

View that allows reception of Content Security Policy violation reports sent by browsers in response to CSP header set by ``ContentSecurityPolicyMiddleware`. This should be used only if long term, continuous CSP report analysis is required. For one time CSP setup CspBuilder is much simpler.

This view can be configured to either log received reports or store them in database. See documentation for details.

require_ajax

A view decorator which ensures that the request being processed by view is an AJAX request. Example usage:

@require_ajax
def myview(request):
    ...

Models

CspReport

Content Security Policy violation report object. Only makes sense if ContentSecurityPolicyMiddleware and csp_report view are used. With this model, the reports can be then analysed in Django admin site.

PasswordExpiry

Associate a password expiry date with a user.

Logging

All django-security modules send important log messages to security facility. The application should configure a handler to receive them:

LOGGING = {
    ...
    'loggers': {
        'security': {
            'handlers': ['console',],
            'level': 'INFO',
            'propagate': False,
            'formatter': 'verbose',
        },
    },
    ...
}

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_security-1.0.4.tar.gz (25.2 kB view details)

Uploaded Source

Built Distribution

django_security-1.0.4-py3-none-any.whl (27.7 kB view details)

Uploaded Python 3

File details

Details for the file django_security-1.0.4.tar.gz.

File metadata

  • Download URL: django_security-1.0.4.tar.gz
  • Upload date:
  • Size: 25.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.12.5

File hashes

Hashes for django_security-1.0.4.tar.gz
Algorithm Hash digest
SHA256 2108f14b3e6a4e8a87c498742c18a02583614259ea2b67e1b29c420e26275d9d
MD5 2f62963270629eb02d256d2c4e03618f
BLAKE2b-256 94d353e822c8ea32ae4db9c3db23c002df48ec85bacd5328d1b8350c76feb823

See more details on using hashes here.

File details

Details for the file django_security-1.0.4-py3-none-any.whl.

File metadata

File hashes

Hashes for django_security-1.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6b3aa11bbf3ecc7fbf48bcc4e20d7cba390fcd68843e7318f41a637fda34e33c
MD5 1ed3022a682d394388eadf4b4c1ebe77
BLAKE2b-256 b32b7d6aa4797be2d69f3c53c63dd51ca58f6473e105c4507590affe8288785d

See more details on using hashes here.

Supported by

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