← All posts
exposed secretsnextjscursorsecurityvibe coding

Exposed Secrets Fix NextJS Cursor: A Practical Guide

O

OverMCP Team

Quick answer

To fix exposed secrets in a Next.js app built with Cursor, immediately rotate the leaked keys (e.g., API keys, tokens), remove them from your codebase (especially client-side code), and move them to secure environment variables on your hosting platform (e.g., Vercel). Then run a secret leak scanner to catch any remaining exposures and set up continuous monitoring to prevent future leaks.

What to check first

Before you panic, verify the extent of the leak:

  • [ ] Scan your entire Git history for any committed secrets (not just the latest commit).
  • [ ] Check client-side JavaScript files (bundle, source maps) for hardcoded secrets.
  • [ ] Review your `.env` files – are they accidentally committed or exposed in public repos?
  • [ ] Inspect third-party integrations – did you leak a Stripe, OpenAI, or AWS key?
  • [ ] Look for secrets in logs – are any keys printed in server logs or error messages?
  • [ ] Check your hosting dashboard (Vercel, Netlify) – are environment variables properly set?
  • [ ] Use a [secret leak scanner](https://www.overmcp.com/tools/leak) to automate the search.
  • Step-by-step fix

    1. Rotate the exposed secrets immediately

    If you've confirmed a leak, don't just delete the key – rotate it. For example:

  • OpenAI API key: Go to OpenAI API keys page, revoke the old key, create a new one.
  • Stripe secret key: Access the Stripe Dashboard > Developers > API keys, roll the key.
  • AWS access keys: Use IAM to deactivate and create new keys.
  • 2. Remove secrets from your codebase

    Search your project for any hardcoded secrets. Common patterns:

    // ❌ BAD: Hardcoded in client component (Cursor might generate this)
    const openAiKey = "sk-1234567890abcdef";
    
    // ✅ GOOD: Use environment variable
    const openAiKey = process.env.OPENAI_API_KEY;

    Also check for secrets in:

  • getServerSideProps or API routes that send secrets to the client
  • next.config.js if you're exposing env vars via publicRuntimeConfig
  • Source maps (disable in production: productionBrowserSourceMaps: false)
  • 3. Move secrets to environment variables

    In Next.js, use .env.local for local development and set variables in your hosting provider:

    # .env.local (never commit this!)
    OPENAI_API_KEY=sk-...
    STRIPE_SECRET_KEY=sk_live_...

    On Vercel: Go to Project Settings > Environment Variables, add the keys. Mark sensitive keys as "Secret" so they don't appear in logs.

    4. Add secrets to `.gitignore`

    Ensure .env.local and any other sensitive files are never committed:

    # .gitignore
    .env.local
    .env.*.local
    *.pem

    5. Scan your entire Git history

    If a secret was committed in the past, it's still accessible. Use a tool like git filter-branch or BFG Repo-Cleaner to purge it, but first rotate the key – history rewriting is risky.

    6. Set up continuous monitoring

    Prevent future leaks by integrating a continuous security monitoring tool that scans every commit and deployment for secrets. OverMCP can automatically check your Vercel deployments for exposed keys.

    Common mistakes

  • Using client-side environment variables with NEXT_PUBLIC_ prefix for secrets – these get bundled into client JS and are visible to anyone.
  • Committing `.env` files because Cursor's initial project template might include a placeholder .env that you forget to ignore.
  • Hardcoding test keys in comments or example code – Cursor may generate code with placeholder secrets that slip into production.
  • Leaving secrets in source maps – if you have productionBrowserSourceMaps: true, your API keys can be extracted from the browser.
  • Not rotating keys after a leak – simply removing the key from code doesn't invalidate it; attackers can still use it.
  • How OverMCP helps

    OverMCP is a free AI app security scanner built for vibe-coded apps. It automatically scans your Next.js project for exposed secrets in code, Git history, and client-side bundles. Connect your Vercel project to get continuous security monitoring and catch leaks before they become breaches.

    FAQ

    How do I check if my Next.js app has leaked secrets?

    Use a secret leak scanner to scan your public repository, client-side JavaScript, and environment variables. Also check your hosting platform's logs for any keys accidentally printed.

    Can I use `NEXT_PUBLIC_` for API keys?

    No. Any variable prefixed with NEXT_PUBLIC_ is exposed to the browser. Only use it for non-sensitive values like public URLs. Always keep secrets in server-side environment variables.

    What if the leaked key is in a public GitHub repo?

    Immediately rotate the key. Then remove it from the repo's Git history using git filter-branch or BFG. Finally, add a .gitignore rule and consider using a secrets scanner in your CI/CD pipeline.

    Is your app secure?

    Free scan in 30 seconds. No signup needed.

    Scan My App Free