fix: skip JWT validation for OPTIONS preflight requests#359
fix: skip JWT validation for OPTIONS preflight requests#359rajan-chari wants to merge 1 commit intomainfrom
Conversation
The JWT middleware validated all HTTP methods on protected paths, causing CORS preflight (OPTIONS) requests to fail with 401/500 since they carry no Authorization header or JSON body. Add an early return for OPTIONS requests before auth checks, matching the behavior of the TypeScript SDK. Includes a new test. Fixes #317
There was a problem hiding this comment.
Pull request overview
This PR updates the FastAPI JWT validation middleware in microsoft-teams-apps to bypass authentication for CORS preflight OPTIONS requests, preventing unauthorized preflight requests to protected endpoints (e.g., /api/messages) from being rejected before reaching downstream CORS/route handling.
Changes:
- Skip JWT validation when
request.method == "OPTIONS"(in addition to non-validated paths). - Extend the JWT middleware test harness to allow specifying HTTP method on mocked requests.
- Add a new test ensuring
OPTIONSpreflight requests bypass auth validation and call through.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
packages/apps/src/microsoft_teams/apps/auth/jwt_middleware.py |
Adds an early-return condition to bypass JWT validation for OPTIONS requests. |
packages/apps/tests/test_jwt_middleware.py |
Updates request mock helper to include method and adds a regression test for OPTIONS preflight behavior. |
|
|
||
| @pytest.mark.asyncio | ||
| async def test_options_preflight_bypasses_auth(self, mock_call_next): | ||
| """OPTIONS preflight requests to validated paths bypass auth and call call_next directly.""" |
There was a problem hiding this comment.
Docstring has a duplicated word (“call call_next”). Consider rephrasing to remove the duplication (e.g., “bypass auth and call_next directly”).
Manual Test Results — Echo Bot (Python)Tested on branch
Code reviewOne-line change at CI: All green on Python 3.12/3.13/3.14. |
Summary
create_jwt_validation_middlewarevalidates all HTTP methods on protected paths. CORS preflight (OPTIONS /api/messages) has noAuthorizationheader or JSON body, so the middleware rejects it with 401 (caught as 500 by the generic handler).request.method == "OPTIONS"check to the early-return condition atjwt_middleware.py:38, allowing preflight requests to pass through without auth validation.test_options_preflight_bypasses_authverifies OPTIONS requests on validated paths skip auth and call through.Note: This fix addresses the auth-layer crash (500 → 405). Full CORS preflight support (returning 200 with proper headers) requires
CORSMiddlewareconfiguration, which is a separate enhancement — see the linked issue below.Test plan
Fixes #317
🤖 Generated with Claude Code