libddwaf 1.30.0.1.0 → 2.0.1.0.1
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 +163 -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
|
@@ -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
|
|
@@ -23,14 +23,23 @@ module Datadog
|
|
|
23
23
|
# Builds a WAF context.
|
|
24
24
|
#
|
|
25
25
|
# @raise [LibDDWAFError] if libddwaf could not create the context.
|
|
26
|
-
# @return [
|
|
26
|
+
# @return [Context] the WAF context
|
|
27
27
|
def build_context
|
|
28
28
|
ensure_pointer_presence!
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
output_allocator = LibDDWAF.ddwaf_get_default_allocator
|
|
31
|
+
raise LibDDWAFError, "Could not obtain the native allocator" if output_allocator.null?
|
|
32
|
+
|
|
33
|
+
context_obj = LibDDWAF.ddwaf_context_init(@handle_ptr, output_allocator)
|
|
31
34
|
raise LibDDWAFError, "Could not create context" if context_obj.null?
|
|
32
35
|
|
|
33
|
-
|
|
36
|
+
begin
|
|
37
|
+
context = Context.new(context_obj)
|
|
38
|
+
initialized = true
|
|
39
|
+
context
|
|
40
|
+
ensure
|
|
41
|
+
LibDDWAF.ddwaf_context_destroy(context_obj) unless initialized
|
|
42
|
+
end
|
|
34
43
|
end
|
|
35
44
|
|
|
36
45
|
# Returns the list of known addresses in the WAF handle.
|
|
@@ -7,21 +7,18 @@ module Datadog
|
|
|
7
7
|
#
|
|
8
8
|
# It handles merging of potentially overlapping configurations.
|
|
9
9
|
class HandleBuilder
|
|
10
|
-
|
|
11
|
-
handle_config_obj = LibDDWAF::HandleBuilderConfig.new
|
|
12
|
-
if handle_config_obj.null?
|
|
13
|
-
raise LibDDWAFError, "Could not create config struct"
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
handle_config_obj[:limits][:max_container_size] = limits[:max_container_size] || LibDDWAF::DEFAULT_MAX_CONTAINER_SIZE
|
|
17
|
-
handle_config_obj[:limits][:max_container_depth] = limits[:max_container_depth] || LibDDWAF::DEFAULT_MAX_CONTAINER_DEPTH
|
|
18
|
-
handle_config_obj[:limits][:max_string_length] = limits[:max_string_length] || LibDDWAF::DEFAULT_MAX_STRING_LENGTH
|
|
10
|
+
OBFUSCATOR_CONFIG_PATH = "libddwaf-rb/obfuscator"
|
|
19
11
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
12
|
+
def initialize(obfuscator: {})
|
|
13
|
+
@builder_ptr = LibDDWAF.ddwaf_builder_init
|
|
14
|
+
raise LibDDWAFError, "Could not create builder" if @builder_ptr.null?
|
|
23
15
|
|
|
24
|
-
|
|
16
|
+
begin
|
|
17
|
+
configure_obfuscator(obfuscator)
|
|
18
|
+
initialized = true
|
|
19
|
+
ensure
|
|
20
|
+
LibDDWAF.ddwaf_builder_destroy(@builder_ptr) unless initialized
|
|
21
|
+
end
|
|
25
22
|
end
|
|
26
23
|
|
|
27
24
|
# Destroys the WAF builder and sets the pointer to nil.
|
|
@@ -44,7 +41,13 @@ module Datadog
|
|
|
44
41
|
handle_obj = LibDDWAF.ddwaf_builder_build_instance(@builder_ptr)
|
|
45
42
|
raise LibDDWAFError, "Could not create handle" if handle_obj.null?
|
|
46
43
|
|
|
47
|
-
|
|
44
|
+
begin
|
|
45
|
+
handle = Handle.new(handle_obj)
|
|
46
|
+
initialized = true
|
|
47
|
+
handle
|
|
48
|
+
ensure
|
|
49
|
+
LibDDWAF.ddwaf_destroy(handle_obj) unless initialized
|
|
50
|
+
end
|
|
48
51
|
end
|
|
49
52
|
|
|
50
53
|
# :section: Configuration management methods
|
|
@@ -52,21 +55,28 @@ module Datadog
|
|
|
52
55
|
|
|
53
56
|
# Adds or updates a configuration in the WAF handle builder for the given path.
|
|
54
57
|
#
|
|
55
|
-
# @return [
|
|
58
|
+
# @return [ConfigurationResult] success flag and diagnostics object
|
|
56
59
|
# NOTE: default config that was read from file at application startup
|
|
57
60
|
# has to be removed before adding configurations obtained through Remote Configuration.
|
|
58
61
|
def add_or_update_config(config, path:)
|
|
59
62
|
ensure_pointer_presence!
|
|
63
|
+
raise ArgumentError, "Configuration path is reserved for the obfuscator" if path == OBFUSCATOR_CONFIG_PATH
|
|
64
|
+
|
|
65
|
+
path_length = path.bytesize
|
|
66
|
+
raise ArgumentError, "Configuration path exceeds the native length limit" if path_length > 0xffff_ffff
|
|
67
|
+
|
|
68
|
+
allocator = LibDDWAF.ddwaf_get_default_allocator
|
|
69
|
+
raise LibDDWAFError, "Could not obtain the native allocator" if allocator.null?
|
|
60
70
|
|
|
61
|
-
config_obj = Converter.ruby_to_object(config,
|
|
71
|
+
config_obj = Converter.ruby_to_object(config, truncate: false)
|
|
62
72
|
diagnostics_obj = LibDDWAF::Object.new
|
|
63
73
|
|
|
64
|
-
LibDDWAF.ddwaf_builder_add_or_update_config(@builder_ptr, path,
|
|
74
|
+
success = LibDDWAF.ddwaf_builder_add_or_update_config(@builder_ptr, path, path_length, config_obj, diagnostics_obj)
|
|
65
75
|
|
|
66
|
-
Converter.object_to_ruby(diagnostics_obj)
|
|
76
|
+
ConfigurationResult.new(success: success, diagnostics: Converter.object_to_ruby(diagnostics_obj))
|
|
67
77
|
ensure
|
|
68
|
-
LibDDWAF.
|
|
69
|
-
LibDDWAF.
|
|
78
|
+
LibDDWAF.ddwaf_object_destroy(config_obj, allocator) if config_obj
|
|
79
|
+
LibDDWAF.ddwaf_object_destroy(diagnostics_obj, allocator) if diagnostics_obj
|
|
70
80
|
end
|
|
71
81
|
|
|
72
82
|
# Removes a configuration from the WAF handle builder for the given path.
|
|
@@ -74,12 +84,34 @@ module Datadog
|
|
|
74
84
|
# @return [Boolean] true if the configuration was removed, false otherwise
|
|
75
85
|
def remove_config_at_path(path)
|
|
76
86
|
ensure_pointer_presence!
|
|
87
|
+
raise ArgumentError, "Configuration path is reserved for the obfuscator" if path == OBFUSCATOR_CONFIG_PATH
|
|
77
88
|
|
|
78
|
-
|
|
89
|
+
path_length = path.bytesize
|
|
90
|
+
raise ArgumentError, "Configuration path exceeds the native length limit" if path_length > 0xffff_ffff
|
|
91
|
+
|
|
92
|
+
LibDDWAF.ddwaf_builder_remove_config(@builder_ptr, path, path_length)
|
|
79
93
|
end
|
|
80
94
|
|
|
81
95
|
private
|
|
82
96
|
|
|
97
|
+
def configure_obfuscator(obfuscator)
|
|
98
|
+
settings = {
|
|
99
|
+
key_regex: obfuscator[:key_regex],
|
|
100
|
+
value_regex: obfuscator[:value_regex]
|
|
101
|
+
}.reject { |_, pattern| pattern.nil? }
|
|
102
|
+
|
|
103
|
+
allocator = LibDDWAF.ddwaf_get_default_allocator
|
|
104
|
+
raise LibDDWAFError, "Could not obtain the native allocator" if allocator.null?
|
|
105
|
+
|
|
106
|
+
config_obj = Converter.ruby_to_object({obfuscator: settings}, truncate: false)
|
|
107
|
+
success = LibDDWAF.ddwaf_builder_add_or_update_config(
|
|
108
|
+
@builder_ptr, OBFUSCATOR_CONFIG_PATH, OBFUSCATOR_CONFIG_PATH.bytesize, config_obj, nil
|
|
109
|
+
)
|
|
110
|
+
raise LibDDWAFError, "Could not configure obfuscator" unless success
|
|
111
|
+
ensure
|
|
112
|
+
LibDDWAF.ddwaf_object_destroy(config_obj, allocator) if config_obj
|
|
113
|
+
end
|
|
114
|
+
|
|
83
115
|
def ensure_pointer_presence!
|
|
84
116
|
return if @builder_ptr
|
|
85
117
|
|
|
@@ -9,14 +9,6 @@ module Datadog
|
|
|
9
9
|
# FFI-binding for C-libddwaf
|
|
10
10
|
# See https://github.com/DataDog/libddwaf
|
|
11
11
|
module LibDDWAF
|
|
12
|
-
DEFAULT_MAX_CONTAINER_SIZE = 256
|
|
13
|
-
DEFAULT_MAX_CONTAINER_DEPTH = 20
|
|
14
|
-
DEFAULT_MAX_STRING_LENGTH = 16_384 # in bytes, UTF-8 worst case being 4x size in terms of code point
|
|
15
|
-
|
|
16
|
-
DDWAF_MAX_CONTAINER_SIZE = 256
|
|
17
|
-
DDWAF_MAX_CONTAINER_DEPTH = 20
|
|
18
|
-
DDWAF_MAX_STRING_LENGTH = 4096
|
|
19
|
-
|
|
20
12
|
DDWAF_RUN_TIMEOUT = 5000
|
|
21
13
|
|
|
22
14
|
extend ::FFI::Library
|
|
@@ -82,15 +74,17 @@ module Datadog
|
|
|
82
74
|
|
|
83
75
|
# ddwaf::object data structure
|
|
84
76
|
|
|
85
|
-
DDWAF_OBJ_TYPE = enum :ddwaf_obj_invalid,
|
|
86
|
-
:
|
|
87
|
-
:
|
|
88
|
-
:
|
|
89
|
-
:
|
|
90
|
-
:
|
|
91
|
-
:
|
|
92
|
-
:
|
|
93
|
-
:
|
|
77
|
+
DDWAF_OBJ_TYPE = enum :ddwaf_obj_invalid, 0x00,
|
|
78
|
+
:ddwaf_obj_null, 0x01,
|
|
79
|
+
:ddwaf_obj_bool, 0x02,
|
|
80
|
+
:ddwaf_obj_signed, 0x04,
|
|
81
|
+
:ddwaf_obj_unsigned, 0x06,
|
|
82
|
+
:ddwaf_obj_float, 0x08,
|
|
83
|
+
:ddwaf_obj_string, 0x10,
|
|
84
|
+
:ddwaf_obj_literal_string, 0x12,
|
|
85
|
+
:ddwaf_obj_small_string, 0x14,
|
|
86
|
+
:ddwaf_obj_array, 0x20,
|
|
87
|
+
:ddwaf_obj_map, 0x40
|
|
94
88
|
|
|
95
89
|
typedef DDWAF_OBJ_TYPE, :ddwaf_obj_type
|
|
96
90
|
|
|
@@ -118,24 +112,53 @@ module Datadog
|
|
|
118
112
|
|
|
119
113
|
typedef SizeTPtr.by_ref, :sizeptr
|
|
120
114
|
|
|
121
|
-
|
|
115
|
+
class ObjectBool < ::FFI::Struct
|
|
116
|
+
layout :type, :uint8, :val, :bool
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
class ObjectSigned < ::FFI::Struct
|
|
120
|
+
layout :type, :uint8, :val, :int64
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
class ObjectUnsigned < ::FFI::Struct
|
|
124
|
+
layout :type, :uint8, :val, :uint64
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
class ObjectFloat < ::FFI::Struct
|
|
128
|
+
layout :type, :uint8, :val, :double
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
class ObjectString < ::FFI::Struct
|
|
132
|
+
layout :type, :uint8, :size, :uint32, :ptr, :pointer
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
class ObjectSmallString < ::FFI::Struct
|
|
136
|
+
layout :type, :uint8, :size, :uint8, :data, [:char, 14]
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
class ObjectArray < ::FFI::Struct
|
|
140
|
+
layout :type, :uint8, :size, :uint16, :capacity, :uint16, :ptr, :pointer
|
|
141
|
+
end
|
|
142
|
+
|
|
143
|
+
class ObjectMap < ::FFI::Struct
|
|
144
|
+
layout :type, :uint8, :size, :uint16, :capacity, :uint16, :ptr, :pointer
|
|
145
|
+
end
|
|
146
|
+
|
|
122
147
|
class ObjectValueUnion < ::FFI::Union
|
|
123
|
-
layout :
|
|
124
|
-
:
|
|
125
|
-
:
|
|
126
|
-
:
|
|
127
|
-
:
|
|
128
|
-
:
|
|
148
|
+
layout :b8, ObjectBool,
|
|
149
|
+
:i64, ObjectSigned,
|
|
150
|
+
:u64, ObjectUnsigned,
|
|
151
|
+
:f64, ObjectFloat,
|
|
152
|
+
:str, ObjectString,
|
|
153
|
+
:sstr, ObjectSmallString,
|
|
154
|
+
:array, ObjectArray,
|
|
155
|
+
:map, ObjectMap
|
|
129
156
|
end
|
|
130
157
|
|
|
131
|
-
#
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
:parameterNameLength, :uint64,
|
|
136
|
-
:valueUnion, ObjectValueUnion,
|
|
137
|
-
:nbEntries, :uint64,
|
|
138
|
-
:type, :ddwaf_obj_type
|
|
158
|
+
# See: https://github.com/DataDog/libddwaf/blob/2.0.0/include/ddwaf.h
|
|
159
|
+
class Object < ::FFI::Union
|
|
160
|
+
layout :type, :uint8,
|
|
161
|
+
:via, ObjectValueUnion
|
|
139
162
|
|
|
140
163
|
def truncated?
|
|
141
164
|
@truncated == true
|
|
@@ -146,94 +169,76 @@ module Datadog
|
|
|
146
169
|
end
|
|
147
170
|
end
|
|
148
171
|
|
|
172
|
+
class ObjectKV < ::FFI::Struct
|
|
173
|
+
layout :key, Object,
|
|
174
|
+
:val, Object
|
|
175
|
+
end
|
|
176
|
+
|
|
149
177
|
typedef Object.by_ref, :ddwaf_object
|
|
150
178
|
|
|
179
|
+
# allocators
|
|
180
|
+
|
|
181
|
+
typedef :pointer, :ddwaf_allocator
|
|
182
|
+
|
|
183
|
+
attach_function :ddwaf_get_default_allocator, [], :ddwaf_allocator
|
|
184
|
+
attach_function :ddwaf_allocator_alloc, [:ddwaf_allocator, :size_t, :size_t], :pointer
|
|
185
|
+
attach_function :ddwaf_allocator_free, [:ddwaf_allocator, :pointer, :size_t, :size_t], :void
|
|
186
|
+
|
|
151
187
|
## setters
|
|
152
188
|
|
|
153
|
-
attach_function :
|
|
154
|
-
attach_function :
|
|
155
|
-
attach_function :
|
|
156
|
-
attach_function :
|
|
157
|
-
attach_function :
|
|
158
|
-
attach_function :
|
|
159
|
-
|
|
160
|
-
attach_function :
|
|
161
|
-
|
|
162
|
-
attach_function :
|
|
163
|
-
attach_function :
|
|
164
|
-
attach_function :
|
|
165
|
-
|
|
166
|
-
attach_function :
|
|
167
|
-
attach_function :
|
|
168
|
-
|
|
169
|
-
attach_function :
|
|
170
|
-
attach_function :
|
|
171
|
-
attach_function :
|
|
172
|
-
attach_function :ddwaf_object_map_addl_nc, [:ddwaf_object, :charptr, :size_t, :pointer], :bool
|
|
189
|
+
attach_function :ddwaf_object_set_invalid, [:ddwaf_object], :ddwaf_object
|
|
190
|
+
attach_function :ddwaf_object_set_null, [:ddwaf_object], :ddwaf_object
|
|
191
|
+
attach_function :ddwaf_object_set_bool, [:ddwaf_object, :bool], :ddwaf_object
|
|
192
|
+
attach_function :ddwaf_object_set_signed, [:ddwaf_object, :int64], :ddwaf_object
|
|
193
|
+
attach_function :ddwaf_object_set_unsigned, [:ddwaf_object, :uint64], :ddwaf_object
|
|
194
|
+
attach_function :ddwaf_object_set_float, [:ddwaf_object, :double], :ddwaf_object
|
|
195
|
+
|
|
196
|
+
attach_function :ddwaf_object_from_json, [:ddwaf_object, :charptr, :uint32, :ddwaf_allocator], :bool
|
|
197
|
+
|
|
198
|
+
attach_function :ddwaf_object_set_string, [:ddwaf_object, :charptr, :uint32, :ddwaf_allocator], :ddwaf_object
|
|
199
|
+
attach_function :ddwaf_object_set_string_literal, [:ddwaf_object, :charptr, :uint32], :ddwaf_object
|
|
200
|
+
attach_function :ddwaf_object_set_string_nocopy, [:ddwaf_object, :charptr, :uint32], :ddwaf_object
|
|
201
|
+
|
|
202
|
+
attach_function :ddwaf_object_set_array, [:ddwaf_object, :uint16, :ddwaf_allocator], :ddwaf_object
|
|
203
|
+
attach_function :ddwaf_object_set_map, [:ddwaf_object, :uint16, :ddwaf_allocator], :ddwaf_object
|
|
204
|
+
|
|
205
|
+
attach_function :ddwaf_object_insert, [:ddwaf_object, :ddwaf_allocator], :ddwaf_object
|
|
206
|
+
attach_function :ddwaf_object_insert_key, [:ddwaf_object, :charptr, :uint32, :ddwaf_allocator], :ddwaf_object
|
|
207
|
+
attach_function :ddwaf_object_insert_key_nocopy, [:ddwaf_object, :charptr, :uint32, :ddwaf_allocator], :ddwaf_object
|
|
173
208
|
|
|
174
209
|
## getters
|
|
175
210
|
|
|
176
|
-
attach_function :
|
|
177
|
-
attach_function :
|
|
178
|
-
attach_function :
|
|
179
|
-
attach_function :ddwaf_object_get_key, [:ddwaf_object, :sizeptr], :charptr
|
|
211
|
+
attach_function :ddwaf_object_get_type, [:ddwaf_object], DDWAF_OBJ_TYPE
|
|
212
|
+
attach_function :ddwaf_object_get_size, [:ddwaf_object], :size_t
|
|
213
|
+
attach_function :ddwaf_object_get_length, [:ddwaf_object], :size_t
|
|
180
214
|
attach_function :ddwaf_object_get_string, [:ddwaf_object, :sizeptr], :charptr
|
|
181
215
|
attach_function :ddwaf_object_get_unsigned, [:ddwaf_object], :uint64
|
|
182
216
|
attach_function :ddwaf_object_get_signed, [:ddwaf_object], :int64
|
|
183
|
-
attach_function :ddwaf_object_get_index, [:ddwaf_object, :size_t], :ddwaf_object
|
|
184
217
|
attach_function :ddwaf_object_get_bool, [:ddwaf_object], :bool
|
|
185
218
|
attach_function :ddwaf_object_get_float, [:ddwaf_object], :double
|
|
186
219
|
|
|
187
|
-
|
|
220
|
+
attach_function :ddwaf_object_at_key, [:ddwaf_object, :size_t], :ddwaf_object
|
|
221
|
+
attach_function :ddwaf_object_at_value, [:ddwaf_object, :size_t], :ddwaf_object
|
|
188
222
|
|
|
189
|
-
|
|
190
|
-
|
|
223
|
+
## destruction
|
|
224
|
+
|
|
225
|
+
attach_function :ddwaf_object_destroy, [:ddwaf_object, :ddwaf_allocator], :void
|
|
191
226
|
|
|
192
227
|
# handle builder
|
|
193
228
|
|
|
194
229
|
typedef :pointer, :ddwaf_builder
|
|
195
230
|
typedef :pointer, :ddwaf_handle
|
|
196
|
-
typedef :pointer, :ddwaf_diagnostics
|
|
197
|
-
|
|
198
|
-
callback :ddwaf_object_free_fn, [:ddwaf_object], :void
|
|
199
|
-
|
|
200
|
-
# Ruby representation of ddwaf_config
|
|
201
|
-
# https://github.com/DataDog/libddwaf/blob/10e3a1dfc7bc9bb8ab11a09a9f8b6b339eaf3271/include/ddwaf.h#L129-L152
|
|
202
|
-
class HandleBuilderConfig < ::FFI::Struct
|
|
203
|
-
# Ruby representation of ddwaf_config_limits
|
|
204
|
-
# https://github.com/DataDog/libddwaf/blob/10e3a1dfc7bc9bb8ab11a09a9f8b6b339eaf3271/include/ddwaf.h#L131-L138
|
|
205
|
-
class Limits < ::FFI::Struct
|
|
206
|
-
layout :max_container_size, :uint32,
|
|
207
|
-
:max_container_depth, :uint32,
|
|
208
|
-
:max_string_length, :uint32
|
|
209
|
-
end
|
|
210
231
|
|
|
211
|
-
|
|
212
|
-
# https://github.com/DataDog/libddwaf/blob/10e3a1dfc7bc9bb8ab11a09a9f8b6b339eaf3271/include/ddwaf.h#L141-L146
|
|
213
|
-
class Obfuscator < ::FFI::Struct
|
|
214
|
-
layout :key_regex, :pointer, # should be :charptr
|
|
215
|
-
:value_regex, :pointer # should be :charptr
|
|
216
|
-
end
|
|
217
|
-
|
|
218
|
-
layout :limits, Limits,
|
|
219
|
-
:obfuscator, Obfuscator,
|
|
220
|
-
:free_fn, :pointer # :ddwaf_object_free_fn
|
|
221
|
-
end
|
|
222
|
-
|
|
223
|
-
typedef HandleBuilderConfig.by_ref, :ddwaf_config
|
|
224
|
-
|
|
225
|
-
attach_function :ddwaf_builder_init, [:ddwaf_config], :ddwaf_builder
|
|
232
|
+
attach_function :ddwaf_builder_init, [], :ddwaf_builder
|
|
226
233
|
attach_function :ddwaf_builder_destroy, [:ddwaf_builder], :void
|
|
227
234
|
|
|
228
|
-
attach_function :ddwaf_builder_add_or_update_config, [:ddwaf_builder, :string, :
|
|
229
|
-
attach_function :ddwaf_builder_remove_config, [:ddwaf_builder, :string, :
|
|
235
|
+
attach_function :ddwaf_builder_add_or_update_config, [:ddwaf_builder, :string, :uint32, :ddwaf_object, :ddwaf_object], :bool
|
|
236
|
+
attach_function :ddwaf_builder_remove_config, [:ddwaf_builder, :string, :uint32], :bool
|
|
230
237
|
|
|
231
238
|
attach_function :ddwaf_builder_build_instance, [:ddwaf_builder], :ddwaf_handle
|
|
232
239
|
|
|
233
240
|
# handle
|
|
234
241
|
|
|
235
|
-
callback :ddwaf_object_free_fn, [:ddwaf_object], :void
|
|
236
|
-
|
|
237
242
|
attach_function :ddwaf_destroy, [:ddwaf_handle], :void
|
|
238
243
|
|
|
239
244
|
attach_function :ddwaf_known_addresses, [:ddwaf_handle, UInt32Ptr], :charptrptr
|
|
@@ -247,16 +252,23 @@ module Datadog
|
|
|
247
252
|
:ddwaf_match, 1
|
|
248
253
|
typedef DDWAF_RET_CODE, :ddwaf_ret_code
|
|
249
254
|
|
|
250
|
-
#
|
|
255
|
+
# evaluation
|
|
251
256
|
|
|
252
257
|
typedef :pointer, :ddwaf_context
|
|
258
|
+
typedef :pointer, :ddwaf_subcontext
|
|
259
|
+
typedef :uint64, :timeout_us
|
|
253
260
|
|
|
254
|
-
attach_function :ddwaf_context_init, [:ddwaf_handle], :ddwaf_context
|
|
261
|
+
attach_function :ddwaf_context_init, [:ddwaf_handle, :ddwaf_allocator], :ddwaf_context
|
|
255
262
|
attach_function :ddwaf_context_destroy, [:ddwaf_context], :void
|
|
256
263
|
|
|
257
|
-
|
|
264
|
+
attach_function :ddwaf_context_eval,
|
|
265
|
+
[:ddwaf_context, :ddwaf_object, :ddwaf_allocator, :ddwaf_object, :timeout_us], :ddwaf_ret_code, blocking: true
|
|
266
|
+
|
|
267
|
+
attach_function :ddwaf_subcontext_init, [:ddwaf_context], :ddwaf_subcontext
|
|
268
|
+
attach_function :ddwaf_subcontext_destroy, [:ddwaf_subcontext], :void
|
|
258
269
|
|
|
259
|
-
attach_function :
|
|
270
|
+
attach_function :ddwaf_subcontext_eval,
|
|
271
|
+
[:ddwaf_subcontext, :ddwaf_object, :ddwaf_allocator, :ddwaf_object, :timeout_us], :ddwaf_ret_code, blocking: true
|
|
260
272
|
|
|
261
273
|
# logging
|
|
262
274
|
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Datadog
|
|
4
|
+
module AppSec
|
|
5
|
+
module WAF
|
|
6
|
+
class Subcontext
|
|
7
|
+
def initialize(subcontext_ptr, context:)
|
|
8
|
+
@subcontext_ptr = subcontext_ptr
|
|
9
|
+
@context = context
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def finalize!
|
|
13
|
+
Thread.handle_interrupt(::Object => :never) do
|
|
14
|
+
pointer = @subcontext_ptr
|
|
15
|
+
return unless pointer
|
|
16
|
+
|
|
17
|
+
@subcontext_ptr = nil
|
|
18
|
+
LibDDWAF.ddwaf_subcontext_destroy(pointer)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def eval(data, timeout = LibDDWAF::DDWAF_RUN_TIMEOUT)
|
|
23
|
+
ensure_pointer_presence!
|
|
24
|
+
|
|
25
|
+
Evaluation.call(data) do |input_obj, allocator, result_obj|
|
|
26
|
+
LibDDWAF.ddwaf_subcontext_eval(
|
|
27
|
+
@subcontext_ptr, #: ::FFI::Pointer
|
|
28
|
+
input_obj, allocator, result_obj, timeout
|
|
29
|
+
)
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
def ensure_pointer_presence!
|
|
36
|
+
return if @subcontext_ptr
|
|
37
|
+
|
|
38
|
+
raise InstanceFinalizedError, "Cannot use WAF subcontext after it has been finalized"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -2,10 +2,10 @@ module Datadog
|
|
|
2
2
|
module AppSec
|
|
3
3
|
module WAF
|
|
4
4
|
module VERSION
|
|
5
|
-
BASE_STRING = "
|
|
5
|
+
BASE_STRING = "2.0.1"
|
|
6
6
|
# NOTE: Every change to the `BASE_STRING` should be accompanied
|
|
7
7
|
# by a reset of the patch version in the `STRING` below.
|
|
8
|
-
STRING = "#{BASE_STRING}.1
|
|
8
|
+
STRING = "#{BASE_STRING}.0.1"
|
|
9
9
|
MINIMUM_RUBY_VERSION = "2.5"
|
|
10
10
|
end
|
|
11
11
|
end
|
data/lib/datadog/appsec/waf.rb
CHANGED
|
@@ -2,12 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
require "datadog/appsec/waf/lib_ddwaf"
|
|
4
4
|
|
|
5
|
+
require "datadog/appsec/waf/configuration_result"
|
|
5
6
|
require "datadog/appsec/waf/handle_builder"
|
|
6
7
|
require "datadog/appsec/waf/handle"
|
|
7
8
|
require "datadog/appsec/waf/converter"
|
|
8
9
|
require "datadog/appsec/waf/errors"
|
|
9
10
|
require "datadog/appsec/waf/result"
|
|
11
|
+
require "datadog/appsec/waf/evaluation"
|
|
10
12
|
require "datadog/appsec/waf/context"
|
|
13
|
+
require "datadog/appsec/waf/subcontext"
|
|
11
14
|
require "datadog/appsec/waf/version"
|
|
12
15
|
|
|
13
16
|
module Datadog
|
|
@@ -24,9 +27,10 @@ module Datadog
|
|
|
24
27
|
end
|
|
25
28
|
|
|
26
29
|
def log_callback(level, func, file, line, message, len)
|
|
27
|
-
|
|
30
|
+
logger = WAF.logger
|
|
31
|
+
return if logger.nil?
|
|
28
32
|
|
|
29
|
-
|
|
33
|
+
logger.debug do
|
|
30
34
|
{
|
|
31
35
|
level: level,
|
|
32
36
|
func: func,
|
|
@@ -35,6 +39,9 @@ module Datadog
|
|
|
35
39
|
message: message.read_bytes(len)
|
|
36
40
|
}.inspect
|
|
37
41
|
end
|
|
42
|
+
rescue Exception # standard:disable Lint/RescueException
|
|
43
|
+
# Exceptions must not escape a native callback after input ownership transfers.
|
|
44
|
+
nil
|
|
38
45
|
end
|
|
39
46
|
|
|
40
47
|
def logger
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module Datadog
|
|
2
|
+
module AppSec
|
|
3
|
+
module WAF
|
|
4
|
+
class ConfigurationResult
|
|
5
|
+
@success: bool
|
|
6
|
+
|
|
7
|
+
@diagnostics: WAF::opaque
|
|
8
|
+
|
|
9
|
+
attr_reader diagnostics: WAF::opaque
|
|
10
|
+
|
|
11
|
+
def initialize: (success: bool, diagnostics: WAF::opaque) -> void
|
|
12
|
+
|
|
13
|
+
def success?: () -> bool
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -2,38 +2,19 @@ module Datadog
|
|
|
2
2
|
module AppSec
|
|
3
3
|
module WAF
|
|
4
4
|
class Context
|
|
5
|
-
@context_ptr: ::FFI::Pointer
|
|
6
|
-
|
|
7
|
-
@retained: Array[untyped]
|
|
8
|
-
|
|
9
|
-
EMPTY_RESULT: {
|
|
10
|
-
"events" => WAF::events,
|
|
11
|
-
"actions" => WAF::actions,
|
|
12
|
-
"attributes" => WAF::attributes,
|
|
13
|
-
"duration" => ::Integer,
|
|
14
|
-
"timeout" => bool,
|
|
15
|
-
"keep" => bool
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
SUCCESS_RESULT_CODES: ::Array[::Symbol]
|
|
19
|
-
|
|
20
|
-
RESULT_CODE_TO_STATUS: ::Hash[::Symbol, ::Symbol]
|
|
5
|
+
@context_ptr: ::FFI::Pointer?
|
|
21
6
|
|
|
22
7
|
def initialize: (::FFI::Pointer context_ptr) -> void
|
|
23
8
|
|
|
24
9
|
def finalize!: () -> void
|
|
25
10
|
|
|
26
|
-
def
|
|
11
|
+
def build_subcontext: [T] () { (Subcontext) -> T } -> T
|
|
12
|
+
|
|
13
|
+
def eval: (::Hash[WAF::input, WAF::input] data, ?::Integer timeout) -> Result
|
|
27
14
|
|
|
28
15
|
private
|
|
29
16
|
|
|
30
17
|
def ensure_pointer_presence!: () -> void
|
|
31
|
-
|
|
32
|
-
def retained: () -> Array[untyped]
|
|
33
|
-
|
|
34
|
-
def retain: (top object) -> void
|
|
35
|
-
|
|
36
|
-
def release: (top object) -> void
|
|
37
18
|
end
|
|
38
19
|
end
|
|
39
20
|
end
|