PC-008HIGHSecrets & API Keys

Hardcoded JWT Signing Secret

Detects hardcoded HMAC signing secrets assigned to JWT_SECRET, AUTH_SECRET, or SECRET_KEY variables in application code or configuration files.

Rule Slug
jwt-hardcoded-signing-secret
Affected Files
*
Detection Method
Regex + Entropy (>= 3.3)
Live Rule Tester100% Browser-Local
HIGH: Detected 1 violation
Line 5:JWT_SECRET = 'k9X#mP2$v••••••••••••••6uA0'(entropy: 5)

Vulnerable Pattern vs. Hardened Fix

Vulnerable Pattern
// auth/tokens.ts
import jwt from 'jsonwebtoken';

// HIGH RISK: Hardcoded signing secret in source code
const JWT_SECRET = 'k9X#mP2$vL8qR5nW3zB7cF1jH4tY6uA0';

export function signSession(userId: string) {
  return jwt.sign({ sub: userId, role: 'user' }, JWT_SECRET, { expiresIn: '7d' });
}
Recommended Fix
// auth/tokens.ts
import jwt from 'jsonwebtoken';

const jwtSecret = process.env.JWT_SECRET;
if (!jwtSecret || jwtSecret.length < 32) {
  throw new Error('JWT_SECRET must be set and at least 32 characters long');
}

export function signSession(userId: string) {
  return jwt.sign({ sub: userId, role: 'user' }, jwtSecret, { expiresIn: '7d' });
}

Why This Fails in Production

JSON Web Tokens (JWTs) signed with symmetric algorithms such as HS256, HS384, or HS512 rely entirely on the confidentiality and unpredictability of a shared secret key. When a developer hardcodes JWT_SECRET or AUTH_SECRET directly in application code (or falls back to a literal string like process.env.JWT_SECRET || "super-secret-prod-key-9x8k"), anyone who obtains read access to the repository, a leaked sourcemap, or a container image can mint arbitrary, valid session tokens offline.

With the signing secret in hand, an attacker no longer needs to crack passwords, bypass multi-factor authentication, or exploit SQL injection. They can simply construct a JSON payload containing {"sub": "1", "role": "admin", "exp": 1999999999}, sign it with the leaked HS256 secret using a one-line script, and attach it as an Authorization: Bearer header. Every stateless API endpoint will accept the forged token as completely authentic.

A subtle variant of this vulnerability occurs when teams leave a non-empty fallback string in code for convenience during local onboarding. If a production deployment accidentally omits the JWT_SECRET environment variable due to a typo in Terraform or GitHub Actions, the application silently boots using the public fallback secret from Git, leaving production open without throwing a startup error.

Always fail fast at startup if JWT_SECRET is undefined, generate at least 256 bits of cryptographically random entropy (openssl rand -base64 32), and inject the secret solely through runtime environment configuration.

Remediation Steps

  1. 1Generate a fresh 256-bit cryptographically random secret using openssl rand -base64 32 and update your production secret manager.
  2. 2Remove all literal fallback strings (e.g., || "default_secret") so the server refuses to boot if JWT_SECRET is missing.
  3. 3Note that rotating the JWT signing secret immediately invalidates all active user sessions. Plan a brief key-ring rotation window if uninterrupted re-authentication is required.
  4. 4Consider migrating high-security multi-service architectures to asymmetric RS256/EdDSA signing with JWKS rotation.

Frequently Asked Questions

How does PushCheck avoid false positives on placeholder values like JWT_SECRET="changeme"?
This rule combines regex detection with Shannon entropy validation (minimum entropy 3.3) and a placeholder denylist so values like changeme, your_secret_here, or test_secret do not trigger noise.
Why is process.env.JWT_SECRET || "dev-secret" dangerous in production?
If a deployment misconfiguration drops the environment variable, your production app silently falls back to the publicly known literal string in Git, enabling instant token forgery.
How long should a symmetric HS256 JWT secret be?
At least 32 random bytes (256 bits), typically encoded as 44 base64 characters or 64 hex characters generated via openssl rand.

Related Secrets & API Keys Rules

Scan local project folder →