← All posts
JWTsecurityvibe codingsecret exposureNext.js

JWT Secret Exposure Fix: Secure Your Vibe-Coded App

O

OverMCP Team

Quick answer

To fix JWT secret exposure in a vibe-coded app: immediately revoke the exposed secret, rotate all active tokens, and move the secret out of your codebase into environment variables or a secrets manager. Then scan your entire Git history with a free AI app security scanner to ensure no old commits still contain the secret.

What is JWT secret exposure?

JSON Web Token (JWT) secrets are used to sign and verify tokens that authenticate users. If this secret leaks—for example, hardcoded in a GitHub repo—anyone can forge valid tokens, impersonate users, and gain unauthorized access. Vibe-coded apps are especially prone because AI tools often generate code with secrets inline.

What to check first

Check these five things immediately:

  • Environment variables: Are you using process.env.JWT_SECRET or hardcoding? Search for JWT_SECRET, jwtSecret, or similar in your codebase.
  • Git history: Run git log -p -S JWT_SECRET to find any commit that exposed the secret.
  • Client-side code: If your app is deployed, check the browser's Developer Tools > Sources for any leaked secrets in .js bundles.
  • Third-party services: Check Supabase, Vercel, or hosting provider logs for unusual token usage.
  • Dependencies: Some AI-generated code pulls in JWT libraries with insecure defaults—verify your jsonwebtoken version is up-to-date.
  • Step-by-step fix

    1. Rotate the exposed secret

    Generate a new, strong secret (at least 256 bits) using:

    node -e "console.log(require('crypto').randomBytes(32).toString('hex'))"

    Update your .env file or environment variables in your hosting platform (Vercel, Netlify, etc.).

    2. Invalidate all existing tokens

    Add a token blacklist or simply change the secret—old tokens become invalid automatically. If you need immediate revocation, implement a token version field in your database:

    // In your JWT payload
    const token = jwt.sign({ userId, tokenVersion: user.tokenVersion }, process.env.JWT_SECRET);
    
    // On login, increment tokenVersion
    user.tokenVersion += 1;
    await user.save();

    3. Remove the secret from Git history

    Use git filter-branch or BFG Repo-Cleaner to purge the exposed secret from all commits. For example with BFG:

    java -jar bfg.jar --replace-text passwords.txt my-repo.git

    Then force-push the clean history. Note: Anyone who already cloned your repo will have the secret. Ask them to rotate.

    4. Add a `.env` file and never commit it

    Ensure .env is in your .gitignore. If you're using Next.js, Vite, or similar, follow this pattern:

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

    5. Scan for other leaked secrets

    Run a secret leak scanner across your entire repository to catch any other hardcoded keys (API keys, database passwords).

    Common mistakes

  • Hardcoding in config files: AI tools often generate a config.js with secrets inline. Always move them to environment variables.
  • Committing `.env` files: A single accidental git add . can expose your secret. Use a pre-commit hook to block it (e.g., npx secretlint).
  • Using weak secrets: Some AI-generated code uses 'my-secret' or 'secret123'. Generate a random one.
  • Not rotating after a leak: Some devs just delete the commit and think it's fixed. But the secret is still in Git history.
  • Ignoring token expiration: JWT tokens should have short expiration times (e.g., 15 minutes) to limit damage from leaks.
  • How to prevent it in the future

  • Use a secrets manager like Vercel Environment Variables, AWS Secrets Manager, or Doppler.
  • Implement a CI/CD scanner that runs on every push to detect secrets. OverMCP offers continuous security monitoring for this.
  • Educate your AI tool: When using Cursor or Copilot, prompt it to "never hardcode secrets."
  • Audit your code regularly: Schedule weekly scans with OverMCP's Vercel security scanner to catch issues early.
  • FAQ

    How do I know if my JWT secret is exposed?

    Check your Git history with git log -p -S JWT_SECRET, search your deployed JavaScript bundles for the secret, or run a dedicated free AI app security scanner.

    Can I invalidate JWTs without changing the secret?

    Yes, by maintaining a token blacklist or using a token version in your database. But changing the secret is simpler and ensures all tokens are invalidated immediately.

    What if the secret was used in client-side code?

    Never put JWT secrets in client-side code; they belong only on the server. If you did, assume it's compromised and rotate immediately. Use refresh tokens stored in HTTP-only cookies for better security.

    ---

    Regularly scanning your vibe-coded app for exposed secrets is the best defense. OverMCP's secret leak scanner can help you find and fix these issues before they become breaches.

    Is your app secure?

    Free scan in 30 seconds. No signup needed.

    Scan My App Free