Skip to content

Commit 4007189

Browse files
committed
Make sure to validate the AST in addition to just selectors and values
This could be useful when the AST we are given doesn’t necessarily come from our internal parser.
1 parent 50165c7 commit 4007189

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

validate.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,31 @@ import (
77
func validateRecurse(ast Expression, fields FieldConfigurations, maxRawValueLength int) (int, error) {
88
switch node := ast.(type) {
99
case *UnaryExpression:
10-
return validateRecurse(node, fields, maxRawValueLength)
10+
switch node.Operator {
11+
case UnaryOpNot:
12+
// this is fine
13+
default:
14+
return 0, fmt.Errorf("Invalid unary expression operator: %d", node.Operator)
15+
}
16+
17+
if node.Operand == nil {
18+
return 0, fmt.Errorf("Invalid unary expression operand: nil")
19+
}
20+
return validateRecurse(node.Operand, fields, maxRawValueLength)
1121
case *BinaryExpression:
22+
switch node.Operator {
23+
case BinaryOpAnd, BinaryOpOr:
24+
// this is fine
25+
default:
26+
return 0, fmt.Errorf("Invalid binary expression operator: %d", node.Operator)
27+
}
28+
29+
if node.Left == nil {
30+
return 0, fmt.Errorf("Invalid left hand side of binary expression: nil")
31+
} else if node.Right == nil {
32+
return 0, fmt.Errorf("Invalid right hand side of binary expression: nil")
33+
}
34+
1235
leftMatches, err := validateRecurse(node.Left, fields, maxRawValueLength)
1336
if err != nil {
1437
return leftMatches, err
@@ -73,6 +96,13 @@ func validateRecurse(ast Expression, fields FieldConfigurations, maxRawValueLeng
7396

7497
node.Value.Converted = coerced
7598
}
99+
} else {
100+
switch node.Operator {
101+
case MatchIsEmpty, MatchIsNotEmpty:
102+
// these don't require values
103+
default:
104+
return 1, fmt.Errorf("Match operator %q requires a non-nil value", node.Operator)
105+
}
76106
}
77107
return 1, nil
78108
}

0 commit comments

Comments
 (0)