firebase-android-sdk icon indicating copy to clipboard operation
firebase-android-sdk copied to clipboard

firebase-ai: LiveGenerativeModel.connect() WebSocket does not attach X-Firebase-AppCheck header

Open jacksonej opened this issue 7 months ago • 1 comments

Environment

  • Firebase Component: AI Logic (firebase-ai)
  • Component version: 17.11.0 (via firebase-bom 34.12.0); also present on main as of today.

Problem

When App Check is enforced on firebasevertexai.googleapis.com, Gemini Live sessions fail with:

ServiceConnectionHandshakeFailedException: Channel was closed by the server. Details: Firebase App Check token is invalid.

HTTPS methods on the same FirebaseAI instance (generateContent, generateContentStream, countTokens) succeed with identical App Check setup. Only the Live API path (liveModel().connect()) fails.

Looking at APIController.kt:258-259:

suspend fun getWebSocketSession(location: String): DefaultClientWebSocketSession =
  client.webSocketSession(getBidiEndpoint(location)) { applyCommonHeaders() }

The WebSocket config lambda calls only applyCommonHeaders(). It does not call applyHeaderProvider(), which is what sets the X-Firebase-AppCheck header (via AppCheckHeaderProvider.generateHeaders()). Other methods like countTokens (line 273) correctly call both.

Result: WebSocket upgrade request is sent without an App Check token, so the server rejects it.

Steps to reproduce

  1. Enable App Check enforcement on firebasevertexai.googleapis.com.
  2. Install a provider factory at startup:
    Firebase.appCheck.installAppCheckProviderFactory(PlayIntegrityAppCheckProviderFactory.getInstance())
    
  3. Call Live API:
    FirebaseAI.getInstance(
        backend = GenerativeBackend.vertexAI("us-central1"),
        useLimitedUseAppCheckTokens = true
    ).liveModel("gemini-live-2.5-flash-native-audio").connect()
    
  4. Observe ServiceConnectionHandshakeFailedException: ... App Check token is invalid.
  5. Confirm generativeModel(...).generateContentStream(...) works on the same setup.

jacksonej avatar Apr 23 '26 06:04 jacksonej

Hi @jacksonej, thank you for reaching out. I was able to reproduce the issue with the steps and code snippets you shared. I'll raise this to our engineers along with the PR you submitted to resolve the issue. Thanks!

lehcar09 avatar Apr 23 '26 18:04 lehcar09