Interview kitsBlog

Your dream job? Lets Git IT.
Interactive technical interview preparation platform designed for modern developers.

XGitHub

Platform

  • Categories

Resources

  • Blog
  • About the app
  • FAQ
  • Feedback

Legal

  • Privacy Policy
  • Terms of Service

© 2026 LetsGit.IT. All rights reserved.

LetsGit.IT/Categories/Next.js
Next.jshard

Server Actions — what are they and what security rule must you remember?

Tags
#server-actions#security#validation
Back to categoryPractice quiz

Answer

Server Actions are functions that run on the server and can be invoked from forms or client components. Treat them like public endpoints: validate input and enforce auth/authorization on the server (don’t rely on the UI).

Advanced answer

Deep dive

Server Actions are server-side functions (typically marked with `'use server'`) that can be called from:

  • `<form action={myAction}>` submissions,
  • Client Components via action props.

They run on the server, so they can access secrets, databases, and internal services. They also integrate with cache invalidation (`revalidatePath`, `revalidateTag`) after mutations.

Example

'use server'

export async function updateName(formData: FormData) {
  const name = String(formData.get('name') ?? '')
  if (name.length < 2) throw new Error('Invalid name')

  const user = await requireUser()
  await db.user.update({ where: { id: user.id }, data: { name } })
}

The security rule to remember

Treat a Server Action like a **public API endpoint**:

  • validate and sanitize inputs,
  • authenticate the caller,
  • authorize the operation (ownership/role checks),
  • don’t rely on “the button is hidden in the UI”.

Common pitfalls

  • Missing authorization checks (any logged-in user can trigger it).
  • Accepting unchecked input (mass assignment / injection risks).
  • Returning sensitive data back to the client unintentionally.

Related questions

Next.js
Cache invalidation: what do `revalidatePath` and `revalidateTag` do?
#nextjs#cache#revalidate
Next.js
Server Actions (`'use server'`): what are they and what are common restrictions?
#nextjs#server-actions#mutations
Next.js
Next.js App Router auth: how do you keep it secure and SSR-friendly?
#nextjs#auth#cookies
Next.js
Environment variables in Next.js: what does `NEXT_PUBLIC_` mean?
#nextjs#env#security
Next.js
Route Handler vs Server Action — how do you choose?
#server-actions#route-handlers#security
Security
How should passwords be stored securely?
#passwords#hashing#security