forked from johnjohnsp1/capsule8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
453 lines (409 loc) · 12.7 KB
/
validate.go
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// Copyright 2017 Capsule8, 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 expression
import (
"errors"
"fmt"
"unicode"
api "github.com/capsule8/capsule8/api/v0"
)
func validateIdentifier(ident string) error {
if len(ident) == 0 {
return errors.New("Invalid identifier: \"\"")
}
// First character must be letter or underscore
if !(unicode.IsLetter(rune(ident[0])) || ident[0] == '_') {
return errors.New("Identifiers must begin with letters or an underscore")
}
// Successive characters must be letters, digits, or underscore
for _, r := range ident {
if !(unicode.IsDigit(r) || unicode.IsLetter(r) || r == '_') {
return errors.New("Identifiers must contain only letters, digits, or underscores")
}
}
return nil
}
func validateValue(value *api.Value) error {
switch value.GetType() {
case api.ValueType_STRING:
if _, ok := value.GetValue().(*api.Value_StringValue); !ok {
return errors.New("STRING value has no StringValue set")
}
case api.ValueType_SINT8, api.ValueType_SINT16, api.ValueType_SINT32, api.ValueType_SINT64:
if _, ok := value.GetValue().(*api.Value_SignedValue); !ok {
return errors.New("SINT{8,16,32,64} value has no SignedValue set")
}
case api.ValueType_UINT8, api.ValueType_UINT16, api.ValueType_UINT32, api.ValueType_UINT64:
if _, ok := value.GetValue().(*api.Value_UnsignedValue); !ok {
return errors.New("UINT{8,16,32,64} value has no UnsignedValue set")
}
case api.ValueType_BOOL:
if _, ok := value.GetValue().(*api.Value_BoolValue); !ok {
return errors.New("BOOL value has no BoolValue set")
}
case api.ValueType_DOUBLE:
if _, ok := value.GetValue().(*api.Value_DoubleValue); !ok {
return errors.New("DOUBLE value has no DoubleValue set")
}
case api.ValueType_TIMESTAMP:
if _, ok := value.GetValue().(*api.Value_TimestampValue); !ok {
return errors.New("TIMESTAMP value has no TimestampValue set")
}
default:
return fmt.Errorf("Unrecognized value type %d",
value.GetType())
}
return nil
}
func validateNode(node *api.Expression, logical bool) error {
switch node.GetType() {
case api.Expression_IDENTIFIER:
ident := node.GetIdentifier()
if ident == "" {
return errors.New("Identifier missing for IDENTIFIER node")
}
return validateIdentifier(ident)
case api.Expression_VALUE:
value := node.GetValue()
if value == nil {
return errors.New("Value missing for VALUE node")
}
return validateValue(value)
case api.Expression_LOGICAL_AND, api.Expression_LOGICAL_OR:
if !logical {
return errors.New("Unexpected logical node")
}
operands := node.GetBinaryOp()
if operands == nil {
return errors.New("BinaryOp missing for logical AND/OR node")
}
if operands.Lhs == nil {
return errors.New("BinaryOp missing lhs")
}
if operands.Rhs == nil {
return errors.New("BinaryOp missing rhs")
}
err := validateNode(operands.Lhs, true)
if err != nil {
return err
}
return validateNode(operands.Rhs, true)
case api.Expression_EQ, api.Expression_NE, api.Expression_LT,
api.Expression_LE, api.Expression_GT, api.Expression_GE,
api.Expression_LIKE:
operands := node.GetBinaryOp()
if operands == nil {
return errors.New("BinaryOp missing for comparison node")
}
if operands.Lhs == nil {
return errors.New("BinaryOp missing lhs")
}
if operands.Rhs == nil {
return errors.New("BinaryOp missing rhs")
}
err := validateNode(operands.Lhs, false)
if err != nil {
return err
}
return validateNode(operands.Rhs, false)
case api.Expression_IS_NULL, api.Expression_IS_NOT_NULL:
operand := node.GetUnaryOp()
if operand == nil {
return errors.New("UnaryOp missing for unary compare")
}
return validateNode(operand, false)
case api.Expression_BITWISE_AND:
operands := node.GetBinaryOp()
if operands == nil {
return errors.New("BinaryOp missing for bitwise node")
}
if operands.Lhs == nil {
return errors.New("BinaryOp missing lhs")
}
if operands.Rhs == nil {
return errors.New("BinaryOp missing rhs")
}
err := validateNode(operands.Lhs, false)
if err != nil {
return err
}
return validateNode(operands.Rhs, false)
default:
return fmt.Errorf("Unrecognized expression type %d",
node.GetType())
}
return nil
}
func validateTree(tree *api.Expression) error {
return validateNode(tree, true)
}
func validateBitwiseAnd(expr *api.Expression) error {
// lhs must be identifier; rhs must be an integer value
operands := expr.GetBinaryOp()
if operands.Lhs.GetType() != api.Expression_IDENTIFIER {
return errors.New("BITWISE-AND lhs must be an identifier")
}
if operands.Rhs.GetType() != api.Expression_VALUE ||
!isValueTypeInteger(operands.Rhs.GetValue().GetType()) {
return errors.New("BITWISE-AND rhs must be an integer value")
}
err := validateKernelFilterNode(operands.Lhs)
if err == nil {
err = validateKernelFilterNode(operands.Rhs)
}
return err
}
func validateKernelFilterNode(node *api.Expression) error {
switch node.GetType() {
case api.Expression_IDENTIFIER:
return nil
case api.Expression_VALUE:
value := node.GetValue()
switch value.GetType() {
case api.ValueType_STRING,
api.ValueType_SINT8, api.ValueType_SINT16,
api.ValueType_SINT32, api.ValueType_SINT64,
api.ValueType_UINT8, api.ValueType_UINT16,
api.ValueType_UINT32, api.ValueType_UINT64:
return nil
}
return errors.New("Value must be string or integer")
case api.Expression_LOGICAL_AND, api.Expression_LOGICAL_OR:
operands := node.GetBinaryOp()
err := validateKernelFilterTree(operands.Lhs)
if err == nil {
err = validateKernelFilterTree(operands.Rhs)
}
return err
case api.Expression_EQ:
// lhs must be identifier; rhs must be value, can be any type
operands := node.GetBinaryOp()
if operands.Lhs.GetType() != api.Expression_IDENTIFIER {
return errors.New("Comparison lhs must be an identifier")
}
if operands.Rhs.GetType() != api.Expression_VALUE {
return errors.New("Comparison rhs must be a value")
}
err := validateKernelFilterNode(operands.Lhs)
if err == nil {
err = validateKernelFilterNode(operands.Rhs)
}
return err
case api.Expression_NE:
// if lhs is bitwise-and, rhs must be integer value 0
// if lhs is an identifier, rhs must be value of any type
operands := node.GetBinaryOp()
if operands.Lhs.GetType() == api.Expression_BITWISE_AND {
err := validateBitwiseAnd(operands.Lhs)
if err != nil {
return err
}
if operands.Rhs.GetType() != api.Expression_VALUE {
return errors.New("Rhs of comparison with bitwise-and must be 0")
}
value := operands.Rhs.GetValue()
switch value.GetType() {
case api.ValueType_SINT8, api.ValueType_SINT16,
api.ValueType_SINT32, api.ValueType_SINT64:
if value.GetSignedValue() != 0 {
return errors.New("Rhs of comparison with bitwise-and must be 0")
}
case api.ValueType_UINT8, api.ValueType_UINT16,
api.ValueType_UINT32, api.ValueType_UINT64:
if value.GetUnsignedValue() != 0 {
return errors.New("Rhs of comparison with bitwise-and must be 0")
}
default:
return errors.New("Rhs of comparison with bitwise-and must be 0")
}
return nil
}
if operands.Lhs.GetType() != api.Expression_IDENTIFIER {
return errors.New("Comparison lhs must be an identifier")
}
if operands.Rhs.GetType() != api.Expression_VALUE {
return errors.New("Comparison rhs must be a value")
}
err := validateKernelFilterNode(operands.Lhs)
if err == nil {
err = validateKernelFilterNode(operands.Rhs)
}
return err
case api.Expression_LT, api.Expression_LE,
api.Expression_GT, api.Expression_GE:
// lhs must be identifier; rhs must be value of integer type
operands := node.GetBinaryOp()
if operands.Lhs.GetType() != api.Expression_IDENTIFIER {
return errors.New("Comparison lhs must be an identifier")
}
if operands.Rhs.GetType() != api.Expression_VALUE ||
!isValueTypeInteger(operands.Rhs.GetValue().GetType()) {
return errors.New("Comparison rhs must be an integer value")
}
err := validateKernelFilterNode(operands.Lhs)
if err == nil {
err = validateKernelFilterNode(operands.Rhs)
}
return err
case api.Expression_LIKE:
// lhs must be identifier; rhs must be string value
operands := node.GetBinaryOp()
if operands.Lhs.GetType() != api.Expression_IDENTIFIER {
return errors.New("Comparison lhs must be an identifier")
}
if operands.Rhs.GetType() != api.Expression_VALUE ||
!isValueTypeString(operands.Rhs.GetValue().GetType()) {
return errors.New("Comparison rhs must be a string value")
}
err := validateKernelFilterNode(operands.Lhs)
if err == nil {
err = validateKernelFilterNode(operands.Rhs)
}
return err
}
return fmt.Errorf("Invalid expression type %d", node.GetType())
}
func validateKernelFilterTree(expr *api.Expression) error {
if expr.GetType() == api.Expression_BITWISE_AND {
return validateBitwiseAnd(expr)
}
return validateKernelFilterNode(expr)
}
func validateTypes(expr *api.Expression, types FieldTypeMap) (api.ValueType, error) {
switch op := expr.GetType(); op {
case api.Expression_IDENTIFIER:
ident := expr.GetIdentifier()
t, ok := types[ident]
if !ok {
return 0, fmt.Errorf("Undefined identifier %q", ident)
}
return t, nil
case api.Expression_VALUE:
return expr.GetValue().GetType(), nil
case api.Expression_LOGICAL_AND, api.Expression_LOGICAL_OR:
operands := expr.GetBinaryOp()
lhs, err := validateTypes(operands.Lhs, types)
if err != nil {
return 0, err
}
if lhs != api.ValueType_BOOL {
err = fmt.Errorf("Lhs of AND/OR must be type BOOL; got %s",
api.ValueType_name[int32(lhs)])
return 0, err
}
rhs, err := validateTypes(operands.Rhs, types)
if err != nil {
return 0, err
}
if rhs != api.ValueType_BOOL {
err = fmt.Errorf("Rhs of AND/OR must be type BOOL; got %s",
api.ValueType_name[int32(rhs)])
return 0, err
}
return api.ValueType_BOOL, nil
case api.Expression_EQ, api.Expression_NE:
operands := expr.GetBinaryOp()
lhs, err := validateTypes(operands.Lhs, types)
if err != nil {
return 0, err
}
rhs, err := validateTypes(operands.Rhs, types)
if err != nil {
return 0, err
}
if lhs != rhs {
err = fmt.Errorf("Type mismatch (%s vs. %s)",
api.ValueType_name[int32(lhs)],
api.ValueType_name[int32(rhs)])
return 0, err
}
return api.ValueType_BOOL, nil
case api.Expression_LT, api.Expression_LE,
api.Expression_GT, api.Expression_GE:
operands := expr.GetBinaryOp()
lhs, err := validateTypes(operands.Lhs, types)
if err != nil {
return 0, err
}
if !isValueTypeNumeric(lhs) {
err = fmt.Errorf("Type for %s must be numeric; got %s",
operatorStrings[op],
api.ValueType_name[int32(lhs)])
return 0, err
}
rhs, err := validateTypes(operands.Rhs, types)
if err != nil {
return 0, err
}
if lhs != rhs {
err = fmt.Errorf("Type mismatch (%s vs. %s)",
api.ValueType_name[int32(lhs)],
api.ValueType_name[int32(rhs)])
return 0, err
}
return api.ValueType_BOOL, nil
case api.Expression_LIKE:
operands := expr.GetBinaryOp()
lhs, err := validateTypes(operands.Lhs, types)
if err != nil {
return 0, err
}
if !isValueTypeString(lhs) {
err = fmt.Errorf("Type for LIKE must be STRING; got %s",
api.ValueType_name[int32(lhs)])
return 0, err
}
rhs, err := validateTypes(operands.Rhs, types)
if err != nil {
return 0, err
}
if lhs != rhs {
err = fmt.Errorf("Type mismatch (%s vs. %s)",
api.ValueType_name[int32(lhs)],
api.ValueType_name[int32(rhs)])
return 0, err
}
return api.ValueType_BOOL, nil
case api.Expression_IS_NULL, api.Expression_IS_NOT_NULL:
_, err := validateTypes(expr.GetUnaryOp(), types)
if err != nil {
return 0, err
}
return api.ValueType_BOOL, nil
case api.Expression_BITWISE_AND:
operands := expr.GetBinaryOp()
lhs, err := validateTypes(operands.Lhs, types)
if err != nil {
return 0, err
}
if !isValueTypeInteger(lhs) {
err = fmt.Errorf("Type for %s must be an integer; got %s",
operatorStrings[op],
api.ValueType_name[int32(lhs)])
return 0, err
}
rhs, err := validateTypes(operands.Rhs, types)
if err != nil {
return 0, err
}
if lhs != rhs {
err = fmt.Errorf("Type mismatch (%s vs. %s)",
api.ValueType_name[int32(lhs)],
api.ValueType_name[int32(rhs)])
return 0, err
}
return lhs, nil
}
return 0, nil
}