Skip to main content
CriticalAPIOWASP A02:2021CWE-326

How to fix: JWT HS256 signed with weak or hard-coded secret

HS256 trusts a shared secret. If the secret is short, default, or checked into source, attackers can sign forged tokens offline.

Why it matters

A weak HS256 secret (under 256 bits, or a known default like 'secret') can be cracked offline using hashcat in minutes. Once the secret is known, attackers can forge any JWT: impersonating any user, extending expiry, or granting admin access indefinitely.

HS256 trusts a shared secret. If the secret is short, default, or checked into source, attackers can sign forged tokens offline.

How to fix it

  1. 1Use at least 256 bits (32 bytes) of random entropy for HS256 secrets
  2. 2Prefer asymmetric algorithms (RS256, ES256) so the signing key never leaves the server
  3. 3Rotate secrets and invalidate all existing tokens if a weak or exposed secret is discovered

Code examples

Generate a strong HS256 secret

bash
# Generate a 256-bit (32 byte) random secret
openssl rand -base64 32

# Store as an environment variable: never hard-code it
export JWT_SECRET="$(openssl rand -base64 32)"

Switch to RS256 (recommended)

JavaScript
import { SignJWT, jwtVerify, importPKCS8, importSPKI } from 'jose';

// Sign with private key: only the server has this
const privateKey = await importPKCS8(process.env.JWT_PRIVATE_KEY, 'RS256');
const token = await new SignJWT({ sub: userId, role: 'user' })
  .setProtectedHeader({ alg: 'RS256' })
  .setExpirationTime('1h')
  .setIssuedAt()
  .sign(privateKey);

// Verify with public key: safe to distribute
const publicKey = await importSPKI(process.env.JWT_PUBLIC_KEY, 'RS256');
const { payload } = await jwtVerify(token, publicKey, { algorithms: ['RS256'] });

References

Detected automatically by VulnRadar. The check id is api-jwt-hs256-weak-secret, stable across every scan so you can filter or diff on it.

See every api check

Questions

How severe is JWT HS256 signed with weak or hard-coded secret?
VulnRadar rates this critical severity in the api category. The severity is fixed per check, not scored per site, so the same finding is the same severity on every scan.
Does VulnRadar check for this automatically?
Yes. It runs as part of every scan under the check id api-jwt-hs256-weak-secret, which stays the same between runs so you can filter, diff, or gate CI on it.
Which standards does JWT HS256 signed with weak or hard-coded secret map to?
It maps to CWE-326. In the OWASP Top 10 it falls under A02:2021.

Missing a check?

The detection set is open. If you know a misconfiguration this does not catch, propose it: the issue template asks for the threat model, how to detect it, the false-positive risk, and a test fixture, which is the same thing a maintainer would have to work out anyway.

Propose a checkHow a check is built

Scan your own site for jwt hs256 signed with weak or hard-coded secret

VulnRadar runs this check and hundreds more against a URL in seconds. No agent to install, just paste a URL.

Or scan this site as a demoRead the docs

JWT HS256 signed with weak or hard-coded secret | VulnRadar