Next.js

next/public-env-secret

Never give a secret the NEXT_PUBLIC_ prefix; those values are copied into the JavaScript every visitor downloads.

poważnastatycznefe-nextjs

Dlaczego

At next build, every process.env.NEXT_PUBLIC_… reference is replaced by its value in the client bundle. A name such as NEXT_PUBLIC_STRIPE_SECRET_KEY or NEXT_PUBLIC_SUPABASE_SERVICE_ROLE therefore publishes the secret to anyone who opens the browser's developer tools, and it stays in old bundles and caches after you rotate it.

Poprawka

Drop the prefix and read the value only on the server: in a Server Component, a Route Handler or a Server Action, in a module that starts with import 'server-only'. Rotate the key if a build with the public name was ever deployed. Keys that are public by design (a publishable or anon key, an analytics id) may keep the prefix.

Przykłady

// bad
const stripe = new Stripe(process.env.NEXT_PUBLIC_STRIPE_SECRET_KEY)
// good: lib/stripe.ts
import 'server-only'
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY)

Źródła

Fakty

Identyfikator
next/public-env-secret
Waga
poważna
Sprawdzanie
statyczne sprawdzane w kodzie źródłowym przez audit
Dotyczy plików
jsx
Tylko w projektach z
next
Uczy jej skill
fe-nextjs Next.js App Router
Od wersji
0.1.0

Plik reguły na GitHubie

Przeczytaj w terminalu

npx github:dawidolko/frontend-skills explain next/public-env-secret