{"success":true,"name":"know-that-next-public-means-published-keep-server-secrets-ou","description":"Adding environment variables in a Next.js project, some holding secrets","category":"ai-brimind","path":"ai-brimind/know-that-next-public-means-published-keep-server-secrets-ou/SKILL.md","file":"SKILL.md","content":"---\nname: know-that-next-public-means-published-keep-server-secrets-ou\ndescription: \"Adding environment variables in a Next.js project, some holding secrets\"\n---\n\n# Know That NEXT_PUBLIC_ Means Published: Keep Server Secrets Out of Browser-Visible Env Vars\n\n## When to use\nProject is Next.js, and you are adding or using environment variables — API keys, DB URIs, JWT secrets, third-party tokens — and the NEXT_PUBLIC_ prefix is being applied (or omitted) by copy-paste habit rather than by a deliberate 'may every visitor read this?' decision.\n\n## Example request\nA payment provider's SECRET key stored as NEXT_PUBLIC_STRIPE_KEY because a nearby variable had the prefix → NEXT_PUBLIC_ vars are compiled INTO the JavaScript bundle every visitor downloads; the secret is now published on the internet, and rotating it is the only fix. The DB connection string referenced in a client component 'for a quick check' → it reads as undefined (non-prefixed vars simply do not exist in the browser), and the tempting 'fix' of adding the prefix would be the actual disaster; the correct move is doing the DB work server-side and passing results down. A maps API key that the browser legitimately must use → NEXT_PUBLIC_ is CORRECT there, paired with the provider-side restrictions (domain allowlist) such keys are designed to have; public-by-design keys exist, the sin is publishing private ones. Secret works in dev but is undefined in production → env vars are baked at BUILD time in Next.js; the deployment platform needed the variable set before the build, which is a deploy-process fact worth knowing, not a mystery.\n\n## Instructions\nAct as a Next.js developer who reads NEXT_PUBLIC_ as exactly what it says — 'this value will be published' — because that prefix is not a naming convention, it is a compiler instruction: prefixed variables are inlined into the client JavaScript bundle, downloadable and readable by every visitor, every scraper, and every attacker with a view-source reflex; non-prefixed variables stay on the server and are invisible to the browser by design. For every env variable you add or touch, do this: (1) Classify it with one question before naming it: 'is this value safe on a billboard?' Server secrets — DB URIs, JWT signing secrets, payment SECRET keys, admin tokens, email credentials — are never prefixed, full stop; they may only be read in server territory (API routes, server components, server actions), which your server/client component discipline already maps. Browser-required config — a public maps key, an analytics ID, the public base URL — earns the prefix deliberately, and where the provider offers key restrictions (domain allowlists, publishable-vs-secret pairs), those restrictions are part of making it safe to publish. (2) When a client component 'needs' a secret, hear the design alarm, not a naming problem: the need is a server-side capability — build the API route or server action that USES the secret server-side and hand the client only the result; adding NEXT_PUBLIC_ to make undefined go away is converting a compile-time protection into a leak. (3) Keep the border tidy per your config discipline: all env access through the validated config module, .env.local git-ignored, .env.example listing both kinds with the prefix choices visible — so the next developer inherits the classification, not the guesswork. (4) Remember the build-time nature: NEXT_PUBLIC_ values are frozen into the bundle when the build runs, so the deployment platform must have them set before building, and changing one means rebuilding — 'works locally, undefined in prod' is almost always this. (5) Audit with the attacker's tool, which is just the browser: search the production bundle/page source for your variable names and known key fragments — anything findable there is public NOW, and if it was ever a secret, rotation (not deletion from code) is the remediation, because bundles are cached and archived. Only call the env setup done when you can walk the variable list and answer for each: 'server-only or published — and who decided?' — a secret with the wrong prefix is not a config bug, it is a disclosure with a timestamp.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #230\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}