- docs/agent/external: Vercel react-best-practices and composition-patterns (MIT), Addy Osmani accessibility (MIT), Anthropic frontend-design (Apache-2.0), official Payload skill at v3.92.1 (MIT) — text only, each with its licence, SOURCE.md (repo, commit) and an override note in front of the SKILL.md (ipal-kit first, Payload fetching over swr, DESIGN.md tokens, Payload 3 not 4); manifest.json with sha256 per file - scripts/vendor-external-skills.mjs (maintainer, not shipped): fetches by full commit SHA only, rejects non-text or executable files, regenerates the manifest; --check verifies it - ipal-skills --external [names]: copies ext-* after checking every file against the manifest, downloads nothing, never touches ext-* folders without ipal-kit's SOURCE.md, removes only retired ones it installed; an unknown name stops the run before anything is copied - AGENT.md §14, ipal-setup stage 2 (optional step), README: usage, overrides, how to update the pins - test/externalSkills.test.ts: pins are SHAs, no branch fetch, checksums, text only, licences, override note, installer leaves other folders alone Co-Authored-By: Claude Opus 5.5 <[email protected]>
97 lines
2.6 KiB
Markdown
97 lines
2.6 KiB
Markdown
---
|
|
title: Authenticate Server Actions Like API Routes
|
|
impact: CRITICAL
|
|
impactDescription: prevents unauthorized access to server mutations
|
|
tags: server, server-actions, authentication, security, authorization
|
|
---
|
|
|
|
## Authenticate Server Actions Like API Routes
|
|
|
|
**Impact: CRITICAL (prevents unauthorized access to server mutations)**
|
|
|
|
Server Actions (functions with `"use server"`) are exposed as public endpoints, just like API routes. Always verify authentication and authorization **inside** each Server Action—do not rely solely on middleware, layout guards, or page-level checks, as Server Actions can be invoked directly.
|
|
|
|
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."
|
|
|
|
**Incorrect (no authentication check):**
|
|
|
|
```typescript
|
|
'use server'
|
|
|
|
export async function deleteUser(userId: string) {
|
|
// Anyone can call this! No auth check
|
|
await db.user.delete({ where: { id: userId } })
|
|
return { success: true }
|
|
}
|
|
```
|
|
|
|
**Correct (authentication inside the action):**
|
|
|
|
```typescript
|
|
'use server'
|
|
|
|
import { verifySession } from '@/lib/auth'
|
|
import { unauthorized } from '@/lib/errors'
|
|
|
|
export async function deleteUser(userId: string) {
|
|
// Always check auth inside the action
|
|
const session = await verifySession()
|
|
|
|
if (!session) {
|
|
throw unauthorized('Must be logged in')
|
|
}
|
|
|
|
// Check authorization too
|
|
if (session.user.role !== 'admin' && session.user.id !== userId) {
|
|
throw unauthorized('Cannot delete other users')
|
|
}
|
|
|
|
await db.user.delete({ where: { id: userId } })
|
|
return { success: true }
|
|
}
|
|
```
|
|
|
|
**With input validation:**
|
|
|
|
```typescript
|
|
'use server'
|
|
|
|
import { verifySession } from '@/lib/auth'
|
|
import { z } from 'zod'
|
|
|
|
const updateProfileSchema = z.object({
|
|
userId: z.string().uuid(),
|
|
name: z.string().min(1).max(100),
|
|
email: z.string().email()
|
|
})
|
|
|
|
export async function updateProfile(data: unknown) {
|
|
// Validate input first
|
|
const validated = updateProfileSchema.parse(data)
|
|
|
|
// Then authenticate
|
|
const session = await verifySession()
|
|
if (!session) {
|
|
throw new Error('Unauthorized')
|
|
}
|
|
|
|
// Then authorize
|
|
if (session.user.id !== validated.userId) {
|
|
throw new Error('Can only update own profile')
|
|
}
|
|
|
|
// Finally perform the mutation
|
|
await db.user.update({
|
|
where: { id: validated.userId },
|
|
data: {
|
|
name: validated.name,
|
|
email: validated.email
|
|
}
|
|
})
|
|
|
|
return { success: true }
|
|
}
|
|
```
|
|
|
|
Reference: [https://nextjs.org/docs/app/guides/authentication](https://nextjs.org/docs/app/guides/authentication)
|