{"record":{"id":"746ec5fb97d7d48e","repo":"JuliusBrussee/caveman","slug":"gemini-bearer-credential-requires-a-valid-x-goog-u","errorCode":null,"errorMessage":"gemini bearer credential requires a valid x-goog-user-project","messagePattern":"gemini bearer credential requires a valid x-goog-user-project","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"proxy/providers/adapter.go","lineNumber":547,"sourceCode":"\t\tif out.Get(\"anthropic-version\") == \"\" {\n\t\t\tout.Set(\"anthropic-version\", \"2023-06-01\")\n\t\t}\n\tcase \"gemini\":\n\t\tif credential.Key != \"\" {\n\t\t\tif credential.Scheme == \"bearer\" {\n\t\t\t\t// Standalone env fallback uses this synthetic marker to reach the\n\t\t\t\t// post-sanitization key resolver. It is not OAuth and must not be\n\t\t\t\t// forwarded or subjected to OAuth quota-project validation.\n\t\t\t\tif credential.Key == \"no-key-required\" {\n\t\t\t\t\tbreak\n\t\t\t\t}\n\t\t\t\t// Gemini CLI OAuth/ADC credentials are bearer tokens. Remapping one to\n\t\t\t\t// x-goog-api-key breaks authentication and silently defeats OAuth wrap.\n\t\t\t\t// Google also requires the caller's quota project for user OAuth. Never\n\t\t\t\t// guess it: wrong attribution is a billing and quota correctness bug.\n\t\t\t\tquotaProject := strings.TrimSpace(req.Header.Get(\"x-goog-user-project\"))\n\t\t\t\tif !validGoogleQuotaProject(quotaProject) {\n\t\t\t\t\treturn nil, fmt.Errorf(\"gemini bearer credential requires a valid x-goog-user-project\")\n\t\t\t\t}\n\t\t\t\tout.Set(\"authorization\", \"Bearer \"+credential.Key)\n\t\t\t\tout.Set(\"x-goog-user-project\", quotaProject)\n\t\t\t} else {\n\t\t\t\tout.Set(\"x-goog-api-key\", credential.Key)\n\t\t\t}\n\t\t}\n\tcase \"vertex\":\n\t\tif credential.Key != \"\" {\n\t\t\tout.Set(\"authorization\", \"Bearer \"+credential.Key)\n\t\t}\n\t\tcopyIfPresent(out, req.Header, \"x-vertex-ai-llm-request-type\")\n\t\tcopyIfPresent(out, req.Header, \"x-goog-user-project\")\n\tcase \"azure_openai\":\n\t\tif credential.Key != \"\" {\n\t\t\t// The standalone placeholder is a synthetic no-key marker used by\n\t\t\t// callers that explicitly allow env-backed API-key fallback. It is\n\t\t\t// not an Entra token and must not be forwarded as Authorization;","sourceCodeStart":529,"sourceCodeEnd":565,"githubUrl":"https://github.com/JuliusBrussee/caveman/blob/3ee70a102609e550bd2e68004bf5990a9341c851/proxy/providers/adapter.go#L529-L565","documentation":"When authenticating Gemini with a bearer-type credential (OAuth/ADC from Gemini CLI), the adapter must forward the caller's Google Cloud quota project via x-goog-user-project; remapping the bearer token to x-goog-api-key would break OAuth. If the incoming request's x-goog-user-project header is missing or fails validGoogleQuotaProject, it refuses rather than misattribute quota/billing.","triggerScenarios":"Sending a Gemini request using an OAuth/ADC bearer key without the x-goog-user-project header set, or with an invalid value (empty, wrong format).","commonSituations":"Clients migrated from API-key auth that never set x-goog-user-project; proxies stripping the header; users authenticating with gcloud ADC without a quota project configured (gcloud config set billing/quota_project).","solutions":["Set the request header x-goog-user-project to your Google Cloud project id","Run `gcloud billing projects set-quota-project` (or gcloud config set billing/quota_project) so ADC carries a quota project","Check that intermediate proxies/load balancers do not strip x-goog-user-project","Use an API-key credential instead of a bearer credential if you don't need OAuth"],"exampleFix":"// before\nreq.Header.Set(\"authorization\", \"Bearer \" + oauthToken) // no quota project\n// after\nreq.Header.Set(\"authorization\", \"Bearer \" + oauthToken)\nreq.Header.Set(\"x-goog-user-project\", \"my-gcp-project-id\")","handlingStrategy":"validation","validationCode":"qp := req.Header.Get(\"x-goog-user-project\")\nif strings.TrimSpace(qp) == \"\" {\n\treturn errors.New(\"gemini OAuth requests must set x-goog-user-project\")\n}","typeGuard":null,"tryCatchPattern":"u, err := adapter.ResolveUpstreamURL(ctx, req, route)\nif err != nil && strings.Contains(err.Error(), \"x-goog-user-project\") {\n\t// return 400 telling client to set the quota-project header\n}","preventionTips":["Always set x-goog-user-project when using OAuth/ADC Gemini credentials","Configure gcloud quota project for ADC workflows","Audit proxy middleware for header stripping"],"tags":["authentication","google-cloud","gemini"],"backgroundTag":"missing-api-key","analyzedSha":"3ee70a102609e550bd2e68004bf5990a9341c851","analyzedAt":"2026-09-20T15:53:39.229Z","contentChangedAt":"2026-09-20T15:53:39.229Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}