libddwaf 1.30.0.0.2 → 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 +164 -139
- 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 -96
- data/lib/datadog/appsec/waf/subcontext.rb +43 -0
- data/lib/datadog/appsec/waf/version.rb +2 -2
- data/lib/datadog/appsec/waf.rb +13 -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 +43 -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 -55
- data/sig/datadog/appsec/waf/result.rbs +10 -1
- data/sig/datadog/appsec/waf/subcontext.rbs +23 -0
- data/sig/datadog/appsec/waf.rbs +8 -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 +14 -7
- 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,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,
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
22
|
-
|
|
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.
|
|
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
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
-
|
|
50
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
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
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
112
|
+
MAX_CONTAINER_SIZE
|
|
122
113
|
end
|
|
123
|
-
raise ConversionError, "Could not convert into object: #{val.inspect}" if res.null?
|
|
124
114
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
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
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
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
|
-
|
|
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
|
-
|
|
146
|
-
|
|
147
|
-
|
|
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
|
|
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
|
|
159
|
-
when :ddwaf_obj_string
|
|
160
|
-
|
|
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
|
|
187
|
+
LibDDWAF.ddwaf_object_get_signed(obj)
|
|
167
188
|
when :ddwaf_obj_unsigned
|
|
168
|
-
obj
|
|
189
|
+
LibDDWAF.ddwaf_object_get_unsigned(obj)
|
|
169
190
|
when :ddwaf_obj_float
|
|
170
|
-
obj
|
|
191
|
+
LibDDWAF.ddwaf_object_get_float(obj)
|
|
171
192
|
when :ddwaf_obj_array
|
|
172
|
-
(0...obj
|
|
173
|
-
|
|
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
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
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
|