Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: force to require external url for webhook (#18736)

ecmadao9d90dac24e4f
diff --git a/backend/api/directory-sync/webhook.go b/backend/api/directory-sync/webhook.go
index f940fbfa00..b09e62fec9 100644
--- a/backend/api/directory-sync/webhook.go
+++ b/backend/api/directory-sync/webhook.go
@@ -684,18 +684,7 @@ func (s *Service) validRequestURL(ctx context.Context, c echo.Context) error {
return errors.Errorf("missing authorization token")
}
- setting, err := s.store.GetWorkspaceProfileSetting(ctx)
- if err != nil {
- return err
- }
- // Use command-line flag value if set, otherwise use database value
- externalURL := common.GetEffectiveExternalURL(s.profile.ExternalURL, setting.ExternalUrl)
- if externalURL == "" {
- return errors.Errorf("external URL is empty")
- }
-
workspaceID := c.Param("workspaceID")
-
systemSetting, err := s.store.GetSystemSetting(ctx)
if err != nil {
return errors.Wrap(err, "failed to get system setting")
diff --git a/backend/api/oauth2/discovery.go b/backend/api/oauth2/discovery.go
index 6b9ae28029..a20417db92 100644
--- a/backend/api/oauth2/discovery.go
+++ b/backend/api/oauth2/discovery.go
@@ -2,10 +2,14 @@ package oauth2
import (
"fmt"
+ "log/slog"
"net/http"
"strings"
"github.com/labstack/echo/v4"
+
+ "github.com/bytebase/bytebase/backend/common/log"
+ "github.com/bytebase/bytebase/backend/utils"
)
type authorizationServerMetadata struct {
@@ -33,9 +37,15 @@ type protectedResourceMetadata struct {
// It uses externalURL from profile/setting if configured, otherwise derives from the request.
func (s *Service) getBaseURL(c echo.Context) string {
ctx := c.Request().Context()
- if externalURL := s.getExternalURL(ctx); externalURL != "" {
+
+ externalURL, err := utils.GetEffectiveExternalURL(ctx, s.store, s.profile)
+ if err != nil {
+ slog.Warn("failed to get external url for OAuth2", log.BBError(err))
+ }
+ if externalURL != "" {
return strings.TrimSuffix(externalURL, "/")
}
+
// Derive from request as fallback
req := c.Request()
scheme := "https"
diff --git a/backend/api/oauth2/oauth2.go b/backend/api/oauth2/oauth2.go
index 2b16fd6e16..a8eaab6abe 100644
--- a/backend/api/oauth2/oauth2.go
+++ b/backend/api/oauth2/oauth2.go
@@ -1,7 +1,6 @@
package oauth2
import (
- "context"
"crypto/rand"
"crypto/sha256"
"crypto/subtle"
@@ -17,7 +16,6 @@ import (
"golang.org/x/crypto/bcrypt"
"github.com/bytebase/bytebase/backend/api/auth"
- "github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/component/config"
"github.com/bytebase/bytebase/backend/store"
@@ -49,16 +47,6 @@ func NewService(store *store.Store, profile *config.Profile, secret string) *Ser
}
}
-// getExternalURL returns the effective external URL from profile flag or workspace setting.
-func (s *Service) getExternalURL(ctx context.Context) string {
- setting, err := s.store.GetWorkspaceProfileSetting(ctx)
- if err != nil {
- slog.Warn("failed to get workspace profile setting for OAuth2, using profile external URL", log.BBError(err))
- return s.profile.ExternalURL
- }
- return common.GetEffectiveExternalURL(s.profile.ExternalURL, setting.ExternalUrl)
-}
-
func (s *Service) RegisterRoutes(e *echo.Echo) {
// .well-known endpoints must stay at root (RFC 8615)
e.GET("/.well-known/oauth-authorization-server", s.handleDiscovery)
diff --git a/backend/api/v1/auth_service.go b/backend/api/v1/auth_service.go
index 747c3a1d91..9517d55493 100644
--- a/backend/api/v1/auth_service.go
+++ b/backend/api/v1/auth_service.go
@@ -31,6 +31,7 @@ import (
"github.com/bytebase/bytebase/backend/plugin/idp/oidc"
"github.com/bytebase/bytebase/backend/plugin/idp/wif"
"github.com/bytebase/bytebase/backend/store"
+ "github.com/bytebase/bytebase/backend/utils"
)
const (
@@ -274,14 +275,11 @@ func (s *AuthService) getOrCreateUserWithIDP(ctx context.Context, request *v1pb.
return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("identity provider not found"))
}
- setting, err := s.store.GetWorkspaceProfileSetting(ctx)
+ externalURL, err := utils.GetEffectiveExternalURL(ctx, s.store, s.profile)
if err != nil {
- return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get workspace setting"))
+ return nil, err
}
- // Use command-line flag value if set, otherwise use database value
- externalURL := common.GetEffectiveExternalURL(s.profile.ExternalURL, setting.ExternalUrl)
-
var userInfo *storepb.IdentityProviderUserInfo
switch idp.Type {
case storepb.IdentityProviderType_OAUTH2:
diff --git a/backend/api/v1/common.go b/backend/api/v1/common.go
index e71d68e3ba..7b814d5e71 100644
--- a/backend/api/v1/common.go
+++ b/backend/api/v1/common.go
@@ -19,8 +19,6 @@ import (
type OperatorType string
const (
- setupExternalURLError = "external URL isn't setup yet, see https://docs.bytebase.com/get-started/self-host/external-url"
-
ComparatorTypeEqual OperatorType = "="
ComparatorTypeLess OperatorType = "<"
ComparatorTypeLessEqual OperatorType = "<="
diff --git a/backend/api/v1/idp_service.go b/backend/api/v1/idp_service.go
index 2329a969f5..ffb3a6f1ba 100644
--- a/backend/api/v1/idp_service.go
+++ b/backend/api/v1/idp_service.go
@@ -21,6 +21,7 @@ import (
"github.com/bytebase/bytebase/backend/plugin/idp/oauth2"
"github.com/bytebase/bytebase/backend/plugin/idp/oidc"
"github.com/bytebase/bytebase/backend/store"
+ "github.com/bytebase/bytebase/backend/utils"
)
// IdentityProviderService implements the identity provider service.
@@ -70,14 +71,8 @@ func (s *IdentityProviderService) CreateIdentityProvider(ctx context.Context, re
return nil, err
}
- setting, err := s.store.GetWorkspaceProfileSetting(ctx)
- if err != nil {
- return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get workspace setting"))
- }
- // Use command-line flag value if set, otherwise use database value
- externalURL := common.GetEffectiveExternalURL(s.profile.ExternalURL, setting.ExternalUrl)
- if externalURL == "" {
- return nil, connect.NewError(connect.CodeFailedPrecondition, errors.Errorf(setupExternalURLError))
+ if _, err := utils.GetEffectiveExternalURL(ctx, s.store, s.profile); err != nil {
+ return nil, err
}
if req.Msg.IdentityProvider == nil {
@@ -105,7 +100,7 @@ func (s *IdentityProviderService) CreateIdentityProvider(ctx context.Context, re
return connect.NewResponse(identityProvider), nil
}
- identityProviderMessage, err = s.store.CreateIdentityProvider(ctx, identityProviderMessage)
+ identityProviderMessage, err := s.store.CreateIdentityProvider(ctx, identityProviderMessage)
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to create identity provider"))
}
@@ -229,14 +224,9 @@ func (s *IdentityProviderService) TestIdentityProvider(ctx context.Context, req
return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("identity provider not found"))
}
- setting, err := s.store.GetWorkspaceProfileSetting(ctx)
+ externalURL, err := utils.GetEffectiveExternalURL(ctx, s.store, s.profile)
if err != nil {
- return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get workspace setting"))
- }
- // Use command-line flag value if set, otherwise use database value
- externalURL := common.GetEffectiveExternalURL(s.profile.ExternalURL, setting.ExternalUrl)
- if externalURL == "" {
- return nil, connect.NewError(connect.CodeFailedPrecondition, errors.Errorf(setupExternalURLError))
+ return nil, err
}
switch identityProvider.Type {
@@ -262,7 +252,7 @@ func (s *IdentityProviderService) TestIdentityProvider(ctx context.Context, req
return nil, connect.NewError(connect.CodeInternal, errors.Errorf("failed to new oauth2 identity provider"))
}
- redirectURL := fmt.Sprintf("%s/oauth/callback", setting.ExternalUrl)
+ redirectURL := fmt.Sprintf("%s/oauth/callback", externalURL)
token, err := oauth2IdentityProvider.ExchangeToken(ctx, redirectURL, oauth2Context.Code)
if err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("failed to exchange access token, error: %s", err.Error()))
@@ -313,7 +303,7 @@ func (s *IdentityProviderService) TestIdentityProvider(ctx context.Context, req
return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to create new OIDC identity provider"))
}
- redirectURL := fmt.Sprintf("%s/oidc/callback", setting.ExternalUrl)
+ redirectURL := fmt.Sprintf("%s/oidc/callback", externalURL)
token, err := oidcIdentityProvider.ExchangeToken(ctx, redirectURL, oidcContext.Code)
if err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, errors.Errorf("failed to exchange access token, error: %s", err.Error()))
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.go
index c5be46ae86..0262ae010c 100644
--- a/backend/api/v1/project_service.go
+++ b/backend/api/v1/project_service.go
@@ -23,6 +23,7 @@ import (
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/component/config"
"github.com/bytebase/bytebase/backend/component/iam"
+ "github.com/bytebase/bytebase/backend/utils"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
v1pb "github.com/bytebase/bytebase/backend/generated-go/v1"
@@ -787,6 +788,10 @@ func (s *ProjectService) AddWebhook(ctx context.Context, req *connect.Request[v1
return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("project %q has been deleted", req.Msg.Project))
}
+ if _, err := utils.GetEffectiveExternalURL(ctx, s.store, s.profile); err != nil {
+ return nil, err
+ }
+
create, err := convertToStoreProjectWebhookMessage(req.Msg.Webhook)
if err != nil {
return nil, connect.NewError(connect.CodeInvalidArgument, err)
@@ -834,6 +839,10 @@ func (s *ProjectService) UpdateWebhook(ctx context.Context, req *connect.Request
return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("project %q has been deleted", projectID))
}
+ if _, err := utils.GetEffectiveExternalURL(ctx, s.store, s.profile); err != nil {
+ return nil, err
+ }
+
webhook, err := s.store.GetProjectWebhook(ctx, &store.FindProjectWebhookMessage{
ProjectID: &project.ResourceID,
ID: &webhookIDInt,
@@ -952,14 +961,9 @@ func (s *ProjectService) RemoveWebhook(ctx context.Context, req *connect.Request
// TestWebhook tests a webhook.
func (s *ProjectService) TestWebhook(ctx context.Context, req *connect.Request[v1pb.TestWebhookRequest]) (*connect.Response[v1pb.TestWebhookResponse], error) {
- setting, err := s.store.GetWorkspaceProfileSetting(ctx)
+ externalURL, err := utils.GetEffectiveExternalURL(ctx, s.store, s.profile)
if err != nil {
- return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get workspace setting"))
- }
- // Use command-line flag value if set, otherwise use database value
- externalURL := common.GetEffectiveExternalURL(s.profile.ExternalURL, setting.ExternalUrl)
- if externalURL == "" {
- return nil, connect.NewError(connect.CodeFailedPrecondition, errors.Errorf(setupExternalURLError))
+ return nil, err
}
projectID, err := common.GetProjectID(req.Msg.Project)
diff --git a/backend/common/util.go b/backend/common/util.go
index 68b6b2f245..362eda86ea 100644
--- a/backend/common/util.go
+++ b/backend/common/util.go
@@ -123,15 +123,6 @@ func Unobfuscate(dst, seed string) (string, error) {
return string(unobfuscated), nil
}
-// GetEffectiveExternalURL returns the external URL to use, preferring the command-line flag over the database setting.
-// This ensures that when the --external-url flag is set, it takes precedence over any value stored in the database.
-func GetEffectiveExternalURL(flagValue, dbValue string) string {
- if flagValue != "" {
- return flagValue
- }
- return dbValue
-}
-
// NormalizeExternalURL will format the external url.
func NormalizeExternalURL(url string) (string, error) {
r := strings.TrimSpace(url)
diff --git a/backend/component/webhook/manager.go b/backend/component/webhook/manager.go
index 2dd03fa4f8..047d95d65e 100644
--- a/backend/component/webhook/manager.go
+++ b/backend/component/webhook/manager.go
@@ -14,6 +14,7 @@ import (
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/webhook"
"github.com/bytebase/bytebase/backend/store"
+ "github.com/bytebase/bytebase/backend/utils"
"github.com/pkg/errors"
)
@@ -65,14 +66,12 @@ func (m *Manager) getWebhookContextFromEvent(ctx context.Context, e *Event, even
var webhookCtx webhook.Context
var mentionUsers []*store.UserMessage
- setting, err := m.store.GetWorkspaceProfileSetting(ctx)
+ // Use command-line flag value if set, otherwise use database value
+ externalURL, err := utils.GetEffectiveExternalURL(ctx, m.store, m.profile)
if err != nil {
- return nil, errors.Wrapf(err, "failed to get workspace setting")
+ return nil, err
}
- // Use command-line flag value if set, otherwise use database value
- externalURL := common.GetEffectiveExternalURL(m.profile.ExternalURL, setting.ExternalUrl)
-
level := webhook.WebhookInfo
title := ""
titleZh := ""
diff --git a/backend/utils/setting.go b/backend/utils/setting.go
new file mode 100644
index 0000000000..d625ff36cb
--- /dev/null
+++ b/backend/utils/setting.go
@@ -0,0 +1,32 @@
+package utils
+
+import (
+ "context"
+
+ "connectrpc.com/connect"
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/backend/component/config"
+ "github.com/bytebase/bytebase/backend/store"
+)
+
+const setupExternalURLError = "external URL isn't setup yet, see https://docs.bytebase.com/get-started/self-host/external-url"
+
+// GetEffectiveExternalURL returns the external URL to use, preferring the command-line flag over the database setting.
+// This ensures that when the --external-url flag is set, it takes precedence over any value stored in the database.
+func GetEffectiveExternalURL(ctx context.Context, stores *store.Store, profile *config.Profile) (string, error) {
+ // Use command-line flag value if set, otherwise use database value
+ externalURL := profile.ExternalURL
+ if externalURL == "" {
+ setting, err := stores.GetWorkspaceProfileSetting(ctx)
+ if err != nil {
+ return "", connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get workspace setting"))
+ }
+ externalURL = setting.ExternalUrl
+ }
+
+ if externalURL == "" {
+ return "", connect.NewError(connect.CodeFailedPrecondition, errors.Errorf(setupExternalURLError))
+ }
+ return externalURL, nil
+}
diff --git a/frontend/src/components/ProjectWebhookForm.vue b/frontend/src/components/ProjectWebhookForm.vue
index 40bb8e1f09..bc05dcc440 100644
--- a/frontend/src/components/ProjectWebhookForm.vue
+++ b/frontend/src/components/ProjectWebhookForm.vue
@@ -10,6 +10,9 @@
<NDivider />
</template>
<template #body>
+ <MissingExternalURLAttention
+ class="mb-6"
+ />
<div class="flex flex-col gap-y-4">
<div v-if="create">
<div>
@@ -251,6 +254,7 @@ import { useI18n } from "vue-i18n";
import { useRouter } from "vue-router";
import { BBAttention, BBButtonConfirm } from "@/bbkit";
import RequiredStar from "@/components/RequiredStar.vue";
+import { MissingExternalURLAttention } from "@/components/v2/Form";
import FormLayout from "@/components/v2/Form/FormLayout.vue";
import { useBodyLayoutContext } from "@/layouts/common";
import {