跳到正文
DEV Community· Rares Corches·· 2 小時前AI 評分55

AI 輔助開發的 Next.js 與 Vite 應用如何意外洩露 Stripe、Supabase 和 OpenAI 正式環境金鑰,以及如何在 30 秒內檢查

How AI-Built Next.js & Vite Apps Accidentally Leak Live Stripe, Supabase, and OpenAI Keys (And How to Audit Yours in 30 Seconds)

AI 導讀

文章説明,AI 輔助開發的 Next.js、Vite 應用可能因公開環境變數或 Source Map 洩露金鑰,並整理四類常見生產環境風險。檢查方法包括掃描公開 HTML 和 JavaScript、停用正式環境公開 Source Map、按 CISA KEV 排查依賴漏洞,以及檢查 HSTS 和 TLS 憑證等部署設定。

正文

Over the last year, shipping a production SaaS or client web app with Cursor, Lovable, Bolt, or v0 has gone from taking three weeks to taking an afternoon.

The catch: AI code generators optimize for "make the demo work on the first try." And the fastest way to make a frontend prototype talk to Stripe, OpenAI, or Supabase is often putting the credential straight into a client-side file or prefixing an environment variable with NEXT_PUBLIC_ or VITE_.

Once deployed, those bundles sit on public URLs that automated scrapers scan continuously.

Here are the four most common production leaks we see when auditing externally facing web apps — and how to verify your own site in under a minute.


1. The NEXT_PUBLIC_ / VITE_ Prefix Trap

In Next.js, Vite, and Create React App, any environment variable starting with NEXT_PUBLIC_, VITE_, NUXT_PUBLIC_, or REACT_APP_ is inlined verbatim into the JavaScript shipped to every browser.

Common mistakes that survive into production:

  • NEXT_PUBLIC_STRIPE_SECRET_KEY=sk_live_... (only pk_live_... is safe in browsers)
  • VITE_OPENAI_API_KEY=sk-proj-... or NEXT_PUBLIC_ANTHROPIC_API_KEY=sk-ant-api03-...
  • NEXT_PUBLIC_SUPABASE_SERVICE_ROLE_KEY=eyJ... (unlike the anon key, the service_role JWT bypasses every Row Level Security policy in your database)

Quick check

Inspect your rendered HTML (__NEXT_DATA__, __NUXT__, inline <script> tags) and your first-party JavaScript chunks — or run a read-only external check with our free Frontend Secret Leak Checker (it inspects public HTML, hydration JSON, and first-party JS bundles across 12 live credential families, ignores safe publishable/anon keys, masks matches in memory, and stores nothing).


2. Public .js.map Source Maps in Production

Even when you think your client code is minified, a trailing //# sourceMappingURL=app.chunk.js.map comment tells anyone where to download your unminified TypeScript source, comments, internal API paths, and accidentally hardcoded test values.

Fix

Upload source maps privately to your error tracker during CI, and disable public source map serving in production:

// next.config.js
module.exports = {
  productionBrowserSourceMaps: false,
};
// vite.config.ts
export default defineConfig({
  build: {
    sourcemap: "hidden", // generates .map for private upload, omits sourceMappingURL comment
  },
});

3. Dependency Bloat & CISA KEV Advisories in Lock Files

AI editors frequently install older versions of next, axios, Django, laravel/framework, or transitive dependencies straight from their training cutoff. Running npm audit or checking a raw CVE feed often dumps 40+ noisy alerts with no clear order of action.

What actually matters first:

  1. Is the flaw on CISA's Known Exploited Vulnerabilities (KEV) catalog? (Actively exploited in the wild right now.)
  2. Which single version bump resolves all open advisories for that package at once?

We built a browser-local Lockfile & SBOM CVE Checker that parses package-lock.json, yarn.lock, composer.lock, requirements.txt, poetry.lock, uv.lock, Cargo.lock, go.mod, and CycloneDX/SPDX SBOMs locally in JavaScript (so private paths and registry URLs never leave your browser), cross-references OSV.dev and CISA KEV, and groups all advisories per package into one copyable upgrade command.


4. Missing Basic Production Hygiene (HSTS, robots.txt, SSL & Multi-Region Uptime)

Security leaks rarely happen in isolation. The same fast deploys that ship a service_role key also accidentally deploy:

  • <meta name="robots" content="noindex"> copied over from staging (silently de-indexing your entire site)
  • Missing Strict-Transport-Security (HSTS) and Content-Security-Policy headers
  • Expiring TLS certificates or domains sitting in a forgotten registrar account

You can audit all of those in one shot with our free no-login Website Health & Security Scan, or read the full Cursor, Lovable & Bolt Production Security Checklist.

If you want those checks running automatically every day — plus two-region EU uptime monitoring and a 16-tool hosted MCP server you can connect straight to Cursor, Claude, Codex, or ChatGPT — Beamfar includes a permanent free tier for 1 site (no card required).


What is the wildest environment variable or staging artifact you've caught right before (or after) deploying an AI-assisted app? Drop it in the comments.

來源:DEV Community · dev.to