Cross-Site Request Forgery (CSRF)

Cross-Site Request Forgery (CSRF)

CWE-352 · High severity · Updated November 6, 2026

What it is

CWE-352 occurs when a web application accepts state-changing requests from a user without verifying that the user actually intended to make the request. An attacker hosts a page that issues a request to the target site; if the victim is authenticated and the target site does not check origin, referrer, or a CSRF token, the request executes with the victim's credentials.

Why it matters

CSRF is one of the longest-standing entries in the OWASP Top 10 (mapped to A01 Broken Access Control since 2021). Successful exploitation lets an attacker transfer funds, change passwords, add admin users, post content, or trigger any other state change the victim is authorized to perform. Chromium-based browsers treat cookies with no SameSite attribute as Lax by default, which mitigates many cross-origin POST attacks, but GET-based state changes and same-site iframes remain exploitable when CSRF tokens are missing.

Common patterns

  • State-changing endpoints (POST, PUT, DELETE, PATCH) without CSRF token validation.
  • Cookies issued without the SameSite attribute, or with SameSite=None and no Secure flag.
  • Endpoints that perform state changes on GET (a CWE-650 overlap).
  • Custom session middleware that forgets to verify a per-session CSRF token.
  • API endpoints that authenticate via cookies but accept cross-origin requests via permissive CORS.

Languages affected

JavaScript, TypeScript, Python, Ruby, Java, Go, PHP

What Deva detects

Deva flags code that explicitly disables CSRF protection in Flask, Django, Rails, Laravel, ASP.NET Core, NestJS, and Express. It does not detect routes where CSRF middleware was never added, so review those by hand. It also flags Flask cookies set without SameSite, and wildcard Access-Control-Allow-Origin headers whether or not credentials are allowed.

Example

Vulnerable

import express from 'express'
import session from 'express-session'

const app = express()
app.use(express.urlencoded({ extended: false }))
app.use(session({ secret: process.env.SESSION_SECRET, resave: false, saveUninitialized: false }))

app.post('/account/email', (req, res) => {
  req.session.user.email = req.body.email
  res.json({ ok: true })
})

Fixed

import crypto from 'crypto'
import express from 'express'
import session from 'express-session'

const app = express()
app.set('trust proxy', 1) // Needed behind a TLS proxy, or secure cookies are not set.
app.use(express.urlencoded({ extended: false }))
app.use(session({
  secret: process.env.SESSION_SECRET,
  resave: false,
  saveUninitialized: false,
  cookie: { sameSite: 'lax', secure: true, httpOnly: true },
}))

app.get('/csrf-token', (req, res) => {
  req.session.csrfToken ??= crypto.randomBytes(32).toString('hex')
  res.json({ token: req.session.csrfToken })
})

function requireCsrf(req, res, next) {
  const expected = Buffer.from(req.session.csrfToken ?? '')
  const actual = Buffer.from(req.get('X-CSRF-Token') ?? '')
  const valid = expected.length > 0
    && expected.length === actual.length
    && crypto.timingSafeEqual(expected, actual)
  if (!valid) return res.status(403).json({ error: 'Bad CSRF token' })
  next()
}

app.post('/account/email', requireCsrf, (req, res) => {
  req.session.user.email = req.body.email
  res.json({ ok: true })
})

The vulnerable version accepts a POST to change the account email without any anti-forgery defense. An attacker's page submits a cross-origin form, and wherever the browser attaches the session cookie, the email changes. The fix sets SameSite=Lax (defense in depth), stores a random token in the session, exposes it at /csrf-token so the SPA can send it in a header, and rejects writes without it. The older csurf package is deprecated, so use this pattern or a maintained library.

Where this fits in OWASP Top 10

Compliance framework references

These references explain how the weakness relates to control topics. They do not mean Deva offers evaluation for every framework listed. CMMC evaluation and SPRS scoring are currently unavailable. See the FAQ for offered frameworks and preview status. Deva provides code-level evidence for your assessment, without certifying compliance.

FrameworkControls
OWASP Top 10:2025A01:2025 Broken Access Control
NIST 800-53 Rev 5AC-4 Information Flow Enforcement, SI-10 Information Input Validation
PCI-DSS v4.0.16.2.4 Software engineering techniques
CMMC L2AC.L2-3.1.3 CUI flow control

Related CWEs

  • CWE-287 · Improper authentication: CSRF abuses authenticated sessions.
  • CWE-650 · Trusting HTTP method to be safe: GET-based state changes.
  • CWE-863 · Incorrect authorization: same root cause family.

Related glossary terms