libddwaf 1.30.0.0.2-x86_64-linux → 2.0.1.0.0-x86_64-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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 714ad0cf35c0cea18ee98403a0579e12360edc72f31fbc7922df6d5192f86a1c
4
- data.tar.gz: 74bd043598c90f9945e3c80ea09755760cdff92a6b1bfb6e44b8915885c4e9c2
3
+ metadata.gz: ffdaec7cbe67777cb8352d1bf77ef19eebbbc5f4c8c8733d5097a395e3691e57
4
+ data.tar.gz: de9571389a970479bc5917933246c6d85f301d876e59538cc6f196052b354d4b
5
5
  SHA512:
6
- metadata.gz: 97d641490701fecc327f9ad418143ec94513f6758031c3240c55026c2efd520410c2257e19d9accc1efc551b116f829493bdb13274d7e65f5a7bcc0385dbe378
7
- data.tar.gz: 386527ee8327af4da5d4863dc8d117b2cf0c5bdf39d54571f130f11aeab2d64b7e9465d76ca87e0a42546dce62dcaff70739720792b2ed73fc36c976f7fcd18c
6
+ metadata.gz: 2d2fb2d264f638a3aead0661e0520a341c026d88c1fc541fb12c20e67ff4838cca38f38438694ad8b3093365c7cba3da76e5ef3460533326cc3444dcfc193a55
7
+ data.tar.gz: f8ce09bded0c0e58143ad7dabe7810d906a375baeb2f0358f164ad1d6ac8f2fa7ccccd9ddd7bae66c24e6b07992c8313a70b48b2330e65c13397959dcee4c9a7
data/CHANGELOG.md CHANGED
@@ -1,5 +1,19 @@
1
1
  # Unreleased
2
2
 
3
+ ## Added
4
+
5
+ - Add `Context#build_subcontext`, which yields a `Subcontext` for temporary evaluations using inherited context data and finalizes it when the block returns. Use `Subcontext#eval` to evaluate data within it.
6
+
7
+ ## Changed
8
+
9
+ - Change `HandleBuilder#add_or_update_config` to return a `ConfigurationResult` instead of a diagnostics hash. Use `success?` to read the native success flag and `diagnostics` to access the diagnostics hash.
10
+ - Upgrade libddwaf to 2.0.0.
11
+ - Raise the minimum supported macOS version to 14.2.1 for the bundled native library.
12
+ - Replace `Context#run(persistent_data, ephemeral_data, timeout)` with `Context#eval(data, timeout)`. Use a subcontext to evaluate ephemeral input.
13
+ - Update the low-level FFI API and native object layouts for libddwaf 2.0.0.
14
+ - Defer asynchronous Ruby interruptions, including `Timeout` exceptions and `Thread#kill`, until input conversion, native evaluation, result conversion, and cleanup complete, to preserve native memory ownership. This can delay interruption for large payloads; libddwaf's evaluation timeout still applies.
15
+ - Remove the `coerce:` keyword from `Converter.ruby_to_object`
16
+
3
17
  # 2025-09-24 v1.25.1.1.0
4
18
 
5
19
  ## Changed
data/README.md CHANGED
@@ -66,6 +66,36 @@ rake binary[x86_64-linux:llvm] # Hybrid build: linked to llvm static libs a
66
66
 
67
67
  See upstream libddwaf for details about the [hybrid portable build](https://github.com/DataDog/libddwaf/blob/master/docker/libddwaf/README.md).
68
68
 
69
+ ## Releasing
70
+
71
+ Releases use the manually triggered [Publish workflow](https://github.com/DataDog/libddwaf-rb/actions/workflows/publish.yml)
72
+ on `master`.
73
+
74
+ For each release:
75
+
76
+ 1. Release the matching libddwaf binaries upstream first.
77
+ 2. Update `lib/datadog/appsec/waf/version.rb` and `CHANGELOG.md`, merge into
78
+ `master`, and confirm that the Test and Package workflows pass.
79
+ 3. Run the Publish workflow on `master` and approve its `rubygems.org` deployment.
80
+ 4. Confirm that all five gems are available on RubyGems.org: the generic gem,
81
+ `x86_64-linux`, `aarch64-linux`, `x86_64-darwin`, and `arm64-darwin`.
82
+
83
+ The workflow fetches the upstream binaries, builds the generic gem, and runs
84
+ the specs. It then uses `rubygems/release-gem` to run `rake release`, which
85
+ delegates to `libddwaf:release`: build all five gems, push the version tag,
86
+ and publish each gem with Sigstore attestation support. Do not create the
87
+ release tag beforehand.
88
+
89
+ After all five gems are available, the workflow automatically notifies dd-trace-rb
90
+ of the released version, triggering a PR to update its pin.
91
+
92
+ If notification fails, rerun the failed notification job. To notify consumers
93
+ manually about an already published version:
94
+
95
+ ```
96
+ gh workflow run "Notify Consumers" --ref master -f version='<published-version>'
97
+ ```
98
+
69
99
  ## Testing with Docker
70
100
 
71
101
  Unless using Docker for Mac, remember to enable foreign CPU emulation via QEMU:
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module AppSec
5
+ module WAF
6
+ class ConfigurationResult
7
+ attr_reader :diagnostics
8
+
9
+ def initialize(success:, diagnostics:)
10
+ @success = success
11
+ @diagnostics = diagnostics
12
+ end
13
+
14
+ def success?
15
+ @success
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -4,112 +4,56 @@ module Datadog
4
4
  module AppSec
5
5
  module WAF
6
6
  # Ruby representation of the ddwaf_context in libddwaf
7
- # See https://github.com/DataDog/libddwaf/blob/10e3a1dfc7bc9bb8ab11a09a9f8b6b339eaf3271/BINDING_IMPL_NOTES.md?plain=1#L125-L158
7
+ # See https://github.com/DataDog/libddwaf/blob/2.0.0/src/interface.cpp
8
8
  class Context
9
- EMPTY_RESULT = {
10
- "events" => [], #: WAF::events
11
- "actions" => {}, #: WAF::actions
12
- "attributes" => {}, #: WAF::attributes
13
- "duration" => 0,
14
- "timeout" => false,
15
- "keep" => false
16
- }.freeze
17
- SUCCESS_RESULT_CODES = %i[ddwaf_ok ddwaf_match].freeze
18
- RESULT_CODE_TO_STATUS = {
19
- ddwaf_ok: :ok,
20
- ddwaf_match: :match,
21
- ddwaf_err_internal: :err_internal,
22
- ddwaf_err_invalid_object: :err_invalid_object,
23
- ddwaf_err_invalid_argument: :err_invalid_argument
24
- }.freeze
25
-
26
9
  def initialize(context_ptr)
27
10
  @context_ptr = context_ptr
28
11
  end
29
12
 
30
- # Destroys the WAF context and sets the pointer to nil.
31
- #
32
- # The instance becomes unusable after this method is called.
33
13
  def finalize!
34
- context_ptr_to_destroy = @context_ptr
35
- @context_ptr = nil
36
-
37
- retained.each do |retained_obj|
38
- next unless retained_obj.is_a?(LibDDWAF::Object)
14
+ Thread.handle_interrupt(::Object => :never) do
15
+ pointer = @context_ptr
16
+ return unless pointer
39
17
 
40
- LibDDWAF.ddwaf_object_free(retained_obj)
18
+ @context_ptr = nil
19
+ LibDDWAF.ddwaf_context_destroy(pointer)
41
20
  end
42
-
43
- retained.clear
44
- LibDDWAF.ddwaf_context_destroy(context_ptr_to_destroy)
45
21
  end
46
22
 
47
- # Runs the WAF context with the given persistent and ephemeral data.
48
- #
49
- # @raise [ConversionError] if the conversion of persistent or ephemeral data fails
50
- # @raise [LibDDWAFError] if libddwaf could not create the result object
51
- #
52
- # @return [Result] the result of the WAF run
53
- def run(persistent_data, ephemeral_data, timeout = LibDDWAF::DDWAF_RUN_TIMEOUT)
54
- ensure_pointer_presence!
55
-
56
- persistent_data_obj = Converter.ruby_to_object(
57
- persistent_data,
58
- max_container_size: LibDDWAF::DDWAF_MAX_CONTAINER_SIZE,
59
- max_container_depth: LibDDWAF::DDWAF_MAX_CONTAINER_DEPTH,
60
- max_string_length: LibDDWAF::DDWAF_MAX_STRING_LENGTH,
61
- coerce: false
62
- )
63
- if persistent_data_obj.null?
64
- raise ConversionError, "Could not convert persistent data: #{persistent_data.inspect}"
65
- end
23
+ def build_subcontext
24
+ raise ArgumentError, "Block is required to build a subcontext" unless block_given?
66
25
 
67
- # retain C objects in memory for subsequent calls to run
68
- retain(persistent_data_obj)
26
+ subcontext = nil #: Subcontext?
69
27
 
70
- ephemeral_data_obj = Converter.ruby_to_object(
71
- ephemeral_data,
72
- max_container_size: LibDDWAF::DDWAF_MAX_CONTAINER_SIZE,
73
- max_container_depth: LibDDWAF::DDWAF_MAX_CONTAINER_DEPTH,
74
- max_string_length: LibDDWAF::DDWAF_MAX_STRING_LENGTH,
75
- coerce: false
76
- )
77
- if ephemeral_data_obj.null?
78
- raise ConversionError, "Could not convert ephemeral data: #{ephemeral_data.inspect}"
79
- end
28
+ begin
29
+ subcontext = Thread.handle_interrupt(::Object => :never) do
30
+ ensure_pointer_presence!
80
31
 
81
- result_obj = LibDDWAF::Object.new
82
- raise LibDDWAFError, "Could not create result object" if result_obj.null?
32
+ pointer = LibDDWAF.ddwaf_subcontext_init(@context_ptr)
33
+ raise LibDDWAFError, "Could not create subcontext" if pointer.null?
83
34
 
84
- code = LibDDWAF.ddwaf_run(@context_ptr, persistent_data_obj, ephemeral_data_obj, result_obj, timeout)
85
- result = Converter.object_to_ruby(result_obj)
35
+ begin
36
+ subcontext = Subcontext.new(pointer, context: self)
37
+ ensure
38
+ LibDDWAF.ddwaf_subcontext_destroy(pointer) unless subcontext
39
+ end
40
+ end
86
41
 
87
- # NOTE: In case of the error, `libddwaf` will not "fill" the result
88
- # object, so it will be empty and the conversion of it will return
89
- # `nil`, but that is not a conversion issue.
90
- if SUCCESS_RESULT_CODES.include?(code) && result.nil?
91
- raise ConversionError, "Could not convert result into object: #{code}"
42
+ yield subcontext
43
+ ensure
44
+ subcontext&.finalize!
92
45
  end
46
+ end
93
47
 
94
- result ||= EMPTY_RESULT
95
- result = Result.new(
96
- status: RESULT_CODE_TO_STATUS[code],
97
- events: result["events"],
98
- actions: result["actions"],
99
- attributes: result["attributes"],
100
- duration: result["duration"],
101
- timeout: result["timeout"],
102
- keep: result["keep"]
103
- )
48
+ def eval(data, timeout = LibDDWAF::DDWAF_RUN_TIMEOUT)
49
+ ensure_pointer_presence!
104
50
 
105
- if persistent_data_obj.truncated? || ephemeral_data_obj.truncated?
106
- result.mark_input_truncated!
51
+ Evaluation.call(data) do |input_obj, allocator, result_obj|
52
+ LibDDWAF.ddwaf_context_eval(
53
+ @context_ptr, #: ::FFI::Pointer
54
+ input_obj, allocator, result_obj, timeout
55
+ )
107
56
  end
108
-
109
- result
110
- ensure
111
- LibDDWAF.ddwaf_object_free(result_obj) if result_obj
112
- LibDDWAF.ddwaf_object_free(ephemeral_data_obj) if ephemeral_data_obj
113
57
  end
114
58
 
115
59
  private
@@ -119,18 +63,6 @@ module Datadog
119
63
 
120
64
  raise InstanceFinalizedError, "Cannot use WAF context after it has been finalized"
121
65
  end
122
-
123
- def retained
124
- @retained ||= []
125
- end
126
-
127
- def retain(object)
128
- retained << object
129
- end
130
-
131
- def release(object)
132
- retained.delete(object)
133
- end
134
66
  end
135
67
  end
136
68
  end
@@ -3,188 +3,213 @@
3
3
  module Datadog
4
4
  module AppSec
5
5
  module WAF
6
- # Module responsible for Ruby-to-C and C-to-Ruby conversions
7
6
  module Converter
7
+ MAX_CONTAINER_SIZE = 256
8
+ MAX_CONTAINER_DEPTH = 20
9
+ MAX_STRING_LENGTH = 4096
10
+
8
11
  INT64_MIN = -(2**63)
12
+ UINT16_MAX = 2**16 - 1
13
+ UINT32_MAX = 2**32 - 1
9
14
  UINT64_MAX = 2**64 - 1
10
15
 
11
16
  module_function
12
17
 
13
18
  # standard:disable Metrics/MethodLength,Metrics/CyclomaticComplexity
14
- def ruby_to_object(val, max_container_size: nil, max_container_depth: nil, max_string_length: nil, top_obj: nil, coerce: true)
15
- case val
16
- when Array
17
- obj = LibDDWAF::Object.new
18
- res = LibDDWAF.ddwaf_object_array(obj)
19
- raise ConversionError, "Could not convert into object: #{val}" if res.null?
19
+ def ruby_to_object(val, object: nil, allocator: nil, top_obj: nil, current_depth: 0, truncate: true)
20
+ if object.nil?
21
+ allocator = LibDDWAF.ddwaf_get_default_allocator
22
+ raise ConversionError, "Could not obtain the native allocator" if allocator.null?
23
+
24
+ object = LibDDWAF::Object.new
25
+
26
+ begin
27
+ ruby_to_object(val, object: object, allocator: allocator, top_obj: object, truncate: truncate)
28
+ complete = true
29
+ return object
30
+ ensure
31
+ LibDDWAF.ddwaf_object_destroy(object, allocator) unless complete
32
+ end
33
+ end
20
34
 
21
- if max_container_depth == 0
22
- top_obj&.mark_truncated!
35
+ result = case val
36
+ when Array
37
+ array_to_object(object, val, allocator, top_obj: top_obj, current_depth: current_depth, truncate: truncate)
38
+ when Hash
39
+ hash_to_object(object, val, allocator, top_obj: top_obj, current_depth: current_depth, truncate: truncate)
40
+ when JSONString
41
+ json_to_object(object, val, allocator)
42
+ when String, Symbol
43
+ string_to_object(object, val.to_s, allocator, top_obj: top_obj, truncate: truncate)
44
+ when Integer
45
+ if val < 0
46
+ LibDDWAF.ddwaf_object_set_signed(object, val.clamp(INT64_MIN, -1))
23
47
  else
24
- val.each.with_index do |e, i|
25
- if max_container_size && i >= max_container_size
26
- (top_obj || obj).mark_truncated!
27
- break val
28
- end
29
-
30
- member = Converter.ruby_to_object(
31
- e,
32
- max_container_size: max_container_size,
33
- max_container_depth: (max_container_depth - 1 if max_container_depth),
34
- max_string_length: max_string_length,
35
- top_obj: top_obj || obj,
36
- coerce: coerce
37
- )
38
- e_res = LibDDWAF.ddwaf_object_array_add(obj, member)
39
- raise ConversionError, "Could not add to array object: #{e.inspect}" unless e_res
40
- end
48
+ LibDDWAF.ddwaf_object_set_unsigned(object, val.clamp(0, UINT64_MAX))
41
49
  end
50
+ when Float
51
+ LibDDWAF.ddwaf_object_set_float(object, val)
52
+ when TrueClass, FalseClass
53
+ LibDDWAF.ddwaf_object_set_bool(object, val)
54
+ when NilClass
55
+ LibDDWAF.ddwaf_object_set_null(object)
56
+ else
57
+ string_to_object(object, "", allocator, top_obj: top_obj, truncate: truncate)
58
+ end
42
59
 
43
- obj
44
- when Hash
45
- obj = LibDDWAF::Object.new
46
- res = LibDDWAF.ddwaf_object_map(obj)
47
- raise ConversionError, "Could not convert into object: #{val}" if res.null?
60
+ raise ConversionError, "Could not populate native object" if result.to_ptr.null?
61
+
62
+ object
63
+ end
64
+
65
+ def array_to_object(object, array, allocator, top_obj:, current_depth:, truncate: true)
66
+ raise ConversionError, "Array exceeds the native size limit" if !truncate && array.size > UINT16_MAX
48
67
 
49
- if max_container_depth == 0
50
- top_obj&.mark_truncated!
68
+ max_capacity =
69
+ if !truncate || current_depth.zero?
70
+ UINT16_MAX
71
+ elsif current_depth > MAX_CONTAINER_DEPTH
72
+ 0
51
73
  else
52
- val.each.with_index do |e, i|
53
- if max_container_size && i >= max_container_size
54
- (top_obj || obj).mark_truncated!
55
- break val
56
- end
57
-
58
- # for Steep, which doesn't handle |(k, v), i|
59
- k = e[0].to_s
60
- v = e[1]
61
-
62
- if max_string_length && k.length > max_string_length
63
- k = k[0, max_string_length]
64
- (top_obj || obj).mark_truncated!
65
- end
66
- member = Converter.ruby_to_object(
67
- v,
68
- max_container_size: max_container_size,
69
- max_container_depth: (max_container_depth - 1 if max_container_depth),
70
- max_string_length: max_string_length,
71
- top_obj: top_obj || obj,
72
- coerce: coerce
73
- )
74
- kv_res = LibDDWAF.ddwaf_object_map_addl(obj, k, k.bytesize, member)
75
- raise ConversionError, "Could not add to map object: #{e[0].inspect} => #{v.inspect}" unless kv_res
76
- end
74
+ MAX_CONTAINER_SIZE
77
75
  end
78
76
 
79
- obj
80
- when String
81
- obj = LibDDWAF::Object.new
82
- encoded_val = val.to_s.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
83
- if max_string_length && encoded_val.length > max_string_length
84
- encoded_val = encoded_val[0, max_string_length] #: String
85
- (top_obj || obj).mark_truncated!
86
- end
87
- res = LibDDWAF.ddwaf_object_stringl(obj, encoded_val, encoded_val.bytesize)
88
- raise ConversionError, "Could not convert into object: #{val.inspect}" if res.null?
89
-
90
- obj
91
- when Symbol
92
- obj = LibDDWAF::Object.new
93
- str = val.to_s
94
- if max_string_length && str.length > max_string_length
95
- str = str[0, max_string_length] #: String
96
- (top_obj || obj).mark_truncated!
97
- end
98
- res = LibDDWAF.ddwaf_object_stringl(obj, str, str.bytesize)
99
- raise ConversionError, "Could not convert into object: #{val.inspect}" if res.null?
77
+ capacity = (array.size > max_capacity) ? max_capacity : array.size
100
78
 
101
- obj
102
- when Integer
103
- obj = LibDDWAF::Object.new
104
- res = if coerce
105
- LibDDWAF.ddwaf_object_string(obj, val.to_s)
106
- elsif val < 0
107
- clamped = val.clamp(INT64_MIN, -1) #: Integer
108
- LibDDWAF.ddwaf_object_signed(obj, clamped)
109
- else
110
- clamped = val.clamp(0, UINT64_MAX) #: Integer
111
- LibDDWAF.ddwaf_object_unsigned(obj, clamped)
79
+ container = LibDDWAF.ddwaf_object_set_array(object, capacity, allocator)
80
+ raise ConversionError, "Could not create array object" if container.to_ptr.null?
81
+
82
+ array.each_with_index do |element, i|
83
+ if i >= capacity
84
+ top_obj.mark_truncated!
85
+ break
112
86
  end
113
- raise ConversionError, "Could not convert into object: #{val.inspect}" if res.null?
114
87
 
115
- obj
116
- when Float
117
- obj = LibDDWAF::Object.new
118
- res = if coerce
119
- LibDDWAF.ddwaf_object_string(obj, val.to_s)
88
+ slot = LibDDWAF.ddwaf_object_insert(object, allocator)
89
+ raise ConversionError, "Could not insert array element" if slot.to_ptr.null?
90
+
91
+ ruby_to_object(
92
+ element,
93
+ object: slot,
94
+ allocator: allocator,
95
+ top_obj: top_obj,
96
+ current_depth: current_depth + 1,
97
+ truncate: truncate
98
+ )
99
+ end
100
+ object
101
+ end
102
+
103
+ def hash_to_object(object, hash, allocator, top_obj:, current_depth:, truncate: true)
104
+ raise ConversionError, "Map exceeds the native size limit" if !truncate && hash.size > UINT16_MAX
105
+
106
+ max_capacity =
107
+ if !truncate || current_depth.zero?
108
+ UINT16_MAX
109
+ elsif current_depth > MAX_CONTAINER_DEPTH
110
+ 0
120
111
  else
121
- LibDDWAF.ddwaf_object_float(obj, val)
112
+ MAX_CONTAINER_SIZE
122
113
  end
123
- raise ConversionError, "Could not convert into object: #{val.inspect}" if res.null?
124
114
 
125
- obj
126
- when TrueClass, FalseClass
127
- obj = LibDDWAF::Object.new
128
- res = if coerce
129
- LibDDWAF.ddwaf_object_string(obj, val.to_s)
130
- else
131
- LibDDWAF.ddwaf_object_bool(obj, val)
115
+ capacity = (hash.size > max_capacity) ? max_capacity : hash.size
116
+
117
+ container = LibDDWAF.ddwaf_object_set_map(object, capacity, allocator)
118
+ raise ConversionError, "Could not create map object" if container.to_ptr.null?
119
+
120
+ hash.each_with_index do |entry, i|
121
+ if i >= capacity
122
+ top_obj.mark_truncated!
123
+ break
132
124
  end
133
- raise ConversionError, "Could not convert into object: #{val.inspect}" if res.null?
134
125
 
135
- obj
136
- when NilClass
137
- obj = LibDDWAF::Object.new
138
- res = if coerce
139
- LibDDWAF.ddwaf_object_string(obj, "")
140
- else
141
- LibDDWAF.ddwaf_object_null(obj)
126
+ key = entry[0].to_s
127
+ if truncate
128
+ key = key.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
129
+ if key.length > MAX_STRING_LENGTH
130
+ top_obj.mark_truncated!
131
+ key = key[0, MAX_STRING_LENGTH] #: String
132
+ end
142
133
  end
143
- raise ConversionError, "Could not convert into object: #{val.inspect}" if res.null?
134
+ length = key.bytesize
135
+ raise ConversionError, "Map key exceeds the native length limit" if length > UINT32_MAX
136
+
137
+ slot = LibDDWAF.ddwaf_object_insert_key(object, key, length, allocator)
138
+ raise ConversionError, "Could not insert map entry" if slot.to_ptr.null?
139
+
140
+ ruby_to_object(
141
+ entry[1],
142
+ object: slot,
143
+ allocator: allocator,
144
+ top_obj: top_obj,
145
+ current_depth: current_depth + 1,
146
+ truncate: truncate
147
+ )
148
+ end
149
+ object
150
+ end
144
151
 
145
- obj
146
- else
147
- Converter.ruby_to_object("")
152
+ def json_to_object(object, json, allocator)
153
+ # TODO: Pass request limits to JSON parsing once libddwaf supports them.
154
+ length = json.bytesize
155
+ raise ConversionError, "JSON string exceeds the native length limit" if length > UINT32_MAX
156
+
157
+ # In libddwaf 2.0.0, failed parsing leaves this fresh slot invalid.
158
+ unless LibDDWAF.ddwaf_object_from_json(object, json, length, allocator)
159
+ raise ConversionError, "Could not convert JSON string"
148
160
  end
161
+ object
162
+ end
163
+
164
+ def string_to_object(object, string, allocator, top_obj:, truncate: true)
165
+ string = string.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
166
+ if truncate && string.length > MAX_STRING_LENGTH
167
+ top_obj.mark_truncated!
168
+ string = string[0, MAX_STRING_LENGTH] #: String
169
+ end
170
+ length = string.bytesize
171
+ raise ConversionError, "String exceeds the native length limit" if length > UINT32_MAX
172
+
173
+ LibDDWAF.ddwaf_object_set_string(object, string, length, allocator)
149
174
  end
150
175
  # standard:enable Metrics/MethodLength,Metrics/CyclomaticComplexity
151
176
 
152
177
  # standard:disable Metrics/MethodLength,Metrics/CyclomaticComplexity
153
178
  def object_to_ruby(obj)
154
- case obj[:type]
179
+ case LibDDWAF.ddwaf_object_get_type(obj)
155
180
  when :ddwaf_obj_invalid, :ddwaf_obj_null
156
181
  nil
157
182
  when :ddwaf_obj_bool
158
- obj[:valueUnion][:boolean]
159
- when :ddwaf_obj_string
160
- # NOTE: FFI's `AbstractMemoryPointer#read_bytes` returns a binary string,
161
- # which is not automatically encoded as UTF-8 and will raise an error
162
- # if it contains non-ASCII characters and is used in a JSON#generate call.
163
- bytes = obj[:valueUnion][:stringValue].read_bytes(obj[:nbEntries])
164
- bytes.ascii_only? ? bytes : bytes.force_encoding(Encoding::UTF_8)
183
+ LibDDWAF.ddwaf_object_get_bool(obj)
184
+ when :ddwaf_obj_string, :ddwaf_obj_literal_string, :ddwaf_obj_small_string
185
+ object_to_string(obj)
165
186
  when :ddwaf_obj_signed
166
- obj[:valueUnion][:intValue]
187
+ LibDDWAF.ddwaf_object_get_signed(obj)
167
188
  when :ddwaf_obj_unsigned
168
- obj[:valueUnion][:uintValue]
189
+ LibDDWAF.ddwaf_object_get_unsigned(obj)
169
190
  when :ddwaf_obj_float
170
- obj[:valueUnion][:f64]
191
+ LibDDWAF.ddwaf_object_get_float(obj)
171
192
  when :ddwaf_obj_array
172
- (0...obj[:nbEntries]).each.with_object([]) do |i, a| #$ ::Array[WAF::opaque]
173
- ptr = obj[:valueUnion][:array] + i * LibDDWAF::Object.size
174
- e = Converter.object_to_ruby(LibDDWAF::Object.new(ptr))
175
- a << e
193
+ (0...LibDDWAF.ddwaf_object_get_size(obj)).each_with_object([]) do |i, array| #$ ::Array[WAF::opaque]
194
+ array << object_to_ruby(LibDDWAF.ddwaf_object_at_value(obj, i))
176
195
  end
177
196
  when :ddwaf_obj_map
178
- (0...obj[:nbEntries]).each.with_object({}) do |i, h| #$ ::Hash[::String, WAF::opaque]
179
- ptr = obj[:valueUnion][:array] + i * Datadog::AppSec::WAF::LibDDWAF::Object.size
180
- o = Datadog::AppSec::WAF::LibDDWAF::Object.new(ptr)
181
- l = o[:parameterNameLength]
182
- k = o[:parameterName].read_bytes(l)
183
- v = Converter.object_to_ruby(LibDDWAF::Object.new(ptr))
184
- h[k] = v
197
+ (0...LibDDWAF.ddwaf_object_get_size(obj)).each_with_object({}) do |i, hash| #$ ::Hash[::String, WAF::opaque]
198
+ key = object_to_string(LibDDWAF.ddwaf_object_at_key(obj, i))
199
+ value = object_to_ruby(LibDDWAF.ddwaf_object_at_value(obj, i))
200
+ hash[key] = value
185
201
  end
186
202
  end
187
203
  end
204
+
205
+ def object_to_string(obj)
206
+ length = LibDDWAF::SizeTPtr.new
207
+ pointer = LibDDWAF.ddwaf_object_get_string(obj, length)
208
+ bytes = pointer.read_bytes(length[:value])
209
+
210
+ # FFI reads binary strings; restore UTF-8 for non-ASCII content.
211
+ bytes.ascii_only? ? bytes : bytes.force_encoding(Encoding::UTF_8)
212
+ end
188
213
  # standard:enable Metrics/MethodLength,Metrics/CyclomaticComplexity
189
214
  end
190
215
  end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Datadog
4
+ module AppSec
5
+ module WAF
6
+ # Internal evaluation conversion, ownership and result handling for both native adapters.
7
+ module Evaluation
8
+ SUCCESS_RESULT_CODES = %i[ddwaf_ok ddwaf_match].freeze
9
+
10
+ RESULT_CODE_TO_STATUS = {
11
+ ddwaf_ok: :ok,
12
+ ddwaf_match: :match,
13
+ ddwaf_err_internal: :err_internal,
14
+ ddwaf_err_invalid_object: :err_invalid_object,
15
+ ddwaf_err_invalid_argument: :err_invalid_argument
16
+ }.freeze
17
+
18
+ module_function
19
+
20
+ def call(data)
21
+ allocator = LibDDWAF.ddwaf_get_default_allocator
22
+ raise LibDDWAFError, "Could not obtain the native allocator" if allocator.null?
23
+
24
+ Thread.handle_interrupt(::Object => :never) do
25
+ input_obj = nil
26
+ result_obj = nil
27
+ input_transferred = false
28
+
29
+ begin
30
+ input_obj = Converter.ruby_to_object(data)
31
+ result_obj = LibDDWAF::Object.new
32
+
33
+ code = yield input_obj, allocator, result_obj
34
+ # only INVALID_ARGUMENT returns before input ownership transfers.
35
+ input_transferred = code != :ddwaf_err_invalid_argument
36
+
37
+ status = RESULT_CODE_TO_STATUS.fetch(code)
38
+ result = if SUCCESS_RESULT_CODES.include?(code)
39
+ decoded = Converter.object_to_ruby(result_obj) #: WAF::result
40
+ raise ConversionError, "Could not convert result into object: #{code}" unless decoded.is_a?(Hash)
41
+
42
+ Result.new(
43
+ status: status,
44
+ events: decoded["events"],
45
+ actions: decoded["actions"],
46
+ attributes: decoded["attributes"],
47
+ duration: decoded["duration"],
48
+ timeout: decoded["timeout"],
49
+ keep: decoded["keep"]
50
+ )
51
+ else
52
+ Result.new(status: status, events: [], actions: {}, attributes: {}, duration: 0, timeout: false, keep: false)
53
+ end
54
+ result.mark_input_truncated! if input_obj.truncated?
55
+ result
56
+ ensure
57
+ LibDDWAF.ddwaf_object_destroy(result_obj, allocator) if result_obj
58
+ LibDDWAF.ddwaf_object_destroy(input_obj, allocator) if input_obj && !input_transferred
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end