CSRF — Security · NestJS

When the browser sends the login cookie by itself, any page can make it send one: cross-site request forgery. Protect the routes that change state with a double-submit token that only your own page can present, bound to the session it was issued for.

What you will learn

Read the theory for CSRF

All Security lessons

All NestJS courses

loading types…

What you'll learn

  • Explain a CSRF attack step by step, and why it needs credentials the browser attaches on its own (cookies) and cannot touch a bearer-token API
  • Describe the double-submit cookie pattern: a token in a cookie and the same token in a header, which a foreign page cannot read
  • Configure csrf-csrf with a secret, a session identifier, a cookie and a header, and mount it after cookie-parser on the routes that change state
  • Read the 403 the middleware answers and know why a token from before sign-in stops working after