Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: validate cel when creating/updating (#5276)
p0ny52232ba8046f
diff --git a/backend/api/v1/risk_service.go b/backend/api/v1/risk_service.goindex 70bc676514..2e8b4ea316 100644--- a/backend/api/v1/risk_service.go+++ b/backend/api/v1/risk_service.go@@ -8,9 +8,15 @@ import ("google.golang.org/grpc/status""google.golang.org/protobuf/types/known/emptypb"+ "github.com/google/cel-go/cel"+ "github.com/pkg/errors"++ v1alpha1 "google.golang.org/genproto/googleapis/api/expr/v1alpha1"+"github.com/bytebase/bytebase/backend/common"enterpriseAPI "github.com/bytebase/bytebase/backend/enterprise/api"api "github.com/bytebase/bytebase/backend/legacyapi"+ "github.com/bytebase/bytebase/backend/runner/approval""github.com/bytebase/bytebase/backend/store"v1pb "github.com/bytebase/bytebase/proto/generated-go/v1")@@ -60,6 +66,9 @@ func (s *RiskService) CreateRisk(ctx context.Context, request *v1pb.CreateRiskReif !s.licenseService.IsFeatureEnabled(api.FeatureCustomApproval) {return nil, status.Errorf(codes.PermissionDenied, api.FeatureCustomApproval.AccessErrorMessage())}+ if err := validateRiskExpression(request.Risk.Expression); err != nil {+ return nil, status.Errorf(codes.InvalidArgument, "failed to validate risk expression, error: %v", err)+ }principalID := ctx.Value(common.PrincipalIDContextKey).(int)risk, err := s.store.CreateRisk(ctx, &store.RiskMessage{Source: convertSource(request.Risk.Source),@@ -108,6 +117,9 @@ func (s *RiskService) UpdateRisk(ctx context.Context, request *v1pb.UpdateRiskRecase "risk.level":patch.Level = &request.Risk.Levelcase "risk.expression":+ if err := validateRiskExpression(request.Risk.Expression); err != nil {+ return nil, status.Errorf(codes.InvalidArgument, "failed to validate risk expression, error: %v", err)+ }patch.Expression = request.Risk.Expression}}@@ -174,3 +186,19 @@ func convertSource(source v1pb.Risk_Source) store.RiskSource {}return store.RiskSourceUnknown}++func validateRiskExpression(expression *v1alpha1.ParsedExpr) error {+ if expression == nil || expression.Expr == nil {+ return nil+ }+ e, err := cel.NewEnv(approval.RiskFactors...)+ if err != nil {+ return errors.Wrap(err, "failed to create cel env")+ }+ ast := cel.ParsedExprToAst(expression)+ _, issues := e.Check(ast)+ if issues != nil {+ return errors.Wrap(issues.Err(), "invalid cel expression")+ }+ return nil+}diff --git a/backend/api/v1/setting_service.go b/backend/api/v1/setting_service.goindex 8f77b29416..5089d59770 100644--- a/backend/api/v1/setting_service.go+++ b/backend/api/v1/setting_service.go@@ -7,10 +7,13 @@ import ("google.golang.org/grpc/status""google.golang.org/protobuf/encoding/protojson"+ "github.com/google/cel-go/cel"+"github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/component/config"enterpriseAPI "github.com/bytebase/bytebase/backend/enterprise/api"api "github.com/bytebase/bytebase/backend/legacyapi"+ "github.com/bytebase/bytebase/backend/runner/approval""github.com/bytebase/bytebase/backend/store"storepb "github.com/bytebase/bytebase/proto/generated-go/store"v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"@@ -104,8 +107,24 @@ func (s *SettingService) SetSetting(ctx context.Context, request *v1pb.SetSettin}settingValue = string(bytes)}- if apiSettingName == api.SettingWorkspaceApproval && !s.licenseService.IsFeatureEnabled(api.FeatureCustomApproval) {- return nil, status.Errorf(codes.PermissionDenied, api.FeatureCustomApproval.AccessErrorMessage())+ if apiSettingName == api.SettingWorkspaceApproval {+ payload := new(storepb.WorkspaceApprovalSetting)+ if err := protojson.Unmarshal([]byte(settingValue), payload); err != nil {+ return nil, status.Errorf(codes.Internal, "failed to unmarshal setting value: %v", err)+ }+ e, err := cel.NewEnv(approval.ApprovalFactors...)+ if err != nil {+ return nil, status.Errorf(codes.Internal, "failed to create cel env: %v", err)+ }+ for _, rule := range payload.Rules {+ if rule.Expression != nil && rule.Expression.Expr != nil {+ ast := cel.ParsedExprToAst(rule.Expression)+ _, issues := e.Check(ast)+ if issues != nil {+ return nil, status.Errorf(codes.InvalidArgument, "invalid cel expression: %v, issues: %v", rule.Expression.String(), issues.Err())+ }+ }+ }}setting, err := s.store.UpsertSettingV2(ctx, &store.SetSettingMessage{@@ -113,7 +132,7 @@ func (s *SettingService) SetSetting(ctx context.Context, request *v1pb.SetSettinValue: settingValue,}, ctx.Value(common.PrincipalIDContextKey).(int))if err != nil {- return nil, err+ return nil, status.Errorf(codes.Internal, "failed to set setting: %v", err)}return convertToSettingMessage(setting), nildiff --git a/backend/runner/approval/runner.go b/backend/runner/approval/runner.goindex 037924de97..8c8f9aa59c 100644--- a/backend/runner/approval/runner.go+++ b/backend/runner/approval/runner.go@@ -29,7 +29,8 @@ import (v1pb "github.com/bytebase/bytebase/proto/generated-go/v1")-var riskFactors = []cel.EnvOption{+// RiskFactors are the variables when evaluating the risk level.+var RiskFactors = []cel.EnvOption{// string factors// use environment.resource_idcel.Variable("environment_id", cel.StringType),@@ -43,7 +44,8 @@ var riskFactors = []cel.EnvOption{cel.Variable("affected_rows", cel.IntType),}-var riskVariables = []cel.EnvOption{+// ApprovalFactors are the variables when finding the approval template.+var ApprovalFactors = []cel.EnvOption{cel.Variable("level", cel.IntType),cel.Variable("source", cel.IntType),}@@ -193,7 +195,7 @@ func (r *Runner) Run(ctx context.Context, wg *sync.WaitGroup) {}func getApprovalTemplate(approvalSetting *storepb.WorkspaceApprovalSetting, riskLevel int64, riskSource store.RiskSource) (*storepb.ApprovalTemplate, error) {- e, err := cel.NewEnv(riskVariables...)+ e, err := cel.NewEnv(ApprovalFactors...)if err != nil {return nil, err}@@ -330,7 +332,7 @@ func getTaskRiskLevel(ctx context.Context, s *store.Store, issue *store.IssueMesdatabaseName = database.DatabaseName}- e, err := cel.NewEnv(riskFactors...)+ e, err := cel.NewEnv(RiskFactors...)if err != nil {return 0, false, err}