Skip to content

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

fix: disallow adding bytebase's own postgres instance (#858)

Danny Xuf4bc53e1231d
diff --git a/bin/server/cmd/root.go b/bin/server/cmd/root.go
index ee8b8ce7c2..ce1d59951c 100644
--- a/bin/server/cmd/root.go
+++ b/bin/server/cmd/root.go
@@ -351,7 +351,7 @@ func (m *Main) Run(ctx context.Context) error {
m.db = db
- s := server.NewServer(m.l, m.lvl, version, host, m.profile.port, frontendHost, frontendPort, m.profile.mode, m.profile.dataDir, m.profile.backupRunnerInterval, config.secret, readonly, demo, debug)
+ s := server.NewServer(m.l, m.lvl, version, host, m.profile.port, frontendHost, frontendPort, m.profile.datastorePort, m.profile.mode, m.profile.dataDir, m.profile.backupRunnerInterval, config.secret, readonly, demo, debug)
s.SettingService = settingService
s.PrincipalService = store.NewPrincipalService(m.l, db, s.CacheService)
s.MemberService = store.NewMemberService(m.l, db, s.CacheService)
diff --git a/server/instance.go b/server/instance.go
index 4316db34e5..7857a6b948 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -3,6 +3,7 @@ package server
import (
"context"
"fmt"
+ "net"
"net/http"
"strconv"
@@ -26,8 +27,10 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
if err := jsonapi.UnmarshalPayload(c.Request().Body, instanceCreate); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted create instance request").SetInternal(err)
}
-
instanceCreate.CreatorID = c.Get(getPrincipalIDContextKey()).(int)
+ if err := s.disallowBytebaseStore(instanceCreate.Engine, instanceCreate.Host, instanceCreate.Port); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
+ }
instance, err := s.InstanceService.CreateInstance(ctx, instanceCreate)
if err != nil {
@@ -125,8 +128,24 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
if err := jsonapi.UnmarshalPayload(c.Request().Body, instancePatch); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch instance request").SetInternal(err)
}
+ instance, err := s.composeInstanceByID(ctx, id)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to get instance ID: %v", id)).SetInternal(err)
+ }
+ if instance == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", id))
+ }
+ host, port := instance.Host, instance.Port
+ if instancePatch.Host != nil {
+ host = *instancePatch.Host
+ }
+ if instancePatch.Port != nil {
+ port = *instancePatch.Port
+ }
+ if err := s.disallowBytebaseStore(instance.Engine, host, port); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
+ }
- var instance *api.Instance
if instancePatch.RowStatus != nil || instancePatch.Name != nil || instancePatch.ExternalLink != nil || instancePatch.Host != nil || instancePatch.Port != nil {
// Users can switch instance status from ARCHIVED to NORMAL.
// So we need to check the current instance count with NORMAL status for quota limitation.
@@ -568,3 +587,30 @@ func (s *Server) instanceCountGuard(ctx context.Context) error {
return nil
}
+
+// disallowBytebaseStore prevents users adding Bytebase's own Postgres database.
+// Otherwise, users can take control of the database which is a security issue.
+func (s *Server) disallowBytebaseStore(engine db.Type, host, port string) error {
+ if engine != db.Postgres {
+ return nil
+ }
+ if port != fmt.Sprintf("%v", s.datastorePort) {
+ return nil
+ }
+
+ existErr := fmt.Errorf("instance doesn't exist for host %q and port %q", host, port)
+ if host == "localhost" || host == "127.0.0.1" || host == "::1" {
+ return existErr
+ }
+ // Check loopback addresses
+ ips, err := net.LookupIP(host)
+ if err != nil {
+ return nil
+ }
+ for _, ip := range ips {
+ if ip.IsLoopback() {
+ return existErr
+ }
+ }
+ return nil
+}
diff --git a/server/server.go b/server/server.go
index 4208b3eefa..6745abf226 100644
--- a/server/server.go
+++ b/server/server.go
@@ -73,20 +73,21 @@ type Server struct {
e *echo.Echo
- l *zap.Logger
- lvl *zap.AtomicLevel
- version string
- mode string
- host string
- port int
- frontendHost string
- frontendPort int
- startedTs int64
- secret string
- readonly bool
- demo bool
- dataDir string
- subscription *enterprise.Subscription
+ l *zap.Logger
+ lvl *zap.AtomicLevel
+ version string
+ mode string
+ host string
+ port int
+ frontendHost string
+ frontendPort int
+ datastorePort int
+ startedTs int64
+ secret string
+ readonly bool
+ demo bool
+ dataDir string
+ subscription *enterprise.Subscription
}
//go:embed acl_casbin_model.conf
@@ -102,7 +103,7 @@ var casbinDBAPolicy string
var casbinDeveloperPolicy string
// NewServer creates a server.
-func NewServer(logger *zap.Logger, loggerLevel *zap.AtomicLevel, version string, host string, port int, frontendHost string, frontendPort int, mode string, dataDir string, backupRunnerInterval time.Duration, secret string, readonly bool, demo bool, debug bool) *Server {
+func NewServer(logger *zap.Logger, loggerLevel *zap.AtomicLevel, version string, host string, port int, frontendHost string, frontendPort, datastorePort int, mode string, dataDir string, backupRunnerInterval time.Duration, secret string, readonly bool, demo bool, debug bool) *Server {
e := echo.New()
e.Debug = debug
e.HideBanner = true
@@ -116,21 +117,22 @@ func NewServer(logger *zap.Logger, loggerLevel *zap.AtomicLevel, version string,
embedFrontend(logger, e)
s := &Server{
- l: logger,
- lvl: loggerLevel,
- CacheService: NewCacheService(logger),
- e: e,
- version: version,
- mode: mode,
- host: host,
- port: port,
- frontendHost: frontendHost,
- frontendPort: frontendPort,
- startedTs: time.Now().Unix(),
- secret: secret,
- readonly: readonly,
- demo: demo,
- dataDir: dataDir,
+ l: logger,
+ lvl: loggerLevel,
+ CacheService: NewCacheService(logger),
+ e: e,
+ version: version,
+ mode: mode,
+ host: host,
+ port: port,
+ frontendHost: frontendHost,
+ frontendPort: frontendPort,
+ datastorePort: datastorePort,
+ startedTs: time.Now().Unix(),
+ secret: secret,
+ readonly: readonly,
+ demo: demo,
+ dataDir: dataDir,
}
if !readonly {
diff --git a/server/sql.go b/server/sql.go
index fb5badc31d..d6f2f92354 100644
--- a/server/sql.go
+++ b/server/sql.go
@@ -24,6 +24,9 @@ func (s *Server) registerSQLRoutes(g *echo.Group) {
if err := jsonapi.UnmarshalPayload(c.Request().Body, connectionInfo); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted sql ping request").SetInternal(err)
}
+ if err := s.disallowBytebaseStore(connectionInfo.Engine, connectionInfo.Host, connectionInfo.Port); err != nil {
+ return echo.NewHTTPError(http.StatusBadRequest, err.Error()).SetInternal(err)
+ }
password := connectionInfo.Password
// Instance detail page has a Test Connection button, if user doesn't input new password and doesn't specify