libddwaf 1.30.0.0.2-aarch64-linux → 2.0.1.0.0-aarch64-linux

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -23,14 +23,23 @@ module Datadog
23
23
  # Builds a WAF context.
24
24
  #
25
25
  # @raise [LibDDWAFError] if libddwaf could not create the context.
26
- # @return [Handle] the WAF handle
26
+ # @return [Context] the WAF context
27
27
  def build_context
28
28
  ensure_pointer_presence!
29
29
 
30
- context_obj = LibDDWAF.ddwaf_context_init(@handle_ptr)
30
+ output_allocator = LibDDWAF.ddwaf_get_default_allocator
31
+ raise LibDDWAFError, "Could not obtain the native allocator" if output_allocator.null?
32
+
33
+ context_obj = LibDDWAF.ddwaf_context_init(@handle_ptr, output_allocator)
31
34
  raise LibDDWAFError, "Could not create context" if context_obj.null?
32
35
 
33
- Context.new(context_obj)
36
+ begin
37
+ context = Context.new(context_obj)
38
+ initialized = true
39
+ context
40
+ ensure
41
+ LibDDWAF.ddwaf_context_destroy(context_obj) unless initialized
42
+ end
34
43
  end
35
44
 
36
45
  # Returns the list of known addresses in the WAF handle.
@@ -7,21 +7,18 @@ module Datadog
7
7
  #
8
8
  # It handles merging of potentially overlapping configurations.
9
9
  class HandleBuilder
10
- def initialize(limits: {}, obfuscator: {})
11
- handle_config_obj = LibDDWAF::HandleBuilderConfig.new
12
- if handle_config_obj.null?
13
- raise LibDDWAFError, "Could not create config struct"
14
- end
15
-
16
- handle_config_obj[:limits][:max_container_size] = limits[:max_container_size] || LibDDWAF::DEFAULT_MAX_CONTAINER_SIZE
17
- handle_config_obj[:limits][:max_container_depth] = limits[:max_container_depth] || LibDDWAF::DEFAULT_MAX_CONTAINER_DEPTH
18
- handle_config_obj[:limits][:max_string_length] = limits[:max_string_length] || LibDDWAF::DEFAULT_MAX_STRING_LENGTH
10
+ OBFUSCATOR_CONFIG_PATH = "libddwaf-rb/obfuscator"
19
11
 
20
- handle_config_obj[:obfuscator][:key_regex] = FFI::MemoryPointer.from_string(obfuscator[:key_regex]) if obfuscator[:key_regex]
21
- handle_config_obj[:obfuscator][:value_regex] = FFI::MemoryPointer.from_string(obfuscator[:value_regex]) if obfuscator[:value_regex]
22
- handle_config_obj[:free_fn] = LibDDWAF::ObjectNoFree
12
+ def initialize(obfuscator: {})
13
+ @builder_ptr = LibDDWAF.ddwaf_builder_init
14
+ raise LibDDWAFError, "Could not create builder" if @builder_ptr.null?
23
15
 
24
- @builder_ptr = LibDDWAF.ddwaf_builder_init(handle_config_obj)
16
+ begin
17
+ configure_obfuscator(obfuscator)
18
+ initialized = true
19
+ ensure
20
+ LibDDWAF.ddwaf_builder_destroy(@builder_ptr) unless initialized
21
+ end
25
22
  end
26
23
 
27
24
  # Destroys the WAF builder and sets the pointer to nil.
@@ -44,7 +41,13 @@ module Datadog
44
41
  handle_obj = LibDDWAF.ddwaf_builder_build_instance(@builder_ptr)
45
42
  raise LibDDWAFError, "Could not create handle" if handle_obj.null?
46
43
 
47
- Handle.new(handle_obj)
44
+ begin
45
+ handle = Handle.new(handle_obj)
46
+ initialized = true
47
+ handle
48
+ ensure
49
+ LibDDWAF.ddwaf_destroy(handle_obj) unless initialized
50
+ end
48
51
  end
49
52
 
50
53
  # :section: Configuration management methods
@@ -52,21 +55,28 @@ module Datadog
52
55
 
53
56
  # Adds or updates a configuration in the WAF handle builder for the given path.
54
57
  #
55
- # @return [Hash] diagnostics object
58
+ # @return [ConfigurationResult] success flag and diagnostics object
56
59
  # NOTE: default config that was read from file at application startup
57
60
  # has to be removed before adding configurations obtained through Remote Configuration.
58
61
  def add_or_update_config(config, path:)
59
62
  ensure_pointer_presence!
63
+ raise ArgumentError, "Configuration path is reserved for the obfuscator" if path == OBFUSCATOR_CONFIG_PATH
64
+
65
+ path_length = path.bytesize
66
+ raise ArgumentError, "Configuration path exceeds the native length limit" if path_length > 0xffff_ffff
67
+
68
+ allocator = LibDDWAF.ddwaf_get_default_allocator
69
+ raise LibDDWAFError, "Could not obtain the native allocator" if allocator.null?
60
70
 
61
- config_obj = Converter.ruby_to_object(config, coerce: false)
71
+ config_obj = Converter.ruby_to_object(config, truncate: false)
62
72
  diagnostics_obj = LibDDWAF::Object.new
63
73
 
64
- LibDDWAF.ddwaf_builder_add_or_update_config(@builder_ptr, path, path.length, config_obj, diagnostics_obj)
74
+ success = LibDDWAF.ddwaf_builder_add_or_update_config(@builder_ptr, path, path_length, config_obj, diagnostics_obj)
65
75
 
66
- Converter.object_to_ruby(diagnostics_obj)
76
+ ConfigurationResult.new(success: success, diagnostics: Converter.object_to_ruby(diagnostics_obj))
67
77
  ensure
68
- LibDDWAF.ddwaf_object_free(config_obj) if config_obj
69
- LibDDWAF.ddwaf_object_free(diagnostics_obj) if diagnostics_obj
78
+ LibDDWAF.ddwaf_object_destroy(config_obj, allocator) if config_obj
79
+ LibDDWAF.ddwaf_object_destroy(diagnostics_obj, allocator) if diagnostics_obj
70
80
  end
71
81
 
72
82
  # Removes a configuration from the WAF handle builder for the given path.
@@ -74,12 +84,34 @@ module Datadog
74
84
  # @return [Boolean] true if the configuration was removed, false otherwise
75
85
  def remove_config_at_path(path)
76
86
  ensure_pointer_presence!
87
+ raise ArgumentError, "Configuration path is reserved for the obfuscator" if path == OBFUSCATOR_CONFIG_PATH
77
88
 
78
- LibDDWAF.ddwaf_builder_remove_config(@builder_ptr, path, path.length)
89
+ path_length = path.bytesize
90
+ raise ArgumentError, "Configuration path exceeds the native length limit" if path_length > 0xffff_ffff
91
+
92
+ LibDDWAF.ddwaf_builder_remove_config(@builder_ptr, path, path_length)
79
93
  end
80
94
 
81
95
  private
82
96
 
97
+ def configure_obfuscator(obfuscator)
98
+ settings = {
99
+ key_regex: obfuscator[:key_regex],
100
+ value_regex: obfuscator[:value_regex]
101
+ }.reject { |_, pattern| pattern.nil? }
102
+
103
+ allocator = LibDDWAF.ddwaf_get_default_allocator
104
+ raise LibDDWAFError, "Could not obtain the native allocator" if allocator.null?
105
+
106
+ config_obj = Converter.ruby_to_object({obfuscator: settings}, truncate: false)
107
+ success = LibDDWAF.ddwaf_builder_add_or_update_config(
108
+ @builder_ptr, OBFUSCATOR_CONFIG_PATH, OBFUSCATOR_CONFIG_PATH.bytesize, config_obj, nil
109
+ )
110
+ raise LibDDWAFError, "Could not configure obfuscator" unless success
111
+ ensure
112
+ LibDDWAF.ddwaf_object_destroy(config_obj, allocator) if config_obj
113
+ end
114
+
83
115
  def ensure_pointer_presence!
84
116
  return if @builder_ptr
85
117
 
@@ -9,14 +9,6 @@ module Datadog
9
9
  # FFI-binding for C-libddwaf
10
10
  # See https://github.com/DataDog/libddwaf
11
11
  module LibDDWAF
12
- DEFAULT_MAX_CONTAINER_SIZE = 256
13
- DEFAULT_MAX_CONTAINER_DEPTH = 20
14
- DEFAULT_MAX_STRING_LENGTH = 16_384 # in bytes, UTF-8 worst case being 4x size in terms of code point
15
-
16
- DDWAF_MAX_CONTAINER_SIZE = 256
17
- DDWAF_MAX_CONTAINER_DEPTH = 20
18
- DDWAF_MAX_STRING_LENGTH = 4096
19
-
20
12
  DDWAF_RUN_TIMEOUT = 5000
21
13
 
22
14
  extend ::FFI::Library
@@ -82,15 +74,17 @@ module Datadog
82
74
 
83
75
  # ddwaf::object data structure
84
76
 
85
- DDWAF_OBJ_TYPE = enum :ddwaf_obj_invalid, 0,
86
- :ddwaf_obj_signed, 1 << 0,
87
- :ddwaf_obj_unsigned, 1 << 1,
88
- :ddwaf_obj_string, 1 << 2,
89
- :ddwaf_obj_array, 1 << 3,
90
- :ddwaf_obj_map, 1 << 4,
91
- :ddwaf_obj_bool, 1 << 5,
92
- :ddwaf_obj_float, 1 << 6,
93
- :ddwaf_obj_null, 1 << 7
77
+ DDWAF_OBJ_TYPE = enum :ddwaf_obj_invalid, 0x00,
78
+ :ddwaf_obj_null, 0x01,
79
+ :ddwaf_obj_bool, 0x02,
80
+ :ddwaf_obj_signed, 0x04,
81
+ :ddwaf_obj_unsigned, 0x06,
82
+ :ddwaf_obj_float, 0x08,
83
+ :ddwaf_obj_string, 0x10,
84
+ :ddwaf_obj_literal_string, 0x12,
85
+ :ddwaf_obj_small_string, 0x14,
86
+ :ddwaf_obj_array, 0x20,
87
+ :ddwaf_obj_map, 0x40
94
88
 
95
89
  typedef DDWAF_OBJ_TYPE, :ddwaf_obj_type
96
90
 
@@ -118,24 +112,53 @@ module Datadog
118
112
 
119
113
  typedef SizeTPtr.by_ref, :sizeptr
120
114
 
121
- # Ruby representation of C union
115
+ class ObjectBool < ::FFI::Struct
116
+ layout :type, :uint8, :val, :bool
117
+ end
118
+
119
+ class ObjectSigned < ::FFI::Struct
120
+ layout :type, :uint8, :val, :int64
121
+ end
122
+
123
+ class ObjectUnsigned < ::FFI::Struct
124
+ layout :type, :uint8, :val, :uint64
125
+ end
126
+
127
+ class ObjectFloat < ::FFI::Struct
128
+ layout :type, :uint8, :val, :double
129
+ end
130
+
131
+ class ObjectString < ::FFI::Struct
132
+ layout :type, :uint8, :size, :uint32, :ptr, :pointer
133
+ end
134
+
135
+ class ObjectSmallString < ::FFI::Struct
136
+ layout :type, :uint8, :size, :uint8, :data, [:char, 14]
137
+ end
138
+
139
+ class ObjectArray < ::FFI::Struct
140
+ layout :type, :uint8, :size, :uint16, :capacity, :uint16, :ptr, :pointer
141
+ end
142
+
143
+ class ObjectMap < ::FFI::Struct
144
+ layout :type, :uint8, :size, :uint16, :capacity, :uint16, :ptr, :pointer
145
+ end
146
+
122
147
  class ObjectValueUnion < ::FFI::Union
123
- layout :stringValue, :charptr,
124
- :uintValue, :uint64,
125
- :intValue, :int64,
126
- :array, :pointer,
127
- :boolean, :bool,
128
- :f64, :double
148
+ layout :b8, ObjectBool,
149
+ :i64, ObjectSigned,
150
+ :u64, ObjectUnsigned,
151
+ :f64, ObjectFloat,
152
+ :str, ObjectString,
153
+ :sstr, ObjectSmallString,
154
+ :array, ObjectArray,
155
+ :map, ObjectMap
129
156
  end
130
157
 
131
- # Ruby representation of ddwaf_object
132
- # See https://github.com/DataDog/libddwaf/blob/10e3a1dfc7bc9bb8ab11a09a9f8b6b339eaf3271/include/ddwaf.h#L94C1-L115C3
133
- class Object < ::FFI::Struct
134
- layout :parameterName, :charptr,
135
- :parameterNameLength, :uint64,
136
- :valueUnion, ObjectValueUnion,
137
- :nbEntries, :uint64,
138
- :type, :ddwaf_obj_type
158
+ # See: https://github.com/DataDog/libddwaf/blob/2.0.0/include/ddwaf.h
159
+ class Object < ::FFI::Union
160
+ layout :type, :uint8,
161
+ :via, ObjectValueUnion
139
162
 
140
163
  def truncated?
141
164
  @truncated == true
@@ -146,93 +169,76 @@ module Datadog
146
169
  end
147
170
  end
148
171
 
172
+ class ObjectKV < ::FFI::Struct
173
+ layout :key, Object,
174
+ :val, Object
175
+ end
176
+
149
177
  typedef Object.by_ref, :ddwaf_object
150
178
 
179
+ # allocators
180
+
181
+ typedef :pointer, :ddwaf_allocator
182
+
183
+ attach_function :ddwaf_get_default_allocator, [], :ddwaf_allocator
184
+ attach_function :ddwaf_allocator_alloc, [:ddwaf_allocator, :size_t, :size_t], :pointer
185
+ attach_function :ddwaf_allocator_free, [:ddwaf_allocator, :pointer, :size_t, :size_t], :void
186
+
151
187
  ## setters
152
188
 
153
- attach_function :ddwaf_object_invalid, [:ddwaf_object], :ddwaf_object
154
- attach_function :ddwaf_object_string, [:ddwaf_object, :string], :ddwaf_object
155
- attach_function :ddwaf_object_stringl, [:ddwaf_object, :charptr, :size_t], :ddwaf_object
156
- attach_function :ddwaf_object_stringl_nc, [:ddwaf_object, :charptr, :size_t], :ddwaf_object
157
- attach_function :ddwaf_object_string_from_unsigned, [:ddwaf_object, :uint64], :ddwaf_object
158
- attach_function :ddwaf_object_string_from_signed, [:ddwaf_object, :int64], :ddwaf_object
159
- attach_function :ddwaf_object_unsigned, [:ddwaf_object, :uint64], :ddwaf_object
160
- attach_function :ddwaf_object_signed, [:ddwaf_object, :int64], :ddwaf_object
161
- attach_function :ddwaf_object_bool, [:ddwaf_object, :bool], :ddwaf_object
162
- attach_function :ddwaf_object_null, [:ddwaf_object], :ddwaf_object
163
- attach_function :ddwaf_object_float, [:ddwaf_object, :double], :ddwaf_object
164
-
165
- attach_function :ddwaf_object_array, [:ddwaf_object], :ddwaf_object
166
- attach_function :ddwaf_object_array_add, [:ddwaf_object, :ddwaf_object], :bool
167
-
168
- attach_function :ddwaf_object_map, [:ddwaf_object], :ddwaf_object
169
- attach_function :ddwaf_object_map_add, [:ddwaf_object, :string, :pointer], :bool
170
- attach_function :ddwaf_object_map_addl, [:ddwaf_object, :charptr, :size_t, :pointer], :bool
171
- attach_function :ddwaf_object_map_addl_nc, [:ddwaf_object, :charptr, :size_t, :pointer], :bool
189
+ attach_function :ddwaf_object_set_invalid, [:ddwaf_object], :ddwaf_object
190
+ attach_function :ddwaf_object_set_null, [:ddwaf_object], :ddwaf_object
191
+ attach_function :ddwaf_object_set_bool, [:ddwaf_object, :bool], :ddwaf_object
192
+ attach_function :ddwaf_object_set_signed, [:ddwaf_object, :int64], :ddwaf_object
193
+ attach_function :ddwaf_object_set_unsigned, [:ddwaf_object, :uint64], :ddwaf_object
194
+ attach_function :ddwaf_object_set_float, [:ddwaf_object, :double], :ddwaf_object
195
+
196
+ attach_function :ddwaf_object_from_json, [:ddwaf_object, :charptr, :uint32, :ddwaf_allocator], :bool
197
+
198
+ attach_function :ddwaf_object_set_string, [:ddwaf_object, :charptr, :uint32, :ddwaf_allocator], :ddwaf_object
199
+ attach_function :ddwaf_object_set_string_literal, [:ddwaf_object, :charptr, :uint32], :ddwaf_object
200
+ attach_function :ddwaf_object_set_string_nocopy, [:ddwaf_object, :charptr, :uint32], :ddwaf_object
201
+
202
+ attach_function :ddwaf_object_set_array, [:ddwaf_object, :uint16, :ddwaf_allocator], :ddwaf_object
203
+ attach_function :ddwaf_object_set_map, [:ddwaf_object, :uint16, :ddwaf_allocator], :ddwaf_object
204
+
205
+ attach_function :ddwaf_object_insert, [:ddwaf_object, :ddwaf_allocator], :ddwaf_object
206
+ attach_function :ddwaf_object_insert_key, [:ddwaf_object, :charptr, :uint32, :ddwaf_allocator], :ddwaf_object
207
+ attach_function :ddwaf_object_insert_key_nocopy, [:ddwaf_object, :charptr, :uint32, :ddwaf_allocator], :ddwaf_object
172
208
 
173
209
  ## getters
174
210
 
175
- attach_function :ddwaf_object_type, [:ddwaf_object], DDWAF_OBJ_TYPE
176
- attach_function :ddwaf_object_size, [:ddwaf_object], :uint64
177
- attach_function :ddwaf_object_length, [:ddwaf_object], :size_t
178
- attach_function :ddwaf_object_get_key, [:ddwaf_object, :sizeptr], :charptr
211
+ attach_function :ddwaf_object_get_type, [:ddwaf_object], DDWAF_OBJ_TYPE
212
+ attach_function :ddwaf_object_get_size, [:ddwaf_object], :size_t
213
+ attach_function :ddwaf_object_get_length, [:ddwaf_object], :size_t
179
214
  attach_function :ddwaf_object_get_string, [:ddwaf_object, :sizeptr], :charptr
180
215
  attach_function :ddwaf_object_get_unsigned, [:ddwaf_object], :uint64
181
216
  attach_function :ddwaf_object_get_signed, [:ddwaf_object], :int64
182
- attach_function :ddwaf_object_get_index, [:ddwaf_object, :size_t], :ddwaf_object
183
217
  attach_function :ddwaf_object_get_bool, [:ddwaf_object], :bool
184
218
  attach_function :ddwaf_object_get_float, [:ddwaf_object], :double
185
219
 
186
- ## freeers
220
+ attach_function :ddwaf_object_at_key, [:ddwaf_object, :size_t], :ddwaf_object
221
+ attach_function :ddwaf_object_at_value, [:ddwaf_object, :size_t], :ddwaf_object
187
222
 
188
- ObjectFree = attach_function :ddwaf_object_free, [:ddwaf_object], :void
189
- ObjectNoFree = ::FFI::Pointer::NULL
223
+ ## destruction
224
+
225
+ attach_function :ddwaf_object_destroy, [:ddwaf_object, :ddwaf_allocator], :void
190
226
 
191
227
  # handle builder
192
228
 
193
229
  typedef :pointer, :ddwaf_builder
194
230
  typedef :pointer, :ddwaf_handle
195
- typedef :pointer, :ddwaf_diagnostics
196
-
197
- callback :ddwaf_object_free_fn, [:ddwaf_object], :void
198
-
199
- # Ruby representation of ddwaf_config
200
- # https://github.com/DataDog/libddwaf/blob/10e3a1dfc7bc9bb8ab11a09a9f8b6b339eaf3271/include/ddwaf.h#L129-L152
201
- class HandleBuilderConfig < ::FFI::Struct
202
- # Ruby representation of ddwaf_config_limits
203
- # https://github.com/DataDog/libddwaf/blob/10e3a1dfc7bc9bb8ab11a09a9f8b6b339eaf3271/include/ddwaf.h#L131-L138
204
- class Limits < ::FFI::Struct
205
- layout :max_container_size, :uint32,
206
- :max_container_depth, :uint32,
207
- :max_string_length, :uint32
208
- end
209
231
 
210
- # Ruby representation of ddwaf_config_obfuscator
211
- # https://github.com/DataDog/libddwaf/blob/10e3a1dfc7bc9bb8ab11a09a9f8b6b339eaf3271/include/ddwaf.h#L141-L146
212
- class Obfuscator < ::FFI::Struct
213
- layout :key_regex, :pointer, # should be :charptr
214
- :value_regex, :pointer # should be :charptr
215
- end
216
-
217
- layout :limits, Limits,
218
- :obfuscator, Obfuscator,
219
- :free_fn, :pointer # :ddwaf_object_free_fn
220
- end
221
-
222
- typedef HandleBuilderConfig.by_ref, :ddwaf_config
223
-
224
- attach_function :ddwaf_builder_init, [:ddwaf_config], :ddwaf_builder
232
+ attach_function :ddwaf_builder_init, [], :ddwaf_builder
225
233
  attach_function :ddwaf_builder_destroy, [:ddwaf_builder], :void
226
234
 
227
- attach_function :ddwaf_builder_add_or_update_config, [:ddwaf_builder, :string, :size_t, :ddwaf_object, :ddwaf_diagnostics], :bool
228
- attach_function :ddwaf_builder_remove_config, [:ddwaf_builder, :string, :size_t], :bool
235
+ attach_function :ddwaf_builder_add_or_update_config, [:ddwaf_builder, :string, :uint32, :ddwaf_object, :ddwaf_object], :bool
236
+ attach_function :ddwaf_builder_remove_config, [:ddwaf_builder, :string, :uint32], :bool
229
237
 
230
238
  attach_function :ddwaf_builder_build_instance, [:ddwaf_builder], :ddwaf_handle
231
239
 
232
240
  # handle
233
241
 
234
- callback :ddwaf_object_free_fn, [:ddwaf_object], :void
235
-
236
242
  attach_function :ddwaf_destroy, [:ddwaf_handle], :void
237
243
 
238
244
  attach_function :ddwaf_known_addresses, [:ddwaf_handle, UInt32Ptr], :charptrptr
@@ -246,16 +252,23 @@ module Datadog
246
252
  :ddwaf_match, 1
247
253
  typedef DDWAF_RET_CODE, :ddwaf_ret_code
248
254
 
249
- # running
255
+ # evaluation
250
256
 
251
257
  typedef :pointer, :ddwaf_context
258
+ typedef :pointer, :ddwaf_subcontext
259
+ typedef :uint64, :timeout_us
252
260
 
253
- attach_function :ddwaf_context_init, [:ddwaf_handle], :ddwaf_context
261
+ attach_function :ddwaf_context_init, [:ddwaf_handle, :ddwaf_allocator], :ddwaf_context
254
262
  attach_function :ddwaf_context_destroy, [:ddwaf_context], :void
255
263
 
256
- typedef :uint64, :timeout_us
264
+ attach_function :ddwaf_context_eval,
265
+ [:ddwaf_context, :ddwaf_object, :ddwaf_allocator, :ddwaf_object, :timeout_us], :ddwaf_ret_code, blocking: true
266
+
267
+ attach_function :ddwaf_subcontext_init, [:ddwaf_context], :ddwaf_subcontext
268
+ attach_function :ddwaf_subcontext_destroy, [:ddwaf_subcontext], :void
257
269
 
258
- attach_function :ddwaf_run, [:ddwaf_context, :ddwaf_object, :ddwaf_object, :ddwaf_object, :timeout_us], :ddwaf_ret_code, blocking: true
270
+ attach_function :ddwaf_subcontext_eval,
271
+ [:ddwaf_subcontext, :ddwaf_object, :ddwaf_allocator, :ddwaf_object, :timeout_us], :ddwaf_ret_code, blocking: true
259
272
 
260
273
  # logging
261
274
 
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module AppSec
5
+ module WAF
6
+ class Subcontext
7
+ def initialize(subcontext_ptr, context:)
8
+ @subcontext_ptr = subcontext_ptr
9
+ @context = context
10
+ end
11
+
12
+ def finalize!
13
+ Thread.handle_interrupt(::Object => :never) do
14
+ pointer = @subcontext_ptr
15
+ return unless pointer
16
+
17
+ @subcontext_ptr = nil
18
+ LibDDWAF.ddwaf_subcontext_destroy(pointer)
19
+ end
20
+ end
21
+
22
+ def eval(data, timeout = LibDDWAF::DDWAF_RUN_TIMEOUT)
23
+ ensure_pointer_presence!
24
+
25
+ Evaluation.call(data) do |input_obj, allocator, result_obj|
26
+ LibDDWAF.ddwaf_subcontext_eval(
27
+ @subcontext_ptr, #: ::FFI::Pointer
28
+ input_obj, allocator, result_obj, timeout
29
+ )
30
+ end
31
+ end
32
+
33
+ private
34
+
35
+ def ensure_pointer_presence!
36
+ return if @subcontext_ptr
37
+
38
+ raise InstanceFinalizedError, "Cannot use WAF subcontext after it has been finalized"
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -2,10 +2,10 @@ module Datadog
2
2
  module AppSec
3
3
  module WAF
4
4
  module VERSION
5
- BASE_STRING = "1.30.0"
5
+ BASE_STRING = "2.0.1"
6
6
  # NOTE: Every change to the `BASE_STRING` should be accompanied
7
7
  # by a reset of the patch version in the `STRING` below.
8
- STRING = "#{BASE_STRING}.0.2"
8
+ STRING = "#{BASE_STRING}.0.0"
9
9
  MINIMUM_RUBY_VERSION = "2.5"
10
10
  end
11
11
  end
@@ -2,17 +2,24 @@
2
2
 
3
3
  require "datadog/appsec/waf/lib_ddwaf"
4
4
 
5
+ require "datadog/appsec/waf/configuration_result"
5
6
  require "datadog/appsec/waf/handle_builder"
6
7
  require "datadog/appsec/waf/handle"
7
8
  require "datadog/appsec/waf/converter"
8
9
  require "datadog/appsec/waf/errors"
9
10
  require "datadog/appsec/waf/result"
11
+ require "datadog/appsec/waf/evaluation"
10
12
  require "datadog/appsec/waf/context"
13
+ require "datadog/appsec/waf/subcontext"
11
14
  require "datadog/appsec/waf/version"
12
15
 
13
16
  module Datadog
14
17
  module AppSec
15
18
  module WAF
19
+ # A marker class representing string containing JSON.
20
+ # If given as an address value will trigger parsing into native WAF objects
21
+ JSONString = Class.new(String)
22
+
16
23
  module_function
17
24
 
18
25
  def version
@@ -20,9 +27,10 @@ module Datadog
20
27
  end
21
28
 
22
29
  def log_callback(level, func, file, line, message, len)
23
- return if WAF.logger.nil?
30
+ logger = WAF.logger
31
+ return if logger.nil?
24
32
 
25
- WAF.logger.debug do
33
+ logger.debug do
26
34
  {
27
35
  level: level,
28
36
  func: func,
@@ -31,6 +39,9 @@ module Datadog
31
39
  message: message.read_bytes(len)
32
40
  }.inspect
33
41
  end
42
+ rescue Exception # standard:disable Lint/RescueException
43
+ # Exceptions must not escape a native callback after input ownership transfers.
44
+ nil
34
45
  end
35
46
 
36
47
  def logger
@@ -0,0 +1,17 @@
1
+ module Datadog
2
+ module AppSec
3
+ module WAF
4
+ class ConfigurationResult
5
+ @success: bool
6
+
7
+ @diagnostics: WAF::opaque
8
+
9
+ attr_reader diagnostics: WAF::opaque
10
+
11
+ def initialize: (success: bool, diagnostics: WAF::opaque) -> void
12
+
13
+ def success?: () -> bool
14
+ end
15
+ end
16
+ end
17
+ end
@@ -2,38 +2,19 @@ module Datadog
2
2
  module AppSec
3
3
  module WAF
4
4
  class Context
5
- @context_ptr: ::FFI::Pointer
6
-
7
- @retained: Array[untyped]
8
-
9
- EMPTY_RESULT: {
10
- "events" => WAF::events,
11
- "actions" => WAF::actions,
12
- "attributes" => WAF::attributes,
13
- "duration" => ::Integer,
14
- "timeout" => bool,
15
- "keep" => bool
16
- }
17
-
18
- SUCCESS_RESULT_CODES: ::Array[::Symbol]
19
-
20
- RESULT_CODE_TO_STATUS: ::Hash[::Symbol, ::Symbol]
5
+ @context_ptr: ::FFI::Pointer?
21
6
 
22
7
  def initialize: (::FFI::Pointer context_ptr) -> void
23
8
 
24
9
  def finalize!: () -> void
25
10
 
26
- def run: (WAF::input persistent_data, WAF::input ephemeral_data, ?::Integer timeout) -> Result
11
+ def build_subcontext: [T] () { (Subcontext) -> T } -> T
12
+
13
+ def eval: (::Hash[WAF::input, WAF::input] data, ?::Integer timeout) -> Result
27
14
 
28
15
  private
29
16
 
30
17
  def ensure_pointer_presence!: () -> void
31
-
32
- def retained: () -> Array[untyped]
33
-
34
- def retain: (top object) -> void
35
-
36
- def release: (top object) -> void
37
18
  end
38
19
  end
39
20
  end
@@ -2,16 +2,54 @@ module Datadog
2
2
  module AppSec
3
3
  module WAF
4
4
  module Converter
5
+ MAX_CONTAINER_SIZE: Integer
6
+
7
+ MAX_CONTAINER_DEPTH: Integer
8
+
9
+ MAX_STRING_LENGTH: Integer
10
+
11
+ INT64_MIN: Integer
12
+
13
+ UINT16_MAX: Integer
14
+
15
+ UINT32_MAX: Integer
16
+
17
+ UINT64_MAX: Integer
18
+
5
19
  def self?.ruby_to_object: (
6
20
  top val,
7
- ?max_container_size: ::Integer?,
8
- ?max_container_depth: ::Integer?,
9
- ?max_string_length: ::Integer?,
10
- ?top_obj: LibDDWAF::Object?,
11
- ?coerce: bool?
21
+ ?object: LibDDWAF::Object?,
22
+ ?allocator: ::FFI::Pointer,
23
+ ?top_obj: LibDDWAF::Object,
24
+ ?current_depth: ::Integer,
25
+ ?truncate: bool
12
26
  ) -> LibDDWAF::Object
13
27
 
28
+ def self?.array_to_object: (
29
+ LibDDWAF::Object object,
30
+ ::Array[untyped] array,
31
+ ::FFI::Pointer allocator,
32
+ top_obj: LibDDWAF::Object,
33
+ current_depth: ::Integer,
34
+ ?truncate: bool
35
+ ) -> LibDDWAF::Object
36
+
37
+ def self?.hash_to_object: (
38
+ LibDDWAF::Object object,
39
+ ::Hash[untyped, untyped] hash,
40
+ ::FFI::Pointer allocator,
41
+ top_obj: LibDDWAF::Object,
42
+ current_depth: ::Integer,
43
+ ?truncate: bool
44
+ ) -> LibDDWAF::Object
45
+
46
+ def self?.json_to_object: (LibDDWAF::Object object, JSONString json, ::FFI::Pointer allocator) -> LibDDWAF::Object
47
+
48
+ def self?.string_to_object: (LibDDWAF::Object object, ::String string, ::FFI::Pointer allocator, top_obj: LibDDWAF::Object, ?truncate: bool) -> LibDDWAF::Object
49
+
14
50
  def self?.object_to_ruby: (LibDDWAF::Object obj) -> WAF::opaque
51
+
52
+ def self?.object_to_string: (LibDDWAF::Object obj) -> ::String
15
53
  end
16
54
  end
17
55
  end
@@ -0,0 +1,15 @@
1
+ module Datadog
2
+ module AppSec
3
+ module WAF
4
+ module Evaluation
5
+ SUCCESS_RESULT_CODES: ::Array[::Symbol]
6
+
7
+ RESULT_CODE_TO_STATUS: ::Hash[::Symbol, ::Symbol]
8
+
9
+ def self?.call: (::Hash[WAF::input, WAF::input] data) {
10
+ (LibDDWAF::Object input_obj, ::FFI::Pointer allocator, LibDDWAF::Object result_obj) -> ::Symbol
11
+ } -> Result
12
+ end
13
+ end
14
+ end
15
+ end