libddwaf 1.30.0.1.0 → 2.0.1.0.0
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 +4 -4
- data/CHANGELOG.md +14 -0
- data/README.md +30 -0
- data/lib/datadog/appsec/waf/configuration_result.rb +20 -0
- data/lib/datadog/appsec/waf/context.rb +31 -99
- data/lib/datadog/appsec/waf/converter.rb +161 -151
- data/lib/datadog/appsec/waf/evaluation.rb +65 -0
- data/lib/datadog/appsec/waf/handle.rb +12 -3
- data/lib/datadog/appsec/waf/handle_builder.rb +53 -21
- data/lib/datadog/appsec/waf/lib_ddwaf.rb +109 -97
- data/lib/datadog/appsec/waf/subcontext.rb +43 -0
- data/lib/datadog/appsec/waf/version.rb +2 -2
- data/lib/datadog/appsec/waf.rb +9 -2
- data/sig/datadog/appsec/waf/configuration_result.rbs +17 -0
- data/sig/datadog/appsec/waf/context.rbs +4 -23
- data/sig/datadog/appsec/waf/converter.rbs +37 -5
- data/sig/datadog/appsec/waf/evaluation.rbs +15 -0
- data/sig/datadog/appsec/waf/handle_builder.rbs +6 -2
- data/sig/datadog/appsec/waf/lib_ddwaf.rbs +73 -56
- data/sig/datadog/appsec/waf/result.rbs +10 -1
- data/sig/datadog/appsec/waf/subcontext.rbs +23 -0
- data/sig/datadog/appsec/waf.rbs +5 -9
- data/sig/thread.rbs +3 -0
- data/vendor/libddwaf/libddwaf-2.0.1-darwin-arm64/lib/libddwaf.dylib +0 -0
- data/vendor/libddwaf/libddwaf-2.0.1-darwin-x86_64/lib/libddwaf.dylib +0 -0
- data/vendor/libddwaf/libddwaf-2.0.1-linux-aarch64/lib/libddwaf.so +0 -0
- data/vendor/libddwaf/libddwaf-2.0.1-linux-x86_64/lib/libddwaf.so +0 -0
- metadata +13 -6
- data/vendor/libddwaf/libddwaf-1.30.0-darwin-arm64/lib/libddwaf.dylib +0 -0
- data/vendor/libddwaf/libddwaf-1.30.0-darwin-x86_64/lib/libddwaf.dylib +0 -0
- data/vendor/libddwaf/libddwaf-1.30.0-linux-aarch64/lib/libddwaf.so +0 -0
- data/vendor/libddwaf/libddwaf-1.30.0-linux-x86_64/lib/libddwaf.so +0 -0
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b6c301df9a3527106b11ccff6cfaec72cd8db9a1e8e6c1849cc06765dd44bf14
|
|
4
|
+
data.tar.gz: 28a68b6a7ed5cc0dbfd36a31fe834ecfc1fe485358252358a600beaac86b0483
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8704765a67aa948dfb44ad8ed6deec9beb83d97d798c7779ebdce82e562df8cdbf426bced3651094b5ad59ae150fcdd412ac6ca4370ff61f98518bb45f0b7881
|
|
7
|
+
data.tar.gz: bfcb07a5c8933ca585171f490784d8d56a206ed05a7e0a90934553bcabc30c34d90bff1ec45d811326bbd0ce31c9c076005aa4d14bfd93df67a9420df03928f3
|
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/
|
|
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
|
-
|
|
35
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
68
|
-
retain(persistent_data_obj)
|
|
26
|
+
subcontext = nil #: Subcontext?
|
|
69
27
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
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
|
-
|
|
82
|
-
|
|
32
|
+
pointer = LibDDWAF.ddwaf_subcontext_init(@context_ptr)
|
|
33
|
+
raise LibDDWAFError, "Could not create subcontext" if pointer.null?
|
|
83
34
|
|
|
84
|
-
|
|
85
|
-
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
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
|
-
|
|
95
|
-
|
|
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
|
-
|
|
106
|
-
|
|
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,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
|
|
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,
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
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
|
-
|
|
23
|
-
|
|
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.
|
|
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
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
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
|
-
|
|
52
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
88
|
-
|
|
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
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
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
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
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
|
-
|
|
112
|
+
MAX_CONTAINER_SIZE
|
|
135
113
|
end
|
|
136
|
-
raise ConversionError, "Could not convert into object: #{val.inspect}" if res.null?
|
|
137
114
|
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
-
|
|
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
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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"
|
|
161
160
|
end
|
|
162
|
-
|
|
163
|
-
|
|
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)
|
|
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
|
|
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
|
|
174
|
-
when :ddwaf_obj_string
|
|
175
|
-
|
|
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
|
|
187
|
+
LibDDWAF.ddwaf_object_get_signed(obj)
|
|
182
188
|
when :ddwaf_obj_unsigned
|
|
183
|
-
obj
|
|
189
|
+
LibDDWAF.ddwaf_object_get_unsigned(obj)
|
|
184
190
|
when :ddwaf_obj_float
|
|
185
|
-
obj
|
|
191
|
+
LibDDWAF.ddwaf_object_get_float(obj)
|
|
186
192
|
when :ddwaf_obj_array
|
|
187
|
-
(0...obj
|
|
188
|
-
|
|
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
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
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
|
+
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
|
|
203
213
|
# standard:enable Metrics/MethodLength,Metrics/CyclomaticComplexity
|
|
204
214
|
end
|
|
205
215
|
end
|