Repository navigation
Description
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) — callsvalidate_byok_request(uid)which may call_byok_ctx.set(None)in thread copyutils/other/endpoints.py:get_current_user_uid_ws_listen(WS) —validate_byok_websocketruns inasyncio.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— addget_validated_byok_keys, remove BYOK from auth deps, revert WS dep to syncbackend/utils/byok.py— simplify or removeBYOKMiddleware, remove_check_byok_validityContextVar mutationsbackend/routers/transcribe.py— addbyok_keys=Depends(get_validated_byok_keys), callset_byok_keys()in handlerbackend/routers/chat.py— same patternbackend/main.py— removeBYOKMiddlewareif no longer needed- All other routers using
get_current_user_uidthat need BYOK — add the new dep
by AI for @beastoin
Activity
test(byok): add 52 UAT tests covering all BYOK code paths (#6945)
fix(byok): add thread-safe BYOK validation deps (#6945)
fix(byok): separate BYOK validation from auth deps (#6945)
fix(byok): add BYOK validation dep to /v4/listen handler (#6945)
fix(byok): add BYOK validation to chat and voice endpoints (#6945)
fix(byok): add BYOK validation to sync-local-files endpoints (#6945)
test(byok): update tests for separated BYOK dep pattern (#6945)
test(byok): add 52 UAT tests covering all BYOK code paths (#6945)
fix(byok): add thread-safe BYOK validation deps (#6945)
fix(byok): separate BYOK validation from auth deps (#6945)
fix(byok): add BYOK validation dep to /v4/listen handler (#6945)
fix(byok): add BYOK validation to chat and voice endpoints (#6945)
87 remaining items
test(byok): add 52 UAT tests covering all BYOK code paths (#6945)
feat(auth): add unified AuthMiddleware for HTTP auth + BYOK (#6945)
refactor(auth): deprecate HTTP auth deps, update with_rate_limit (#6945)
refactor(auth): wire AuthMiddleware into app startup (#6945)
refactor(auth): migrate all routers from Depends() to request.state (#…
test(auth): add 51 tests for AuthMiddleware route matching (#6945)
fix(test): update speaker embedding tests for request.state pattern (#…
chore: add auth middleware tests to test.sh (#6945)
fix(auth): add middle wildcard matching, fix announcement allowlist (#…
fix(auth): rate limiter fails closed when uid is missing (#6945)
fix(auth): restore Pydantic body params shadowed by request migration (…
test(auth): add tests for middle wildcard, announcement rules (#6945)
fix(auth): only install validated BYOK keys in ContextVar (#6945)
fix(auth): remove request: Request from nested closures/generators (#…
fix(test): update byok security tests for middleware pattern (#6945)