Merge pull request #7102 from router-for-me/aistudio

fix(home): restrict websocket access in home mode
This commit is contained in:
Chén Mù
2026-10-29 19:42:56 +08:00
committed by GitHub
4 changed files with 77 additions and 2 deletions

View File

@@ -565,11 +565,14 @@ func (s *Server) codexAlphaSearch(c *gin.Context) {
}
// AttachWebsocketRoute registers a websocket upgrade handler on the primary Gin engine.
// The handler is served as-is without additional middleware beyond the standard stack already configured.
// Home mode does not support AI Studio and must not expose its websocket gateway.
func (s *Server) AttachWebsocketRoute(path string, handler http.Handler) {
if s == nil || s.engine == nil || handler == nil {
return
}
if cfg := s.getConfig(); cfg != nil && cfg.Home.Enabled {
return
}
trimmed := strings.TrimSpace(path)
if trimmed == "" {
trimmed = "/v1/ws"

View File

@@ -0,0 +1,68 @@
package api
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/router-for-me/CLIProxyAPI/v8/internal/config"
)
func TestAttachWebsocketRouteDisabledInHomeMode(t *testing.T) {
for _, homeEnabled := range []bool{false, true} {
for _, wsAuth := range []bool{false, true} {
t.Run(fmt.Sprintf("home=%t/ws-auth=%t", homeEnabled, wsAuth), func(t *testing.T) {
cfg := &config.Config{
SDKConfig: config.SDKConfig{APIKeys: []string{"test-key"}},
Home: config.HomeConfig{Enabled: homeEnabled},
WebsocketAuth: wsAuth,
}
if homeEnabled {
// Home mode clears local API keys during startup.
cfg.APIKeys = nil
}
server := newTestServerWithConfig(t, cfg)
// Isolate route registration from the Home heartbeat readiness gate.
server.engine = gin.New()
handlerCalled := false
server.AttachWebsocketRoute("/v1/ws", http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
handlerCalled = true
w.WriteHeader(http.StatusNoContent)
}))
for _, route := range server.engine.Routes() {
if homeEnabled && route.Path == "/v1/ws" {
t.Fatal("websocket route must not be registered in Home mode")
}
}
for _, token := range []string{"", "test-key", "invalid-key"} {
handlerCalled = false
req := httptest.NewRequest(http.MethodGet, "/v1/ws", nil)
req.Header.Set("Connection", "Upgrade")
req.Header.Set("Upgrade", "websocket")
if token != "" {
req.Header.Set("Authorization", "Bearer "+token)
}
recorder := httptest.NewRecorder()
server.engine.ServeHTTP(recorder, req)
wantStatus := http.StatusNoContent
if homeEnabled {
wantStatus = http.StatusNotFound
} else if wsAuth && token != "test-key" {
wantStatus = http.StatusUnauthorized
}
if recorder.Code != wantStatus {
t.Errorf("token=%q: status = %d, want %d", token, recorder.Code, wantStatus)
}
if wantCalled := wantStatus == http.StatusNoContent; handlerCalled != wantCalled {
t.Errorf("token=%q: handler called = %t, want %t", token, handlerCalled, wantCalled)
}
}
})
}
}
}

View File

@@ -344,7 +344,7 @@ func forceHomeRuntimeConfig(cfg *config.Config) {
cfg.UsageStatisticsEnabled = true
cfg.DisableCooling = true
cfg.SaveCooldownStatus = false
cfg.WebsocketAuth = false
cfg.WebsocketAuth = true
cfg.RemoteManagement.AllowRemote = false
cfg.RemoteManagement.DisableControlPanel = true
cfg.Plugins.StoreAuth = nil

View File

@@ -78,6 +78,7 @@ func TestForceHomeRuntimeConfigEnablesUsageStatistics(t *testing.T) {
UsageStatisticsEnabled: false,
DisableCooling: false,
SaveCooldownStatus: true,
WebsocketAuth: false,
}
forceHomeRuntimeConfig(cfg)
@@ -91,6 +92,9 @@ func TestForceHomeRuntimeConfigEnablesUsageStatistics(t *testing.T) {
if cfg.SaveCooldownStatus {
t.Fatal("expected home runtime config to force cooldown status persistence disabled")
}
if !cfg.WebsocketAuth {
t.Fatal("expected home runtime config to force websocket authentication enabled")
}
}
func TestLifetimeRegistryObservesBarrierFromAppliedHomeConfig(t *testing.T) {