🛡️ Sentinel: [CRITICAL/HIGH] Fix hardcoded JWT secret and restrict CORS#1
🛡️ Sentinel: [CRITICAL/HIGH] Fix hardcoded JWT secret and restrict CORS#1davidraehles wants to merge 3 commits intomainfrom
Conversation
- Remove hardcoded fallback for JWT_SECRET in production - Restrict CORS origins to trusted domains only - Add security journaling in .jules/sentinel.md Co-authored-by: davidraehles <6085055+davidraehles@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
There was a problem hiding this comment.
Pull request overview
This PR addresses critical security vulnerabilities related to hardcoded JWT secrets and overly permissive CORS configuration. The changes enforce environment-based configuration for sensitive security settings while maintaining development-friendly fallbacks.
Changes:
- Enforced JWT_SECRET environment variable requirement in production with fail-fast validation
- Restricted CORS to specific origins using environment-based configuration with localhost development fallbacks
- Added Sentinel journal documentation describing the vulnerabilities and preventive measures
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| backend/src/middleware/auth.ts | Added production environment check for JWT_SECRET with development fallback, preventing hardcoded secrets in production |
| backend/src/server.ts | Replaced open CORS policy with origin whitelist based on ALLOWED_ORIGINS environment variable |
| .jules/sentinel.md | Added security journal entry documenting the hardcoded secret and CORS vulnerabilities with lessons learned |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| const JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key-change-in-production'; | ||
| // 🛡️ Sentinel: Ensure JWT_SECRET is set in environment, especially in production | ||
| if (!process.env.JWT_SECRET && process.env.NODE_ENV === 'production') { |
There was a problem hiding this comment.
The production check uses a top-level throw statement that executes at module load time. This is good for fail-fast behavior, but consider that if NODE_ENV is not set (undefined), the check will pass even in production environments. Consider making the check more strict by also validating when NODE_ENV is undefined or empty. For example:
if (!process.env.JWT_SECRET && (!process.env.NODE_ENV || process.env.NODE_ENV === 'production'))
This ensures that the JWT_SECRET is required unless explicitly running in a known development environment.
| if (!process.env.JWT_SECRET && process.env.NODE_ENV === 'production') { | |
| if ( | |
| !process.env.JWT_SECRET && | |
| (!process.env.NODE_ENV || process.env.NODE_ENV === 'production') | |
| ) { |
There was a problem hiding this comment.
Fixed. Expanded the condition to also require JWT_SECRET when NODE_ENV is undefined — now only explicitly set dev/test environments skip the check.
There was a problem hiding this comment.
Fixed. Expanded the condition to also require JWT_SECRET when NODE_ENV is undefined — now only explicitly set dev/test environments skip the check.
| app.use(cors({ | ||
| origin: process.env.ALLOWED_ORIGINS | ||
| ? process.env.ALLOWED_ORIGINS.split(',').map(s => s.trim()) | ||
| : ['http://localhost:5173', 'http://localhost:3000'], | ||
| methods: ['GET', 'POST', 'PUT', 'DELETE'], | ||
| allowedHeaders: ['Content-Type', 'Authorization'] | ||
| })); |
There was a problem hiding this comment.
The CORS configuration falls back to localhost origins when ALLOWED_ORIGINS is not set. While this is acceptable for development, consider adding a production safety check similar to the JWT_SECRET validation. In production, allowing localhost origins could be a security issue. Consider adding validation before the CORS middleware to ensure ALLOWED_ORIGINS is set in production environments.
There was a problem hiding this comment.
Fixed. Added a matching ALLOWED_ORIGINS production check before the CORS middleware — uses the same stricter pattern (throws when NODE_ENV is undefined or 'production').
There was a problem hiding this comment.
Fixed. Added a matching ALLOWED_ORIGINS production check before the CORS middleware — uses the same stricter pattern (throws when NODE_ENV is undefined or 'production').
… and CORS origins
- Require JWT_SECRET and ALLOWED_ORIGINS when NODE_ENV is 'production' or undefined - Improve CORS configuration with origin trimming - Update security journal with latest learnings Co-authored-by: davidraehles <6085055+davidraehles@users.noreply.github.com>
🚨 Severity: CRITICAL/HIGH
💡 Vulnerability: Hardcoded fallback for
JWT_SECRETand wide-open CORS policy.🎯 Impact: Attackers could potentially exploit the known secret to forge tokens or perform cross-origin attacks.
🔧 Fix: Removed hardcoded secret fallback (enforced in production) and restricted CORS origins.
✅ Verification: Verified build success and correct logic via code review.
PR created automatically by Jules for task 15109285565255988971 started by @davidraehles