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:
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
# Run directly (recommended)
npx codemod@latest run @codemod/secure-jwt-secret-keys
# Or install globally
npm install -g @codemod/secure-jwt-secret-keys
codemod run @codemod/secure-jwt-secret-keys
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
// Before
import jwt from 'jsonwebtoken';
import { sign } from 'jsonwebtoken';
import * as jsonwebtoken from 'jsonwebtoken';
jwt.sign(data, 'secret1');
sign(data, 'secret2');
jsonwebtoken.sign(data, 'secret3');
// After
import jwt from 'jsonwebtoken';
import { sign } from 'jsonwebtoken';
import * as jsonwebtoken from 'jsonwebtoken';
jwt.sign(data, process.env.JWT_SECRET);
sign(data, process.env.JWT_SECRET);
jsonwebtoken.sign(data, process.env.JWT_SECRET);
# Run tests
pnpm test
# Test specific case
pnpm test --filter positive-default-import
# Update test snapshots
pnpm test -u
# Type check
pnpm check-types
Contributing
Fork the repository
Create a feature branch
Add test cases for your changes
Run pnpm test to ensure all tests pass
Submit a pull request
License
MIT
Ready to evolve your codebase?
Build tailored, compiler-aware skills to assess, plan, and automate complex codebase changes.