From fcca011b85d0b4d179fab2f3c95358c929f7c7e7 Mon Sep 17 00:00:00 2001 From: Gustavo Caso Date: Fri, 6 Oct 2023 15:55:05 +0200 Subject: [PATCH] handle boolean values --- lib/datadog/appsec/processor.rb | 23 +++++++++-------------- sig/datadog/appsec/processor.rbs | 4 +--- spec/datadog/appsec/processor_spec.rb | 12 ++++++++++++ 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/lib/datadog/appsec/processor.rb b/lib/datadog/appsec/processor.rb index 0bc6a3084d5..2d0c42d1f90 100644 --- a/lib/datadog/appsec/processor.rb +++ b/lib/datadog/appsec/processor.rb @@ -22,9 +22,16 @@ def run(input, timeout = WAF::LibDDWAF::DDWAF_RUN_TIMEOUT) start_ns = Core::Utils::Time.get_time(:nanosecond) - cleaned_input = remove_empty_entries(input) + input.reject! do |_, v| + case v + when TrueClass, FalseClass + false + else + v.nil? ? true : v.empty? + end + end - _code, res = @context.run(cleaned_input, timeout) + _code, res = @context.run(input, timeout) stop_ns = Core::Utils::Time.get_time(:nanosecond) @@ -62,18 +69,6 @@ def extract_schema? Datadog.configuration.appsec.api_security.enabled && Datadog.configuration.appsec.api_security.sample_rate.sample? end - - def remove_empty_entries(entries) - entries.each_with_object({}) do |(k, v), acc| - acc[k] = v unless empty_value?(v) - end - end - - def empty_value?(v) - return true unless v - - v.empty? - end end attr_reader :diagnostics, :addresses diff --git a/sig/datadog/appsec/processor.rbs b/sig/datadog/appsec/processor.rbs index 2c78c48531c..0f2be11a9b3 100644 --- a/sig/datadog/appsec/processor.rbs +++ b/sig/datadog/appsec/processor.rbs @@ -15,14 +15,12 @@ module Datadog @run_mutex: ::Thread::Mutex def initialize: (Processor processor) -> void - def run: (data input, ?::Integer timeout) -> WAF::Result + def run: (Hash[untyped, untyped] input, ?::Integer timeout) -> WAF::Result def extract_schema: () -> WAF::Result? def finalize: () -> void private def extract_schema?: () -> bool - def remove_empty_entries: (untyped entries) -> data - def empty_value?: (untyped v) -> bool end def self.active_context: () -> Context diff --git a/spec/datadog/appsec/processor_spec.rb b/spec/datadog/appsec/processor_spec.rb index 098f0035e30..596c6b7fa83 100644 --- a/spec/datadog/appsec/processor_spec.rb +++ b/spec/datadog/appsec/processor_spec.rb @@ -233,6 +233,18 @@ context.run(input, timeout) end + it 'do not removes boolean values' do + input = { + 'false_value' => false, + 'true_value' => true + } + expect(context.instance_variable_get(:@context)).to receive(:run).with( + input, timeout + ).and_call_original + + context.run(input, timeout) + end + it 'removes empty string values' do input = { 'empty_string_value' => '',