Skip to content

Cross-Site Request Forgery

CSRF

Medium — good to knowSecurity

ELI5 — The Vibe Check

CSRF tricks your browser into making requests to another site while you're logged in. Imagine a malicious website with an invisible button that clicks 'transfer $1000' on your bank's site — using your browser's saved session. Your bank thinks YOU clicked it. CSRF tokens prevent this.

Real Talk

Cross-Site Request Forgery (CSRF) exploits the browser's automatic cookie sending behavior. Attackers craft requests that impersonate authenticated users. Prevention involves CSRF tokens (random values embedded in forms that attackers can't read), SameSite cookie attributes, and checking the Origin header.

Show Me The Code

// Express with csurf middleware
import csrf from 'csurf';
app.use(csrf({ cookie: true }));

// In your form handler route:
app.get('/form', (req, res) => {
  res.render('form', { csrfToken: req.csrfToken() });
});

// In your HTML form:
// <input type="hidden" name="_csrf" value="<%= csrfToken %>">

When You'll Hear This

"The delete endpoint needs CSRF protection." / "Set SameSite=Strict on session cookies to block CSRF."

Made with passive-aggressive love by manoga.digital. Powered by Claude.