fix: skip JWT validation for OPTIONS preflight requests#359
Open
rajan-chari wants to merge 1 commit intomainfrom
Open
fix: skip JWT validation for OPTIONS preflight requests#359rajan-chari wants to merge 1 commit intomainfrom
rajan-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
Contributor
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”).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.Test plan
curl -X OPTIONS /api/messagesreturns 200curl -X POST /api/messages(no auth) still returns 401Fixes #317
🤖 Generated with Claude Code