Your JWT Secret Is Probably Too Weak. Here Is the 30-Second Fix
DEV Community

Your JWT Secret Is Probably Too Weak. Here Is the 30-Second Fix

"secret", "changeme" and "mysupersecretkey" are not keys. They are passwords, and passwords get cracked offline.

Open the config of almost any tutorial project and you will find a line like this:

const token = jwt.sign(payload, 'secret');

It works. Tokens are issued and verified. And it is one of the most common ways a JWT-based login gets taken over.

Why a weak secret is a real vulnerability

With HS256, HS384 and HS512, the same secret signs tokens and verifies them. The signature is an HMAC of the header and payload.

The important part: anyone holding one valid token can test guesses for your secret offline. They compute the HMAC of that token's first two segments with a candidate secret and compare it to the signature. No requests to your server, no rate limits, no logs. A match means they have your secret and can sign any token they like, with any user ID and any role.

This is not theoretical. Weak signing keys are a standard exercise in security labs and a standard check in scanners. Security tooling can try a list of common weak secrets against a token with no traffic to your server, and the usual cracking tools (hashcat has a dedicated JWT mode, 16500) test candidates at enormous speed. A word like secret1 falls almost instantly. A random 32-byte value does not.

What the standard actually says

The JSON Web Algorithms specification (RFC 7518, section 3.2) states that for HS256 a key of the same size as the hash output or larger must be used. That is 256 bits, or 32 bytes. HS384 needs 384 bits (48 bytes) and HS512 needs 512 bits (64 bytes).

Note the word is bits of key material, not characters. The ASCII word secret is 6 characters, but it is not 48 bits of cryptographic secret: its real strength is how many guesses it takes to find, and that is tiny.

Algorithm Minimum secret Bytes Hex characters
HS256 256 bits 32 64
HS384 384 bits 48 96
HS512 512 bits 64 128

The fix: generate random bytes, don't invent a phrase

The secret must come from a cryptographically secure random generator. A memorable passphrase does not have enough entropy, even if it is long.

Node.js:

const crypto = require('crypto');
console.log(crypto.randomBytes(32).toString('base64url')); // 43 characters, 256 bits

OpenSSL (any terminal):

openssl rand -base64 32

Python:

import secrets
print(secrets.token_urlsafe(32))

Each prints a fresh, random 256-bit value. Use 48 or 64 bytes if you sign with HS384 or HS512.

If you would rather not open a terminal, you can generate one in the browser. The JWT Secret Key Generator uses crypto.getRandomValues locally, so the key is never sent anywhere.

A 30-second checklist

  • Is it random? Generated, not typed.
  • Is it long enough? At least 32 bytes for HS256, 48 for HS384, 64 for HS512.
  • Is it out of your code? Load it from an environment variable or a secret manager, not from a source file.
  • Is it different per environment? Development, staging and production should never share a secret. A leaked dev key must not sign production tokens.
  • Is it logged anywhere? Check startup logs, error reports and CI output.
  • Can you rotate it? If the answer is "that would log everyone out and we've never tried", fix that before you need it.

Loading it safely in Node.js

const jwt = require('jsonwebtoken');
const secret = process.env.JWT_SECRET;
if (!secret || Buffer.byteLength(secret) < 32) {
  throw new Error('JWT_SECRET is missing or too short');
}
const token = jwt.sign({ sub: 'u_4821' }, secret, {
  algorithm: 'HS256',
  expiresIn: '15m',
});
// Pin the algorithm when verifying
const payload = jwt.verify(token, secret, { algorithms: ['HS256'] });

Two small habits matter here. Crash on startup if the secret is missing or short, so a bad deploy fails loudly rather than quietly signing with undefined. And always pass algorithms when verifying, so the token's own header cannot choose the algorithm for you.

What if your secret is already weak?

Treat it as exposed. Generate a new one, deploy it, and accept that existing sessions are invalidated. Short-lived access tokens make this painless. If you cannot afford the logout, rotate gradually by accepting the old and new secrets for a short window, then retiring the old one. There is a walkthrough in How to Rotate JWT Signing Secrets Without Logging Everyone Out.

If your system has several services that only need to verify tokens, consider asymmetric signing (RS256 or ES256) so verifiers never hold a secret that can also forge. That trade-off is covered in JWT Secret Key Length and in HS256 vs RS256.

The short version

A JWT secret is a key, not a password. 32 random bytes for HS256, 48 for HS384, 64 for HS512. Generate it, store it outside your code, use a different one per environment. If yours is a word you can remember, replace it today.

What was the worst JWT secret you have found in a real codebase? I would like to hear it.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.