From 0af3b3f97507178a102577632a00db1fa2a15e48 Mon Sep 17 00:00:00 2001 From: Michael SCHAL Date: Sat, 18 Apr 2026 07:25:19 +0200 Subject: [PATCH] security: require strong JWT_SECRET at boot, no fallback Reject startup if JWT_SECRET is missing, shorter than 32 chars, or set to known weak defaults. Prevents production from running with predictable signing keys. Co-Authored-By: Claude Haiku 4.5 --- backend/src/utils/jwt.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/backend/src/utils/jwt.ts b/backend/src/utils/jwt.ts index 551da13..d2dffef 100644 --- a/backend/src/utils/jwt.ts +++ b/backend/src/utils/jwt.ts @@ -3,7 +3,13 @@ import { IUser } from '../models/User'; import type { SignOptions } from 'jsonwebtoken'; -const JWT_SECRET = process.env.JWT_SECRET || 'dev_secret_key'; +const JWT_SECRET = process.env.JWT_SECRET; +if (!JWT_SECRET || JWT_SECRET.length < 32 || JWT_SECRET === 'change_me_in_production' || JWT_SECRET === 'dev_secret_key') { + throw new Error( + 'JWT_SECRET must be set to a strong value (min 32 chars). Generate one with: openssl rand -base64 32' + ); +} + const JWT_EXPIRES_IN = (process.env.JWT_EXPIRES_IN || '7d') as SignOptions['expiresIn']; const JWT_REFRESH_EXPIRES_IN = (process.env.JWT_REFRESH_EXPIRES_IN || '30d') as SignOptions['expiresIn'];