Module: Datadog::AppSec::WAF

Defined in:
lib/datadog/appsec/waf.rb,
lib/datadog/appsec/waf/version.rb

Defined Under Namespace

Modules: LibDDWAF, VERSION Classes: Context, Handle, Result

Class Method Summary collapse

Class Method Details

.logger=(logger) ⇒ Object



348
349
350
351
352
353
354
# File 'lib/datadog/appsec/waf.rb', line 348

def self.logger=(logger)
  @log_cb = proc do |level, func, file, line, message, len|
    logger.debug { { level: level, func: func, file: file, line: line, message: message.read_bytes(len) }.inspect }
  end

  Datadog::AppSec::WAF::LibDDWAF.ddwaf_set_log_cb(@log_cb, :ddwaf_log_trace)
end

.object_to_ruby(obj) ⇒ Object



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
# File 'lib/datadog/appsec/waf.rb', line 320

def self.object_to_ruby(obj)
  case obj[:type]
  when :ddwaf_obj_invalid
    nil
  when :ddwaf_obj_string
    obj[:valueUnion][:stringValue].read_bytes(obj[:nbEntries])
  when :ddwaf_obj_signed
    obj[:valueUnion][:intValue]
  when :ddwaf_obj_unsigned
    obj[:valueUnion][:uintValue]
  when :ddwaf_obj_array
    (0...obj[:nbEntries]).each.with_object([]) do |i, a|
      ptr = obj[:valueUnion][:array] + i * LibDDWAF::Object.size
      e = object_to_ruby(LibDDWAF::Object.new(ptr))
      a << e
    end
  when :ddwaf_obj_map
    (0...obj[:nbEntries]).each.with_object({}) do |i, h|
      ptr = obj[:valueUnion][:array] + i * Datadog::AppSec::WAF::LibDDWAF::Object.size
      o = Datadog::AppSec::WAF::LibDDWAF::Object.new(ptr)
      l = o[:parameterNameLength]
      k = o[:parameterName].read_bytes(l)
      v = object_to_ruby(LibDDWAF::Object.new(ptr))
      h[k] = v
    end
  end
end

.ruby_to_object(val) ⇒ Object



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
# File 'lib/datadog/appsec/waf.rb', line 243

def self.ruby_to_object(val)
  case val
  when Array
    obj = LibDDWAF::Object.new
    res = LibDDWAF.ddwaf_object_array(obj)
    if res.null?
      fail LibDDWAF::Error, "Could not convert into object: #{val}"
    end

    val.each do |e|
      res = LibDDWAF.ddwaf_object_array_add(obj, ruby_to_object(e))
      unless res
        fail LibDDWAF::Error, "Could not add to map object: #{k.inspect} => #{v.inspect}"
      end
    end

    obj
  when Hash
    obj = LibDDWAF::Object.new
    res = LibDDWAF.ddwaf_object_map(obj)
    if res.null?
      fail LibDDWAF::Error, "Could not convert into object: #{val}"
    end

    val.each do |k, v|
      res = LibDDWAF.ddwaf_object_map_addl(obj, k.to_s, k.to_s.size, ruby_to_object(v))
      unless res
        fail LibDDWAF::Error, "Could not add to map object: #{k.inspect} => #{v.inspect}"
      end
    end

    obj
  when String
    obj = LibDDWAF::Object.new
    res = LibDDWAF.ddwaf_object_stringl(obj, val, val.size)
    if res.null?
      fail LibDDWAF::Error, "Could not convert into object: #{val}"
    end

    obj
  when Symbol
    obj = LibDDWAF::Object.new
    res = LibDDWAF.ddwaf_object_stringl(obj, val.to_s, val.size)
    if res.null?
      fail LibDDWAF::Error, "Could not convert into object: #{val}"
    end

    obj
  when Integer
    obj = LibDDWAF::Object.new
    res = LibDDWAF.ddwaf_object_string(obj, val.to_s)
    if res.null?
      fail LibDDWAF::Error, "Could not convert into object: #{val}"
    end

    obj
  when Float
    obj = LibDDWAF::Object.new
    res = LibDDWAF.ddwaf_object_string(obj, val.to_s)
    if res.null?
      fail LibDDWAF::Error, "Could not convert into object: #{val}"
    end

    obj
  when TrueClass, FalseClass
    obj = LibDDWAF::Object.new
    res = LibDDWAF.ddwaf_object_string(obj, val.to_s)
    if res.null?
      fail LibDDWAF::Error, "Could not convert into object: #{val}"
    end

    obj
  else
    ruby_to_object(''.freeze)
  end
end

.versionObject



236
237
238
239
240
241
# File 'lib/datadog/appsec/waf.rb', line 236

def self.version
  version = LibDDWAF::Version.new
  LibDDWAF.ddwaf_get_version(version.pointer)

  [version[:major], version[:minor], version[:patch]]
end