vibesupport
Security Tutorial

Preventing PII Leaks: Why AI Apps Send Sensitive Database Rows to the Browser

When you build an app with Next.js App Router, the boundary between server and client components is seamless. You can fetch database data in a Server Component and pass the result as a prop directly to a Client Component:

tsx
// VULNERABLE SERVER COMPONENT
import { db } from "@/server/db";
import { users } from "@/drizzle/schema";
import { eq } from "drizzle-orm";
import { UserCard } from "./user-card"; // Client component!

export default async function ProfilePage({ params }) {
  // AI writes this:
  const [user] = await db.select().from(users).where(eq(users.id, params.id));

  // Passing the entire database row into a Client Component!
  return <UserCard user={user} />;
}

The UserCard component might only display the user's name and avatar.

However, behind the scenes, Next.js must serialize that entire user object into the HTML response or the React Server Components (RSC) flight stream so the client component can hydrate.

If your users table contains passwordHash, stripeCustomerId, twoFactorSecret, or email, every single one of those fields is sent in plain text over the network to the visitor's browser.


Inspecting RSC Flight Streams

Open Chrome DevTools on your deployed website:

  • Go to the Network tab.
  • Reload the page.
  • Look at the primary document response or any request with the _rsc query parameter.
  • Search the raw response body for your database field names.

You will often find entire database rows serialized as JSON strings embedded directly in the HTML payload. Anyone viewing page source can read the private columns, even though the visual UI only shows a name.


The DTO Solution: Column Projection

Never pass raw database entities across the server-client boundary. Use Data Transfer Objects (DTOs) and selective SQL queries.

1. In Drizzle ORM: Select Only What You Need

ts
// SECURE: Explicit column selection
export async function getPublicUserProfile(userId: string) {
  const [profile] = await db
    .select({
      id: users.id,
      name: users.name,
      avatarUrl: users.avatarUrl,
      // Notice: passwordHash, email, stripeCustomerId are EXCLUDED!
    })
    .from(users)
    .where(eq(users.id, userId));

  return profile ?? null;
}

2. DTO Type Safety in TypeScript

Define explicit DTO types so components cannot accidentally request private fields:

ts
// types/user.ts
export interface PublicUserDto {
  id: string;
  name: string;
  avatarUrl: string | null;
}

Enforcing 'server-only' Boundaries

To prevent sensitive backend code or database clients from ever being imported into client components, install and use the server-only package:

bash
bun add server-only

Add it to the top of every file in your server/ directory:

ts
// server/db.ts
import "server-only";
import { drizzle } from "drizzle-orm/neon-http";
// ...

If any developer or AI assistant accidentally tries to import db into a file with "use client", Next.js will fail the build immediately with a compilation error.


Summary Checklist

  • ☐No Client Components receive full database row objects as props.
  • ☐Database queries explicitly specify needed columns rather than using raw select() without arguments.
  • ☐Password hashes, salts, and billing tokens are never included in API route responses.
  • ☐Server utility files are protected with import "server-only".
BUILT WITH AI? A SECOND LOOK BEFORE YOU LAUNCH

Don't let a hidden bug derail your launch.

vibesupport scans your AI-built codebase or live website for exposed secrets, missing database RLS, and broken access controls—then pairs you with senior engineers to fix them directly in GitHub.

Related Tutorials & Guides