-
Notifications
You must be signed in to change notification settings - Fork 346
Expand file tree
/
Copy pathcel.go
More file actions
246 lines (238 loc) · 7.43 KB
/
cel.go
File metadata and controls
246 lines (238 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright 2020-2025 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package buflintvalidate
import (
"fmt"
"strings"
"buf.build/gen/go/bufbuild/protovalidate/protocolbuffers/go/buf/validate"
"github.com/bufbuild/buf/private/bufpkg/bufprotosource"
celpv "github.com/bufbuild/protovalidate-go/cel"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types"
"google.golang.org/protobuf/reflect/protoreflect"
"google.golang.org/protobuf/types/dynamicpb"
)
const (
// https://buf.build/bufbuild/protovalidate/docs/main:buf.validate#buf.validate.FieldConstraints
celFieldNumberInFieldConstraints = 23
// https://buf.build/bufbuild/protovalidate/docs/main:buf.validate#buf.validate.MessageConstraints
celFieldNumberInMessageConstraints = 3
)
func checkCELForMessage(
add func(bufprotosource.Descriptor, bufprotosource.Location, []bufprotosource.Location, string, ...interface{}),
messageConstraints *validate.MessageConstraints,
messageDescriptor protoreflect.MessageDescriptor,
message bufprotosource.Message,
) error {
celEnv, err := cel.NewEnv(
cel.Lib(celpv.NewLibrary()),
)
if err != nil {
return err
}
celEnv, err = celEnv.Extend(
cel.Types(dynamicpb.NewMessage(messageDescriptor)),
cel.Variable("this", cel.ObjectType(string(messageDescriptor.FullName()))),
)
if err != nil {
return err
}
checkCEL(
celEnv,
messageConstraints.GetCel(),
fmt.Sprintf("message %q", message.Name()),
fmt.Sprintf("Message %q", message.Name()),
"(buf.validate.message).cel",
func(index int, format string, args ...interface{}) {
messageConstraintsOptionLocation := message.OptionExtensionLocation(
validate.E_Message,
celFieldNumberInMessageConstraints,
int32(index),
)
add(message, messageConstraintsOptionLocation, nil, format, args...)
},
)
return nil
}
func checkCELForField(
adder *adder,
fieldConstraints *validate.FieldConstraints,
fieldDescriptor protoreflect.FieldDescriptor,
// forItems is true if the CEL rule is defined on a non-repeated field or on each item of a repeated field.
forItems bool,
) error {
if len(fieldConstraints.GetCel()) == 0 {
return nil
}
celEnv, err := cel.NewEnv(
cel.Lib(celpv.NewLibrary()),
)
if err != nil {
return err
}
celEnv, err = celEnv.Extend(
append(
celpv.RequiredEnvOptions(fieldDescriptor),
cel.Variable("this", celpv.ProtoFieldToType(fieldDescriptor, false, forItems)),
)...,
)
if err != nil {
return err
}
checkCEL(
celEnv,
fieldConstraints.GetCel(),
fmt.Sprintf("field %q", adder.fieldName()),
fmt.Sprintf("Field %q", adder.fieldName()),
adder.getFieldRuleName(celFieldNumberInFieldConstraints),
func(index int, format string, args ...interface{}) {
adder.addForPathf(
[]int32{celFieldNumberInFieldConstraints, int32(index)},
format,
args...,
)
},
)
return nil
}
// Returns true only if all cel expressions compile
func checkCEL(
celEnv *cel.Env,
celConstraints []*validate.Constraint,
parentName string,
parentNameCapitalized string,
celName string,
add func(int, string, ...interface{}),
) bool {
allCelExpressionsCompile := true
idToConstraintIndices := make(map[string][]int, len(celConstraints))
for i, celConstraint := range celConstraints {
if celID := celConstraint.GetId(); celID != "" {
for _, char := range celID {
if 'a' <= char && char <= 'z' {
continue
} else if 'A' <= char && char <= 'Z' {
continue
} else if '0' <= char && char <= '9' {
continue
} else if char == '_' || char == '-' || char == '.' {
continue
}
add(
i,
"%s has invalid characters for %s.id. IDs must contain only characters a-z, A-Z, 0-9, '.', '_', '-'.",
parentNameCapitalized,
celName,
)
break
}
idToConstraintIndices[celID] = append(idToConstraintIndices[celID], i)
} else {
add(i, "%s has an empty %s.id. IDs should always be specified.", parentNameCapitalized, celName)
}
if len(strings.TrimSpace(celConstraint.GetExpression())) == 0 {
add(i, "%s has an empty %s.expression. Expressions should always be specified.", parentNameCapitalized, celName)
continue
}
ast, compileIssues := celEnv.Compile(celConstraint.GetExpression())
switch {
case ast.OutputType().IsAssignableType(cel.BoolType):
if celConstraint.GetMessage() == "" {
add(
i,
"%s has an empty %s.message for an expression that evaluates to a boolean. If an expression evaluates to a boolean, a message should always be specified.",
parentNameCapitalized,
celName,
)
}
case ast.OutputType().IsAssignableType(cel.StringType):
if celConstraint.GetMessage() != "" {
add(
i,
"%s has a %s with an expression that evaluates to a string, and also has a message. The message is redundant - since the expression evaluates to a string, its result will be printed instead of the message, so the message should be removed.",
parentNameCapitalized,
celName,
)
}
case ast.OutputType().IsExactType(types.ErrorType):
// If the output type is error, it means compilation has failed and we
// only need to add the compilation issues.
default:
add(
i,
"%s.expression on %s evaluates to a %s, only string and boolean are allowed.",
celName,
parentName,
cel.FormatCELType(ast.OutputType()),
)
}
if compileIssues.Err() != nil {
allCelExpressionsCompile = false
for _, parsedIssue := range parseCelIssuesText(compileIssues.Err().Error()) {
add(
i,
"%s.expression on %s fails to compile: %s",
celName,
parentName,
parsedIssue,
)
}
}
}
for celID, constraintIndices := range idToConstraintIndices {
if len(constraintIndices) <= 1 {
continue
}
for _, constraintIndex := range constraintIndices {
add(
constraintIndex,
"%s.id (%q) is not unique within %s.",
celName,
celID,
parentName,
)
}
}
return allCelExpressionsCompile
}
// this depends on the undocumented behavior of cel-go's error message
//
// maps a string in this form:
// "ERROR: <input>:1:6: found no matching overload for '_+_' applied to '(int, string)'
// | this + 'xyz' > (this * 'xyz')
// | .....^
// ERROR: <input>:1:22: found no matching overload for '_*_' applied to '(int, string)'
// | this + 'xyz' > (this * 'xyz')
// | .....................^"
// to a string slice:
// [ "found no matching overload for '_+_' applied to '(int, string)'
// | this + 'xyz' > (this * 'xyz')
// | .....^",
// "found no matching overload for '_*_' applied to '(int, string)'
// | this + 'xyz' > (this * 'xyz')
// | .....................^"]
func parseCelIssuesText(issuesText string) []string {
issues := strings.Split(issuesText, "ERROR: <input>:")
parsedIssues := make([]string, 0, len(issues)-1)
for _, issue := range issues {
issue = strings.TrimSpace(issue)
if len(issue) == 0 {
continue
}
// now issue looks like 1:2:<error message>
parts := strings.SplitAfterN(issue, ":", 3)
parsedIssues = append(parsedIssues, strings.TrimSpace(parts[len(parts)-1]))
}
return parsedIssues
}