Vercel Security Best Practices
Secure your Vercel + Next.js deployments. From the NEXT_PUBLIC_ prefix trap to Server Action authorization gaps.
Verify your app follows these best practices automatically.
Vercel's tight integration with Next.js creates a unique security surface. The NEXT_PUBLIC_ prefix silently exposes variables to the browser, Server Actions can be called directly by anyone, and Edge Middleware runs before your app — making it both a security tool and an attack surface. These practices cover Vercel-specific risks beyond generic deployment security.
Quick Wins
Security Best Practices
#1Never Put Secrets in NEXT_PUBLIC_ Variables
criticalThe NEXT_PUBLIC_ prefix inlines variables into your client JavaScript bundle at build time. Any variable with this prefix is visible to all users in the browser.
Implementation
Only use NEXT_PUBLIC_ for truly public values (API URLs, site names). Keep secrets in unprefixed variables accessible only in server components and API routes.
NEXT_PUBLIC_STRIPE_SECRET=sk_live_abc123STRIPE_SECRET=sk_live_abc123
NEXT_PUBLIC_STRIPE_PUBLISHABLE=pk_live_abc123#2Validate Authorization in Server Actions
criticalNext.js Server Actions can be invoked directly via POST requests — they're not protected by the UI that triggers them. Every Server Action must validate the caller's identity and permissions.
Implementation
Check session validity and resource ownership at the start of every Server Action, not just in the component that calls it
#3Scope Environment Variables to Correct Environments
criticalVercel lets you set different values for Production, Preview, and Development. Using the same production keys in Preview means any preview deployment can access production data.
Implementation
In Vercel Dashboard → Environment Variables, set environment-specific values. Use staging API keys for Preview.
#4Enable Deployment Protection for Previews
highEvery git push creates a public preview URL. Without protection, unreleased features, staging data, and test accounts are accessible to anyone.
Implementation
Project Settings → Deployment Protection → Enable for Preview. Choose Vercel Authentication or password protection.
#5Use Edge Middleware for Security Checks
highVercel Edge Middleware runs before your app handles the request. Use it for authentication, geo-blocking, and bot detection — it executes faster than API routes.
Implementation
Add middleware.ts at your project root to check auth tokens, block suspicious IPs, or redirect unauthenticated users
#6Enable Vercel Firewall Rules
mediumVercel Firewall provides rate limiting and IP blocking at the edge, before requests reach your application.
Implementation
Dashboard → Firewall → Add rules for rate limiting on auth endpoints and blocking known malicious IPs
Common Mistakes to Avoid
Secrets in NEXT_PUBLIC_ variables
NEXT_PUBLIC_ inlines values into the client bundle at build time — visible to every user in DevTools
Remove the NEXT_PUBLIC_ prefix. Access the variable only in server components, API routes, or Server Actions
Server Actions without authorization checks
Server Actions are callable via direct POST requests — the UI button is not a security boundary
Add getServerSession() or equivalent auth check at the start of every Server Action
Production API keys in Preview deployments
Anyone with a preview URL can trigger actions against production databases and services
Use Vercel's per-environment variable scoping to set staging keys for Preview
Verify Your Vercel App Security
Following best practices is the first step. Verify your app is actually secure with a comprehensive security scan.
Get Starter ScanFrequently Asked Questions
Are my environment variables secure on Vercel?
Yes, Vercel encrypts them at rest and in transit. The risk is the NEXT_PUBLIC_ prefix — it embeds the value in your client JavaScript bundle. Only use NEXT_PUBLIC_ for values that are intentionally public.
Do Server Actions need separate security checks?
Yes. Server Actions are exposed as POST endpoints that anyone can call directly with fetch or curl. The UI component that triggers them provides no security. Validate authentication and authorization inside every Server Action.
How is Vercel security different from Netlify?
Vercel's main risk is the NEXT_PUBLIC_ prefix silently exposing variables and Server Actions being directly callable. Netlify's risks center on build-time variable embedding and Netlify Functions needing explicit auth. Both require preview protection, but the specific mechanisms differ.
Related Vercel Security Resources
Similar Platforms
Last updated: January 2026