-
Notifications
You must be signed in to change notification settings - Fork 0
🛡️ Sentinel: [HIGH] Harden JWT Auth against Weak Secrets in L9 Commerce Engine #341
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,12 @@ | ||
| const jwt = require('jsonwebtoken'); | ||
| const { jwtSecret } = require('../../config'); | ||
| const config = require('../../config'); | ||
|
|
||
| const WEAK_SECRETS = ['dev_secret_change_in_production', 'test_secret', 'dev_secret', 'default_secret', 'secret']; | ||
|
|
||
| const isWeakSecret = (secret) => { | ||
| if (!secret) return true; | ||
| return WEAK_SECRETS.includes(secret.toLowerCase().trim()); | ||
| }; | ||
|
|
||
| const authenticateToken = (req, res, next) => { | ||
| const authHeader = req.headers['authorization']; | ||
|
|
@@ -9,12 +16,20 @@ const authenticateToken = (req, res, next) => { | |
| return res.status(401).json({ error: 'Access token required' }); | ||
| } | ||
|
|
||
| if (!jwtSecret || jwtSecret === 'dev_secret_change_in_production') { | ||
| const activeSecret = process.env.JWT_SECRET || config.jwtSecret; | ||
|
|
||
| if (!activeSecret) { | ||
| console.error('[Security] JWT_SECRET is not properly configured.'); | ||
| return res.status(500).json({ error: 'Internal server configuration error' }); | ||
| } | ||
|
|
||
| jwt.verify(token, jwtSecret, (err, user) => { | ||
| // [Security Hardening] Reject weak secrets in production environment | ||
| if (process.env.NODE_ENV === 'production' && isWeakSecret(activeSecret)) { | ||
| console.error('[Security] JWT_SECRET is weak, insecure, or default. Blocking authenticated endpoint access in production.'); | ||
| return res.status(500).json({ error: 'Internal server configuration error' }); | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default-secret test now failsMedium Severity Weak secrets are rejected only when Additional Locations (1)Reviewed by Cursor Bugbot for commit 59d93b8. Configure here. |
||
|
|
||
| jwt.verify(token, activeSecret, (err, user) => { | ||
| if (err) { | ||
| return res.status(403).json({ error: 'Invalid or expired token' }); | ||
| } | ||
|
|
||


There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Env secret bypasses config
Medium Severity
authenticateTokenresolves the secret asprocess.env.JWT_SECRET || config.jwtSecret, so a live env value always wins overconfig.jwtSecret. Existing suites that mockconfigand sign tokens with the mock secret (for exampletests/security.test.js) can hit 403 wheneverJWT_SECRETis set in the process environment, including from another test file or a developer shell.Reviewed by Cursor Bugbot for commit 59d93b8. Configure here.