Skip to content

HOTFIX: API key auth was broken -- AuthenticationError blocked fallback path#285

Merged
DevanshuNEU merged 1 commit into
OpenCodeIntel:mainfrom
DevanshuNEU:hotfix/api-key-auth-fallback
Mar 7, 2026
Merged

HOTFIX: API key auth was broken -- AuthenticationError blocked fallback path#285
DevanshuNEU merged 1 commit into
OpenCodeIntel:mainfrom
DevanshuNEU:hotfix/api-key-auth-fallback

Conversation

@DevanshuNEU

@DevanshuNEU DevanshuNEU commented Mar 7, 2026

Copy link
Copy Markdown
Collaborator

Production bug -- MCP 401 on every request

One line in middleware/auth.py was blocking ALL API key authentication since day one.

Root cause

# BEFORE (broken): AuthenticationError raises 401, API key path never runs
except AuthenticationError as e:
    raise HTTPException(status_code=401, detail=str(e))

# AFTER (fixed): allows fallback to _validate_api_key
except AuthenticationError:
    return None

When a ci_ API key hits _validate_jwt, it fails local JWT decode, then falls back to Supabase API verification, which also fails and raises AuthenticationError. This was caught and immediately raised HTTP 401 -- the _validate_api_key function never got a chance to run.

Impact

Fix

1 file, 3 lines changed. Catch AuthenticationError and return None (same as InvalidTokenError) to allow the API key fallback path.

392 backend tests pass.

Summary by CodeRabbit

  • Bug Fixes
    • Authentication now falls back to API key validation when JWT authentication fails, reducing unnecessary immediate rejections and improving successful request handling.

@vercel

vercel Bot commented Mar 7, 2026

Copy link
Copy Markdown

@DevanshuNEU is attempting to deploy a commit to the Dev's projects Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Mar 7, 2026

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: ac03eedd-88bc-42b9-b627-00599afeb105

📥 Commits

Reviewing files that changed from the base of the PR and between db69945 and 2a072c5.

📒 Files selected for processing (1)
  • backend/middleware/auth.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • backend/middleware/auth.py

📝 Walkthrough

Walkthrough

Modified JWT validation in the authentication middleware: _validate_jwt now returns None on AuthenticationError instead of raising an HTTP 401, allowing the request to continue and enabling fallback to API key validation.

Changes

Cohort / File(s) Summary
JWT Fallback Logic
backend/middleware/auth.py
Changed _validate_jwt behavior to return None on AuthenticationError (previously raised HTTP 401), preserving fallback paths (e.g., API key validation) instead of terminating with 401.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Middleware as Auth Middleware
    participant JWT as JWT Validator
    participant APIKey as API Key Validator
    participant App as Application

    Client->>Middleware: Send request with auth header
    Middleware->>JWT: Validate JWT
    alt JWT valid
        JWT-->>Middleware: Return user info
        Middleware->>App: Forward authenticated request
    else JWT invalid or AuthenticationError (now returns None)
        JWT-->>Middleware: Return None
        Middleware->>APIKey: Check API key
        alt API key valid
            APIKey-->>Middleware: Return API key identity
            Middleware->>App: Forward authenticated request
        else
            APIKey-->>Middleware: Reject
            Middleware-->>Client: Respond 401
        end
    end
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐰 I hopped through headers, found a sigh—

JWT returned None, not a cry,
Keys in the burrow waited near,
Fallback ready, calm and clear,
🥕 hopping on, the request went by.

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: fixing a production bug where AuthenticationError in JWT validation was blocking the fallback to API key authentication.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

… cause)

_validate_jwt caught AuthenticationError and raised HTTPException 401
immediately, preventing _validate_api_key from ever running.

ci_ API keys hit verify_jwt -> fail local decode -> fall back to
Supabase API call -> also fails -> raises AuthenticationError.
The middleware treated this as a hard failure instead of allowing
the API key path to try next.

Fix: catch AuthenticationError and return None (same as InvalidTokenError).
Root cause of all MCP 401 errors since day one.
@DevanshuNEU DevanshuNEU force-pushed the hotfix/api-key-auth-fallback branch from db69945 to 2a072c5 Compare March 7, 2026 19:01
@vercel

vercel Bot commented Mar 7, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
opencodeintel Ignored Ignored Preview Mar 7, 2026 7:08pm

@DevanshuNEU DevanshuNEU merged commit 76af000 into OpenCodeIntel:main Mar 7, 2026
8 checks passed
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.

1 participant