Class: Datadog::AppSec::WAF::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/appsec/waf.rb

Constant Summary collapse

DEFAULT_TIMEOUT_US =
10_0000
ACTION_MAP_OUT =
{
  ddwaf_err_internal:         :err_internal,
  ddwaf_err_invalid_object:   :err_invalid_object,
  ddwaf_err_invalid_argument: :err_invalid_argument,
  ddwaf_good:                 :good,
  ddwaf_monitor:              :monitor,
  ddwaf_block:                :block,
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(handle) ⇒ Context

Returns a new instance of Context.



424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/datadog/appsec/waf.rb', line 424

def initialize(handle)
  handle_obj = handle.handle_obj
  free_func = Datadog::AppSec::WAF::LibDDWAF::ObjectNoFree

  @context_obj = Datadog::AppSec::WAF::LibDDWAF.ddwaf_context_init(handle_obj, free_func)
  if @context_obj.null?
    fail LibDDWAF::Error, 'Could not create context'
  end

  @input_objs = []

  ObjectSpace.define_finalizer(self, Context.finalizer(context_obj, @input_objs))
end

Instance Attribute Details

#context_objObject (readonly)

Returns the value of attribute context_obj.



422
423
424
# File 'lib/datadog/appsec/waf.rb', line 422

def context_obj
  @context_obj
end

Class Method Details

.finalizer(context_obj, input_objs) ⇒ Object



438
439
440
441
442
443
444
445
# File 'lib/datadog/appsec/waf.rb', line 438

def self.finalizer(context_obj, input_objs)
  proc do |object_id|
    input_objs.each do |input_obj|
      Datadog::AppSec::WAF::LibDDWAF.ddwaf_object_free(input_obj)
    end
    Datadog::AppSec::WAF::LibDDWAF.ddwaf_context_destroy(context_obj)
  end
end

Instance Method Details

#run(input, timeout = DEFAULT_TIMEOUT_US) ⇒ Object



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

def run(input, timeout = DEFAULT_TIMEOUT_US)
  input_obj = Datadog::AppSec::WAF.ruby_to_object(input)
  if input_obj.null?
    fail LibDDWAF::Error, "Could not convert input: #{input.inspect}"
  end

  result_obj = Datadog::AppSec::WAF::LibDDWAF::Result.new
  if result_obj.null?
    fail LibDDWAF::Error, "Could not create result object"
  end

  # retain C objects in memory for subsequent calls to run
  @input_objs << input_obj

  code = Datadog::AppSec::WAF::LibDDWAF.ddwaf_run(@context_obj, input_obj, result_obj, timeout)

  result = Result.new(
    ACTION_MAP_OUT[code],
    (JSON.parse(result_obj[:data]) if result_obj[:data] != nil),
    result_obj[:total_runtime],
    result_obj[:timeout],
  )

  [ACTION_MAP_OUT[code], result]
ensure
  Datadog::AppSec::WAF::LibDDWAF.ddwaf_result_free(result_obj) if result_obj
end