Skip to content

Commit

Permalink
handle boolean values
Browse files Browse the repository at this point in the history
  • Loading branch information
GustavoCaso committed Oct 6, 2023
1 parent 0b90575 commit fcca011
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
23 changes: 9 additions & 14 deletions lib/datadog/appsec/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions sig/datadog/appsec/processor.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions spec/datadog/appsec/processor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' => '',
Expand Down

0 comments on commit fcca011

Please sign in to comment.