Skip to the page

Data access and security · mid

CSRF, password hashing, secrets and login throttling

Cookies need CSRF protection and tokens in headers do not. Passwords are stored as slow salted hashes, secrets live outside the code, and throttling must use a shared cache and the real client address.

The notes

What you must know

  • CSRF: another site makes the browser send a request with your cookies. Django's CsrfViewMiddleware needs a token (csrftoken cookie) in a form field or the X-CSRFToken header on unsafe methods, and checks Origin against the host and CSRF_TRUSTED_ORIGINS (with the scheme).
  • DRF SessionAuthentication enforces CSRF for logged-in users; a React app sends X-CSRFToken. A token in the Authorization header is not sent by the browser on its own, so CSRF does not apply (XSS does).
  • SameSite=Lax blocks most cross-site POSTs but not every case; keep the token check. @csrf_exempt only on endpoints that authenticate another way, such as signed webhooks. GET must never change data.
  • FastAPI has no built-in CSRF protection. With cookie auth use a double-submit token or a library, check Origin, and set SameSite and Secure.
  • Passwords: store only slow salted hashes. Django's default is PBKDF2-SHA256; its docs recommend Argon2 first in PASSWORD_HASHERS. FastAPI's docs use pwdlib with Argon2. Django re-hashes on login; keep old hashers listed.
  • Secrets live in environment variables or a secret manager, read with django-environ or pydantic-settings (SecretStr hides values). .env files only on laptops and in .gitignore. A leaked SECRET_KEY: rotate it, keep the old one in SECRET_KEY_FALLBACKS briefly, rotate other secrets in the same repo, and treat them as exposed even after cleaning git history.
  • Login throttling at 5 a minute still allowed 120: DRF stores counters in Django's cache, and the default LocMemCache is per process (3 pods x 8 workers = 24 counters). Use Redis. DRF reads X-Forwarded-For, which the client controls; set NUM_PROXIES to the real number of proxies.
  • Better design: edge limits (WAF or Nginx limit_req), limits per IP and per account, growing delays or CAPTCHA, temporary lockouts that cannot lock real users out forever, MFA or OTP for risky logins, and alerts on failed logins.

The red pen

Common traps: mistake → fix

  • Mistake: SameSite makes CSRF tokens unnecessary

    Fix: keep the token check.

  • Mistake: A JWT in a cookie needs no CSRF protection

    Fix: cookies are sent automatically.

  • Mistake: Throttling works the same in every pod

    Fix: a per-process cache gives each worker its own counter.

  • Mistake: Cleaning git history fixes a leaked secret

    Fix: it is still exposed; rotate it.

Easy to hard

Questions interviewers ask, easy to hard

  1. How does Django protect against CSRF?
  2. Our React app calls a DRF API with session cookies. What do we need?
  3. How should a Django or FastAPI app store passwords?
  4. Where do secrets live, and what do you do after a leak?
  5. Why does an attacker still get 120 login tries a minute after we added throttling?

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview