fix(security): gate /v1beta endpoints behind api_keys auth#46
Open
Leslielu wants to merge 1 commit into
Open
Conversation
The auth gate only matched startswith("/v1/"), so the Google-native
/v1beta/* endpoints (Gemini CLI) bypassed api_keys entirely. Any request,
including ?key=test, was forwarded straight to the Gemini upstream, draining
the host IP's quota and triggering 429 rate limits for legitimate users.
- Widen gate from startswith("/v1/") to startswith("/v1") so /v1beta is covered
- _authorized() now also accepts x-goog-api-key header and ?key= query param
(Gemini CLI native styles), and checks every key source instead of
short-circuiting on a present-but-wrong Bearer header
- Applied to both monolithic gemini_web2api.py and package gemini_web2api/server.py
Co-Authored-By: Claude <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The
api_keysauth gate only matchedself.path.startswith("/v1/"). The Google-native/v1beta/*endpoints (Gemini CLI:/v1beta/models/{model}:generateContentand:streamGenerateContent) don't start with/v1/, so they bypassed_authorized()entirely. Any request — including?key=test— was forwarded straight to the Gemini upstream.Impact
On a publicly exposed instance, anyone can abuse
/v1betawith an arbitrary key to proxy Gemini through your host, draining your egress IP's quota and triggering429 Too Many Requestsfor all legitimate users. (Observed in the wild: a host received continuous?key=testtraffic on/v1betaand got rate-limited by Gemini for days.)Fix
startswith("/v1/")tostartswith("/v1")so/v1betais covered (do_GET+do_POST)._authorized()now also acceptsx-goog-api-keyheader and?key=query param (the native styles Gemini CLI sends), so legitimate CLI usage keeps working under auth.Bearerheader and never fell back tox-api-key; now every key source is checked.gemini_web2api.pyand the packagegemini_web2api/server.py.Compatibility
Gemini CLI users must set
GEMINI_API_KEYto a real key fromapi_keys(any placeholder worked before only because/v1betawas unauthenticated).Verification
/v1beta/models?key=test/v1beta/models(no key)/v1beta/models?key=<valid>/v1/modelswrong Bearer/v1/modelsvalid Bearer🤖 Generated with Claude Code