Skip to content

Open source CapCut alternative.

TypeScript

fix(site): keep a stray Gemini API key from hijacking Vertex interactions auth

David Leacaa17952e85
diff --git a/site/src/lib/inference/adapters/gemini-client.ts b/site/src/lib/inference/adapters/gemini-client.ts
index dac53d42..1492683d 100644
--- a/site/src/lib/inference/adapters/gemini-client.ts
+++ b/site/src/lib/inference/adapters/gemini-client.ts
@@ -30,6 +30,45 @@ export function defaultGeminiClientFactory(options: GoogleGenAIOptions): GeminiC
return new GoogleGenAI(options);
}
+// Env vars @google/genai treats as an implicit API key.
+const geminiApiKeyEnvVars = ["GEMINI_API_KEY", "GOOGLE_API_KEY"] as const;
+
+// Build a Gemini client with the implicit API-key env vars scrubbed for the span
+// of construction. In Vertex mode we authenticate as a service-account principal,
+// but the SDK's interactions (NextGen) sub-client re-reads GEMINI_API_KEY /
+// GOOGLE_API_KEY from the process environment as an auth fallback and sends it as
+// `x-goog-api-key` before ever trying the Vertex JWT — and Vertex rejects API keys
+// (401 CREDENTIALS_MISSING). Constructing with those vars absent bakes the
+// sub-client's key to null, so its auth falls through to the JWT. `realize` forces
+// the lazily-built sub-client into existence inside the window; the scrub and
+// restore are fully synchronous, so no concurrent request observes the gap.
+export function constructGeminiClient<T>(
+ options: GoogleGenAIOptions,
+ construct: (options: GoogleGenAIOptions) => T,
+ realize?: (client: T) => void,
+): T {
+ if (!options.vertexai) {
+ return construct(options);
+ }
+ const saved = geminiApiKeyEnvVars.map(
+ (name) => [name, process.env[name]] as const,
+ );
+ for (const [name] of saved) {
+ delete process.env[name];
+ }
+ try {
+ const client = construct(options);
+ realize?.(client);
+ return client;
+ } finally {
+ for (const [name, value] of saved) {
+ if (value !== undefined) {
+ process.env[name] = value;
+ }
+ }
+ }
+}
+
export function geminiClientConfig(environment: AdapterEnvironment): {
configured: boolean;
options: GoogleGenAIOptions;
diff --git a/site/src/lib/inference/adapters/gemini-omni-video.ts b/site/src/lib/inference/adapters/gemini-omni-video.ts
index dc247b8d..c269e772 100644
--- a/site/src/lib/inference/adapters/gemini-omni-video.ts
+++ b/site/src/lib/inference/adapters/gemini-omni-video.ts
@@ -4,6 +4,7 @@ import type { GoogleGenAIOptions, Interactions } from "@google/genai";
type Interaction = Interactions.Interaction;
import {
+ constructGeminiClient,
geminiApiError,
geminiClientConfig,
stringValue,
@@ -40,7 +41,15 @@ export type GeminiOmniClient = Pick<GoogleGenAI, "interactions">;
export type GeminiOmniClientFactory = (options: GoogleGenAIOptions) => GeminiOmniClient;
const defaultOmniClientFactory: GeminiOmniClientFactory = (options) =>
- new GoogleGenAI(options);
+ constructGeminiClient(
+ options,
+ (opts) => new GoogleGenAI(opts),
+ // Realize the interactions (NextGen) sub-client while the API-key env vars are
+ // scrubbed, so its auth binds to the Vertex JWT instead of a stray env key.
+ (client) => {
+ void client.interactions;
+ },
+ );
export function createGeminiOmniVideoAssetProvider(
environment: AdapterEnvironment = process.env,