Skip to content

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

feat(cel): enable cel-go/ext Strings extension for conditions (#20535)

Danny Xub4e381bfe0a0
diff --git a/backend/api/v1/masking_evaluator.go b/backend/api/v1/masking_evaluator.go
index dafc6cbb90..35d8e3f721 100644
--- a/backend/api/v1/masking_evaluator.go
+++ b/backend/api/v1/masking_evaluator.go
@@ -6,6 +6,7 @@ import (
"time"
"github.com/google/cel-go/cel"
+ "github.com/google/cel-go/ext"
"github.com/pkg/errors"
"google.golang.org/genproto/googleapis/type/expr"
@@ -241,6 +242,7 @@ func evaluateMaskingExemptionPolicyCondition(expression *expr.Expr, attributes m
maskingExemptionPolicyEnv, err := cel.NewEnv(
cel.Variable("resource", cel.MapType(cel.StringType, cel.AnyType)),
cel.Variable("request", cel.MapType(cel.StringType, cel.AnyType)),
+ ext.Strings(),
)
if err != nil {
return false, errors.Wrapf(err, "failed to create CEL environment for masking exemption policy")
diff --git a/backend/common/cel.go b/backend/common/cel.go
index 7faf0136a9..c240736d82 100644
--- a/backend/common/cel.go
+++ b/backend/common/cel.go
@@ -7,6 +7,7 @@ import (
"connectrpc.com/connect"
"github.com/google/cel-go/cel"
celtypes "github.com/google/cel-go/common/types"
+ "github.com/google/cel-go/ext"
"github.com/pkg/errors"
exprproto "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
"google.golang.org/genproto/googleapis/type/expr"
@@ -37,6 +38,8 @@ var ApprovalFactors = []cel.EnvOption{
cel.Variable(CELAttributeRequestExport, cel.BoolType),
// Risk scope
cel.Variable(CELAttributeRiskLevel, cel.StringType),
+ // String extension functions (split, join, trim, etc.)
+ ext.Strings(),
// Size limit
cel.ParserExpressionSizeLimit(celLimit),
}
@@ -77,6 +80,7 @@ var IAMPolicyConditionCELAttributes = []cel.EnvOption{
cel.Variable(CELAttributeResourceSchemaName, cel.StringType),
cel.Variable(CELAttributeResourceTableName, cel.StringType),
cel.Variable(CELAttributeRequestTime, cel.TimestampType),
+ ext.Strings(),
cel.ParserExpressionSizeLimit(celLimit),
}
@@ -90,6 +94,7 @@ var MaskingRulePolicyCELAttributes = []cel.EnvOption{
cel.Variable(CELAttributeResourceTableName, cel.StringType),
cel.Variable(CELAttributeResourceColumnName, cel.StringType),
cel.Variable(CELAttributeResourceClassificationLevel, cel.IntType),
+ ext.Strings(),
cel.ParserExpressionSizeLimit(celLimit),
}
@@ -102,6 +107,7 @@ var MaskingExemptionPolicyCELAttributes = []cel.EnvOption{
cel.Variable(CELAttributeResourceColumnName, cel.StringType),
cel.Variable(CELAttributeResourceClassificationLevel, cel.IntType),
cel.Variable(CELAttributeRequestTime, cel.TimestampType),
+ ext.Strings(),
cel.ParserExpressionSizeLimit(celLimit),
}
@@ -111,6 +117,7 @@ var DatabaseGroupCELAttributes = []cel.EnvOption{
cel.Variable(CELAttributeResourceInstanceID, cel.StringType),
cel.Variable(CELAttributeResourceDatabaseName, cel.StringType),
cel.Variable(CELAttributeResourceDatabaseLabels, cel.MapType(cel.StringType, cel.StringType)),
+ ext.Strings(),
cel.ParserExpressionSizeLimit(celLimit),
}
diff --git a/backend/common/cel_test.go b/backend/common/cel_test.go
index b459cd44aa..9e7bdbba9a 100644
--- a/backend/common/cel_test.go
+++ b/backend/common/cel_test.go
@@ -209,6 +209,60 @@ func TestApprovalFactorsIncludesRiskLevel(t *testing.T) {
a.Nil(issues)
}
+func TestStringsExtensionEnabled(t *testing.T) {
+ a := require.New(t)
+
+ // ext.Strings() functions (split/join/trim) should be available in all
+ // user-authored condition environments.
+ envs := []struct {
+ name string
+ opts []cel.EnvOption
+ }{
+ {"approval", ApprovalFactors},
+ {"iam", IAMPolicyConditionCELAttributes},
+ {"maskingRule", MaskingRulePolicyCELAttributes},
+ {"maskingExemption", MaskingExemptionPolicyCELAttributes},
+ {"databaseGroup", DatabaseGroupCELAttributes},
+ }
+ for _, env := range envs {
+ e, err := cel.NewEnv(env.opts...)
+ a.NoError(err, env.name)
+
+ _, issues := e.Compile(`"a-b-c".split("-")[0] == "a"`)
+ a.Nil(issues, "%s: split should compile", env.name)
+
+ _, issues = e.Compile(`["a", "b"].join("-") == "a-b"`)
+ a.Nil(issues, "%s: join should compile", env.name)
+
+ _, issues = e.Compile(`" x ".trim() == "x"`)
+ a.Nil(issues, "%s: trim should compile", env.name)
+ }
+
+ // Evaluate a string-extension expression end to end.
+ e, err := cel.NewEnv(MaskingRulePolicyCELAttributes...)
+ a.NoError(err)
+ ast, issues := e.Compile(`"3-1-2".split("-")[0] == "3"`)
+ a.Nil(issues)
+ prg, err := e.Program(ast)
+ a.NoError(err)
+ out, _, err := prg.Eval(map[string]any{})
+ a.NoError(err)
+ a.Equal(true, out.Value())
+}
+
+func TestFallbackApprovalFactorsExcludeStringsExtension(t *testing.T) {
+ a := require.New(t)
+
+ // Fallback factors are deliberately restricted; ext.Strings() functions
+ // like split() must not be available there.
+ e, err := cel.NewEnv(FallbackApprovalFactors...)
+ a.NoError(err)
+
+ _, issues := e.Compile(`resource.project_id.split("-")[0] == "proj"`)
+ a.NotNil(issues)
+ a.Error(issues.Err())
+}
+
func TestFallbackFactorsDoNotIncludeRiskLevel(t *testing.T) {
a := require.New(t)