Skip to content

Desktop Rust backend: BYOK keys not used for Gemini/Anthropic proxy routes, paywall doesn't exempt BYOK users #7357

Description

@beastoin

Summary

The desktop Rust backend (desktop/Backend-Rust/) only supports BYOK for TTS (OpenAI). Gemini and Anthropic proxy routes always use server-side API keys, ignoring user-provided BYOK keys. Additionally, the paywall middleware does not forward BYOK headers to Python, so the request-level BYOK escape hatch cannot fire.

Current State

TTS route — BYOK works

src/routes/tts.rs:160-181 — openai_key_for_request() checks x-byok-openai header. If present, uses user's key and skips rate limiting. Falls back to server key otherwise.

// tts.rs:164-170
if let Some(value) = headers
    .get("x-byok-openai")
    .and_then(|value| value.to_str().ok())
    .map(str::trim)
    .filter(|value| !value.is_empty())
{
    return Ok((value, false)); // user's key, no rate limit
}

Gemini proxy — BYOK NOT implemented

src/routes/proxy.rs:151-186 — Always uses either Vertex AI service account token or server gemini_api_key from config. No x-byok-gemini header check exists anywhere in the file.

// proxy.rs:172-174 (fallback path)
if let Some(gemini_key) = state.config.gemini_api_key.as_ref() {
    let url = build_gemini_url(&effective_path, gemini_key);

Anthropic chat completions — BYOK NOT implemented

src/routes/chat_completions.rs:481-491 — Always uses server anthropic_api_key from config. No x-byok-anthropic header check.

// chat_completions.rs:482-491
let api_key = match route.provider {
    Provider::Anthropic => state
        .config
        .anthropic_api_key
        .as_ref()
        .ok_or_else(|| {
            tracing::error!("chat_completions: ANTHROPIC_API_KEY not configured");
            StatusCode::INTERNAL_SERVER_ERROR
        })?,
};

Paywall middleware — does NOT forward BYOK headers

src/paywall.rs:61-79 — is_paywalled() calls Python's /v1/users/me/paywall with only Authorization and X-App-Platform: macos headers. No X-BYOK-* headers are forwarded.

// paywall.rs:72-78
let response = self
    .http
    .get(&url)
    .header("Authorization", format!("Bearer {}", bearer_token))
    .header("X-App-Platform", "macos")
    .send()
    .await;

Python's is_trial_paywalled() (backend/utils/subscription.py:87-94) has a request-level escape hatch: if all 4 X-BYOK-* headers are present, it returns False immediately (not paywalled). But since Rust never forwards these headers, this escape hatch cannot fire for Rust-proxied paywall checks.

PaywalledAuthUser extractor — blocks BYOK users with 402

src/auth.rs:290-296 — If is_paywalled() returns true, returns 402 error. Does not check request headers for BYOK keys before blocking.

// auth.rs:290-295
if checker.0.is_paywalled(&uid, token).await {
    return Err(AuthError {
        error: "trial_expired".to_string(),
        message: "Desktop trial expired. Upgrade or bring your own keys.".to_string(),
    });
}

The error message tells users to "bring your own keys" but the code does not honor those keys.

Impact

A trial-expired free user who activates BYOK:

Feature Backend Works?
Chat Python (/v2/messages) Yes — Python checks BYOK headers directly
TTS Rust (/tts) Yes — checks x-byok-openai
Gemini (proactive insights, memory, tasks) Rust (/v1/proxy/gemini/*) No — uses server key, user's Gemini key ignored
Anthropic (chat completions via Rust) Rust (/v1/chat/completions) No — uses server key, user's Anthropic key ignored
Paywall gate Rust → Python No BYOK exemption — all Rust routes blocked for paywalled users

Affected Files

  • desktop/Backend-Rust/src/routes/tts.rs:160-181 — BYOK implemented (reference implementation)
  • desktop/Backend-Rust/src/routes/proxy.rs:151-186 — BYOK missing
  • desktop/Backend-Rust/src/routes/chat_completions.rs:481-491 — BYOK missing
  • desktop/Backend-Rust/src/paywall.rs:61-79 — BYOK headers not forwarded
  • desktop/Backend-Rust/src/auth.rs:246-305 — PaywalledAuthUser does not check BYOK
  • backend/utils/subscription.py:87-94 — Python request-level BYOK escape hatch (works but unreachable from Rust)
  • desktop/Desktop/Sources/APIClient.swift:95-99 — Swift client sends all 4 X-BYOK-* headers

Related

by AI for @beastoin

Activity

  1. added
    p2
    intelligence
    maintainer
    on May 18, 2026
  2. added 20 commits that reference this issue on May 18, 2026
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

    intelligencemaintainerp2

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions