mirror of
https://github.com/multica-ai/multica.git
synced 2026-08-04 07:34:25 +02:00
* feat(vcs): gate self-hosted Git providers to self-host deployments only (MUL-3772) The Forgejo/Gitea/GitLab integration is intended for self-hosted Multica, where Multica can reach a Git instance on the operator's own network. On the managed multi-tenant cloud it adds an SSRF surface (connect validates a user-supplied instance URL from the server) and would store third-party Git tokens for all tenants under one key, while only serving the small subset of users whose instance is publicly reachable. Product decision: offer it on self-host only. - Add an explicit deployment switch MULTICA_VCS_INTEGRATION_ENABLED (default off). Connect, rotate, and webhook now require BOTH the switch on AND a valid MULTICA_VCS_SECRET_KEY — the switch is the product boundary, not key presence alone. When off, connect/rotate return 404 and the webhook returns a bare 404 (no config leak), independent of the frontend. - /api/config exposes vcs_integration_available (mirrors the switch, omitted when false) so the Settings UI hides the whole "Git providers" section on cloud instead of surfacing an operator-only "missing key" hint. - docker-compose.selfhost.yml defaults the switch on; .env.example documents it. - Docs (en/zh) lead with a callout: available on self-hosted Multica only, not Multica Cloud, and clarify "self-hosted" means Multica itself, not just Git. #5006 / #5883 stay in place — the schema and backend capability are retained; this only gates availability. No cloud VCS connection can exist (connect always required the key, which the cloud never set), so nothing needs migrating. Verified: go build/vet + VCS/config handler tests on a fresh migrated DB (incl. a new disabled-deployment 404 test); pnpm typecheck (core + views) and the integrations-tab + core schema/config vitest suites pass. Co-authored-by: multica-agent <github@multica.ai> * fix(vcs): complete self-host integration gating (MUL-5138) Co-authored-by: multica-agent <github@multica.ai> --------- Co-authored-by: Bohan-J <bohan@devv.ai> Co-authored-by: multica-agent <github@multica.ai>
432 lines
15 KiB
Go
432 lines
15 KiB
Go
package handler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/multica-ai/multica/server/internal/auth"
|
|
)
|
|
|
|
func TestGetConfigReportsCdnSignedMode(t *testing.T) {
|
|
origStorage := testHandler.Storage
|
|
origSigner := testHandler.CFSigner
|
|
testHandler.Storage = &mockStorage{}
|
|
defer func() {
|
|
testHandler.Storage = origStorage
|
|
testHandler.CFSigner = origSigner
|
|
}()
|
|
|
|
fetch := func() AppConfig {
|
|
t.Helper()
|
|
req := httptest.NewRequest(http.MethodGet, "/api/config", nil)
|
|
w := httptest.NewRecorder()
|
|
testHandler.GetConfig(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("GetConfig: expected 200, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
var cfg AppConfig
|
|
if err := json.Unmarshal(w.Body.Bytes(), &cfg); err != nil {
|
|
t.Fatalf("decode config: %v", err)
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
testHandler.CFSigner = nil
|
|
if cfg := fetch(); cfg.CdnSigned {
|
|
t.Fatalf("cdn_signed: want false without a CloudFront signer, got true")
|
|
}
|
|
|
|
// With signing enabled the same cdn_domain serves private content via
|
|
// signed URLs only — clients must be told raw storage URLs won't load.
|
|
testHandler.CFSigner = &auth.CloudFrontSigner{}
|
|
cfg := fetch()
|
|
if !cfg.CdnSigned {
|
|
t.Fatalf("cdn_signed: want true with a CloudFront signer, got false")
|
|
}
|
|
if cfg.CdnDomain != "cdn.example.com" {
|
|
t.Fatalf("cdn_domain: want cdn.example.com alongside cdn_signed, got %q", cfg.CdnDomain)
|
|
}
|
|
}
|
|
|
|
func TestGetConfigIncludesRuntimeAuthConfig(t *testing.T) {
|
|
origStorage := testHandler.Storage
|
|
testHandler.Storage = &mockStorage{}
|
|
defer func() { testHandler.Storage = origStorage }()
|
|
|
|
t.Setenv("ALLOW_SIGNUP", "false")
|
|
t.Setenv("GOOGLE_CLIENT_ID", "google-client-id")
|
|
t.Setenv("POSTHOG_API_KEY", "phc_test")
|
|
t.Setenv("POSTHOG_HOST", "https://eu.i.posthog.com")
|
|
t.Setenv("MULTICA_PUBLIC_URL", "https://api.example.com/")
|
|
t.Setenv("MULTICA_APP_URL", "https://app.example.com/")
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/config", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
testHandler.GetConfig(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("GetConfig: expected 200, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var cfg AppConfig
|
|
if err := json.Unmarshal(w.Body.Bytes(), &cfg); err != nil {
|
|
t.Fatalf("decode config: %v", err)
|
|
}
|
|
|
|
if cfg.CdnDomain != "cdn.example.com" {
|
|
t.Fatalf("cdn_domain: want cdn.example.com, got %q", cfg.CdnDomain)
|
|
}
|
|
if cfg.AllowSignup {
|
|
t.Fatalf("allow_signup: want false, got true")
|
|
}
|
|
if cfg.GoogleClientID != "google-client-id" {
|
|
t.Fatalf("google_client_id: want google-client-id, got %q", cfg.GoogleClientID)
|
|
}
|
|
if cfg.PosthogKey != "phc_test" {
|
|
t.Fatalf("posthog_key: want phc_test, got %q", cfg.PosthogKey)
|
|
}
|
|
if cfg.PosthogHost != "https://eu.i.posthog.com" {
|
|
t.Fatalf("posthog_host: want https://eu.i.posthog.com, got %q", cfg.PosthogHost)
|
|
}
|
|
if cfg.AnalyticsEnvironment != "dev" {
|
|
t.Fatalf("analytics_environment: want dev, got %q", cfg.AnalyticsEnvironment)
|
|
}
|
|
if cfg.WorkspaceCreationDisabled {
|
|
t.Fatalf("workspace_creation_disabled: want false by default, got true")
|
|
}
|
|
if cfg.DaemonServerURL != "https://api.example.com" {
|
|
t.Fatalf("daemon_server_url: want https://api.example.com, got %q", cfg.DaemonServerURL)
|
|
}
|
|
if cfg.DaemonAppURL != "https://app.example.com" {
|
|
t.Fatalf("daemon_app_url: want https://app.example.com, got %q", cfg.DaemonAppURL)
|
|
}
|
|
}
|
|
|
|
func TestGetConfigHonorsVCSIntegrationSwitch(t *testing.T) {
|
|
origCfg := testHandler.cfg
|
|
t.Cleanup(func() { testHandler.cfg = origCfg })
|
|
|
|
fetch := func() map[string]json.RawMessage {
|
|
t.Helper()
|
|
req := httptest.NewRequest(http.MethodGet, "/api/config", nil)
|
|
w := httptest.NewRecorder()
|
|
testHandler.GetConfig(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("GetConfig: expected 200, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
var cfg map[string]json.RawMessage
|
|
if err := json.Unmarshal(w.Body.Bytes(), &cfg); err != nil {
|
|
t.Fatalf("decode config: %v", err)
|
|
}
|
|
return cfg
|
|
}
|
|
|
|
testHandler.cfg.VCSIntegrationEnabled = false
|
|
if _, ok := fetch()["vcs_integration_available"]; ok {
|
|
t.Fatal("vcs_integration_available must be omitted when the integration is disabled")
|
|
}
|
|
|
|
testHandler.cfg.VCSIntegrationEnabled = true
|
|
raw, ok := fetch()["vcs_integration_available"]
|
|
if !ok {
|
|
t.Fatal("vcs_integration_available must be present when the integration is enabled")
|
|
}
|
|
if string(raw) != "true" {
|
|
t.Fatalf("vcs_integration_available: want true, got %s", raw)
|
|
}
|
|
}
|
|
|
|
func TestGetConfigUsesAppURLForSameOriginDaemonSetup(t *testing.T) {
|
|
t.Setenv("MULTICA_PUBLIC_URL", "")
|
|
t.Setenv("MULTICA_APP_URL", "https://multica.internal.example/")
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/config", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
testHandler.GetConfig(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("GetConfig: expected 200, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var cfg AppConfig
|
|
if err := json.Unmarshal(w.Body.Bytes(), &cfg); err != nil {
|
|
t.Fatalf("decode config: %v", err)
|
|
}
|
|
if cfg.DaemonServerURL != "https://multica.internal.example" {
|
|
t.Fatalf("daemon_server_url: want same-origin URL, got %q", cfg.DaemonServerURL)
|
|
}
|
|
if cfg.DaemonAppURL != "https://multica.internal.example" {
|
|
t.Fatalf("daemon_app_url: want app URL, got %q", cfg.DaemonAppURL)
|
|
}
|
|
}
|
|
|
|
func TestGetConfigUsesFrontendOriginForSameOriginDaemonSetup(t *testing.T) {
|
|
t.Setenv("MULTICA_PUBLIC_URL", "")
|
|
t.Setenv("MULTICA_APP_URL", "")
|
|
t.Setenv("FRONTEND_ORIGIN", "https://multica.internal.example/")
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/config", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
testHandler.GetConfig(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("GetConfig: expected 200, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var cfg AppConfig
|
|
if err := json.Unmarshal(w.Body.Bytes(), &cfg); err != nil {
|
|
t.Fatalf("decode config: %v", err)
|
|
}
|
|
if cfg.DaemonServerURL != "https://multica.internal.example" {
|
|
t.Fatalf("daemon_server_url: want same-origin URL, got %q", cfg.DaemonServerURL)
|
|
}
|
|
if cfg.DaemonAppURL != "https://multica.internal.example" {
|
|
t.Fatalf("daemon_app_url: want frontend origin, got %q", cfg.DaemonAppURL)
|
|
}
|
|
}
|
|
|
|
func TestGetConfigOmitsOfficialCloudDaemonSetup(t *testing.T) {
|
|
t.Setenv("MULTICA_PUBLIC_URL", "https://api.multica.ai")
|
|
t.Setenv("MULTICA_APP_URL", "")
|
|
t.Setenv("FRONTEND_ORIGIN", "https://multica.ai")
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/config", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
testHandler.GetConfig(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("GetConfig: expected 200, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var cfg AppConfig
|
|
if err := json.Unmarshal(w.Body.Bytes(), &cfg); err != nil {
|
|
t.Fatalf("decode config: %v", err)
|
|
}
|
|
if cfg.DaemonServerURL != "" {
|
|
t.Fatalf("daemon_server_url: want omitted for cloud, got %q", cfg.DaemonServerURL)
|
|
}
|
|
if cfg.DaemonAppURL != "" {
|
|
t.Fatalf("daemon_app_url: want omitted for cloud, got %q", cfg.DaemonAppURL)
|
|
}
|
|
}
|
|
|
|
// TestGetConfigOmitsCloudDaemonSetupWithoutPublicURL reproduces the production
|
|
// regression behind the broken "Add a computer" command: the official cloud
|
|
// frontend is multica.ai, but the deployment does not set MULTICA_PUBLIC_URL to
|
|
// the api host. Previously this fell through to the same-origin branch and
|
|
// emitted daemon_server_url=https://multica.ai, which the dialog turned into
|
|
// `multica setup self-host --server-url https://multica.ai` — pointing the
|
|
// daemon's backend at the frontend (no /health, no WebSocket proxy). The
|
|
// official cloud must be recognised by its frontend host alone so the daemon
|
|
// setup URLs are omitted and the dialog falls back to `multica setup`.
|
|
func TestGetConfigOmitsCloudDaemonSetupWithoutPublicURL(t *testing.T) {
|
|
t.Setenv("MULTICA_PUBLIC_URL", "")
|
|
t.Setenv("MULTICA_APP_URL", "")
|
|
t.Setenv("FRONTEND_ORIGIN", "https://multica.ai")
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/config", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
testHandler.GetConfig(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("GetConfig: expected 200, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var cfg AppConfig
|
|
if err := json.Unmarshal(w.Body.Bytes(), &cfg); err != nil {
|
|
t.Fatalf("decode config: %v", err)
|
|
}
|
|
if cfg.DaemonServerURL != "" {
|
|
t.Fatalf("daemon_server_url: want omitted for official cloud, got %q", cfg.DaemonServerURL)
|
|
}
|
|
if cfg.DaemonAppURL != "" {
|
|
t.Fatalf("daemon_app_url: want omitted for official cloud, got %q", cfg.DaemonAppURL)
|
|
}
|
|
}
|
|
|
|
// TestGetConfigOmitsCloudDaemonSetupForConfiguredAppURL covers the official
|
|
// cloud frontend when it is configured through MULTICA_APP_URL.
|
|
func TestGetConfigOmitsCloudDaemonSetupForConfiguredAppURL(t *testing.T) {
|
|
t.Setenv("MULTICA_PUBLIC_URL", "")
|
|
t.Setenv("MULTICA_APP_URL", "https://multica.ai")
|
|
t.Setenv("FRONTEND_ORIGIN", "")
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/config", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
testHandler.GetConfig(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("GetConfig: expected 200, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var cfg AppConfig
|
|
if err := json.Unmarshal(w.Body.Bytes(), &cfg); err != nil {
|
|
t.Fatalf("decode config: %v", err)
|
|
}
|
|
if cfg.DaemonServerURL != "" {
|
|
t.Fatalf("daemon_server_url: want omitted for official cloud, got %q", cfg.DaemonServerURL)
|
|
}
|
|
if cfg.DaemonAppURL != "" {
|
|
t.Fatalf("daemon_app_url: want omitted for official cloud, got %q", cfg.DaemonAppURL)
|
|
}
|
|
}
|
|
|
|
func TestURLHostEqualsCanonicalizesCommonHostForms(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
raw string
|
|
want bool
|
|
}{
|
|
{name: "full URL", raw: "https://api.multica.ai", want: true},
|
|
{name: "bare host", raw: "api.multica.ai", want: true},
|
|
{name: "host port", raw: "api.multica.ai:8080", want: true},
|
|
{name: "trailing dot", raw: "https://api.multica.ai.", want: true},
|
|
{name: "different host", raw: "https://evil.example", want: false},
|
|
{name: "empty", raw: "", want: false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := urlHostEquals(tt.raw, "api.multica.ai"); got != tt.want {
|
|
t.Fatalf("urlHostEquals(%q): want %v, got %v", tt.raw, tt.want, got)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestGetConfigExposesWorkspaceCreationDisabled verifies that the self-host
|
|
// gate added by #3433 surfaces to the frontend through /api/config so the UI
|
|
// can hide every "Create workspace" affordance.
|
|
func TestGetConfigExposesWorkspaceCreationDisabled(t *testing.T) {
|
|
origStorage := testHandler.Storage
|
|
testHandler.Storage = &mockStorage{}
|
|
defer func() { testHandler.Storage = origStorage }()
|
|
|
|
t.Setenv("DISABLE_WORKSPACE_CREATION", "true")
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/config", nil)
|
|
w := httptest.NewRecorder()
|
|
|
|
testHandler.GetConfig(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("GetConfig: expected 200, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
|
|
var cfg AppConfig
|
|
if err := json.Unmarshal(w.Body.Bytes(), &cfg); err != nil {
|
|
t.Fatalf("decode config: %v", err)
|
|
}
|
|
if !cfg.WorkspaceCreationDisabled {
|
|
t.Fatalf("workspace_creation_disabled: want true with env on, got false (body=%s)", w.Body.String())
|
|
}
|
|
}
|
|
|
|
// TestGetConfigExposesServerVersion verifies /api/config reports the build
|
|
// version main.go stamps into handler.Config via -X main.version on a
|
|
// self-hosted deployment, so operators can confirm what's deployed from the
|
|
// Help popover.
|
|
func TestGetConfigExposesServerVersion(t *testing.T) {
|
|
origCfg := testHandler.cfg
|
|
defer func() { testHandler.cfg = origCfg }()
|
|
|
|
// Self-hosted frontend origin: the version row is meant for these deployments.
|
|
t.Setenv("MULTICA_APP_URL", "https://multica.self-hosted.example")
|
|
t.Setenv("FRONTEND_ORIGIN", "")
|
|
|
|
testHandler.cfg.ServerVersion = ""
|
|
req := httptest.NewRequest(http.MethodGet, "/api/config", nil)
|
|
w := httptest.NewRecorder()
|
|
testHandler.GetConfig(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("GetConfig: expected 200, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
var cfg AppConfig
|
|
if err := json.Unmarshal(w.Body.Bytes(), &cfg); err != nil {
|
|
t.Fatalf("decode config: %v", err)
|
|
}
|
|
if cfg.ServerVersion != "" {
|
|
t.Fatalf("server_version: want empty for dev build, got %q", cfg.ServerVersion)
|
|
}
|
|
|
|
testHandler.cfg.ServerVersion = "1.2.3"
|
|
w = httptest.NewRecorder()
|
|
testHandler.GetConfig(w, req)
|
|
if err := json.Unmarshal(w.Body.Bytes(), &cfg); err != nil {
|
|
t.Fatalf("decode config: %v", err)
|
|
}
|
|
if cfg.ServerVersion != "1.2.3" {
|
|
t.Fatalf("server_version: want 1.2.3, got %q", cfg.ServerVersion)
|
|
}
|
|
}
|
|
|
|
// TestGetConfigOmitsServerVersionOnOfficialCloud verifies the build version is
|
|
// suppressed on the managed cloud (frontend host multica.ai) even when the
|
|
// binary is stamped, while a self-hosted frontend origin still reports it. The
|
|
// managed cloud is continuously deployed, so its users don't need the row.
|
|
func TestGetConfigOmitsServerVersionOnOfficialCloud(t *testing.T) {
|
|
origCfg := testHandler.cfg
|
|
defer func() { testHandler.cfg = origCfg }()
|
|
testHandler.cfg.ServerVersion = "1.2.3"
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/api/config", nil)
|
|
|
|
// Official cloud: frontend host multica.ai -> version omitted.
|
|
t.Setenv("MULTICA_APP_URL", "https://multica.ai")
|
|
t.Setenv("FRONTEND_ORIGIN", "")
|
|
w := httptest.NewRecorder()
|
|
testHandler.GetConfig(w, req)
|
|
var cfg AppConfig
|
|
if err := json.Unmarshal(w.Body.Bytes(), &cfg); err != nil {
|
|
t.Fatalf("decode config: %v", err)
|
|
}
|
|
if cfg.ServerVersion != "" {
|
|
t.Fatalf("server_version: want omitted on official cloud, got %q", cfg.ServerVersion)
|
|
}
|
|
|
|
// Self-hosted: operator's own frontend origin -> version reported.
|
|
t.Setenv("MULTICA_APP_URL", "https://multica.self-hosted.example")
|
|
w = httptest.NewRecorder()
|
|
testHandler.GetConfig(w, req)
|
|
if err := json.Unmarshal(w.Body.Bytes(), &cfg); err != nil {
|
|
t.Fatalf("decode config: %v", err)
|
|
}
|
|
if cfg.ServerVersion != "1.2.3" {
|
|
t.Fatalf("server_version: want 1.2.3 on self-hosted, got %q", cfg.ServerVersion)
|
|
}
|
|
}
|
|
|
|
func TestGetConfigExposesFrontendFeatureFlags(t *testing.T) {
|
|
h := &Handler{}
|
|
req := httptest.NewRequest(http.MethodGet, "/api/config", nil)
|
|
w := httptest.NewRecorder()
|
|
h.GetConfig(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("GetConfig default flags: expected 200, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
var cfg AppConfig
|
|
if err := json.Unmarshal(w.Body.Bytes(), &cfg); err != nil {
|
|
t.Fatalf("decode default config: %v", err)
|
|
}
|
|
if cfg.FeatureFlags["composio_mcp_apps"] {
|
|
t.Fatalf("composio_mcp_apps: want false by default, got true")
|
|
}
|
|
if !cfg.FeatureFlags["agents_skill_toggles"] {
|
|
t.Fatalf("agents_skill_toggles: want true for installed v0.4.0 clients, got false")
|
|
}
|
|
|
|
withComposioMCPAppsFlag(t, h, true)
|
|
w = httptest.NewRecorder()
|
|
h.GetConfig(w, req)
|
|
if w.Code != http.StatusOK {
|
|
t.Fatalf("GetConfig enabled flags: expected 200, got %d: %s", w.Code, w.Body.String())
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &cfg); err != nil {
|
|
t.Fatalf("decode enabled config: %v", err)
|
|
}
|
|
if !cfg.FeatureFlags["composio_mcp_apps"] {
|
|
t.Fatalf("composio_mcp_apps: want true with flag enabled, got false")
|
|
}
|
|
}
|