Next.js Server Actions block CSRF by default, but API route handlers do not
Next.js Server Actions include built-in Cross-Site Request Forgery (CSRF) protection by automatically verifying that each request's Origin header matches the app's own deployed domain. This means malicious requests from external sites are rejected before any action code even executes. However, API route handlers in the app/api/ directory are general-purpose endpoints and receive no such automatic origin checking from Next.js. Because route handlers are designed to serve diverse callers — including mobile apps, webhooks, and third-party services — the framework makes no assumption about allowed origins. Developers must manually add origin validation to sensitive route handlers to prevent authenticated users' session cookies from being exploited by malicious sites.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in