@codemod/secure-jwt-secret-keys
Hardcoded JWT secret keys in jsonwebtoken.sign() calls pose a critical security vulnerability. When JWT signing secrets are embedded directly in source code, they can be exposed through version control, logs, or other means, allowing attackers to forge authentication tokens and completely bypass application security.
This codemod identifies hardcoded secrets and replaces them with secure environment variable references.
🚨 Security Issue
CWE-798: Use of Hard-coded Credentials
Hardcoded JWT secrets allow attackers to:
- Forge authentication tokens
- Bypass application security
- Impersonate users
- Access unauthorized resources
What it does
This codemod automatically transforms hardcoded JWT secrets to use environment variables:
Before
javascript
After
javascript
Supported patterns
The codemod handles all common JWT signing patterns:
- Default imports:
jwt.sign() - Named imports:
sign()from{ sign } - Namespace imports:
jsonwebtoken.sign() - CommonJS:
require('jsonwebtoken') - String literals:
'secret'and"secret" - Template literals:
`secret-${env}`
Installation
bash
Usage
The codemod will scan your JavaScript/TypeScript files and:
- Detect hardcoded secrets in
jwt.sign()calls - Transform them to use
process.env.JWT_SECRET - Preserve all other function parameters and formatting
- Skip files that don't use jsonwebtoken
What gets transformed
✅ Will transform:
javascript
❌ Will NOT transform:
javascript
Configuration
After running the codemod, make sure to:
- Set the environment variable:
bash
- Add to your
.envfile:
text
- Use a strong secret:
bash
File patterns
The codemod processes these file types:
**/*.js- JavaScript files**/*.ts- TypeScript files**/*.jsx- React JavaScript files**/*.tsx- React TypeScript files
Excluded patterns:
node_modules/- Third-party dependencies- Test files (
*.test.*,*.spec.*,__tests__/) - Configuration files (
*.config.*) - Build outputs (
dist/,build/)
Examples
React/Express app
javascript
Multiple import styles
javascript
Security References
- CWE-798: Use of Hard-coded Credentials
- OWASP Top 10 A02:2021 - Cryptographic Failures
- JSON Web Token Best Practices
Development
bash
Contributing
- Fork the repository
- Create a feature branch
- Add test cases for your changes
- Run
pnpm testto ensure all tests pass - Submit a pull request
License
MIT