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
- 1Use at least 256 bits (32 bytes) of random entropy for HS256 secrets
- 2Prefer asymmetric algorithms (RS256, ES256) so the signing key never leaves the server
- 3Rotate secrets and invalidate all existing tokens if a weak or exposed secret is discovered
Code examples
Generate a strong HS256 secret
# 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)
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.
More api checks
- LowTRACE method referenced in API response body
- InfoREST endpoint allows DELETE
- LowGraphQL introspection field suggestions
- MediumGraphQL endpoint has no rate-limit headers
- LowOpenAPI server URL leaks internal host
- LowCORS preflight result not cached
- HighBearer token in URL query string
- MediumJSONP callback parameter accepted
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.