Skip to content

Commit 142e453

Browse files
fix: Allow OPTIONS requests on /openapi.json for CORS preflight (#1518)
* fix: Allow OPTIONS requests on /openapi.json for CORS preflight 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. * style: Remove extra blank line in DocsAuthMiddleware Fix minor style issue with double blank line after OPTIONS check. Signed-off-by: Mihai Criveti <[email protected]> --------- Signed-off-by: Mihai Criveti <[email protected]> Co-authored-by: Jason Sievert <[email protected]>
1 parent 93bf8cf commit 142e453

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

mcpgateway/main.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,10 @@ class DocsAuthMiddleware(BaseHTTPMiddleware):
910910
If a request to one of these paths is made without a valid token,
911911
the request is rejected with a 401 or 403 error.
912912
913+
Note:
914+
OPTIONS requests are exempt from authentication to support CORS preflight
915+
as per RFC 7231 Section 4.3.7 (OPTIONS must not require authentication).
916+
913917
Note:
914918
When DOCS_ALLOW_BASIC_AUTH is enabled, Basic Authentication
915919
is also accepted using BASIC_AUTH_USER and BASIC_AUTH_PASSWORD credentials.
@@ -951,6 +955,10 @@ async def dispatch(self, request: Request, call_next):
951955
"""
952956
protected_paths = ["/docs", "/redoc", "/openapi.json"]
953957

958+
# Allow OPTIONS requests to pass through for CORS preflight (RFC 7231)
959+
if request.method == "OPTIONS":
960+
return await call_next(request)
961+
954962
if any(request.url.path.startswith(p) for p in protected_paths):
955963
try:
956964
token = request.headers.get("Authorization")

0 commit comments

Comments
 (0)