| 1 | --- |
| 2 | title: Authenticate Server Actions Like API Routes |
| 3 | impact: CRITICAL |
| 4 | impactDescription: prevents unauthorized access to server mutations |
| 5 | tags: server, server-actions, authentication, security, authorization |
| 6 | --- |
| 7 | |
| 8 | ## Authenticate Server Actions Like API Routes |
| 9 | |
| 10 | **Impact: CRITICAL (prevents unauthorized access to server mutations)** |
| 11 | |
| 12 | Server Actions (functions with `"use server"`) are exposed as public endpoints, just like API routes. Always verify authentication and authorization **inside |
| 13 | |
| 14 | Next.js documentation explicitly states: "Treat Server Actions with the same security considerations as public-facing API endpoints, and verify if the user is allowed to perform a mutation." |
| 15 | |
| 16 | **Incorrect (no authentication check):** |
| 17 | |
| 18 | ```typescript |
| 19 | "use server"; |
| 20 | |
| 21 | export async function deleteUser |
| 22 | // Anyone can call this! No auth check |
| 23 | await db.user |
| 24 | return { success: |
| 25 | } |
| 26 | ``` |
| 27 | |
| 28 | **Correct (authentication inside the action):** |
| 29 | |
| 30 | ```typescript |
| 31 | "use server"; |
| 32 | |
| 33 | import { verifySession } |
| 34 | import { unauthorized } |
| 35 | |
| 36 | export async function deleteUser |
| 37 | // Always check auth inside the action |
| 38 | const session = |
| 39 | |
| 40 | if (!session) |
| 41 | throw unauthorized("Must be logged in" |
| 42 | } |
| 43 | |
| 44 | // Check authorization too |
| 45 | if (session.user |
| 46 | throw unauthorized("Cannot delete other users" |
| 47 | } |
| 48 | |
| 49 | await db.user |
| 50 | return { success: |
| 51 | } |
| 52 | ``` |
| 53 | |
| 54 | **With input validation:** |
| 55 | |
| 56 | ```typescript |
| 57 | "use server"; |
| 58 | |
| 59 | import { verifySession } |
| 60 | import { z } |
| 61 | |
| 62 | const updateProfileSchema = z |
| 63 | userId: z.string |
| 64 | name: z.string |
| 65 | email: z.string |
| 66 | }); |
| 67 | |
| 68 | export async function updateProfile |
| 69 | // Validate input first |
| 70 | const validated = |
| 71 | |
| 72 | // Then authenticate |
| 73 | const session = |
| 74 | if (!session) |
| 75 | throw new Error( |
| 76 | } |
| 77 | |
| 78 | // Then authorize |
| 79 | if (session.user |
| 80 | throw new Error( |
| 81 | } |
| 82 | |
| 83 | // Finally perform the mutation |
| 84 | await db.user |
| 85 | where: { id: validated |
| 86 | data: { |
| 87 | name: validated.name |
| 88 | email: validated.email |
| 89 | }, |
| 90 | }); |
| 91 | |
| 92 | return { success: |
| 93 | } |
| 94 | ``` |
| 95 | |
| 96 | Reference: [https://nextjs.org/docs/app/guides/authentication](https://nextjs.org/docs/app/guides/authentication |