-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathencoder.go
507 lines (428 loc) · 16.2 KB
/
encoder.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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package waf
import (
"context"
"fmt"
"math"
"reflect"
"strings"
"time"
"unicode"
"github.com/DataDog/go-libddwaf/v3/errors"
"github.com/DataDog/go-libddwaf/v3/internal/bindings"
"github.com/DataDog/go-libddwaf/v3/internal/unsafe"
"github.com/DataDog/go-libddwaf/v3/timer"
)
// Encode Go values into wafObjects. Only the subset of Go types representable into wafObjects
// will be encoded while ignoring the rest of it.
// The encoder allocates the memory required for new wafObjects into the Go memory, which must be kept
// referenced for their lifetime in the C world. This lifetime depends on the ddwaf function being used with.
// the encoded result. The Go references of the allocated wafObjects, along with every Go pointer they may
// reference now or in the future, are stored and referenced in the `cgoRefs` field. The user MUST leverage
// `keepAlive()` with it according to its ddwaf use-case.
type encoder struct {
// timer makes sure the encoder doesn't spend too much time doing its job.
timer timer.Timer
// For each TruncationReason, holds the size that is required to avoid truncation for each truncation that happened.
truncations map[TruncationReason][]int
cgoRefs cgoRefPool
containerMaxSize int
stringMaxSize int
objectMaxDepth int
}
// TruncationReason is a flag representing reasons why some input was not encoded in full.
type TruncationReason uint8
const (
// StringTooLong indicates a string exceeded the maximum string length configured. The truncation
// values indicate the actual length of truncated strings.
StringTooLong TruncationReason = 1 << iota
// ContainerTooLarge indicates a container (list, map, struct) exceeded the maximum number of
// elements configured. The truncation values indicate the actual number of elements in the
// truncated container.
ContainerTooLarge
// ObjectTooDeep indicates an overall object exceeded the maximum encoding depths configured. The
// truncation values indicate an estimated actual depth of the truncated object. The value is
// guaranteed to be less than or equal to the actual depth (it may not be more).
ObjectTooDeep
)
func (reason TruncationReason) String() string {
switch reason {
case ObjectTooDeep:
return "depth"
case ContainerTooLarge:
return "container-size"
case StringTooLong:
return "string-size"
default:
return fmt.Sprintf("TruncationReason(%v)", int(reason))
}
}
const (
AppsecFieldTag = "ddwaf"
AppsecFieldTagValueIgnore = "ignore"
)
type native interface {
int64 | uint64 | uintptr
}
func newLimitedEncoder(timer timer.Timer) encoder {
return encoder{
timer: timer,
containerMaxSize: bindings.WafMaxContainerSize,
stringMaxSize: bindings.WafMaxStringLength,
objectMaxDepth: bindings.WafMaxContainerDepth,
}
}
func newMaxEncoder() encoder {
timer, _ := timer.NewTimer(timer.WithUnlimitedBudget())
return encoder{
timer: timer,
containerMaxSize: math.MaxInt,
stringMaxSize: math.MaxInt,
objectMaxDepth: math.MaxInt,
}
}
// Encode takes a Go value and returns a wafObject pointer and an error.
// The returned wafObject is the root of the tree of nested wafObjects representing the Go value.
// The only error case is if the top-level object is "Unusable" which means that the data is nil or a non-data type
// like a function or a channel.
func (encoder *encoder) Encode(data any) (wo *bindings.WafObject, err error) {
value := reflect.ValueOf(data)
wo = &bindings.WafObject{}
err = encoder.encode(value, wo, encoder.objectMaxDepth)
if len(encoder.truncations[ObjectTooDeep]) != 0 && !encoder.timer.Exhausted() {
encoder.measureObjectDepth(value, encoder.timer.Remaining())
}
return
}
// Truncations returns all truncations that happened since the last call to `Truncations()`, and clears the internal
// list. This is a map from truncation reason to the list of un-truncated value sizes.
func (encoder *encoder) Truncations() map[TruncationReason][]int {
result := encoder.truncations
encoder.truncations = nil
return result
}
func encodeNative[T native](val T, t bindings.WafObjectType, obj *bindings.WafObject) {
obj.Type = t
obj.Value = (uintptr)(val)
}
var nullableTypeKinds = map[reflect.Kind]struct{}{
reflect.Interface: {},
reflect.Pointer: {},
reflect.UnsafePointer: {},
reflect.Map: {},
reflect.Slice: {},
reflect.Func: {},
reflect.Chan: {},
}
// isValueNil check if the value is nullable and if it is actually nil
// we cannot directly use value.IsNil() because it panics on non-pointer values
func isValueNil(value reflect.Value) bool {
_, nullable := nullableTypeKinds[value.Kind()]
return nullable && value.IsNil()
}
func (encoder *encoder) encode(value reflect.Value, obj *bindings.WafObject, depth int) error {
if encoder.timer.Exhausted() {
return errors.ErrTimeout
}
value, kind := resolvePointer(value)
if (kind == reflect.Interface || kind == reflect.Pointer) && !value.IsNil() {
// resolvePointer failed to resolve to something that's not a pointer, it
// has indirected too many times...
return errors.ErrTooManyIndirections
}
// Measure-only runs for leaves
if obj == nil && kind != reflect.Array && kind != reflect.Slice && kind != reflect.Map && kind != reflect.Struct {
// Nothing to do, we were only here to measure object depth!
return nil
}
switch {
// Terminal cases (leaves of the tree)
// Is invalid type: nil interfaces for example, cannot be used to run any reflect method or it's susceptible to panic
case !value.IsValid() || kind == reflect.Invalid:
return errors.ErrUnsupportedValue
// Is nullable type: nil pointers, channels, maps or functions
case isValueNil(value):
encodeNative[uintptr](0, bindings.WafNilType, obj)
// Booleans
case kind == reflect.Bool:
encodeNative(unsafe.NativeToUintptr(value.Bool()), bindings.WafBoolType, obj)
// Numbers
case value.CanInt(): // any int type or alias
encodeNative(value.Int(), bindings.WafIntType, obj)
case value.CanUint(): // any Uint type or alias
encodeNative(value.Uint(), bindings.WafUintType, obj)
case value.CanFloat(): // any float type or alias
encodeNative(unsafe.NativeToUintptr(value.Float()), bindings.WafFloatType, obj)
// Strings
case kind == reflect.String: // string type
encoder.encodeString(value.String(), obj)
case (kind == reflect.Array || kind == reflect.Slice) && value.Type().Elem().Kind() == reflect.Uint8:
// Byte Arrays are skipped voluntarily because they are often used
// to do partial parsing which leads to false positives
return nil
// Containers (internal nodes of the tree)
// All recursive cases can only execute if the depth is superior to 0.
case depth <= 0:
// Record that there was a truncation; we will try to measure the actual depth of the object afterwards.
encoder.addTruncation(ObjectTooDeep, -1)
return errors.ErrMaxDepthExceeded
// Either an array or a slice of an array
case kind == reflect.Array || kind == reflect.Slice:
encoder.encodeArray(value, obj, depth-1)
case kind == reflect.Map:
encoder.encodeMap(value, obj, depth-1)
case kind == reflect.Struct:
encoder.encodeStruct(value, obj, depth-1)
default:
return errors.ErrUnsupportedValue
}
return nil
}
func (encoder *encoder) encodeString(str string, obj *bindings.WafObject) {
size := len(str)
if size > encoder.stringMaxSize {
str = str[:encoder.stringMaxSize]
encoder.addTruncation(StringTooLong, size)
}
encoder.cgoRefs.AllocWafString(obj, str)
}
func getFieldNameFromType(field reflect.StructField) (string, bool) {
fieldName := field.Name
// Private and synthetics fields
if len(fieldName) < 1 || unicode.IsLower(rune(fieldName[0])) {
return "", false
}
// Use the json tag name as field name if present
if tag, ok := field.Tag.Lookup("json"); ok {
if i := strings.IndexByte(tag, byte(',')); i > 0 {
tag = tag[:i]
}
if len(tag) > 0 {
fieldName = tag
}
}
return fieldName, true
}
// encodeStruct takes a reflect.Value and a wafObject pointer and iterates on the struct field to build
// a wafObject map of type wafMapType. The specificities are the following:
// - It will only take the first encoder.containerMaxSize elements of the struct
// - If the field has a json tag it will become the field name
// - Private fields and also values producing an error at encoding will be skipped
// - Even if the element values are invalid or null we still keep them to report the field name
func (encoder *encoder) encodeStruct(value reflect.Value, obj *bindings.WafObject, depth int) {
if encoder.timer.Exhausted() {
return
}
typ := value.Type()
nbFields := typ.NumField()
capacity := nbFields
length := 0
if capacity > encoder.containerMaxSize {
capacity = encoder.containerMaxSize
}
objArray := encoder.cgoRefs.AllocWafArray(obj, bindings.WafMapType, uint64(capacity))
for i := 0; i < nbFields; i++ {
if encoder.timer.Exhausted() {
return
}
if length == capacity {
encoder.addTruncation(ContainerTooLarge, nbFields)
break
}
fieldType := typ.Field(i)
fieldName, usable := getFieldNameFromType(fieldType)
if tag, ok := fieldType.Tag.Lookup(AppsecFieldTag); !usable || ok && tag == AppsecFieldTagValueIgnore {
// Either the struct field is ignored by json marshaling so can we,
// or the field was explicitly set with `ddwaf:ignore`
continue
}
objElem := &objArray[length]
// If the Map key is of unsupported type, skip it
encoder.encodeMapKeyFromString(fieldName, objElem)
if err := encoder.encode(value.Field(i), objElem, depth); err != nil {
// We still need to keep the map key, so we can't discard the full object, instead, we make the value a noop
encodeNative[uintptr](0, bindings.WafInvalidType, objElem)
}
length++
}
// Set the length to the final number of successfully encoded elements
obj.NbEntries = uint64(length)
}
// encodeMap takes a reflect.Value and a wafObject pointer and iterates on the map elements and returns
// a wafObject map of type wafMapType. The specificities are the following:
// - It will only take the first encoder.containerMaxSize elements of the map
// - Even if the element values are invalid or null we still keep them to report the map key
func (encoder *encoder) encodeMap(value reflect.Value, obj *bindings.WafObject, depth int) {
capacity := value.Len()
if capacity > encoder.containerMaxSize {
capacity = encoder.containerMaxSize
}
objArray := encoder.cgoRefs.AllocWafArray(obj, bindings.WafMapType, uint64(capacity))
length := 0
for iter := value.MapRange(); iter.Next(); {
if encoder.timer.Exhausted() {
return
}
if length == capacity {
encoder.addTruncation(ContainerTooLarge, value.Len())
break
}
objElem := &objArray[length]
if err := encoder.encodeMapKey(iter.Key(), objElem); err != nil {
continue
}
if err := encoder.encode(iter.Value(), objElem, depth); err != nil {
// We still need to keep the map key, so we can't discard the full object, instead, we make the value a noop
encodeNative[uintptr](0, bindings.WafInvalidType, objElem)
}
length++
}
// Fix the size because we skipped map entries
obj.NbEntries = uint64(length)
}
// encodeMapKey takes a reflect.Value and a wafObject and returns a wafObject ready to be considered a map entry. We use
// the function cgoRefPool.AllocWafMapKey to store the key in the wafObject. But first we need to grab the real
// underlying value by recursing through the pointer and interface values.
func (encoder *encoder) encodeMapKey(value reflect.Value, obj *bindings.WafObject) error {
value, kind := resolvePointer(value)
var keyStr string
switch {
case kind == reflect.Invalid:
return errors.ErrInvalidMapKey
case kind == reflect.String:
keyStr = value.String()
case value.Type() == reflect.TypeOf([]byte(nil)):
keyStr = string(value.Bytes())
default:
return errors.ErrInvalidMapKey
}
encoder.encodeMapKeyFromString(keyStr, obj)
return nil
}
// encodeMapKeyFromString takes a string and a wafObject and sets the map key attribute on the wafObject to the supplied
// string. The key may be truncated if it exceeds the maximum string size allowed by the encoder.
func (encoder *encoder) encodeMapKeyFromString(keyStr string, obj *bindings.WafObject) {
size := len(keyStr)
if size > encoder.stringMaxSize {
keyStr = keyStr[:encoder.stringMaxSize]
encoder.addTruncation(StringTooLong, size)
}
encoder.cgoRefs.AllocWafMapKey(obj, keyStr)
}
// encodeArray takes a reflect.Value and a wafObject pointer and iterates on the elements and returns
// a wafObject array of type wafArrayType. The specificities are the following:
// - It will only take the first encoder.containerMaxSize elements of the array
// - Elements producing an error at encoding or null values will be skipped
func (encoder *encoder) encodeArray(value reflect.Value, obj *bindings.WafObject, depth int) {
length := value.Len()
capacity := length
if capacity > encoder.containerMaxSize {
capacity = encoder.containerMaxSize
}
currIndex := 0
objArray := encoder.cgoRefs.AllocWafArray(obj, bindings.WafArrayType, uint64(capacity))
for i := 0; i < length; i++ {
if encoder.timer.Exhausted() {
return
}
if currIndex == capacity {
encoder.addTruncation(ContainerTooLarge, length)
break
}
objElem := &objArray[currIndex]
if err := encoder.encode(value.Index(i), objElem, depth); err != nil {
continue
}
// If the element is null or invalid it has no impact on the waf execution, therefore we can skip its
// encoding. In this specific case we just overwrite it at the next loop iteration.
if objElem == nil || objElem.IsUnusable() {
continue
}
currIndex++
}
// Fix the size because we skipped map entries
obj.NbEntries = uint64(currIndex)
}
func (encoder *encoder) addTruncation(reason TruncationReason, size int) {
if encoder.truncations == nil {
encoder.truncations = make(map[TruncationReason][]int, 3)
}
encoder.truncations[reason] = append(encoder.truncations[reason], size)
}
// mesureObjectDepth traverses the provided object recursively to try and obtain
// the real object depth, but limits itself to about 1ms of time budget, past
// which it'll stop and return whatever it has go to so far.
func (encoder *encoder) measureObjectDepth(obj reflect.Value, timeout time.Duration) {
ctx, cancelCtx := context.WithTimeout(context.Background(), timeout)
defer cancelCtx()
depth, _ := depthOf(ctx, obj)
encoder.truncations[ObjectTooDeep] = []int{depth}
}
// depthOf returns the depth of the provided object. This is 0 for scalar values,
// such as strings.
func depthOf(ctx context.Context, obj reflect.Value) (depth int, err error) {
if err = ctx.Err(); err != nil {
// Timed out, won't go any deeper
return 0, err
}
obj, kind := resolvePointer(obj)
var itemDepth int
switch kind {
case reflect.Array, reflect.Slice:
if obj.Type() == reflect.TypeOf([]byte(nil)) {
// We treat byte slices as strings
return 0, nil
}
for i := 0; i < obj.Len(); i++ {
itemDepth, err = depthOf(ctx, obj.Index(i))
depth = max(depth, itemDepth)
if err != nil {
break
}
}
return depth + 1, err
case reflect.Map:
for iter := obj.MapRange(); iter.Next(); {
itemDepth, err = depthOf(ctx, iter.Value())
depth = max(depth, itemDepth)
if err != nil {
break
}
}
return depth + 1, err
case reflect.Struct:
typ := obj.Type()
for i := 0; i < obj.NumField(); i++ {
fieldType := typ.Field(i)
_, usable := getFieldNameFromType(fieldType)
if !usable {
continue
}
itemDepth, err = depthOf(ctx, obj.Field(i))
depth = max(depth, itemDepth)
if err != nil {
break
}
}
return depth + 1, err
default:
return 0, nil
}
}
// resovlePointer attempts to resolve a pointer while limiting the pointer depth
// to be traversed, so that this is not susceptible to an infinite loop when
// provided a self-referencing pointer.
func resolvePointer(obj reflect.Value) (reflect.Value, reflect.Kind) {
kind := obj.Kind()
for limit := 8; limit > 0 && kind == reflect.Pointer || kind == reflect.Interface; limit-- {
if obj.IsNil() {
return obj, kind
}
obj = obj.Elem()
kind = obj.Kind()
}
return obj, kind
}