From 06405f2129ea16f0cdd98b1442ee84da1df6d901 Mon Sep 17 00:00:00 2001 From: Luis Pater Date: Fri, 3 Apr 2026 21:22:03 +0800 Subject: [PATCH] fix(security): enforce stricter localhost validation for GeminiCLIAPIHandler Closes: #2445 --- sdk/api/handlers/gemini/gemini-cli_handlers.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/sdk/api/handlers/gemini/gemini-cli_handlers.go b/sdk/api/handlers/gemini/gemini-cli_handlers.go index b5fd4943..df5efc42 100644 --- a/sdk/api/handlers/gemini/gemini-cli_handlers.go +++ b/sdk/api/handlers/gemini/gemini-cli_handlers.go @@ -9,6 +9,7 @@ import ( "context" "fmt" "io" + "net" "net/http" "strings" "time" @@ -49,7 +50,13 @@ func (h *GeminiCLIAPIHandler) Models() []map[string]any { // CLIHandler handles CLI-specific requests for Gemini API operations. // It restricts access to localhost only and routes requests to appropriate internal handlers. func (h *GeminiCLIAPIHandler) CLIHandler(c *gin.Context) { - if !strings.HasPrefix(c.Request.RemoteAddr, "127.0.0.1:") { + requestHost := c.Request.Host + requestHostname := requestHost + if hostname, _, errSplitHostPort := net.SplitHostPort(requestHost); errSplitHostPort == nil { + requestHostname = hostname + } + + if !strings.HasPrefix(c.Request.RemoteAddr, "127.0.0.1:") || requestHostname != "127.0.0.1" { c.JSON(http.StatusForbidden, handlers.ErrorResponse{ Error: handlers.ErrorDetail{ Message: "CLI reply only allow local access",