Skip to content

fix: skip JWT validation for OPTIONS preflight requests#359

Open
rajan-chari wants to merge 1 commit intomainfrom
fix/317-jwt-options-preflight
Open

fix: skip JWT validation for OPTIONS preflight requests#359
rajan-chari wants to merge 1 commit intomainfrom
fix/317-jwt-options-preflight

Conversation

@rajan-chari
Copy link
Copy Markdown
Contributor

Summary

  • Bug: create_jwt_validation_middleware validates all HTTP methods on protected paths. CORS preflight (OPTIONS /api/messages) has no Authorization header or JSON body, so the middleware rejects it with 401 (caught as 500 by the generic handler).
  • Fix: Add request.method == "OPTIONS" check to the early-return condition at jwt_middleware.py:38, allowing preflight requests to pass through without auth validation.
  • Test: New test test_options_preflight_bypasses_auth verifies OPTIONS requests on validated paths skip auth and call through.

Test plan

  • Existing JWT middleware tests pass (6/6)
  • New OPTIONS preflight test passes
  • Bot deployment test: verify curl -X OPTIONS /api/messages returns 200
  • Bot deployment test: verify curl -X POST /api/messages (no auth) still returns 401
  • Regression: normal authenticated POST flow still works end-to-end

Fixes #317

🤖 Generated with Claude Code

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
Copilot AI review requested due to automatic review settings April 4, 2026 06:10
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 OPTIONS preflight 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."""
Copy link

Copilot AI Apr 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docstring has a duplicated word (“call call_next”). Consider rephrasing to remove the duplication (e.g., “bypass auth and call_next directly”).

Copilot uses AI. Check for mistakes.
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.

[Bug]: JWT middleware returns 500 for OPTIONS preflight requests

2 participants