diff --git a/internal/api/server_routes.go b/internal/api/server_routes.go index bb02a013b8..e2b039a856 100644 --- a/internal/api/server_routes.go +++ b/internal/api/server_routes.go @@ -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" diff --git a/internal/api/server_websocket_route_test.go b/internal/api/server_websocket_route_test.go new file mode 100644 index 0000000000..392c153b21 --- /dev/null +++ b/internal/api/server_websocket_route_test.go @@ -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) + } + } + }) + } + } +} diff --git a/sdk/cliproxy/service_config.go b/sdk/cliproxy/service_config.go index 890af119e0..bc6214f4c1 100644 --- a/sdk/cliproxy/service_config.go +++ b/sdk/cliproxy/service_config.go @@ -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 diff --git a/sdk/cliproxy/service_stale_state_test.go b/sdk/cliproxy/service_stale_state_test.go index 0a61d29560..8577d2abc7 100644 --- a/sdk/cliproxy/service_stale_state_test.go +++ b/sdk/cliproxy/service_stale_state_test.go @@ -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) {