Send the Gemini API key as a header, not a query param #351
main fromfix/350-gemini-key-header·merged by tombeckenham View on GitHubCloses #350
What#
The Gemini models poll put GEMINI_API_KEY in the request URL as ?key=. fetchJson quotes the URL in its error message, so the 400 at 2026-10-18T15:00:30Z put the live key into a PostHog ingest_failed event.
listModels now sends the key in the x-goog-api-key header, which the Gemini API accepts. The URL, and every error message that quotes it, no longer carries the secret. The error itself still throws as before; nothing is swallowed.
Test#
src/server/providers/gemini-list.test.ts stubs a 400 from the listing. It checks that the thrown message still names the status, contains no key, and that the key went in the header. It fails on main.
Not in this PR#
- The exposed key should be rotated, and the PostHog event holding it deleted. That has to happen outside the repo.
connect.securitySchemes(the auth hint served to agents) still saysin: query. That is valid for clients and out of scope here.
Review notes (non-critical, not fixed here)#
Four parallel reviews (correctness, silent failures, test quality, comment accuracy) found nothing critical. They noted:
pricingPromiseandwirePromisestart before the listing loop inlistModels. If the listing throws and one of those loads later rejects, nothing ever handles the rejection. This was already onmain; it is worth a small follow-up.body.models ?? []reads a page with nomodelsfield as empty. Also already onmain.- The test leaves non-listing fetches pending, so a future change that awaits docs before the listing would make it hang instead of fail. It covers only the first page; later pages go through the same code.
- I took the comment wording fix (name
fetchJson) in c4b7435.
🤖 Generated with Claude Code
- 1
Question Answer What it solves A failed Gemini models fetch put the live GEMINI_API_KEYintoingest_failedtelemetry, because the key was in the URL thatfetchJsonquotes in its error.Does it matter Yes. It is a secret leak to a third party (PostHog), and it repeats on every failed Gemini listing fetch. Added complexity Little: 4 lines of code changed, plus one 46-line test. Fallbacks that should be errors None added. The error still throws with its status. Public type/API changes None. The served connect.securitySchemesis unchanged.Users helped/hurt Helps the operator: no secret in telemetry. No catalog output changes. Standards compliance Follows CLAUDE.md. The test is under src/server/providers, uses noany, and touches no generated artifacts. Lint, typecheck, format and the full test suite (2055 tests) pass locally.Verdict Approve. It removes the cause of the leak with the smallest correct diff. The key still has to be rotated outside the repo.
Generated by Claude Code
tombeckenham merged thisc7544d9
tombeckenham closed this
Sign in to join this conversation.
Deploying with
Cloudflare Workers#
The latest updates on your project. Learn more about integrating Git with Workers.
View logs
Branch Preview URL