Categories

Back

Django Production Security Settings: In-Depth Analysis and Implementation

When deploying a Django application, you may encounter several security warnings. These warnings are crucial indicators of potential vulnerabilities in your application's configuration. Let's examine each warning in detail and discuss how to address them.

└──╼ $./manage.py check --deploy
System check identified some issues:

WARNINGS:
?: (security.W004) You have not set a value for the SECURE_HSTS_SECONDS setting. If your entire site is served only over SSL, you may want to consider setting a value and enabling HTTP Strict Transport Security. Be sure to read the documentation first; enabling HSTS carelessly can cause serious, irreversible problems.
?: (security.W008) Your SECURE_SSL_REDIRECT setting is not set to True. Unless your site should be available over both SSL and non-SSL connections, you may want to either set this setting True or configure a load balancer or reverse-proxy server to redirect all connections to HTTPS.
?: (security.W009) Your SECRET_KEY has less than 50 characters, less than 5 unique characters, or it's prefixed with 'django-insecure-' indicating that it was generated automatically by Django. Please generate a long and random value, otherwise many of Django's security-critical features will be vulnerable to attack.
?: (security.W012) SESSION_COOKIE_SECURE is not set to True. Using a secure-only session cookie makes it more difficult for network traffic sniffers to hijack user sessions.
?: (security.W016) You have 'django.middleware.csrf.CsrfViewMiddleware' in your MIDDLEWARE, but you have not set CSRF_COOKIE_SECURE to True. Using a secure-only CSRF cookie makes it more difficult for network traffic sniffers to steal the CSRF token.
?: (security.W018) You should not have DEBUG set to True in deployment.

System check identified 6 issues (0 silenced).

You can check them by running:

python manage.py check --deploy

SECURE_HSTS_SECONDS

HTTP Strict Transport Security (HSTS) is a powerful security feature that enhances website protection against various cyber threats. It functions by instructing web browsers to exclusively connect to a website using HTTPS, even if a user explicitly types "http://" in the address bar. This mechanism helps safeguard against protocol downgrade attacks, cookie hijacking, and man-in-the-middle attacks. HSTS is an IETF standards track protocol, specified in RFC 6797, that allows web servers to declare a policy for secure HTTPS connections. The server communicates this policy to the user agent (like a web browser) via an HTTP response header field named Strict-Transport-Security. This header specifies a time period during which the user agent should only access the server securely. Many websites implementing HSTS reject clear text HTTP connections entirely or automatically redirect users to HTTPS. It's important to note that HSTS protection typically applies only after a user's first visit to the site, following the "trust on first use" principle. When a user enters an HTTP URL for an HSTS-enabled site, their web browser automatically upgrades the connection to HTTPS without making an initial HTTP request, thereby preventing potential HTTP-based attacks. However, this also means that user agents incapable of TLS connections won't be able to access HSTS-enabled sites.

SECURE_HSTS_SECONDS = 31536000  # 1 year

Considerations:

  • Only enable HSTS if you're certain your site will always be served over HTTPS.
  • Start with a short duration (e.g., 3600 for 1 hour) and gradually increase it.
  • Once set, it's difficult to revert as browsers will remember the setting.

Further Reading:

SECURE_SSL_REDIRECT

This setting ensures that all HTTP requests are redirected to HTTPS. It's a crucial first step in ensuring that your site is only accessed over a secure connection.

SECURE_SSL_REDIRECT = True

Considerations:

  • If you're using a reverse proxy or load balancer, you might want to handle SSL redirection there instead.
  • Ensure your site is properly set up for HTTPS before enabling this.

SECRET_KEY

The SECRET_KEY is used for cryptographic signing in Django. It's critical for the security of sessions, passwords, and tokens. A weak SECRET_KEY can compromise many of Django's security features.

You can generate one by using:

from django.core.management.utils import get_random_secret_key


get_random_secret_key()

# generate something like this: '%qrhrb3pm!a6q)9e&+kp+n6b&(^uks%#lq-*wi77nc8o*7rlnf'

also worth mentioning is:

import secrets
SECRET_KEY = secrets.token_urlsafe(50)

In production, use an environment variable.

Never expose your SECRET_KEY in version control or public repositories. Rotate your SECRET_KEY periodically for enhanced security. How to rotate Django SECRET_KEY you see here Rotate Django Secret key

SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY')

SESSION_COOKIE_SECURE

This setting ensures that the session cookie is only sent over HTTPS connections. It prevents the cookie from being transmitted over insecure connections, making it harder for attackers to intercept.

SESSION_COOKIE_SECURE = True

Only enable this if your site is fully served over HTTPS. If enabled and users access your site over HTTP, they won't be able to log in or maintain their session.

CSRF_COOKIE_SECURE

Similar to SESSION_COOKIE_SECURE, this setting ensures that the CSRF token cookie is only sent over HTTPS connections. This protects against cross-site request forgery attacks over insecure connections.

CSRF_COOKIE_SECURE = True

As with SESSION_COOKIE_SECURE, only enable this if your site is fully served over HTTPS. If enabled and users access your site over HTTP, CSRF protection won't work correctly.

DEBUG

The DEBUG setting, when True, provides detailed error pages and enables certain debugging tools. While invaluable during development, it can expose sensitive information in a production environment.

DEBUG = False

Or, using an environment variable:

DEBUG = os.environ.get('DJANGO_DEBUG', 'False') == 'True'

Always set DEBUG to False in production and ensure you have proper error logging in place when DEBUG is False. Consider using different settings files for development and production environments.

By addressing these settings, you significantly enhance the security of your Django application in a production environment. Remember to test your application thoroughly after implementing these changes to ensure everything functions as expected under these secure settings.

Stay in the Loop!

Join our weekly byte-sized updates. We promise not to overflow your inbox!