Next.js · Senior · Question 7 of 75

How does Next.js protect Server Actions from CSRF?

Explanation

Server Actions can be invoked only with POST, and Next.js compares Origin with Host (or X-Forwarded-Host), rejecting requests from other sites. Together with SameSite cookies, this blocks most CSRF attacks. For complex proxy setups there's serverActions.allowedOrigins. Authorization is still checked inside the action.

Further reading

Problem with a question

Next.js · Senior · question 7

Test yourself

Take the Next.js · Senior quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz