feat(core): add GEMINI_API_BASE_URL env var for custom API endpoints#16010
feat(core): add GEMINI_API_BASE_URL env var for custom API endpoints#16010albertpurnama wants to merge 3 commits intogoogle-gemini:mainfrom
Conversation
Allow users to override the default Gemini API base URL by setting the GEMINI_API_BASE_URL environment variable. This enables using the CLI with local LLM proxies like Ollama and LiteLLM. Fixes google-gemini#15430
Summary of ChangesHello @albertpurnama, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request enhances the core functionality by allowing users to specify a custom base URL for the Gemini API through a new environment variable, Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request adds support for a "GEMINI_API_BASE_URL" environment variable, a valuable feature for using local LLM proxies. However, this introduces a high-severity Server-Side Request Forgery (SSRF) vulnerability because the application accepts the URL from the environment variable without any validation, potentially allowing an attacker to direct requests to internal network services. Strict validation on the provided URL is recommended. Additionally, the logging mechanism in "packages/core/src/core/loggingContentGenerator.ts" needs to be updated. The "_getEndpointUrl" method currently logs the default Google API endpoint, which will be misleading when a custom base URL is used. It should be updated to correctly reflect the "GEMINI_API_BASE_URL" for accurate debugging.
| const baseUrl = process.env['GEMINI_API_BASE_URL'] || undefined; | ||
| const httpOptions = { | ||
| headers, | ||
| ...(baseUrl && { baseUrl }), | ||
| }; |
There was a problem hiding this comment.
A Server-Side Request Forgery (SSRF) vulnerability exists due to the introduction of the "GEMINI_API_BASE_URL" environment variable. This variable is used to construct the "baseUrl" for API requests without any validation. An attacker who can control this environment variable can force the application to make requests to arbitrary URLs, including internal network services or cloud provider metadata endpoints (e.g., "169.254.169.254"). This can lead to sensitive information disclosure, internal network scanning, and interaction with internal services. Remediation: The application should validate the "baseUrl" to ensure it points to a trusted, external domain. Implement a strict allow-list of permitted domains or, at a minimum, block requests to private/internal IP addresses and loopback devices. For example, check if the resolved IP address of the hostname is a private IP before making the request.
There was a problem hiding this comment.
seems overly cautious..
Environment variables are trusted input, they're set by the system admin/user who controls the machine. If an attacker can set env vars, they already have shell access and SSRF is the least of your worries.
Update _getEndpointUrl in loggingContentGenerator.ts to check for GEMINI_API_BASE_URL and return the correct server details for telemetry.
|
I agree with @albertpurnama. The security alert seems to think I will set the GEMINI_API_BASE_URL to servers I am not authorized to use. The server I assign in GEMINI_API_BASE_URL is responsible for this function, not the requesting application (Gemini CLI). nist-sp-800-53/r4/ AC and SC families, along other cyber security standards already dictate the contacted server hosts this responsibility. From perspective of user adoption, this change will eliminate the need for users to switch to other CLI tools when using other LLMs, particularly local LLMs. Pls approve. |
Summary
This PR adds support for a
GEMINI_API_BASE_URLenvironment variable that allows users to override the default Gemini API endpoint. This enables using the CLI as a frontend for local-first workflows with OpenAI-compatible proxies like Ollama and LiteLLM.Details
GEMINI_API_BASE_URLsupport incontentGenerator.ts@google/genaiSDK viahttpOptions.baseUrlRelated Issues
Fixes #15430
How to Validate
Test with custom endpoint:
Verify requests go to
localhost:4000instead ofgenerativelanguage.googleapis.comTest without env var:
Verify normal operation with default endpoint
Pre-Merge Checklist