python-django-web-server-security

This document is designed as a security spec that supports:

Django (Python) Web Security Spec (Django 6.0.x, Python 3.x)

This document is designed as a security spec that supports:

  1. Secure-by-default code generation for new Django code.
  2. Security review / vulnerability hunting in existing Django code (passive “notice issues while working” and active “scan the repo and report findings”).

It is intentionally written as a set of normative requirements (“MUST/SHOULD/MAY”) plus audit rules (what bad patterns look like, how to detect them, and how to fix/mitigate them).


0) Safety, boundaries, and anti-abuse constraints (MUST FOLLOW)


1) Operating modes

1.1 Generation mode (default)

When asked to write new Django code or modify existing code:

1.2 Passive review mode (always on while editing)

While working anywhere in a Django repo (even if the user did not ask for a security scan):

1.3 Active audit mode (explicit scan request)

When the user asks to “scan”, “audit”, or “hunt for vulns”:

Recommended audit order:

  1. Deployment entrypoints (ASGI/WSGI), Dockerfiles, Procfiles, systemd units, platform manifests.
  2. settings.py and environment-specific settings modules.
  3. Middleware ordering and enabled protections.
  4. Authn/authz (login, session management, permissions, admin).
  5. CSRF protections and state-changing endpoints.
  6. Templates and XSS.
  7. File handling (uploads/downloads/static/media) and path traversal.
  8. Injection classes (SQL, command execution, unsafe deserialization).
  9. Outbound requests (SSRF).
  10. Redirect handling (open redirects) + CORS + security headers (CSP, HSTS, etc.).
  11. Dependency/pinning and patch posture.

2) Definitions and review guidance

2.1 Untrusted input (treat as attacker-controlled unless proven otherwise)

Examples include:

Django explicitly emphasizes “never trust user-controlled data” and recommends using forms/validation. ([Django Project][2])

2.2 State-changing request

A request is state-changing if it can create/update/delete data, change auth/session state, trigger side effects (purchase, email send, webhook send), or initiate privileged actions.

2.3 Required audit finding format

For each issue found, output:


3) Secure baseline: minimum production configuration (MUST in production)

This is the smallest “production baseline” that prevents common Django misconfigurations. Django provides a “Deployment checklist” and recommends running manage.py check --deploy against production settings. ([Django Project][1])

3.1 Settings management pattern (SHOULD)

3.2 Minimum baseline targets (production)


4) Rules (generation + audit)

Each rule contains: required practice, insecure patterns, detection hints, and remediation.

DJANGO-DEPLOY-001: Do not use Django’s development server in production

Severity: High (if production)

Required:

Insecure patterns:

Detection hints:

Fix:

Note:


DJANGO-DEPLOY-002: DEBUG MUST be disabled in production

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


DJANGO-CONFIG-001: SECRET_KEY must be strong, secret, and rotated safely

Severity: High (Critical if missing in production with signing/sessions)

Required:

Insecure patterns:

Detection hints:

Fix:


DJANGO-HOST-001: Host header must be validated (ALLOWED_HOSTS must be strict)

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


DJANGO-HTTPS-001: If TLS is used cookie transport must be secured

Severity: High (Critical for auth-enabled apps)

NOTE: Only enforce this if TLS is enabled, as it will break non-TLS applications

If using TLS:

Insecure patterns:

Detection hints:

Fix:


DJANGO-PROXY-001: Reverse proxy trust must be configured correctly (SECURE_PROXY_SSL_HEADER)

Severity: Medium (when behind a TLS proxy)

Required:

Insecure patterns:

Detection hints:

Fix:


DJANGO-SESS-001: Session cookies must use secure attributes in production

Severity: Medium (Only if TLS enabled)

Required (production, HTTPS):

Insecure patterns:

IMPORTANT NOTE: Only set Secure in production environment when TLS is configured. When running in a local dev environment over HTTP, do not set Secure property on cookies. You should do this conditionally based on if the app is running in production mode. You should also include a property like SESSION_COOKIE_SECURE which can be used to disable Secure cookies when testing over HTTP.

Detection hints:

Fix:


DJANGO-SESS-002: CSRF cookie settings must be deliberate (HttpOnly has tradeoffs)

Severity: Medium

Required:

Insecure patterns:

Detection hints:

Fix:


DJANGO-CSRF-001: Cookie-authenticated state-changing requests MUST be CSRF-protected

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:

Notes:


DJANGO-XSS-001: Prevent reflected/stored XSS in templates and HTML generation

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


DJANGO-TEMPLATE-001: Never render untrusted template source strings

Severity: High to Critical (depends on context and exposure)

Required:

Insecure patterns:

Detection hints:

Fix:


DJANGO-SQL-001: Prevent SQL injection (use ORM or parameterized raw SQL)

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


DJANGO-CMD-001: Prevent OS command injection

Severity: Critical to High (depends on exposure)

Required:

Insecure patterns:

Detection hints:

Fix:


DJANGO-UPLOAD-001: File uploads must be validated, stored safely, and served safely

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


DJANGO-PATH-001: Prevent path traversal and unsafe file serving (static/media separation)

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


DJANGO-REDIRECT-001: Prevent open redirects (next, return_to, redirect)

Severity: Medium (High when combined with auth flows)

Required:

Insecure patterns:

Detection hints:

Fix:


DJANGO-HEADERS-001: Enable essential security headers (SecurityMiddleware + clickjacking protection)

Severity: Medium to High

Required:

Insecure patterns:

Detection hints:

Fix:

NOTE:


DJANGO-CSP-001: Deploy a Content Security Policy (CSP) as defense-in-depth

Severity: Medium (High for apps rendering untrusted content)

NOTE: It is most important to set the CSP's script-src. All other directives are not as important and can generally be excluded for the ease of development.

Required:

Insecure patterns:

Detection hints:

Fix:


DJANGO-AUTH-001: Password storage must use Django’s secure hashers; password policy must be configured

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


DJANGO-AUTHZ-001: Authorization must be explicit and consistent

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


DJANGO-ADMIN-001: Django admin must be treated as a high-value target

Severity: High

Required:

Insecure patterns:

Detection hints:

Fix:


DJANGO-LOG-001: Logging and error reporting must not leak secrets

Severity: Medium to High

Required:

Insecure patterns:

Detection hints:

Fix:


DJANGO-SUPPLY-001: Dependency and patch hygiene (Django + security-critical deps)

Severity: Medium (High if known vulnerable versions)

Required:

Detection hints:

Fix:


5) Practical scanning heuristics (how to “hunt”)

When actively scanning, use these high-signal patterns:

Always try to confirm:


6) Sources (accessed 2026-01-27)

Primary Django documentation:

- Django Downloads (current stable & supported branches): https://www.djangoproject.com/download/
- Django 6.0 Release Notes: https://docs.djangoproject.com/en/6.0/releases/6.0/
- Django: Deployment checklist (incl. check --deploy, runserver warning, HTTPS/cookies guidance): https://docs.djangoproject.com/en/6.0/howto/deployment/checklist/
- Django: Settings reference (SecurityMiddleware settings, cookies, SECRET_KEY_FALLBACKS, CSP settings): https://docs.djangoproject.com/en/6.0/ref/settings/
- Django: Security in Django (XSS/CSRF/SQLi/clickjacking/HTTPS/host header validation/uploads/CSP): https://docs.djangoproject.com/en/6.0/topics/security/
- Django: CSRF how-to (middleware, csrf_token usage, AJAX header patterns, csrf_exempt cautions): https://docs.djangoproject.com/en/6.0/howto/csrf/
- Django: Performing raw SQL queries (parameterization guidance): https://docs.djangoproject.com/en/6.0/topics/db/sql/
- Django: QuerySet API reference (extra() cautions; “do not quote placeholders” guidance): https://docs.djangoproject.com/en/6.0/ref/models/querysets/
- Django: Template built-ins (autoescape tag): https://docs.djangoproject.com/en/6.0/ref/templates/builtins/
- Django: Template language reference (turning off autoescape & risks): https://docs.djangoproject.com/en/6.0/ref/templates/language/
- Django: Utilities reference (e.g., format_html): https://docs.djangoproject.com/en/6.0/ref/utils/

OWASP:

- OWASP Cheat Sheet Series: Django Security Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/Django_Security_Cheat_Sheet.html

[1]: https://docs.djangoproject.com/en/6.0/howto/deployment/checklist/ "https://docs.djangoproject.com/en/6.0/howto/deployment/checklist/" [2]: https://docs.djangoproject.com/en/6.0/topics/security/ "Security in Django | Django documentation | Django" [3]: https://docs.djangoproject.com/en/6.0/ref/settings/ "Settings | Django documentation | Django" [4]: https://docs.djangoproject.com/en/6.0/howto/csrf/ "How to use Django’s CSRF protection | Django documentation | Django" [5]: https://docs.djangoproject.com/en/6.0/ref/templates/builtins/ "https://docs.djangoproject.com/en/6.0/ref/templates/builtins/" [6]: https://docs.djangoproject.com/en/6.0/ref/utils/ "https://docs.djangoproject.com/en/6.0/ref/utils/" [7]: https://docs.djangoproject.com/en/6.0/topics/db/sql/ "https://docs.djangoproject.com/en/6.0/topics/db/sql/" [8]: https://docs.djangoproject.com/en/6.0/ref/models/querysets/ "https://docs.djangoproject.com/en/6.0/ref/models/querysets/" [9]: https://www.djangoproject.com/download/ "Download Django | Django"