VibeSecurity

Security check

CSRF: when your app needs protection and when it does not

CSRF tricks a logged-in user's browser into sending a request to your app. It matters when the browser attaches credentials automatically, which is what cookies do.

By the VibeSecurity team1 min read

Do you have a CSRF risk?

  • Cookie-based sessions: yes, review it.
  • Bearer tokens that your JavaScript adds to each request: the browser will not add them for another site, so classic CSRF does not apply.
  • Any GET route that changes data is unsafe regardless of the session type.

Check where the request came from

For POST, PUT, PATCH and DELETE, compare the Origin header with your own host and reject mismatches.

Origin check
const origin = request.headers.get('origin');
const host = request.headers.get('host');
if (!origin || new URL(origin).host !== host) {
  return new Response('Forbidden', { status: 403 });
}

Use tokens for sensitive actions

For money movement, email or password changes and account deletion, require a per-session token that a cross-site page cannot read, or ask the user to re-enter their password.

Frequently asked questions

Does SameSite=Lax fully stop CSRF?

It blocks most cross-site POST requests, but it is one layer. Keep origin checks and never change data on GET.

Do single-page apps need CSRF protection?

Only if they authenticate with cookies. Token-in-header apps rely on other protections.

Sources

  1. 1.OWASP Cross-Site Request Forgery Prevention Cheat Sheet
  2. 2.MDN: HTTP cookies