| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package cel |
| 16 |
|
| 17 |
import ( |
| 18 |
"fmt" |
| 19 |
"reflect" |
| 20 |
"regexp" |
| 21 |
|
| 22 |
"github.com/google/cel-go/common/ast" |
| 23 |
"github.com/google/cel-go/common/env" |
| 24 |
"github.com/google/cel-go/common/overloads" |
| 25 |
) |
| 26 |
|
| 27 |
const ( |
| 28 |
durationValidatorName = "cel.validator.duration" |
| 29 |
regexValidatorName = "cel.validator.matches" |
| 30 |
timestampValidatorName = "cel.validator.timestamp" |
| 31 |
homogeneousValidatorName = "cel.validator.homogeneous_literals" |
| 32 |
nestingLimitValidatorName = "cel.validator.comprehension_nesting_limit" |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
HomogeneousAggregateLiteralExemptFunctions = homogeneousValidatorName + ".exempt" |
| 42 |
) |
| 43 |
|
| 44 |
var ( |
| 45 |
astValidatorFactories = map[string]ASTValidatorFactory{ |
| 46 |
nestingLimitValidatorName: func(val *env.Validator) (ASTValidator, error) { |
| 47 |
if limit, found := val.ConfigValue("limit"); found { |
| 48 |
|
| 49 |
if val, isDouble := limit.(float64); isDouble { |
| 50 |
if val != float64(int64(val)) { |
| 51 |
return nil, fmt.Errorf("invalid validator: %s, limit value is not a whole number: %v", nestingLimitValidatorName, limit) |
| 52 |
} |
| 53 |
return ValidateComprehensionNestingLimit(int(val)), nil |
| 54 |
} |
| 55 |
|
| 56 |
if val, isInt := limit.(int); isInt { |
| 57 |
return ValidateComprehensionNestingLimit(val), nil |
| 58 |
} |
| 59 |
return nil, fmt.Errorf("invalid validator: %s unsupported limit type: %v", nestingLimitValidatorName, limit) |
| 60 |
} |
| 61 |
return nil, fmt.Errorf("invalid validator: %s missing limit", nestingLimitValidatorName) |
| 62 |
}, |
| 63 |
durationValidatorName: func(*env.Validator) (ASTValidator, error) { |
| 64 |
return ValidateDurationLiterals(), nil |
| 65 |
}, |
| 66 |
regexValidatorName: func(*env.Validator) (ASTValidator, error) { |
| 67 |
return ValidateRegexLiterals(), nil |
| 68 |
}, |
| 69 |
timestampValidatorName: func(*env.Validator) (ASTValidator, error) { |
| 70 |
return ValidateTimestampLiterals(), nil |
| 71 |
}, |
| 72 |
homogeneousValidatorName: func(*env.Validator) (ASTValidator, error) { |
| 73 |
return ValidateHomogeneousAggregateLiterals(), nil |
| 74 |
}, |
| 75 |
} |
| 76 |
) |
| 77 |
|
| 78 |
|
| 79 |
type ASTValidatorFactory func(*env.Validator) (ASTValidator, error) |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
func ASTValidators(validators ...ASTValidator) EnvOption { |
| 86 |
return func(e *Env) (*Env, error) { |
| 87 |
for _, v := range validators { |
| 88 |
if !e.HasValidator(v.Name()) { |
| 89 |
e.validators = append(e.validators, v) |
| 90 |
} |
| 91 |
} |
| 92 |
return e, nil |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
type ASTValidator interface { |
| 101 |
|
| 102 |
Name() string |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
Validate(*Env, ValidatorConfig, *ast.AST, *Issues) |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
type ConfigurableASTValidator interface { |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
ToConfig() *env.Validator |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
type ValidatorConfig interface { |
| 129 |
GetOrDefault(name string, value any) any |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
type MutableValidatorConfig interface { |
| 135 |
ValidatorConfig |
| 136 |
Set(name string, value any) error |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
type ASTValidatorConfigurer interface { |
| 144 |
Configure(MutableValidatorConfig) error |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
type validatorConfig struct { |
| 149 |
data map[string]any |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
func newValidatorConfig() *validatorConfig { |
| 154 |
return &validatorConfig{ |
| 155 |
data: map[string]any{ |
| 156 |
HomogeneousAggregateLiteralExemptFunctions: []string{}, |
| 157 |
}, |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
func (config *validatorConfig) GetOrDefault(name string, value any) any { |
| 165 |
v, found := config.data[name] |
| 166 |
if !found { |
| 167 |
return value |
| 168 |
} |
| 169 |
return v |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
func (config *validatorConfig) Set(name string, value any) error { |
| 177 |
v, found := config.data[name] |
| 178 |
if found && reflect.TypeOf(v) != reflect.TypeOf(value) { |
| 179 |
return fmt.Errorf("incompatible configuration type for %s, got %T, wanted %T", name, value, v) |
| 180 |
} |
| 181 |
config.data[name] = value |
| 182 |
return nil |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
func ExtendedValidations() EnvOption { |
| 191 |
return ASTValidators( |
| 192 |
ValidateDurationLiterals(), |
| 193 |
ValidateTimestampLiterals(), |
| 194 |
ValidateRegexLiterals(), |
| 195 |
ValidateHomogeneousAggregateLiterals(), |
| 196 |
) |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
func ValidateDurationLiterals() ASTValidator { |
| 201 |
return newFormatValidator(overloads.TypeConvertDuration, 0, evalCall) |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
func ValidateTimestampLiterals() ASTValidator { |
| 206 |
return newFormatValidator(overloads.TypeConvertTimestamp, 0, evalCall) |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
func ValidateRegexLiterals() ASTValidator { |
| 211 |
return newFormatValidator(overloads.Matches, 0, compileRegex) |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
func ValidateHomogeneousAggregateLiterals() ASTValidator { |
| 220 |
return homogeneousAggregateLiteralValidator{} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
func ValidateComprehensionNestingLimit(limit int) ASTValidator { |
| 232 |
return nestingLimitValidator{limit: limit} |
| 233 |
} |
| 234 |
|
| 235 |
type argChecker func(env *Env, call, arg ast.Expr) error |
| 236 |
|
| 237 |
func newFormatValidator(funcName string, argNum int, check argChecker) formatValidator { |
| 238 |
return formatValidator{ |
| 239 |
funcName: funcName, |
| 240 |
check: check, |
| 241 |
argNum: argNum, |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
type formatValidator struct { |
| 246 |
funcName string |
| 247 |
argNum int |
| 248 |
check argChecker |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
func (v formatValidator) Name() string { |
| 253 |
return fmt.Sprintf("cel.validator.%s", v.funcName) |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
func (v formatValidator) ToConfig() *env.Validator { |
| 258 |
return env.NewValidator(v.Name()) |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
func (v formatValidator) Validate(e *Env, _ ValidatorConfig, a *ast.AST, iss *Issues) { |
| 264 |
root := ast.NavigateAST(a) |
| 265 |
funcCalls := ast.MatchDescendants(root, ast.FunctionMatcher(v.funcName)) |
| 266 |
for _, call := range funcCalls { |
| 267 |
callArgs := call.AsCall().Args() |
| 268 |
if len(callArgs) <= v.argNum { |
| 269 |
continue |
| 270 |
} |
| 271 |
litArg := callArgs[v.argNum] |
| 272 |
if litArg.Kind() != ast.LiteralKind { |
| 273 |
continue |
| 274 |
} |
| 275 |
if err := v.check(e, call, litArg); err != nil { |
| 276 |
iss.ReportErrorAtID(litArg.ID(), "invalid %s argument", v.funcName) |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
func evalCall(env *Env, call, arg ast.Expr) error { |
| 282 |
ast := &Ast{impl: ast.NewAST(call, ast.NewSourceInfo(nil))} |
| 283 |
prg, err := env.Program(ast) |
| 284 |
if err != nil { |
| 285 |
return err |
| 286 |
} |
| 287 |
_, _, err = prg.Eval(NoVars()) |
| 288 |
return err |
| 289 |
} |
| 290 |
|
| 291 |
func compileRegex(_ *Env, _, arg ast.Expr) error { |
| 292 |
pattern := arg.AsLiteral().Value().(string) |
| 293 |
_, err := regexp.Compile(pattern) |
| 294 |
return err |
| 295 |
} |
| 296 |
|
| 297 |
type homogeneousAggregateLiteralValidator struct{} |
| 298 |
|
| 299 |
|
| 300 |
func (homogeneousAggregateLiteralValidator) Name() string { |
| 301 |
return homogeneousValidatorName |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
func (v homogeneousAggregateLiteralValidator) ToConfig() *env.Validator { |
| 306 |
return env.NewValidator(v.Name()) |
| 307 |
} |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
func (v homogeneousAggregateLiteralValidator) Validate(_ *Env, c ValidatorConfig, a *ast.AST, iss *Issues) { |
| 314 |
var exemptedFunctions []string |
| 315 |
exemptedFunctions = c.GetOrDefault(HomogeneousAggregateLiteralExemptFunctions, exemptedFunctions).([]string) |
| 316 |
root := ast.NavigateAST(a) |
| 317 |
listExprs := ast.MatchDescendants(root, ast.KindMatcher(ast.ListKind)) |
| 318 |
for _, listExpr := range listExprs { |
| 319 |
if inExemptFunction(listExpr, exemptedFunctions) { |
| 320 |
continue |
| 321 |
} |
| 322 |
l := listExpr.AsList() |
| 323 |
elements := l.Elements() |
| 324 |
optIndices := l.OptionalIndices() |
| 325 |
var elemType *Type |
| 326 |
for i, e := range elements { |
| 327 |
et := a.GetType(e.ID()) |
| 328 |
if isOptionalIndex(i, optIndices) { |
| 329 |
et = et.Parameters()[0] |
| 330 |
} |
| 331 |
if elemType == nil { |
| 332 |
elemType = et |
| 333 |
continue |
| 334 |
} |
| 335 |
if !elemType.IsEquivalentType(et) { |
| 336 |
v.typeMismatch(iss, e.ID(), elemType, et) |
| 337 |
break |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
mapExprs := ast.MatchDescendants(root, ast.KindMatcher(ast.MapKind)) |
| 342 |
for _, mapExpr := range mapExprs { |
| 343 |
if inExemptFunction(mapExpr, exemptedFunctions) { |
| 344 |
continue |
| 345 |
} |
| 346 |
m := mapExpr.AsMap() |
| 347 |
entries := m.Entries() |
| 348 |
var keyType, valType *Type |
| 349 |
for _, e := range entries { |
| 350 |
mapEntry := e.AsMapEntry() |
| 351 |
key, val := mapEntry.Key(), mapEntry.Value() |
| 352 |
kt, vt := a.GetType(key.ID()), a.GetType(val.ID()) |
| 353 |
if mapEntry.IsOptional() { |
| 354 |
vt = vt.Parameters()[0] |
| 355 |
} |
| 356 |
if keyType == nil && valType == nil { |
| 357 |
keyType, valType = kt, vt |
| 358 |
continue |
| 359 |
} |
| 360 |
if !keyType.IsEquivalentType(kt) { |
| 361 |
v.typeMismatch(iss, key.ID(), keyType, kt) |
| 362 |
} |
| 363 |
if !valType.IsEquivalentType(vt) { |
| 364 |
v.typeMismatch(iss, val.ID(), valType, vt) |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
func inExemptFunction(e ast.NavigableExpr, exemptFunctions []string) bool { |
| 371 |
parent, found := e.Parent() |
| 372 |
for found { |
| 373 |
if parent.Kind() == ast.CallKind { |
| 374 |
fnName := parent.AsCall().FunctionName() |
| 375 |
for _, exempt := range exemptFunctions { |
| 376 |
if exempt == fnName { |
| 377 |
return true |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
parent, found = parent.Parent() |
| 382 |
} |
| 383 |
return false |
| 384 |
} |
| 385 |
|
| 386 |
func isOptionalIndex(i int, optIndices []int32) bool { |
| 387 |
for _, optInd := range optIndices { |
| 388 |
if i == int(optInd) { |
| 389 |
return true |
| 390 |
} |
| 391 |
} |
| 392 |
return false |
| 393 |
} |
| 394 |
|
| 395 |
func (homogeneousAggregateLiteralValidator) typeMismatch(iss *Issues, id int64, expected, actual *Type) { |
| 396 |
iss.ReportErrorAtID(id, "expected type '%s' but found '%s'", FormatCELType(expected), FormatCELType(actual)) |
| 397 |
} |
| 398 |
|
| 399 |
type nestingLimitValidator struct { |
| 400 |
limit int |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
func (v nestingLimitValidator) Name() string { |
| 405 |
return nestingLimitValidatorName |
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
func (v nestingLimitValidator) ToConfig() *env.Validator { |
| 411 |
return env.NewValidator(v.Name()).SetConfig(map[string]any{"limit": v.limit}) |
| 412 |
} |
| 413 |
|
| 414 |
|
| 415 |
func (v nestingLimitValidator) Validate(e *Env, _ ValidatorConfig, a *ast.AST, iss *Issues) { |
| 416 |
root := ast.NavigateAST(a) |
| 417 |
comprehensions := ast.MatchDescendants(root, ast.KindMatcher(ast.ComprehensionKind)) |
| 418 |
if len(comprehensions) <= v.limit { |
| 419 |
return |
| 420 |
} |
| 421 |
for _, comp := range comprehensions { |
| 422 |
count := 0 |
| 423 |
e := comp |
| 424 |
hasParent := true |
| 425 |
for hasParent { |
| 426 |
|
| 427 |
if e.Kind() != ast.ComprehensionKind { |
| 428 |
e, hasParent = e.Parent() |
| 429 |
continue |
| 430 |
} |
| 431 |
|
| 432 |
|
| 433 |
iterRange := e.AsComprehension().IterRange() |
| 434 |
if iterRange.Kind() == ast.ListKind && iterRange.AsList().Size() == 0 { |
| 435 |
e, hasParent = e.Parent() |
| 436 |
continue |
| 437 |
} |
| 438 |
|
| 439 |
count++ |
| 440 |
if count > v.limit { |
| 441 |
iss.ReportErrorAtID(comp.ID(), "comprehension exceeds nesting limit") |
| 442 |
break |
| 443 |
} |
| 444 |
e, hasParent = e.Parent() |
| 445 |
} |
| 446 |
} |
| 447 |
} |