Repository navigation
Replies: 1 comment
|
This is a well-articulated RFC and highlights an important security disparity between Server Actions (which have built-in origin verification via The Here are a few technical considerations and additions that would strengthen this feature for Next.js core: 1. Leverage Fetch Metadata (
|
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Edit: Reported as a bug in #101251
Next.js Route Handlers have no built-in CSRF protection, unlike:
PreventRequestForgerymiddleware in the defaultwebmiddleware group, checksSec-Fetch-Sitewith CSRF token fallbackCsrfViewMiddlewarein the defaultstartprojectsettings, checksOriginand CSRF token for all views, including JSON responses, opt-out with@csrf_exempt. Django REST Framework also requires CSRF tokens for session-authenticated API requestsActionController::Basecontrollers by defaultcsrf()middlewareWith Route Handlers written as documented in the
route.jsRequest Body example, another website can submit forms that act as a logged-in visitor. Neither attack below needs permissive CORS configuration or the ability to read the response.The attacker's page submits a form with valid JSON as
Content-Type: text/plain. The browser sends no preflight, andrequest.json()parses the body:1. Acting as the logged-in visitor (
SameSite=Nonesession cookie)The browser sends the session cookie with the cross-site form, and the Route Handler changes the visitor's display name (
200):With
SameSite=Lax, the browser doesn't send the cookie and the same form returns401.2. Logging the visitor in as the attacker (
SameSite=Laxsession cookie)The same kind of form posts the attacker's credentials to a login Route Handler. The response replaces the visitor's session cookie, so the visitor is now logged in as the attacker (
200).SameSite=Laxdoesn't help, because the attack needs no existing cookie:Both cases reproduce with Next.js 16.4.1 (
next devandnext build+next start) and Chrome 156:Server Actions already compare the
Originheader withHost/X-Forwarded-Hostand reject mismatches that aren't listed inserverActions.allowedOrigins. Requests withoutOriginare allowed with a warning:next.js/packages/next/src/server/app-render/action-handler.ts
Lines 731 to 741 in 29bbeab
Route Handlers have no equivalent. The data security guide recommends extra auditing for
route.ts, but doesn't show how to protect a REST API, so everyPOST,PUT,PATCHandDELETEhandler needs its own check.Add opt-in CSRF protection for Route Handlers, using the same
Origincheck as Server Actions, with the goal of making it the default:'same-origin'follows the Fetchmode/credentialsvalue. To also allow other hosts, use an array with the same patterns asserverActions.allowedOrigins:allowedOriginsunset (default)Origincheck, same as today: Route Handlers accept cross-site requestsallowedOrigins: 'same-origin'GET,HEADandOPTIONS, respond with403when theOriginhost doesn't matchHost/X-Forwarded-HostOriginheader: allow, like Server Actions. This keeps native apps, server-to-server requests and webhooks working, because they usually don't sendOrigin.allowedOrigins: ['my-proxy.com', '*.my-proxy.com']'same-origin', and also allow requests from matching hostsA Route Handler intended for browsers on any origin (eg. a public API that uses tokens rather than cookies) opts out with a route segment config export.
'*'followsAccess-Control-Allow-Origin: *:'*'can't collide with an existing host pattern, because theallowedOriginsmatcher already rejects a bare*:next.js/packages/next/src/server/app-render/csrf-protection.ts
Lines 25 to 31 in 29bbeab
A
Sec-Fetch-Sitecheck, like in Laravel and Astro, could follow later for both Server Actions and Route Handlers.Alternatives considered
If built-in protection is unsuitable, document a
proxy.tsOrigincheck for all Route Handlers in theroute.jsAPI reference, after the CORS example, which already points to Proxy for multiple Route Handlers. The Request Body FormData example on the same page accepts cross-site form submissions, and the data security guide only recommends extra auditing forroute.ts. Maybe a new "CSRF" guide would also make sense. Eg:proxy.tsPrior art (guide): the Content Security Policy guide documents a complete strict CSP setup with nonces in
proxy.ts.Also to be considered for an update: CSRF section of "How to Think About Security in Next.js" to link to the built-in protection or the new example. It currently ends:
All reactions