Skip to content

BYOK: separate validated-keys dependency to fix ContextVar thread-copy bug #6945

Closed as not planned

Description

@beastoin

Current Behavior

BYOK key validation uses _byok_ctx.set(None) inside sync FastAPI dependencies (get_current_user_uid for HTTP, get_current_user_uid_ws_listen for WS) to clear headers for non-BYOK users. Starlette runs sync deps in worker threads with a copied ContextVar context, so the clearance is discarded when the thread returns. Non-BYOK users who send BYOK headers keep those headers in the handler's context.

The WS path was partially fixed by making the dep async (commit 4f14486), but validate_byok_websocket still runs via asyncio.to_thread, so the clearance inside it is still lost.

Affected paths:

  • utils/other/endpoints.py:get_current_user_uid (HTTP) — calls validate_byok_request(uid) which may call _byok_ctx.set(None) in thread copy
  • utils/other/endpoints.py:get_current_user_uid_ws_listen (WS) — validate_byok_websocket runs in asyncio.to_thread, clearance lost

Expected Behavior

Non-BYOK users who send BYOK headers should have those headers ignored. BYOK-active users with matching keys should have their keys used downstream.

Solution

Replace the current pattern (ContextVar mutations inside sync deps / thread-offloaded validation) with a single sync dependency that returns validated keys without touching the ContextVar. The handler sets the ContextVar in its own async context.

Auth dep (sync)     → returns uid only, no BYOK side effects
BYOK dep (sync)     → extracts, validates, returns keys (or empty dict)
Handler (async)     → calls set_byok_keys() in its own context

One get_validated_byok_keys dep works for both HTTP and WS — extracts from request or websocket, validates against Firestore, returns keys. No async trick, no asyncio.to_thread, no ContextVar mutations in sync code.

The BYOKMiddleware extraction can be removed since the dep handles it. The middleware's dispatch currently only extracts headers into the ContextVar, which the new dep replaces.

Impact

Low — the bug only affects non-BYOK users who send BYOK headers (edge case). BYOK-active users with matching keys work correctly on current main. Subscription bypass is protected by is_byok_active(uid) which reads Firestore directly.

Files to Modify

  • backend/utils/other/endpoints.py — add get_validated_byok_keys, remove BYOK from auth deps, revert WS dep to sync
  • backend/utils/byok.py — simplify or remove BYOKMiddleware, remove _check_byok_validity ContextVar mutations
  • backend/routers/transcribe.py — add byok_keys=Depends(get_validated_byok_keys), call set_byok_keys() in handler
  • backend/routers/chat.py — same pattern
  • backend/main.py — remove BYOKMiddleware if no longer needed
  • All other routers using get_current_user_uid that need BYOK — add the new dep

by AI for @beastoin

Activity

  1. added
    p2
    bug
    backend
    on Apr 27, 2026
  2. 87 remaining items

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendbugp2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions