Skip to content

Commit 1451fc7

Browse files
authored
Merge pull request #9 from DevanshuNEU/fix/issue-4-cors-security
fix(security): restrict CORS to specific allowed origins
2 parents f9fc137 + cbb51c8 commit 1451fc7

3 files changed

Lines changed: 15 additions & 4 deletions

File tree

.env.example

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ SUPABASE_JWT_SECRET=your-jwt-secret # From Project Settings → API → JWT Sec
2020
API_KEY=change-this-secret-key-for-production
2121
BACKEND_API_URL=http://backend:8000
2222

23+
# CORS Configuration (Security)
24+
# Comma-separated list of allowed origins
25+
# Development: http://localhost:3000
26+
# Production: https://your-app.vercel.app,https://your-domain.com
27+
ALLOWED_ORIGINS=http://localhost:3000
28+
2329
# Redis (auto-configured in Docker, set REDIS_URL in Railway)
2430
REDIS_HOST=redis
2531
REDIS_PORT=6379

backend/.env.example

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ SUPABASE_JWT_SECRET=your_jwt_secret_here
1212
BACKEND_API_URL=http://localhost:8000
1313
API_KEY=dev-secret-key
1414

15+
# CORS Configuration (Security)
16+
ALLOWED_ORIGINS=http://localhost:3000
17+
1518
# Redis Cache
1619
REDIS_HOST=localhost
1720
REDIS_PORT=6379

backend/main.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@
3838
# Include routers
3939
app.include_router(auth_router)
4040

41-
# CORS middleware
41+
# CORS middleware - Restrict to specific origins for security
42+
ALLOWED_ORIGINS = os.getenv("ALLOWED_ORIGINS", "http://localhost:3000").split(",")
43+
4244
app.add_middleware(
4345
CORSMiddleware,
44-
allow_origins=["*"],
46+
allow_origins=ALLOWED_ORIGINS,
4547
allow_credentials=True,
46-
allow_methods=["*"],
47-
allow_headers=["*"],
48+
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
49+
allow_headers=["Authorization", "Content-Type"],
4850
)
4951

5052
# Request size limit middleware

0 commit comments

Comments
 (0)