fix: move CORS allowed origins to env variable#482
Conversation
- Replace hardcoded allowedOrigins array with ALLOWED_ORIGINS env var - Add backend/.env.example documenting all required env vars - Prevents production URLs from leaking into source control - Ensures proper CSRF protection with credentials: true
✅ Deploy Preview for github-spy ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Warning Review limit reached
Your plan currently allows 1 review/hour. Refill in 56 minutes and 20 seconds. Your organization has run out of usage credits. Purchase more in the billing tab. ⌛ How to resolve this issue?After more review capacity refills, a review can be triggered using the We recommend that you space out your commits to avoid hitting the rate limit. 🚦 How do rate limits work?CodeRabbit enforces hourly rate limits for each developer per organization. Our paid plans have higher rate limits than trial, open-source, and free plans. In all cases, review capacity refills continuously over time. Please see our FAQ for further information. 📝 WalkthroughWalkthroughThis PR addresses a CORS security vulnerability by replacing hardcoded allowed origins with an environment-driven configuration. An ChangesEnvironment-driven CORS configuration
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Possibly related issues
Possibly related PRs
Suggested labels
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 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.
🎉 Thank you @adityapichikala for your contribution. Please make sure your PR follows https://github.com/GitMetricsLab/github_tracker/blob/main/CONTRIBUTING.md#-pull-request-guidelines
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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.
Inline comments:
In `@backend/server.js`:
- Around line 18-20: The ALLOWED_ORIGINS parsing assigned to allowedOrigins can
produce an empty/malformed allowlist (e.g., " , ") and accidentally block all
origins; update the logic that reads process.env.ALLOWED_ORIGINS so you split,
map(trim) and then filter out any empty strings (and optionally filter out
invalid/non-HTTP origins), and if the resulting array is empty fall back to the
default ['http://localhost:5173']; ensure this change targets the code that
defines allowedOrigins and references process.env.ALLOWED_ORIGINS so the service
never ends up with an empty allowlist.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: deab92c0-ea98-46d0-897c-bc3fd5d8759f
📒 Files selected for processing (2)
backend/.env.examplebackend/server.js
Related Issue
Description
The backend's CORS configuration had hardcoded origin URLs directly in server.js:
js
const allowedOrigins = ['http://localhost:5173', 'https://github-spy.etlify.app'];
This exposes production URLs in source control and makes it impossible to configure different origins per environment without code changes.
Changes made:
backend/server.js — Replaced the hardcoded allowedOrigins array with process.env.ALLOWED_ORIGINS (comma-separated). Falls back to http://localhost:5173 when the env var is not set, so local development works out of the box.
backend/.env.example (new file) — Added a template documenting all required environment variables including the new ALLOWED_ORIGINS.
How Has This Been Tested?
Tested with a standalone script that spins up an Express server with the same CORS middleware and verifies 4 scenarios:
All 4 tests passed.
Screenshots (if applicable)
Type of Change
Summary by CodeRabbit