fix: resolve CORS middleware error causing backend restart
Some checks failed
CI/CD - Build & Test / Backend Tests (push) Has been cancelled
CI/CD - Build & Test / Frontend Tests (push) Has been cancelled
CI/CD - Build & Test / Security Scans (push) Has been cancelled
CI/CD - Build & Test / Docker Build Test (push) Has been cancelled
CI/CD - Build & Test / Terraform Validate (push) Has been cancelled
Deploy to Production / Build & Test (push) Has been cancelled
Deploy to Production / Security Scan (push) Has been cancelled
Deploy to Production / Build Docker Images (push) Has been cancelled
Deploy to Production / Deploy to Staging (push) Has been cancelled
Deploy to Production / E2E Tests (push) Has been cancelled
Deploy to Production / Deploy to Production (push) Has been cancelled
E2E Tests / Run E2E Tests (push) Has been cancelled
E2E Tests / Visual Regression Tests (push) Has been cancelled
E2E Tests / Smoke Tests (push) Has been cancelled

The issue was duplicate CORS middleware configuration:
- CORS was configured in main.py (correctly)
- CORS was also configured in security_headers.py (incorrectly)

The security_headers.py version was trying to instantiate CORSMiddleware
directly without the 'app' argument, causing:
TypeError: CORSMiddleware.__init__() missing 1 required positional argument: 'app'

Fixed by:
1. Removed CORS middleware from setup_security_middleware()
2. Updated config.py to include http://localhost:8888 in CORS origins
3. Kept CORS configuration only in main.py

Backend now starts successfully and responds to health checks.
This commit is contained in:
Luca Sacchi Ricciardi
2026-04-07 23:35:56 +02:00
parent 02907e4790
commit 9de9981492
2 changed files with 3 additions and 6 deletions

View File

@@ -44,8 +44,8 @@ class Settings(BaseSettings):
# Security
bcrypt_rounds: int = 12
cors_allowed_origins: List[str] = ["http://localhost:3000", "http://localhost:5173"]
cors_allowed_origins_production: List[str] = []
cors_allowed_origins: List[str] = ["http://localhost:3000", "http://localhost:5173", "http://localhost:8888"]
cors_allowed_origins_production: List[str] = ["http://localhost:8888"]
# Audit Logging
audit_logging_enabled: bool = True

View File

@@ -245,10 +245,7 @@ def setup_security_middleware(app):
Args:
app: FastAPI application instance
"""
# Add CORS middleware
cors_middleware = CORSSecurityMiddleware.get_middleware()
app.add_middleware(type(cors_middleware), **cors_middleware.__dict__)
# Note: CORS middleware is configured in main.py
# Add security headers middleware
app.add_middleware(SecurityHeadersMiddleware)