Boundary by OraXLabs · Public source review · 2026-11-23
Django CSRF
request admission.
Boundary reviewed the actual CSRF middleware and defaults in Django 6.1.3 at a pinned commit. The source separates origin checks from token validation and applies HTTPS Referer checks conditionally. Three documented control observations explain what the code does, its intentional exceptions and the application settings that still need verification. No new Django vulnerability, runtime test or deployment assurance is claimed.
Independent educational review. This work was not commissioned or endorsed by Django. It records existing controls and application checks; it is not a newly disclosed vulnerability.
Executive overview
Use the middleware as a layered control and verify its application/deployment conditions. This selected static review found no supported new vulnerability.
- Qualified new vulnerabilities
- 0
- Source control observations
- 3
- Executed tests
- 0
- Remediation status
- Not rechecked · No framework patch required by this sample
Scope and method
Release: Django 6.1.3. Snapshot: f16e25dcb9fe55734ba4ea2b00ac34405b7df74a. Review date: 2026-11-23.
We read the selected middleware and global defaults, with the CSRF, cookie, origin and proxy-setting sections of documentation from the same commit. Source bytes were decoded as text and hashed. No target code was imported, evaluated or installed; no tests, browser attacks or live requests were run.
Reviewed files
The settings document was archived for source identity; unrelated settings sections were not reviewed.
Excluded
- Django utility implementation internals and HttpRequest.get_host/is_secure internals
- Application views, middleware order, runtime configuration, session backend, proxy/certificate/browser behavior
- Installed wheels, package build/release signature authenticity and deployment equivalence
- Vulnerability exploitation or reproduction procedures, target services, customer data and credentials
Pinned source describes selected code, not the safety of all Django behavior or the configuration of a deployed application.
Control observations
These are existing-control observations and integration criteria. Severity is not applicable: no defect or deployed misconfiguration was established.
DJ-CSRF-C01 · Existing control
Unsafe requests pass origin checks and a token check as separate controls
Classification: Source-supported existing control; not a vulnerability. Evidence: Source-supported; deployment conditional.
For non-exempt unsafe methods, process_view checks an available Origin against current scheme/host or configured trusted origins; absent Origin on a secure request invokes strict Referer validation. Both branches proceed to _check_token before acceptance. Matching an allowed origin is not a replacement for a valid token.
Meaning and conditions
The source has layered browser-origin and token admission checks. Coverage depends on middleware/decorator activation, accurate secure-request detection and deliberate trusted-origin configuration.
Source evidence
- django/middleware/csrf.py:414–470 · CsrfViewMiddleware.process_view
- django/middleware/csrf.py:271–340 · _origin_verified / _check_referer
- django/middleware/csrf.py:181–198 · allowed_origins_exact / allowed_origin_subdomains
Counterevidence and limits
- GET/HEAD/OPTIONS/TRACE are intentionally accepted before these checks; application safe-method handlers must not perform state changes.
- csrf_exempt, prior csrf_processing_done and the explicit test-client bypass are recognized exceptions; this sample does not approve applying them to arbitrary write endpoints.
- HTTP without Origin does not take the HTTPS Referer branch; do not describe Referer enforcement as universal. No request.get_host/is_secure/helper implementation was independently audited.
Recommended action
Keep unsafe state changes on protected unsafe-method views; verify middleware activation and review exemption/trusted-origin decisions in the actual application.
Verification criterion
Authorized defensive application regression checks should show intended same-origin and trusted-origin unsafe flows require a valid token, and unintended origins or missing/incorrect tokens are rejected. Confirm safe-method handlers are side-effect free.
Status: Not rechecked — static source reading only; no application, HTTP request, browser or tests executed.
DJ-CSRF-C02 · Existing control
Token format validation precedes unmasking and the comparison-helper call
Classification: Source-supported existing control; not a vulnerability. Evidence: Source-supported call path; helper internals not reviewed.
_check_token obtains a cookie/session secret, rejects a missing secret, reads the POST field or configured header, checks token length/allowed characters, and only then calls _does_token_match. That function normalizes masked tokens before delegating comparison to constant_time_compare.
Meaning and conditions
The reviewed caller order respects the normalization function's format assumptions. Masked and unmasked token forms are intentional compatibility behavior; the source compares the underlying secret rather than requiring a stable full masked token.
Source evidence
- django/middleware/csrf.py:130–157 · _check_token_format / _does_token_match
- django/middleware/csrf.py:221–250 · _get_secret
- django/middleware/csrf.py:350–399 · _check_token
- django/middleware/csrf.py:55–81 · _get_new_csrf_string / _mask_cipher_secret / _unmask_cipher_token
Counterevidence and limits
- POST-field lookup and header fallback are distinct; PUT/DELETE rely on the configured header in this selected path.
- Randomness and comparison utilities are imported dependencies; their internals, cryptographic properties, timing and runtime behavior were not independently tested or certified here.
- A token mask is not an authorization decision, user identity or an XSS defense. Same-site trust, cookie/session handling and application controls still matter.
Recommended action
Use the framework token mechanism rather than implementing custom masking/comparison; preserve format validation before normalization when extending request handling.
Verification criterion
An authorized project test should cover normal masked/unmasked token acceptance, missing/invalid-format token rejection and the actual form/header path used by the application, without relying on the default CSRF-disabled test client.
Status: Not rechecked — no cryptographic assessment, timing experiment or executed token tests.
DJ-CSRF-C03 · Existing control
Deployment settings determine cookie and HTTPS-related coverage
Classification: Conditional integration checklist; not a discovered Django defect. Evidence: Source-supported defaults and documented conditions.
Cookie issuance uses configured Secure/HttpOnly/SameSite attributes; session-backed CSRF storage requires SessionMiddleware before CsrfViewMiddleware. Global defaults include an empty trusted-origin list, cookie Secure=False, session storage disabled and SECURE_PROXY_SSL_HEADER=None. These are defaults, not evidence of any deployed settings.
Meaning and conditions
The middleware source cannot prove a real site's HTTPS termination, middleware order, trusted subdomain ownership or effective cookie policy. A secure-request classification error can change which documented Referer branch is selected, so proxy trust must be established rather than guessed.
Source evidence
- django/conf/global_settings.py:473–482 · SECURE_PROXY_SSL_HEADER / MIDDLEWARE defaults
- django/conf/global_settings.py:573–585 · CSRF settings defaults
- django/middleware/csrf.py:221–267 · _get_secret / _set_csrf_cookie
- docs/ref/settings.txt:387–410 · CSRF_COOKIE_SECURE / CSRF_USE_SESSIONS
- docs/ref/settings.txt:2682–2747 · SECURE_PROXY_SSL_HEADER
- docs/ref/csrf.txt:94–130 · HTTPS/Host/XSS and subdomain limitations
Counterevidence and limits
- Django explicitly documents HTTPS/Host validation and XSS assumptions; this review does not claim CSRF middleware cures XSS, insecure transport or untrusted subdomain cookie control.
- Global MIDDLEWARE=[] is a framework default, not proof that a generated or customer project lacks CSRF middleware.
- CSRF_COOKIE_HTTPONLY=False is not classified as a defect: frontend token-reading integration may intentionally use cookie access, and cookie versus session storage is a documented choice.
Recommended action
Inspect the actual project middleware and deployment configuration. For HTTPS deployments choose secure cookie settings and set proxy-header trust only when the controlled proxy strips and sets that header correctly. Minimize trusted origins and document session-backed middleware order where used.
Verification criterion
In an authorized staging environment inspect effective middleware/settings and response-cookie attributes, and establish Django's secure-request classification behind the real proxy. Check project-specific browser CSRF behavior; source defaults alone are insufficient.
Status: Not rechecked — no deployment configuration, proxy, session backend, cookies or live systems were accessed.
Priorities and closure
- Review actual middleware activation and exemption decisions (DJ-CSRF-C01).
- Check application token integration and format-validation invariants (DJ-CSRF-C02).
- Verify effective HTTPS/proxy/cookie/session/trusted-origin settings in authorized staging (DJ-CSRF-C03).
Not rechecked. No patch authored, fixed-in-source or verified-fixed status asserted; follow-up criteria are recommendations, not completed tests.
Source identity
SHA-256 identifies the reviewed bytes. A checksum is not a security certification or proof that an application runs this source.
- django/middleware/csrf.py
ae1d82bd6dabbd7f3f67d8294b9f2f2d57a76693fe71edb90f41fd57b6887ebf- django/conf/global_settings.py
3c82ce45a0f468df5daea1b1ff5194e604712477ab8d2d07c267b8acd02e84a5- docs/ref/csrf.txt
75d54fe68f85eaf757838884640ceff90fa9d9df6ef7f867366c160752c30c1f- docs/ref/settings.txt
6d179e8542e023d27078c6eda8af2246c2292d01f5dc5d3618575b95b453ff71