Skip to content

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

chore: update server setting (#7060)

Danny Xu138f84efaa07
diff --git a/backend/api/auth/header.go b/backend/api/auth/header.go
index dbcec99f27..a8ce85f0c3 100644
--- a/backend/api/auth/header.go
+++ b/backend/api/auth/header.go
@@ -3,6 +3,7 @@ package auth
import (
"context"
"net/http"
+ "strings"
"time"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
@@ -10,50 +11,54 @@ import (
"google.golang.org/protobuf/proto"
)
-// GatewayResponseModifier is the mux option for modifying response header.
-func GatewayResponseModifier(ctx context.Context, response http.ResponseWriter, _ proto.Message) error {
+// GatewayResponseModifier is the response modifier for grpc gateway.
+type GatewayResponseModifier struct {
+ ExternalURL string
+}
+
+// Modify is the mux option for modifying response header.
+func (m *GatewayResponseModifier) Modify(ctx context.Context, response http.ResponseWriter, _ proto.Message) error {
md, ok := runtime.ServerMetadataFromContext(ctx)
if !ok {
return errors.Errorf("failed to get ServerMetadata from context in the gateway response modifier")
}
- processMetadata(md, GatewayMetadataAccessTokenKey, AccessTokenCookieName, true /* httpOnly */, response)
- processMetadata(md, GatewayMetadataRefreshTokenKey, RefreshTokenCookieName, true /* httpOnly */, response)
- processMetadata(md, GatewayMetadataUserIDKey, UserIDCookieName, false /* httpOnly */, response)
+ isHTTPS := strings.HasPrefix(m.ExternalURL, "https")
+ processMetadata(md, GatewayMetadataAccessTokenKey, AccessTokenCookieName, true /* httpOnly */, isHTTPS, response)
+ processMetadata(md, GatewayMetadataRefreshTokenKey, RefreshTokenCookieName, true /* httpOnly */, isHTTPS, response)
+ processMetadata(md, GatewayMetadataUserIDKey, UserIDCookieName, false /* httpOnly */, isHTTPS, response)
return nil
}
-func processMetadata(md runtime.ServerMetadata, metadataKey, cookieName string, httpOnly bool, response http.ResponseWriter) {
+func processMetadata(md runtime.ServerMetadata, metadataKey, cookieName string, httpOnly, isHTTPS bool, response http.ResponseWriter) {
values := md.HeaderMD.Get(metadataKey)
if len(values) == 0 {
return
}
value := values[0]
if value == "" {
- unsetCookie(response, cookieName)
+ // Unset cookie.
+ http.SetCookie(response, &http.Cookie{
+ Name: cookieName,
+ Value: "",
+ Expires: time.Unix(0, 0),
+ Path: "/",
+ })
} else {
- setCookie(response, cookieName, value, httpOnly)
+ // Set cookie.
+ sameSite := http.SameSiteStrictMode
+ if isHTTPS {
+ sameSite = http.SameSiteNoneMode
+ }
+ http.SetCookie(response, &http.Cookie{
+ Name: cookieName,
+ Value: value,
+ Expires: time.Now().Add(CookieExpDuration),
+ Path: "/",
+ // Http-only helps mitigate the risk of client side script accessing the protected cookie.
+ HttpOnly: httpOnly,
+ // See https://github.com/bytebase/bytebase/issues/31.
+ Secure: isHTTPS,
+ SameSite: sameSite,
+ })
}
}
-
-func setCookie(response http.ResponseWriter, key, value string, httpOnly bool) {
- http.SetCookie(response, &http.Cookie{
- Name: key,
- Value: value,
- Expires: time.Now().Add(CookieExpDuration),
- Path: "/",
- // Http-only helps mitigate the risk of client side script accessing the protected cookie.
- HttpOnly: httpOnly,
- // For now, we allow Bytebase to run on non-https host, see https://github.com/bytebase/bytebase/issues/31
- // cookie.Secure = true
- SameSite: http.SameSiteStrictMode,
- })
-}
-
-func unsetCookie(response http.ResponseWriter, key string) {
- http.SetCookie(response, &http.Cookie{
- Name: key,
- Value: "",
- Expires: time.Unix(0, 0),
- Path: "/",
- })
-}
diff --git a/backend/server/server.go b/backend/server/server.go
index 783ae461a8..0e8d9a08b1 100644
--- a/backend/server/server.go
+++ b/backend/server/server.go
@@ -376,11 +376,6 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {
},
}))
- // Disallow to be embedded in an iFrame.
- e.Use(middleware.SecureWithConfig(middleware.SecureConfig{
- XFrameOptions: "DENY",
- }))
-
// MetricReporter middleware.
e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
@@ -610,7 +605,25 @@ func NewServer(ctx context.Context, profile config.Profile) (*Server, error) {
if err != nil {
return nil, err
}
- mux := grpcRuntime.NewServeMux(grpcRuntime.WithForwardResponseOption(auth.GatewayResponseModifier))
+
+ // Note: the gateway response modifier takes the external url on server startup. If the external URL is changed,
+ // the user has to restart the server to take the latest value.
+ gatewayModifier := auth.GatewayResponseModifier{}
+ workspaceProfileSettingName := api.SettingWorkspaceProfile
+ setting, err := s.store.GetSettingV2(ctx, &store.FindSettingMessage{Name: &workspaceProfileSettingName})
+ if err != nil {
+ return nil, err
+ }
+ if setting != nil {
+ settingValue := new(storepb.WorkspaceProfileSetting)
+ if err := protojson.Unmarshal([]byte(setting.Value), settingValue); err != nil {
+ return nil, err
+ }
+ if settingValue.ExternalUrl != "" {
+ gatewayModifier.ExternalURL = settingValue.ExternalUrl
+ }
+ }
+ mux := grpcRuntime.NewServeMux(grpcRuntime.WithForwardResponseOption(gatewayModifier.Modify))
if err := v1pb.RegisterAuthServiceHandler(ctx, mux, grpcConn); err != nil {
return nil, err
}
diff --git a/backend/sql-server/server.go b/backend/sql-server/server.go
index aa3f10d3d3..8e42d06722 100644
--- a/backend/sql-server/server.go
+++ b/backend/sql-server/server.go
@@ -76,10 +76,6 @@ func NewServer(ctx context.Context, prof Profile) (*Server, error) {
e := echo.New()
e.Debug = prof.Debug
- // Disallow to be embedded in an iFrame.
- e.Use(middleware.SecureWithConfig(middleware.SecureConfig{
- XFrameOptions: "DENY",
- }))
s.e = e
// Middleware