libddwaf 1.30.0.1.0-aarch64-linux → 2.0.1.0.1-aarch64-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: 9de78da9a9a968ff60082db468c27dc6fe91ce75935a0bbc4743095450b51a5e
4
- data.tar.gz: 9d572b8dc18d6fa5d3a930e5da0434973a73155cbfbdd96fe7f4a69e2f7aedec
3
+ metadata.gz: 0b07a401e11dd9ed49c33f39d87337cd053daa0f4a2449b9c380d9e3b5a358fe
4
+ data.tar.gz: 4b810cd625ae5e5b4b572f74d8ce877f23cc43b7e06e71878685ddd7f168dbb0
5
5
  SHA512:
6
- metadata.gz: f46193c941b2e1f53fc86d9fa9b30bfe33be6f88e446de94e8268842c2da36d860df8606112d3877eae32751abc8452f1ca3ee591c3edb818b4b7a2db3c7e139
7
- data.tar.gz: 50189195de9d2805ee21fa7669cfa57c838d37f7ad20f1e3e53156feb21c050288e090396e91fda9ccdaad425a69a904f589d479c976be0d96fbef4b873cb82c
6
+ metadata.gz: 14db2d3a44012ee6f9766f64129dc4daffe09c06d719f64aeab833cae1b23832e1620a6a2513e330c0e8615998b198a64200282437404f2bd22f6487d040929b
7
+ data.tar.gz: 02c9aa9980f2b228e2d1014ce180b58b3e340b193d5d25ba1be357c99bb440d9b99d5288717484161b1d9a62d9c047d7e9d1e19ab462eea3ecaa5c96b33cb37e
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,203 +3,215 @@
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
9
13
  UINT32_MAX = 2**32 - 1
10
14
  UINT64_MAX = 2**64 - 1
11
15
 
12
16
  module_function
13
17
 
14
18
  # standard:disable Metrics/MethodLength,Metrics/CyclomaticComplexity
15
- def ruby_to_object(val, max_container_size: nil, max_container_depth: nil, max_string_length: nil, top_obj: nil, coerce: true)
16
- case val
17
- when Array
18
- container = LibDDWAF::Object.new
19
- res = LibDDWAF.ddwaf_object_array(container)
20
- 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
21
34
 
22
- if max_container_depth == 0
23
- 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))
24
47
  else
25
- val.each.with_index do |e, i|
26
- if max_container_size && i >= max_container_size
27
- (top_obj || container).mark_truncated!
28
- break val
29
- end
30
-
31
- member = Converter.ruby_to_object(
32
- e,
33
- max_container_size: max_container_size,
34
- max_container_depth: (max_container_depth - 1 if max_container_depth),
35
- max_string_length: max_string_length,
36
- top_obj: top_obj || container,
37
- coerce: coerce
38
- )
39
- e_res = LibDDWAF.ddwaf_object_array_add(container, member)
40
- raise ConversionError, "Could not add to array object: #{e.inspect}" unless e_res
41
- end
48
+ LibDDWAF.ddwaf_object_set_unsigned(object, val.clamp(0, UINT64_MAX))
42
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
43
59
 
44
- container_complete = true
45
- container
46
- when Hash
47
- container = LibDDWAF::Object.new
48
- res = LibDDWAF.ddwaf_object_map(container)
49
- 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
50
67
 
51
- if max_container_depth == 0
52
- 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
53
73
  else
54
- val.each.with_index do |e, i|
55
- if max_container_size && i >= max_container_size
56
- (top_obj || container).mark_truncated!
57
- break val
58
- end
59
-
60
- # for Steep, which doesn't handle |(k, v), i|
61
- k = e[0].to_s
62
- v = e[1]
63
-
64
- if max_string_length && k.length > max_string_length
65
- k = k[0, max_string_length]
66
- (top_obj || container).mark_truncated!
67
- end
68
- member = Converter.ruby_to_object(
69
- v,
70
- max_container_size: max_container_size,
71
- max_container_depth: (max_container_depth - 1 if max_container_depth),
72
- max_string_length: max_string_length,
73
- top_obj: top_obj || container,
74
- coerce: coerce
75
- )
76
- kv_res = LibDDWAF.ddwaf_object_map_addl(container, k, k.bytesize, member)
77
- raise ConversionError, "Could not add to map object: #{e[0].inspect} => #{v.inspect}" unless kv_res
78
- end
74
+ MAX_CONTAINER_SIZE
79
75
  end
80
76
 
81
- container_complete = true
82
- container
83
- when JSONString
84
- length = val.bytesize
85
- raise ConversionError, "JSON string exceeds the native length limit" if length > UINT32_MAX
77
+ capacity = (array.size > max_capacity) ? max_capacity : array.size
86
78
 
87
- obj = LibDDWAF::Object.new
88
- unless LibDDWAF.ddwaf_object_from_json(obj, val, length)
89
- raise ConversionError, "Could not convert JSON string"
90
- end
79
+ container = LibDDWAF.ddwaf_object_set_array(object, capacity, allocator)
80
+ raise ConversionError, "Could not create array object" if container.to_ptr.null?
91
81
 
92
- obj
93
- when String
94
- obj = LibDDWAF::Object.new
95
- encoded_val = val.to_s.encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
96
- if max_string_length && encoded_val.length > max_string_length
97
- encoded_val = encoded_val[0, max_string_length] #: String
98
- (top_obj || obj).mark_truncated!
82
+ array.each_with_index do |element, i|
83
+ if i >= capacity
84
+ top_obj.mark_truncated!
85
+ break
99
86
  end
100
- res = LibDDWAF.ddwaf_object_stringl(obj, encoded_val, encoded_val.bytesize)
101
- raise ConversionError, "Could not convert into object: #{val.inspect}" if res.null?
102
-
103
- obj
104
- when Symbol
105
- obj = LibDDWAF::Object.new
106
- str = val.to_s
107
- if max_string_length && str.length > max_string_length
108
- str = str[0, max_string_length] #: String
109
- (top_obj || obj).mark_truncated!
110
- end
111
- res = LibDDWAF.ddwaf_object_stringl(obj, str, str.bytesize)
112
- raise ConversionError, "Could not convert into object: #{val.inspect}" if res.null?
113
87
 
114
- obj
115
- when Integer
116
- obj = LibDDWAF::Object.new
117
- res = if coerce
118
- LibDDWAF.ddwaf_object_string(obj, val.to_s)
119
- elsif val < 0
120
- clamped = val.clamp(INT64_MIN, -1) #: Integer
121
- LibDDWAF.ddwaf_object_signed(obj, clamped)
122
- else
123
- clamped = val.clamp(0, UINT64_MAX) #: Integer
124
- LibDDWAF.ddwaf_object_unsigned(obj, clamped)
125
- end
126
- raise ConversionError, "Could not convert into object: #{val.inspect}" if res.null?
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
127
102
 
128
- obj
129
- when Float
130
- obj = LibDDWAF::Object.new
131
- res = if coerce
132
- LibDDWAF.ddwaf_object_string(obj, val.to_s)
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
133
111
  else
134
- LibDDWAF.ddwaf_object_float(obj, val)
112
+ MAX_CONTAINER_SIZE
135
113
  end
136
- raise ConversionError, "Could not convert into object: #{val.inspect}" if res.null?
137
114
 
138
- obj
139
- when TrueClass, FalseClass
140
- obj = LibDDWAF::Object.new
141
- res = if coerce
142
- LibDDWAF.ddwaf_object_string(obj, val.to_s)
143
- else
144
- 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
145
124
  end
146
- raise ConversionError, "Could not convert into object: #{val.inspect}" if res.null?
147
125
 
148
- obj
149
- when NilClass
150
- obj = LibDDWAF::Object.new
151
- res = if coerce
152
- LibDDWAF.ddwaf_object_string(obj, "")
153
- else
154
- 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
155
133
  end
156
- 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
157
151
 
158
- obj
159
- else
160
- 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"
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
161
169
  end
162
- ensure
163
- LibDDWAF.ddwaf_object_free(container) if container && !container_complete
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)
164
174
  end
165
175
  # standard:enable Metrics/MethodLength,Metrics/CyclomaticComplexity
166
176
 
167
177
  # standard:disable Metrics/MethodLength,Metrics/CyclomaticComplexity
168
178
  def object_to_ruby(obj)
169
- case obj[:type]
179
+ case LibDDWAF.ddwaf_object_get_type(obj)
170
180
  when :ddwaf_obj_invalid, :ddwaf_obj_null
171
181
  nil
172
182
  when :ddwaf_obj_bool
173
- obj[:valueUnion][:boolean]
174
- when :ddwaf_obj_string
175
- # NOTE: FFI's `AbstractMemoryPointer#read_bytes` returns a binary string,
176
- # which is not automatically encoded as UTF-8 and will raise an error
177
- # if it contains non-ASCII characters and is used in a JSON#generate call.
178
- bytes = obj[:valueUnion][:stringValue].read_bytes(obj[:nbEntries])
179
- 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)
180
186
  when :ddwaf_obj_signed
181
- obj[:valueUnion][:intValue]
187
+ LibDDWAF.ddwaf_object_get_signed(obj)
182
188
  when :ddwaf_obj_unsigned
183
- obj[:valueUnion][:uintValue]
189
+ LibDDWAF.ddwaf_object_get_unsigned(obj)
184
190
  when :ddwaf_obj_float
185
- obj[:valueUnion][:f64]
191
+ LibDDWAF.ddwaf_object_get_float(obj)
186
192
  when :ddwaf_obj_array
187
- (0...obj[:nbEntries]).each.with_object([]) do |i, a| #$ ::Array[WAF::opaque]
188
- ptr = obj[:valueUnion][:array] + i * LibDDWAF::Object.size
189
- e = Converter.object_to_ruby(LibDDWAF::Object.new(ptr))
190
- 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))
191
195
  end
192
196
  when :ddwaf_obj_map
193
- (0...obj[:nbEntries]).each.with_object({}) do |i, h| #$ ::Hash[::String, WAF::opaque]
194
- ptr = obj[:valueUnion][:array] + i * Datadog::AppSec::WAF::LibDDWAF::Object.size
195
- o = Datadog::AppSec::WAF::LibDDWAF::Object.new(ptr)
196
- l = o[:parameterNameLength]
197
- k = o[:parameterName].read_bytes(l)
198
- v = Converter.object_to_ruby(LibDDWAF::Object.new(ptr))
199
- 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
200
201
  end
201
202
  end
202
203
  end
204
+
205
+ def object_to_string(obj)
206
+ length = LibDDWAF::SizeTPtr.new
207
+ pointer = LibDDWAF.ddwaf_object_get_string(obj, length)
208
+ return "" if length[:value].zero?
209
+
210
+ bytes = pointer.read_bytes(length[:value])
211
+
212
+ # FFI reads binary strings; restore UTF-8 for non-ASCII content.
213
+ bytes.ascii_only? ? bytes : bytes.force_encoding(Encoding::UTF_8)
214
+ end
203
215
  # standard:enable Metrics/MethodLength,Metrics/CyclomaticComplexity
204
216
  end
205
217
  end