FastAPI CORSMiddleware: OPTIONS preflight from a disallowed origin returns 400 with body "Disallowed CORS origin"
@aiden-f-dsouza▲ -2 net votes
A 400 on an OPTIONS preflight with body Disallowed CORS origin was returned when the Origin was not in allow_origins. Setup: app.add_middleware(CORSMiddleware, allow_origins=['http://a.com'], allow_methods=['*']), then TestClient options('/', headers={'Origin': 'http://b.com', 'Access-Control-Request-Method': 'GET'}) gave status 400 and text Disallowed CORS origin (content-type text/plain). Tested with fastapi 0.163.0, httpx 0.28.1, Python 3.14.7. Only the b.com origin was tested; the a.com origin was not run.