Skip to content

Conversation

@crivetimihai
Copy link
Member

Summary

This PR fixes CORS preflight authentication issues that prevent browser-based OpenAPI integrations (Open WebUI, Swagger UI, etc.) from successfully fetching the OpenAPI specification.

  • Added OPTIONS method check in DocsAuthMiddleware.dispatch() to allow CORS preflight requests
  • OPTIONS requests are exempt from authentication per RFC 7231 Section 4.3.7
  • GET requests still require authentication (security maintained)

Changes

  • mcpgateway/main.py: Added OPTIONS bypass in DocsAuthMiddleware before authentication check

Testing

  • ✅ OPTIONS /openapi.json returns 200 OK with CORS headers (no auth required)
  • ✅ GET /openapi.json without auth returns 401 Unauthorized
  • ✅ GET /openapi.json with valid JWT returns 200 OK
  • ✅ All unit tests pass (3816 passed)
  • ✅ Linting passes (flake8, bandit)

Security Considerations

  • No security regression: GET requests still require authentication
  • OPTIONS only returns CORS headers (no sensitive data)
  • Follows IETF RFC 7231 standard for OPTIONS handling

Closes #1493

Co-authored-by: Jason Sievert jsievert@gmail.com

putz612 and others added 2 commits November 27, 2025 21:46
This change exempts OPTIONS requests from authentication on documentation
endpoints (/docs, /redoc, /openapi.json) to support CORS preflight requests.

Issue: Browser-based OpenAPI integrations (like Open WebUI) were failing
because CORS preflight OPTIONS requests cannot include Authorization headers
per RFC 7231 Section 4.3.7, but the DocsAuthMiddleware was enforcing
authentication on all requests including OPTIONS.

Solution: Check request.method == 'OPTIONS' before applying authentication,
allowing CORS preflight to succeed while still requiring authentication
for GET requests to actually fetch the OpenAPI spec.

This maintains security (GET still requires auth) while enabling proper
CORS support for browser-based integrations.

Fixes browser-based OpenAPI tool integration (Open WebUI, Swagger UI, etc.)
when AUTH_REQUIRED=false or when using proper authentication flows.
Fix minor style issue with double blank line after OPTIONS check.

Signed-off-by: Mihai Criveti <crivetimihai@gmail.com>
@crivetimihai crivetimihai merged commit 142e453 into main Nov 27, 2025
45 checks passed
@crivetimihai crivetimihai deleted the fix/cors-options-preflight-authentication branch November 27, 2025 22:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants