Repository navigation
Description
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 missingdesktop/Backend-Rust/src/routes/chat_completions.rs:481-491— BYOK missingdesktop/Backend-Rust/src/paywall.rs:61-79— BYOK headers not forwardeddesktop/Backend-Rust/src/auth.rs:246-305— PaywalledAuthUser does not check BYOKbackend/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 4X-BYOK-*headers
Related
- Desktop trial paywall: client-side enforcement can be bypassed #7345 — Desktop trial paywall client-side bypass vectors
- feat: Free trial paywall — backend endpoint, desktop countdown UI, pre-expiry nudges (#7329) #7341 — Desktop free trial paywall UX
by AI for @beastoin
Activity
Add shared BYOK header helpers module (issue BasedHardware#7357)
Register byok module in Rust backend (issue BasedHardware#7357)
Forward BYOK headers to Python paywall endpoint (issue BasedHardware#…
Pass request headers to paywall checker for BYOK forwarding (issue Ba…
Add BYOK support to Gemini proxy routes (issue BasedHardware#7357)
Add BYOK support to Anthropic chat completions proxy (issue BasedHard…
Add changelog entry for BYOK gap fix (issue BasedHardware#7357)
Add paywall cache partitioning tests (issue BasedHardware#7357)
Add BYOK fingerprint validation + state cache to Rust backend (issue B…
Add Firestore methods for BYOK state, subscription plan, and account …
Replace Python paywall delegation with native Rust Firestore + Fireba…
Add BYOK fingerprint validation and byok_stripped flag to PaywalledAu…
Gate Gemini proxy BYOK key extraction on byok_stripped flag (issue Ba…
Gate Anthropic proxy BYOK key extraction on byok_stripped flag (issue B…
Gate TTS proxy BYOK key extraction on byok_stripped flag (issue Based…
Wire ByokStateCache and updated PaywallChecker in main.rs (issue Base…
Remove python_api_base from Config (issue BasedHardware#7357)
Fix: paywall escape hatch now respects byok_stripped flag (issue Base…
Pass byok_stripped to paywall checker from auth extractor (issue Base…
Fix: check subscription current_period_end for expired paid plans (is…
Fix: don't cache Firestore errors as inactive BYOK state (issue Based…
Fix: paid plan with missing current_period_end falls back to basic (i…
Add tests for byok_stripped paywall escape hatch prevention (issue Ba…
Desktop Rust backend: implement BYOK for Gemini/Anthropic proxies, fi…
Revert "Desktop Rust backend: implement BYOK for Gemini/Anthropic pro…
Reland: Desktop Rust BYOK fingerprint validation + native paywall (#7357