JWT Secret Exposure Fix: Secure Your Vibe-Coded App
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:
process.env.JWT_SECRET or hardcoding? Search for JWT_SECRET, jwtSecret, or similar in your codebase.git log -p -S JWT_SECRET to find any commit that exposed the secret..js bundles.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.gitThen 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.*.local5. 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
config.js with secrets inline. Always move them to environment variables.git add . can expose your secret. Use a pre-commit hook to block it (e.g., npx secretlint).'my-secret' or 'secret123'. Generate a random one.How to prevent it in the future
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.