Skip to content

Commit cbb51c8

Browse files
committed
fix(security): restrict CORS to specific allowed origins
Fixes #4 SECURITY VULNERABILITY: Previous configuration allowed ANY origin to make authenticated requests, enabling CSRF attacks from malicious websites. Changes: - Replace allow_origins=["*"] with environment-based configuration - Add ALLOWED_ORIGINS environment variable (comma-separated list) - Restrict allow_methods to specific HTTP methods (GET, POST, PUT, DELETE, OPTIONS) - Restrict allow_headers to required headers (Authorization, Content-Type) - Default to http://localhost:3000 for local development Configuration: - Development: ALLOWED_ORIGINS=http://localhost:3000 - Production: ALLOWED_ORIGINS=https://app.vercel.app,https://custom-domain.com Testing: ✅ Verified localhost:3000 receives CORS headers ✅ Verified unauthorized origins are blocked ✅ Backend health check confirms proper CORS restrictions Files modified: - backend/main.py: Updated CORS middleware configuration - .env.example: Added ALLOWED_ORIGINS documentation - backend/.env.example: Added ALLOWED_ORIGINS with example Impact: Prevents cross-site request forgery attacks in production
1 parent f9fc137 commit cbb51c8

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)