BackSecurity

Protecting User Data in Next.js Applications

Next.js provides a clear split between server and client code — and that split itself is the most important security tool available for protecting sensitive user data.

سعيد باعطيةJuly 14, 20266 min read
1

critical rule: NEXT_PUBLIC_ means fully visible to the browser

3

safe places to run sensitive operations: Server Components, Server Actions, Route Handlers

0

sufficient trust in hiding a UI element alone as security

The first rule in Next.js: any environment variable starting with NEXT_PUBLIC_ becomes visible in browser code, so sensitive keys (like a database service role key) must never be placed in a variable with that prefix.

Where Sensitive Operations Belong

Any operation handling secret keys or sensitive data (like calling a database with full privileges) must only happen inside Server Components, Server Actions, or Route Handlers — code that runs server-side only and is never shipped to the browser.

Verify Permissions at Every Layer

Don't rely solely on hiding a UI element to block an unauthorized user; actually verify the user's permissions in every Server Action and Route Handler that touches sensitive data, independent of the UI.

Questions & Answers

01Can a Supabase service role key be used in client code?

Never — it must stay in server-only code (no NEXT_PUBLIC_ prefix) since it has full privileges bypassing Row Level Security.

02Is hiding a link or button in the UI enough protection?

No, you must always verify permissions on the server, since any technical user can call the API directly, bypassing the UI.

Need to Apply These Ideas to Your Project?

I offer free consultations to discuss your current technical setup and how to improve it.