Skip to main content

Nextjs Server Actions

Best practices protocols for using server actions in Nextjs.

Based on the provided search results, here are the main risks of using server actions and a checklist of best practices to mitigate these risks:

Risks

Risks of Using Server Actions

  1. Accidentally exporting sensitive internal functions as publicly accessible endpoints.
  2. Not properly authenticating and authorizing users who can access the server actions.
  3. Exposing vulnerabilities and attack surfaces if unnecessary services and features are enabled on the server.
  4. Data breaches, unauthorized access, and compromised server security if best practices are not followed.

Checklist

Carefully review what server actions are exported, authenticating and authorizing access to them to reduce attack surfaces. By implementing security best practices, and hardening server infrastructure, the risks of using server actions can be effectively mitigated. Treating server actions as security-critical endpoints and applying these mitigation steps as a continuous process is key to maintaining a robust security posture.

  1. Carefully control exports:
    • Never export internal functions that shouldn't be exposed as endpoints.
    • Have clear coding conventions and folder structures for files with server actions.
    • In code reviews, ensure all exported server actions are intended to be publicly accessible.
  2. Always authenticate and authorize:
    • Implement authentication checks in every exported server action to verify the user has permission.
    • Use secure methods like API tokens, OAuth, etc. to authenticate requests to server actions.
    • Employ the principle of least privilege, only granting access when absolutely necessary.
  3. Reduce attack surface:
    • Disable or remove any unnecessary services, features, and software modules on the server.
    • Close all ports that are not required for the server actions to function.
    • Regularly update and patch the operating system, frameworks, and dependencies.
  4. Implement security best practices:
    • Encrypt all sensitive data both in transit and at rest.
    • Set up proper access controls and user roles with least privileges.
    • Enable logging and monitoring to detect and alert on suspicious activities.
    • Perform regular security audits and penetration testing on server endpoints.
    • Have an incident response plan to promptly react to any breaches or attacks.
  5. Secure server infrastructure:
    • Place servers behind firewalls and use network isolation where applicable.
    • Harden the operating system and disable unused functionality.
    • Physically secure the server hardware in access-controlled locations.
    • Maintain offline backups of code and data to enable recovery from incidents.

Use Case Comparison

Server Actions vs other options for interacting with the server.

Server actions are primarily for making state changing actions against the server rather than fetching data.

CriteriaServer ActionstRPCGraphQLREST
Setup32-22
Ease of Use3111
Mutations2111
Queries-11-31
Type Safety112-2
Compatibility (MB)-3231
Without JS3-1-1-1

Rule of thumb:

  • If simple web only solution Server Actions is the best option.
  • For more complex solutions or supporting mobile and desktop apps use tRPC.

TRPC Implementation

tRPC is a TypeScript library for building end-to-end typesafe APIs, eliminating the need for code generation or schemas, and ensuring seamless type safety between frontend and backend.

Problem

Complexity and Boilerplate: Traditional methods of handling server actions often involve repetitive code and complex setups, making the development process cumbersome.

Best Practices

  • Leverage tRPC's Type Safety: Utilize tRPC's type-safe API to reduce errors and improve code maintainability.
  • Simplify Server Actions: Use tRPC to streamline server actions, minimizing the need for boilerplate code.
  • Consistent Error Handling: Implement consistent error handling mechanisms across server actions to ensure robustness.
  • Optimize Performance: Focus on optimizing performance by reducing unnecessary server calls and leveraging efficient data fetching techniques.

See Typescript TRPC