Django 6.1.3 on SQLite (in-memory): ch05-security ALLOWED_HOSTS, SecurityMiddleware redirects, and SECURE_PROXY_SSL_HEADER MIDDLEWARE=[] Host='evil.example' on /quiet/ (no get_host) -> 200 OK (no host read) MIDDLEWARE=[] Host='evil.example' on /host/ (calls get_host) -> 400 Bad Request logger=django.security.DisallowedHost desk/ default stack Host='evil.example' on /wire/status/ (no get_host) -> 400 Bad Request SECURE_SSL_REDIRECT=True Host='evil.example' -> 400 Bad Request (SecurityMiddleware calls get_host -> django.security.DisallowedHost) proxy HTTP + X-Forwarded-Proto=https (no SECURE_PROXY_SSL_HEADER) -> 301 Moved Permanently Location='https://desk.example/wire/status/' HSTS=None proxy HTTP + SECURE_PROXY_SSL_HEADER -> 200 OK HSTS='max-age=31536000; includeSubDomains' nosniff='nosniff' Referrer='same-origin' COOP='same-origin' X-Forwarded-Proto='https, http' -> request.scheme='https' is_secure=True CsrfViewMiddleware: BREACH masking, Origin vs HTTPS Referer, and POST checks BREACH masking: len(secret)=32 len(t1)=64 len(t2)=64 distinct=True unmask(t1)==secret:True unmask(t2)==secret:True mask arithmetic (base-62): secret[0]='a'(0) + mask[0]='C'(28) mod 62 = 28 -> 'C' GET /wire/status/ (no csrf tag): Vary=None set_csrf_cookie=False GET /submit/ (uses csrf_token): Vary='Cookie' set_csrf_cookie=True POST valid cookie + masked form token (HTTP, no Origin/Referer) -> 200 OK (accepted) POST valid cookie + X-CSRFToken header (AJAX) -> 200 OK (accepted) POST missing CSRF cookie -> 403 Forbidden (Forbidden (CSRF cookie not set.): /submit/) POST cookie present, missing token -> 403 Forbidden (Forbidden (CSRF token missing.): /submit/) POST mismatched Origin header (evil.example) -> 403 Forbidden (Forbidden (Origin checking failed - https://evil.example does not match any trusted origins.): /submit/) POST over HTTPS with NO Origin and NO Referer -> 403 Forbidden (Forbidden (Referer checking failed - no Referer.): /submit/) POST over HTTPS with matching https://desk.example Referer -> 200 OK (accepted) Signer, TimestampSigner, SECRET_KEY_FALLBACKS, and Django 6.1 cookie v2 salt Signer(key-v1, salt='desk.invite').sign('ada') -> 'ada:MAN58VCXNviPRcJtIpGmHD5ouSrhlIhhzvUbAI_VCzM' rotated Signer(key-v2, fallback=[key-v1]).unsign(tok_v1) -> 'ada' rotated Signer signs new values with key-v2 (tok_v2 != tok_v1): True wrong salt replay -> BadSignature: Signature "MAN58VCXNviPRcJtIpGmHD5ouSrhlIhhzvUbAI_VCzM" does not match after retiring key-v1 -> BadSignature: Signature "MAN58VCXNviPRcJtIpGmHD5ouSrhlIhhzvUbAI_VCzM" does not match TimestampSigner at t0=1764000000: token='edition-42:1vNYyu:yLPNeIM8-Ux5s9EWrc6TeCqT6UNXhiahQrk9VbI8tOc'; unsign(+30s, max_age=60) -> 'edition-42' TimestampSigner unsign(+90s, max_age=60) -> SignatureExpired: Signature age 90.0 > 60 seconds legacy cookie salt collision: ('ab','c')='abc' == ('a','bc')='abc' -> True Django 6.1 v2 cookie salt: ('ab','c')='django.http.cookies.v2:1:cab' != ('a','bc')='django.http.cookies.v2:2:bca' -> True compressed dumps starts with '.': True; tampered payload -> BadSignature Django 6 ContentSecurityPolicyMiddleware and LazyNonce view does NOT read csp_nonce -> bool_in_view=False Content-Security-Policy="default-src 'self'; script-src 'self'" view READS csp_nonce -> before=False after=True len=22 nonce_in_header=True Content-Security-Policy="default-src 'self'; script-src 'self' 'nonce-<22-char-nonce>'" @csp_override(default-src 'none') -> Content-Security-Policy="default-src 'none'" XFrameOptionsMiddleware, SafeString propagation, mark_safe, and format_html X-Frame-Options: default='DENY' @sameorigin='SAMEORIGIN' @exempt=None mark_safe + mark_safe -> type=SafeString safe=True rendered='ok' mark_safe + untrusted str -> type=str safe=False rendered='<b><script>alert(1)</script>' mark_safe('%s') % str -> type=str safe=False rendered='<b><script>alert(1)</script></b>' mark_safe(f'{x}') -> type=SafeString safe=True rendered='' <-- XSS! format_html('{}', x) -> type=SafeString safe=True rendered='<script>alert(1)</script>' Django 6.1 format_html(f'...') with 0 args -> TypeError: args or kwargs must be provided. Where Django defaults stop: mass assignment and misordered middleware StoryForm (explicit fields): is_valid=True status=Story.Status.DRAFT (crafted 'published' ignored) UnsafeStoryForm (fields='__all__'): is_valid=True status='published' (MASS ASSIGNMENT!) 429 short-circuit ABOVE SecurityMiddleware -> 429 Too Many Requests nosniff=None XFO=None 429 short-circuit BELOW SecurityMiddleware -> 429 Too Many Requests nosniff='nosniff' XFO='DENY' CSRF_USE_SESSIONS with Csrf before Session -> ImproperlyConfigured: CSRF_USE_SESSIONS is enabled, but request.session is not set. SessionMiddleware must appear before CsrfViewMiddleware in MIDDLEWARE.