AI summary by TwinMind

React's most dangerous feature

Theo - t3․gg

26m 37s

Introduction to Security Concerns

  • Video responds to claims that top-level use server in NextJS creates security vulnerabilities
  • Author acknowledges the concerns are valid and scary
  • Not sponsored content - independent analysis of the security implications

Understanding use server Functionality

  • use server tells NextJS compiler that functions can be called from client-side as endpoints
  • Creates server-side functions accessible via POST requests
  • Functions run on server, not client (demonstrated with window undefined error)
  • Can be used inline in components or in separate files

Server Actions Implementation Examples

Basic Form Action

  • Form with action attribute pointing to server function
  • use server function logs "I'm on the server" to server console
  • Demonstrates server-side execution context

Closures and Context Preservation

  • Server actions can access variables from component render context
  • Example: someNumber = Math.random() * 100 preserved in form submission
  • NextJS embeds closure data as encrypted hidden inputs in forms
  • Enables stateful server actions without manual state management

Dynamic Item Management

  • Items array with map rendering individual items
  • Delete buttons with embedded item IDs in closure
  • Each form gets hidden data with specific item context
  • revalidatePath used to refresh UI after mutations

Security Vulnerabilities Explained

The Core Problem

  • Top-level use server exposes ALL exported functions as endpoints
  • Functions don't need to be directly used/imported to be accessible
  • Any exported async function becomes a POST endpoint
  • Accidental exports can leak sensitive data

Dangerous Patterns

  • Internal functions marked "do not use" but still exported
  • Sensitive data functions without authentication checks
  • Functions accessing environment variables or database without validation
  • Example: process.env.SENSITIVE_DATA exposed through unprotected function

Endpoint Enumeration Risk

  • All functions in import tree of a route become accessible endpoints
  • Functions get assigned IDs that can be discovered and called via curl
  • Demonstrated calling sensitive function via direct HTTP POST request
  • Even unused/unbound functions are exposed if exported

Mitigation Strategies

Authentication Requirements

  • Every exported function in use server files MUST include auth checks
  • Import headers from Next.js for request validation
  • Example: Check for secure headers, cookies, or tokens before processing
  • Throw errors or return rejection messages for unauthorized access

File Organization Best Practices

  • Separate sensitive utilities into non-use server files
  • Use server-only import to ensure server-only execution
  • Create dedicated actions folders with clear naming conventions
  • Add comments like "DO NOT EXPORT THIS FILE" for internal utilities

Code Review Requirements

  • Treat use server files as API endpoint definitions
  • Require authentication checks on all exported functions
  • Implement code owner rules for data access layers
  • Review any changes to files containing server actions

Alternative Approaches

  • Use individual use server annotations instead of top-level
  • Implement tRPC integration for standardized authentication
  • Create data access layer with consistent permission patterns
  • Use ESLint plugins to disallow top-level use server

Comparison to GraphQL Security

  • Similar enumeration risks as GraphQL schemas
  • GraphQL requires field-level permission validation
  • Backend development always requires permission checks
  • Web developers unfamiliar with API security patterns at risk

Framework Integration Solutions

tRPC Integration

  • Can wrap server actions with tRPC procedures
  • Maintains authentication and standardization benefits
  • Preserves server action ergonomics and zero-JS requirements
  • Example: createPost.protectedAction.input().mutation()

Data Access Layer Pattern

  • Centralized authentication functions (getCurrentUser)
  • Permission checking functions (canSeeUsername, canSeePhoneNumber)
  • Filtered data return based on user permissions
  • Consistent patterns across all data access

Industry Response and Tooling

  • CJ created ESLint plugin to disallow top-level use server
  • Can override plugin rules for specific files when needed
  • Ryan Florence's perspective: acceptable risk with proper understanding
  • Sebastian Markbåge's security blog post referenced for best practices

Key Takeaways

  • use server = creating API endpoints, not just function calls
  • Every exported function needs authentication
  • Code review critical for any data-exposing functionality
  • Web developers need to think like backend developers
  • Proper file organization and naming conventions essential
  • The risk exists in all backend technologies, not unique to NextJS

TwinMind

Get summaries like this for your own meetings

TwinMind takes notes in the background during meetings, lectures, and calls, then writes the summary for you.

Free plan to get started

No bot joins your calls

Phone, desktop & Chrome apps

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome

Your next meeting, summarized like this

TwinMind listens in the background and turns meetings, lectures, and calls into searchable notes you can chat with. Start free.

Available on phone, desktop & Chrome