<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[PlugLeak Notes]]></title><description><![CDATA[PlugLeak Notes]]></description><link>https://plugleak.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 04 Dec 2026 21:28:36 GMT</lastBuildDate><atom:link href="https://plugleak.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[NEXT_PUBLIC_ and VITE_ aren't for secrets: what actually ends up in your browser bundle]]></title><description><![CDATA[Next.js and Vite inline public env vars at build time. Which keys are safe in the browser, which never are, and how to check your deployed site.]]></description><link>https://plugleak.hashnode.dev/next-public-and-vite-aren-t-for-secrets-what-actually-ends-up-in-your-browser-bundle</link><guid isPermaLink="true">https://plugleak.hashnode.dev/next-public-and-vite-aren-t-for-secrets-what-actually-ends-up-in-your-browser-bundle</guid><category><![CDATA[Security]]></category><category><![CDATA[supabase]]></category><category><![CDATA[Next.js]]></category><category><![CDATA[vite]]></category><category><![CDATA[Web Development]]></category><dc:creator><![CDATA[PlugLeak]]></dc:creator><pubDate>Mon, 16 Nov 2026 04:21:26 GMT</pubDate><content:encoded><![CDATA[<p>Sooner or later someone asks it in a Next.js or Vite thread: "is it fine if my API key is in <code>NEXT_PUBLIC_</code>?" The honest answer is short. No, not if it's a secret. The longer answer is worth knowing, because the reason it's unsafe also explains why "I already removed it" often isn't true.</p>
<h2>What the prefix actually does</h2>
<p>In Next.js, any variable starting with <code>NEXT_PUBLIC_</code> is inlined into the JavaScript bundle that goes to the browser, at build time. Anything without the prefix stays on the server. Vite does the same thing with <code>VITE_</code>: those values land on <code>import.meta.env</code> in client code, and Vite's own docs say they shouldn't contain sensitive information such as API keys.</p>
<p>The detail people miss is <em>at build time</em>. The value is copied into the files you deploy. If you later change the variable in your hosting dashboard, the bundle that's already live keeps the old value until you rebuild. Same story the other way: deleting the line from your source doesn't pull a key out of a bundle that has already shipped.</p>
<h2>Which prefix is yours</h2>
<p>Bolt projects are usually Vite, so <code>VITE_</code>. Cursor and v0 projects are usually Next.js, so <code>NEXT_PUBLIC_</code>. Don't trust the label, though. Look at the repo. A <code>vite.config</code> file and <code>import.meta.env</code> means Vite; a <code>next.config</code> file and <code>process.env.NEXT_PUBLIC_</code> means Next.js. The rule comes from the bundler, not from the tool that generated the code.</p>
<h2>What's fine in a public variable, and what never is</h2>
<table>
<thead>
<tr>
<th>Value</th>
<th>OK with a public prefix?</th>
</tr>
</thead>
<tbody><tr>
<td>Your site URL</td>
<td>Yes</td>
</tr>
<tr>
<td>Stripe publishable key (<code>pk_live_</code>, <code>pk_test_</code>)</td>
<td>Yes. Stripe says only publishable keys belong in front-end code</td>
</tr>
<tr>
<td>Supabase project URL, publishable key (<code>sb_publishable_</code>) or legacy anon key</td>
<td>Yes, <strong>but only if RLS is on</strong>. The key will be visible; it's safe because Row Level Security protects the data</td>
</tr>
<tr>
<td>OpenAI or Anthropic API keys</td>
<td>Never</td>
</tr>
<tr>
<td>Stripe secret (<code>sk_</code>) or restricted (<code>rk_</code>) keys</td>
<td>Never</td>
</tr>
<tr>
<td>Supabase secret key (<code>sb_secret_</code>) or legacy <code>service_role</code> key</td>
<td>Never. Both bypass RLS</td>
</tr>
<tr>
<td>Database URLs and passwords</td>
<td>Never</td>
</tr>
<tr>
<td>GitHub tokens (<code>ghp_</code>, <code>gho_</code>, <code>ghu_</code>, <code>ghs_</code>, <code>ghr_</code>, <code>github_pat_</code>)</td>
<td>Never</td>
</tr>
<tr>
<td>AWS access keys (<code>AKIA…</code>, <code>ASIA…</code>)</td>
<td>Never</td>
</tr>
<tr>
<td>Private keys (<code>-----BEGIN … PRIVATE KEY-----</code>)</td>
<td>Never</td>
</tr>
</tbody></table>
<h2>Where the secret should live</h2>
<p>Give it a name <strong>without</strong> the prefix and read it only in code that never ships to the browser. In Next.js that's a Route Handler or a Server Action:</p>
<pre><code class="language-ts">// app/api/complete/route.ts
export async function POST(request: Request) {
  const prompt = await request.text();
  const res = await fetch("https://api.openai.com/v1/chat/completions", {
    method: "POST",
    headers: {
      Authorization: "Bearer " + process.env.OPENAI_API_KEY,
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ messages: [{ role: "user", content: prompt }] }),
  });
  return new Response(await res.text(), { status: res.status });
}
</code></pre>
<p>One catch: that route is a public URL now. Moving the key out of the bundle stops people from <em>reading</em> it, but anyone can still call your endpoint and spend it for you. Check who's calling before you forward the request.</p>
<p>A plain Vite app has no server, so the secret has to go somewhere that does: your own backend, or a serverless or edge function. With Supabase, that's an Edge Function reading <code>Deno.env.get("OPENAI_API_KEY")</code>, while the browser only ever holds the publishable key. And don't give that function's secret a <code>VITE_</code> name "just to be consistent". That's exactly how it ends up in the bundle.</p>
<h2>Check your live site in five minutes</h2>
<p>Do this on the deployed URL, not on localhost, because that's what visitors get.</p>
<ol>
<li>Open the site in a private window and click around a bit, so lazy-loaded chunks load too.</li>
<li>DevTools → <strong>Sources</strong> → search across all files (Ctrl/Cmd + Shift + F). You can also open a script in the <strong>Network</strong> tab and search its response.</li>
<li>Search for:<ul>
<li><code>sk-</code> and <code>sk_live_</code> (also <code>sk_test_</code>, <code>rk_live_</code>)</li>
<li><code>sb_secret_</code></li>
<li><code>service_role</code></li>
<li><code>AKIA</code></li>
<li><code>ghp_</code></li>
<li><code>-----BEGIN</code></li>
</ul>
</li>
</ol>
<p>A <code>pk_</code> Stripe key, a Supabase publishable key, or a JWT whose <code>role</code> claim is <code>anon</code> can be there on purpose. Anything from the list above is a secret in your bundle.</p>
<p>If you find one, the order matters:</p>
<ol>
<li>Rotate it first.</li>
<li>Then move it to an unprefixed name that only the server reads.</li>
<li>Rebuild and redeploy.</li>
<li>Search the live JS again to confirm it's gone.</li>
</ol>
<hr />
<p><em>Written with AI assistance and edited by hand. Every claim was checked against the Next.js, Vite, Stripe and Supabase documentation.</em></p>
]]></content:encoded></item></channel></rss>