Skip to content

Commit 4e62471

Browse files
fix: move CORS allowed origins to env variable
- 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
1 parent 6c6bc3e commit 4e62471

2 files changed

Lines changed: 22 additions & 3 deletions

File tree

backend/.env.example

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Server
2+
PORT=5000
3+
NODE_ENV=development
4+
5+
# MongoDB
6+
MONGO_URI=mongodb://127.0.0.1:27017/github_tracker
7+
8+
# Session
9+
SESSION_SECRET=your_session_secret_here
10+
11+
# CORS — comma-separated list of allowed frontend origins
12+
# In production, set this to your actual frontend URL(s).
13+
# If not set, defaults to http://localhost:5173
14+
ALLOWED_ORIGINS=http://localhost:5173

backend/server.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,18 @@ const logger = require('./logger');
1313

1414
const app = express();
1515

16-
// CORS configuration
17-
const allowedOrigins = ['http://localhost:5173', 'https://github-spy.etlify.app'];
16+
// CORS configuration — allowed origins are read from the ALLOWED_ORIGINS env var
17+
// (comma-separated). Falls back to localhost for local development.
18+
const allowedOrigins = process.env.ALLOWED_ORIGINS
19+
? process.env.ALLOWED_ORIGINS.split(',').map(origin => origin.trim())
20+
: ['http://localhost:5173'];
21+
1822
app.use(cors({
1923
origin: function (origin, callback) {
24+
// Allow requests with no origin (e.g. server-to-server, curl, mobile apps)
2025
if (!origin || allowedOrigins.indexOf(origin) !== -1) {
2126
callback(null, true);
22-
} else{
27+
} else {
2328
callback(new Error('Blocked by CORS policy'));
2429
}
2530
},

0 commit comments

Comments
 (0)