DEV Community

ANKUSH CHOUDHARY JOHAL
ANKUSH CHOUDHARY JOHAL

Posted on • Originally published at johal.in

Boost security React Server Components vs tRPC: A Practical Guide

Boost Security: React Server Components vs tRPC – A Practical Guide

Modern React applications face growing security risks, from exposed API endpoints to client-side data leaks. Two tools dominate discussions for securing React stacks: React Server Components (RSC) and tRPC. This guide breaks down how each boosts security, practical implementation steps, and when to use one over the other.

What Are React Server Components and tRPC?

React Server Components (RSC) are a React architecture that renders components on the server, sending only HTML and minimal JS to the client. They eliminate the need for client-side data fetching in server-rendered parts of your app, keeping sensitive logic off the client.

tRPC is an end-to-end typesafe RPC framework for TypeScript that replaces traditional REST/GraphQL APIs. It lets you define server-side procedures that are directly callable from the client with full type safety, no API documentation or codegen required.

How React Server Components Boost Security

RSC reduces attack surface by design, with three core security benefits:

  • No sensitive data on the client: Server components can access databases, API keys, and internal services directly without exposing credentials to the client. Only rendered HTML and public data reach the browser.
  • Reduced client-side code: RSC does not send component logic to the client, cutting down on XSS risks from malicious injected scripts targeting client-side state or data fetching.
  • Server-side auth enforcement: You can validate user sessions and permissions directly in server components, ensuring unauthorized users never receive protected data, even before client-side hydration.

How tRPC Boosts Security

tRPC secures client-server communication by removing public API surfaces and enforcing strict validation:

  • No exposed endpoints: tRPC procedures are not accessible via public URLs, eliminating risks from unauthorized API scanning or endpoint abuse common with REST/GraphQL.
  • Built-in input validation: tRPC integrates with Zod for schema validation, blocking malformed inputs that could lead to SQL injection, XSS, or other injection attacks.
  • Per-procedure auth: You can add auth middleware to individual tRPC procedures, ensuring only authenticated users can call sensitive endpoints, with no global auth gaps.
  • Type-safe data transfer: End-to-end types prevent over-fetching or under-fetching data, reducing accidental exposure of sensitive fields.

Practical Implementation: RSC Security Setup

Follow these steps to secure a Next.js app with RSC (App Router):

  1. Initialize a Next.js project with the App Router: npx create-next-app@latest and enable RSC (default in App Router).
  2. Create a server component that fetches sensitive data directly from your database (e.g., Prisma, MongoDB) without client-side fetch calls.
  3. Add session validation at the top of the server component: check for a valid auth cookie or token, redirect unauthorized users via next/navigation.
  4. Avoid passing sensitive data as props to client components; if you must, sanitize and strip sensitive fields first.

Practical Implementation: tRPC Security Setup

Set up tRPC in a React (or Next.js) project with these steps:

  1. Install tRPC dependencies: npm install @trpc/server @trpc/client @trpc/react-query zod.
  2. Define a tRPC router with protected procedures: add auth middleware that validates user sessions, and use Zod schemas to validate all input arguments.
  3. Create a tRPC client with React Query integration, and call procedures directly from client components with full type safety.
  4. Disable public access to tRPC endpoints by restricting CORS to your app's domain, and avoid exposing the tRPC handler via public routes.

Key Tradeoffs and When to Use Each

Choose RSC for security if:

  • You are building a server-rendered app with Next.js App Router.
  • You need to handle highly sensitive data (e.g., payment info, internal dashboards) that should never reach the client.
  • You want to minimize client-side JavaScript for performance and security.

Choose tRPC for security if:

  • You are building a client-heavy SPA or have interactive client-side features.
  • You need typesafe communication between client and server without managing REST/GraphQL APIs.
  • You want to reuse server logic across multiple client platforms (web, mobile) with shared types.

Many teams combine both: use RSC for server-rendered, data-heavy pages, and tRPC for client-side mutations and interactive data fetching.

Conclusion

Both React Server Components and tRPC offer significant security improvements over traditional React stacks with client-side fetching and public APIs. RSC eliminates client-side data exposure by design, while tRPC locks down client-server communication with typesafe, validated procedures. Evaluate your app's architecture and security needs to pick the right tool, or combine them for maximum protection.

Top comments (0)