awesome-everything RU
↑ Back to the climb

Security

CSRF: code and config reading

Crux Read real cookie configs and route handlers, predict the CSRF behaviour, and pick the highest-leverage fix a senior reviewer would make first.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at senior altitude — in orbit
◷ 14 min

CSRF bugs live in cookie attributes and route handlers, not in prose. Read each snippet, predict whether a forged request goes through, and choose the fix a senior reviewer would flag first.

Goal

Practise the loop you run in every security review: read the cookie config and the handler, predict where a forged write lands, and reach for the highest-leverage fix before reasoning about exotic bypasses.

// Express session cookie
app.use(session({
  secret: process.env.SECRET,
  cookie: {
    httpOnly: true,
    secure: true,
    sameSite: 'none',   // <-- needed so the embedded widget on partner.com works
  },
}));
Quiz

The team set sameSite: 'none' so the session works inside an iframe on partner.com. What CSRF posture does this create, and what must follow?

Snippet 2 — the double-submit check

function verifyCsrf(req, res, next) {
  const cookieToken = req.cookies['csrf'];
  const headerToken = req.get('X-CSRF-Token');
  if (cookieToken && cookieToken === headerToken) return next();
  return res.status(403).send('CSRF check failed');
}
Quiz

This plain double-submit check passes when the cookie value equals the header value. What's the weakness a senior reviewer flags, and the fix?

Snippet 3 — the route handler

// "Unsubscribe with one click from the email link"
app.get('/account/email-prefs/unsubscribe', requireSession, (req, res) => {
  db.users.update(req.session.userId, { subscribed: false });  // mutates!
  res.send('You are unsubscribed.');
});
Quiz

Sessions here are SameSite=Lax and there's a CSRF token on all POST routes. Is this GET handler safe? Why or why not?

Snippet 4 — the Origin check

function checkOrigin(req, res, next) {
  const origin = req.get('Origin');
  if (origin === 'https://app.example.com') return next();
  return res.status(403).send('Bad origin');
}
Quiz

Used as the ONLY CSRF defense on state-changing routes, what breaks, and how should this layer be used?

Recap

Every CSRF review is read in cookie attributes and handlers: SameSite=None opts you back into the full pre-2020 surface and mandates a token defense; plain double-submit trusts that only you can write your cookie, so the HMAC-signed variant is the fix; a mutation on a GET is forgeable through a top-level Lax navigation and must move to POST behind a token; and an Origin check is a cheap defense-in-depth filter — falling back to Referer — never the sole lock. Spot the structural defect, fix it at the highest-leverage layer, then confirm the forged request can no longer land.

Continue the climb ↑CSRF: build the exploit, then the defense
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources2
expand
  1. 01
  2. 02

Trademarks belong to their respective owners. Editorial reference only.