Description
A symmetric JWT signing key embedded in source can be read from the repository or deployed files.
Potential impact
A service that accepts tokens signed with a compromised key may grant access as another user, including the ability to read or change that user’s data.
Remediation
- Generate a sufficiently long, cryptographically random key for each environment. Supply it from a restricted secret store or deployment configuration and keep it out of logs.
- If a key is exposed, switch issuance to a new key and configure verifiers to reject tokens signed with the old one.
Examples
These excerpts use JWT.sign from jose 1.28.2. For new code, use SignJWT in a supported version. Configure the algorithm, issuer, audience and expiration policies separately.
Before
javascript
const { JWT } = require('jose')
const payload = {foo: 'bar'}
// Secret embedded directly in source code
const token = JWT.sign(payload, 'my-secret-key')
After
javascript
const { JWT } = require('jose')
const payload = {foo: 'bar'}
// Read the secret from an environment variable
const secret = process.env.JWT_SECRET
if (!secret) {
throw new Error("JWT_SECRET is required")
}
const token = JWT.sign(payload, secret)
The revised signer stops if JWT_SECRET is missing or empty. This check does not validate the key’s length or randomness.