fix: validate env on startup#385
Conversation
✅ Deploy Preview for github-spy ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
📝 WalkthroughWalkthroughThis PR hardens backend security by validating required environment variables at startup and sanitizing user data in authentication responses. The server now terminates immediately if SESSION_SECRET or MONGO_URI are missing, and login endpoints return only non-sensitive user fields (id, username, email) instead of full user objects. ChangesEnvironment validation and user response sanitization
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 inconclusive)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
backend/.env.example (1)
1-1: ⚡ Quick winUncomment the required
SESSION_SECRETkey in the example config.With Line 1 commented, copying
.env.exampleto.envleaves out a required variable and guarantees startup failure. Prefer a placeholder value so the required shape is explicit.Proposed change
-#SESSION_SECRET=your_secret_here +SESSION_SECRET=change_me_before_running🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@backend/.env.example` at line 1, Uncomment the SESSION_SECRET key in the example config so a placeholder value is present (replace the commented line "`#SESSION_SECRET`=your_secret_here" with "SESSION_SECRET=your_secret_here" or another safe placeholder) to ensure copying .env.example to .env includes the required SESSION_SECRET environment variable.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@backend/.env.example`:
- Line 1: Uncomment the SESSION_SECRET key in the example config so a
placeholder value is present (replace the commented line
"`#SESSION_SECRET`=your_secret_here" with "SESSION_SECRET=your_secret_here" or
another safe placeholder) to ensure copying .env.example to .env includes the
required SESSION_SECRET environment variable.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 6da8bcab-2133-4213-8aed-875c0473fc02
📒 Files selected for processing (4)
backend/.env.examplebackend/models/User.jsbackend/routes/auth.jsbackend/server.js
|
@mehul-m-prajapati @Srijatalamarla @ASR1015 please review and merge |
|
@adityack477 will do on weekend |
Related Issue
Description
If SESSION_SECRET is not set in .env, express-session silently uses
undefinedas the secret, making all session cookies insecure.Added a startup check that loops over required env vars and calls
process.exit(1)with a descriptive error if any are missing.backend/server.js— env validation block before middleware setupbackend/.env.example— new file listing required variablesHow Has This Been Tested?
Tested by removing SESSION_SECRET from .env and confirming the
server exits with the expected error message.
Screenshots (if applicable)
Type of Change
Summary by CodeRabbit
Bug Fixes
Chores