contrast-agent 3.13.2 → 4.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/exe/contrast_service +1 -7
- data/ext/cs__assess_active_record_named/cs__active_record_named.c +8 -7
- data/ext/cs__assess_array/cs__assess_array.c +6 -5
- data/ext/cs__assess_basic_object/cs__assess_basic_object.c +5 -5
- data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +2 -1
- data/ext/cs__assess_hash/cs__assess_hash.c +18 -17
- data/ext/cs__assess_hash/cs__assess_hash.h +2 -1
- data/ext/cs__assess_kernel/cs__assess_kernel.c +7 -8
- data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +18 -16
- data/ext/cs__assess_marshal_module/cs__assess_marshal_module.h +1 -0
- data/ext/cs__assess_module/cs__assess_module.c +6 -6
- data/ext/cs__assess_regexp/cs__assess_regexp.c +4 -4
- data/ext/cs__assess_string/cs__assess_string.c +31 -16
- data/ext/cs__assess_string/cs__assess_string.h +6 -1
- data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +4 -2
- data/ext/cs__assess_yield_track/cs__assess_yield_track.c +2 -2
- data/ext/cs__common/cs__common.c +48 -39
- data/ext/cs__common/cs__common.h +16 -21
- data/ext/cs__contrast_patch/cs__contrast_patch.c +27 -25
- data/ext/cs__contrast_patch/cs__contrast_patch.h +5 -7
- data/ext/cs__protect_kernel/cs__protect_kernel.c +11 -12
- data/ext/cs__protect_kernel/cs__protect_kernel.h +2 -2
- data/lib/contrast-agent.rb +1 -1
- data/lib/contrast.rb +13 -23
- data/lib/contrast/agent.rb +39 -47
- data/lib/contrast/agent/assess.rb +12 -12
- data/lib/contrast/agent/assess/contrast_event.rb +151 -85
- data/lib/contrast/agent/assess/events/event_factory.rb +2 -2
- data/lib/contrast/agent/assess/events/source_event.rb +3 -3
- data/lib/contrast/agent/assess/finalizers/freeze.rb +15 -0
- data/lib/contrast/agent/assess/finalizers/hash.rb +97 -0
- data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +11 -4
- data/lib/contrast/agent/assess/policy/patcher.rb +6 -6
- data/lib/contrast/agent/assess/policy/policy.rb +9 -11
- data/lib/contrast/agent/assess/policy/policy_node.rb +17 -12
- data/lib/contrast/agent/assess/policy/policy_scanner.rb +21 -5
- data/lib/contrast/agent/assess/policy/preshift.rb +13 -7
- data/lib/contrast/agent/assess/policy/propagation_method.rb +64 -44
- data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -2
- data/lib/contrast/agent/assess/policy/propagator.rb +18 -18
- data/lib/contrast/agent/assess/policy/propagator/append.rb +8 -5
- data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
- data/lib/contrast/agent/assess/policy/propagator/center.rb +9 -5
- data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
- data/lib/contrast/agent/assess/policy/propagator/database_write.rb +6 -4
- data/lib/contrast/agent/assess/policy/propagator/insert.rb +7 -4
- data/lib/contrast/agent/assess/policy/propagator/keep.rb +4 -1
- data/lib/contrast/agent/assess/policy/propagator/match_data.rb +7 -9
- data/lib/contrast/agent/assess/policy/propagator/next.rb +7 -5
- data/lib/contrast/agent/assess/policy/propagator/prepend.rb +13 -5
- data/lib/contrast/agent/assess/policy/propagator/remove.rb +8 -4
- data/lib/contrast/agent/assess/policy/propagator/replace.rb +5 -2
- data/lib/contrast/agent/assess/policy/propagator/reverse.rb +7 -5
- data/lib/contrast/agent/assess/policy/propagator/select.rb +13 -7
- data/lib/contrast/agent/assess/policy/propagator/splat.rb +10 -9
- data/lib/contrast/agent/assess/policy/propagator/split.rb +27 -22
- data/lib/contrast/agent/assess/policy/propagator/substitution.rb +52 -35
- data/lib/contrast/agent/assess/policy/propagator/trim.rb +11 -5
- data/lib/contrast/agent/assess/policy/rewriter_patch.rb +5 -5
- data/lib/contrast/agent/assess/policy/source_method.rb +90 -72
- data/lib/contrast/agent/assess/policy/source_validation/cross_site_validator.rb +1 -1
- data/lib/contrast/agent/assess/policy/source_validation/source_validation.rb +1 -1
- data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +16 -12
- data/lib/contrast/agent/assess/policy/trigger/xpath.rb +2 -2
- data/lib/contrast/agent/assess/policy/trigger_method.rb +81 -33
- data/lib/contrast/agent/assess/policy/trigger_node.rb +41 -46
- data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -1
- data/lib/contrast/agent/assess/policy/trigger_validation/trigger_validation.rb +2 -2
- data/lib/contrast/agent/assess/properties.rb +15 -5
- data/lib/contrast/agent/assess/property/evented.rb +7 -20
- data/lib/contrast/agent/assess/property/tagged.rb +13 -7
- data/lib/contrast/agent/assess/property/updated.rb +131 -0
- data/lib/contrast/agent/assess/rule.rb +2 -2
- data/lib/contrast/agent/assess/rule/base.rb +3 -4
- data/lib/contrast/agent/assess/rule/provider.rb +3 -3
- data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +58 -5
- data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +24 -9
- data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +85 -16
- data/lib/contrast/agent/assess/tag.rb +1 -1
- data/lib/contrast/agent/assess/tracker.rb +66 -0
- data/lib/contrast/agent/at_exit_hook.rb +6 -6
- data/lib/contrast/agent/class_reopener.rb +14 -11
- data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +1 -1
- data/lib/contrast/agent/deadzone/policy/policy.rb +2 -2
- data/lib/contrast/agent/disable_reaction.rb +1 -1
- data/lib/contrast/agent/exclusion_matcher.rb +1 -1
- data/lib/contrast/agent/inventory.rb +15 -0
- data/lib/contrast/agent/inventory/dependencies.rb +50 -0
- data/lib/contrast/agent/inventory/dependency_analysis.rb +37 -0
- data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +104 -0
- data/lib/contrast/agent/inventory/gemfile_digest_cache.rb +38 -0
- data/lib/contrast/agent/inventory/policy/datastores.rb +2 -2
- data/lib/contrast/agent/inventory/policy/policy.rb +3 -3
- data/lib/contrast/agent/inventory/policy/trigger_node.rb +1 -1
- data/lib/contrast/agent/middleware.rb +33 -34
- data/lib/contrast/agent/patching/policy/after_load_patch.rb +9 -9
- data/lib/contrast/agent/patching/policy/after_load_patcher.rb +23 -22
- data/lib/contrast/agent/patching/policy/module_policy.rb +11 -11
- data/lib/contrast/agent/patching/policy/patch.rb +15 -15
- data/lib/contrast/agent/patching/policy/patcher.rb +43 -44
- data/lib/contrast/agent/patching/policy/policy.rb +23 -12
- data/lib/contrast/agent/patching/policy/policy_node.rb +1 -1
- data/lib/contrast/agent/patching/policy/trigger_node.rb +2 -2
- data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +6 -8
- data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +2 -2
- data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -2
- data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +7 -6
- data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +2 -2
- data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +4 -4
- data/lib/contrast/agent/protect/policy/policy.rb +8 -8
- data/lib/contrast/agent/protect/policy/rule_applicator.rb +1 -1
- data/lib/contrast/agent/protect/policy/trigger_node.rb +1 -1
- data/lib/contrast/agent/protect/rule.rb +18 -18
- data/lib/contrast/agent/protect/rule/base.rb +4 -3
- data/lib/contrast/agent/protect/rule/base_service.rb +1 -1
- data/lib/contrast/agent/protect/rule/cmd_injection.rb +5 -5
- data/lib/contrast/agent/protect/rule/deserialization.rb +1 -1
- data/lib/contrast/agent/protect/rule/http_method_tampering.rb +1 -1
- data/lib/contrast/agent/protect/rule/no_sqli.rb +1 -1
- data/lib/contrast/agent/protect/rule/no_sqli/mongo_no_sql_scanner.rb +1 -0
- data/lib/contrast/agent/protect/rule/path_traversal.rb +4 -5
- data/lib/contrast/agent/protect/rule/sqli.rb +2 -2
- data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +1 -1
- data/lib/contrast/agent/protect/rule/xss.rb +1 -1
- data/lib/contrast/agent/protect/rule/xxe.rb +3 -5
- data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +2 -2
- data/lib/contrast/agent/railtie.rb +1 -1
- data/lib/contrast/agent/reaction_processor.rb +2 -2
- data/lib/contrast/agent/request.rb +45 -43
- data/lib/contrast/agent/request_context.rb +10 -6
- data/lib/contrast/agent/request_handler.rb +1 -1
- data/lib/contrast/agent/response.rb +23 -12
- data/lib/contrast/agent/rewriter.rb +6 -9
- data/lib/contrast/agent/service_heartbeat.rb +2 -2
- data/lib/contrast/agent/static_analysis.rb +9 -9
- data/lib/contrast/agent/thread.rb +1 -1
- data/lib/contrast/agent/thread_watcher.rb +2 -2
- data/lib/contrast/agent/tracepoint_hook.rb +2 -2
- data/lib/contrast/agent/version.rb +1 -1
- data/lib/contrast/api.rb +4 -4
- data/lib/contrast/api/communication.rb +9 -9
- data/lib/contrast/api/communication/messaging_queue.rb +3 -6
- data/lib/contrast/api/communication/response_processor.rb +1 -1
- data/lib/contrast/api/communication/socket_client.rb +41 -6
- data/lib/contrast/api/communication/speedracer.rb +1 -1
- data/lib/contrast/api/communication/tcp_socket.rb +1 -1
- data/lib/contrast/api/communication/unix_socket.rb +1 -1
- data/lib/contrast/api/decorators.rb +17 -14
- data/lib/contrast/api/decorators/address.rb +20 -20
- data/lib/contrast/api/decorators/application_settings.rb +3 -2
- data/lib/contrast/api/decorators/application_update.rb +7 -8
- data/lib/contrast/api/decorators/http_request.rb +13 -12
- data/lib/contrast/api/decorators/input_analysis.rb +3 -2
- data/lib/contrast/api/decorators/library.rb +53 -0
- data/lib/contrast/api/decorators/library_usage_update.rb +30 -0
- data/lib/contrast/api/decorators/message.rb +4 -2
- data/lib/contrast/api/decorators/rasp_rule_sample.rb +2 -1
- data/lib/contrast/api/decorators/route_coverage.rb +3 -2
- data/lib/contrast/api/decorators/server_features.rb +3 -2
- data/lib/contrast/api/decorators/trace_event.rb +28 -25
- data/lib/contrast/api/decorators/trace_event_object.rb +6 -5
- data/lib/contrast/api/decorators/trace_event_signature.rb +5 -4
- data/lib/contrast/api/decorators/trace_taint_range.rb +4 -3
- data/lib/contrast/api/decorators/user_input.rb +4 -4
- data/lib/contrast/common_agent_configuration.rb +2 -2
- data/lib/contrast/components/agent.rb +8 -7
- data/lib/contrast/components/app_context.rb +50 -39
- data/lib/contrast/components/config.rb +32 -50
- data/lib/contrast/components/contrast_service.rb +10 -10
- data/lib/contrast/components/interface.rb +39 -17
- data/lib/contrast/components/inventory.rb +6 -1
- data/lib/contrast/components/logger.rb +1 -1
- data/lib/contrast/components/scope.rb +3 -3
- data/lib/contrast/components/settings.rb +20 -23
- data/lib/contrast/config.rb +18 -18
- data/lib/contrast/config/application_configuration.rb +5 -2
- data/lib/contrast/config/base_configuration.rb +2 -2
- data/lib/contrast/config/inventory_configuration.rb +2 -2
- data/lib/contrast/config/protect_rule_configuration.rb +1 -1
- data/lib/contrast/config/service_configuration.rb +8 -0
- data/lib/contrast/configuration.rb +93 -52
- data/lib/contrast/extension/assess.rb +21 -22
- data/lib/contrast/extension/assess/array.rb +18 -11
- data/lib/contrast/extension/assess/erb.rb +11 -3
- data/lib/contrast/extension/assess/eval_trigger.rb +7 -7
- data/lib/contrast/extension/assess/exec_trigger.rb +2 -2
- data/lib/contrast/extension/assess/fiber.rb +14 -14
- data/lib/contrast/extension/assess/hash.rb +7 -6
- data/lib/contrast/extension/assess/kernel.rb +34 -28
- data/lib/contrast/extension/assess/marshal.rb +67 -0
- data/lib/contrast/extension/assess/regexp.rb +10 -9
- data/lib/contrast/extension/assess/string.rb +23 -23
- data/lib/contrast/extension/inventory.rb +4 -4
- data/lib/contrast/extension/kernel.rb +1 -1
- data/lib/contrast/extension/module.rb +1 -1
- data/lib/contrast/extension/protect.rb +3 -3
- data/lib/contrast/extension/protect/kernel.rb +4 -4
- data/lib/contrast/extension/protect/psych.rb +2 -2
- data/lib/contrast/framework/base_support.rb +1 -1
- data/lib/contrast/framework/manager.rb +10 -11
- data/lib/contrast/framework/rack/patch/session_cookie.rb +23 -29
- data/lib/contrast/framework/rack/patch/support.rb +1 -1
- data/lib/contrast/framework/rack/support.rb +2 -2
- data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +13 -13
- data/lib/contrast/framework/rails/patch/assess_configuration.rb +7 -13
- data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +11 -11
- data/lib/contrast/framework/rails/patch/support.rb +4 -4
- data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +11 -11
- data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +12 -12
- data/lib/contrast/framework/rails/rewrite/active_record_named.rb +4 -4
- data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +12 -12
- data/lib/contrast/framework/rails/support.rb +67 -14
- data/lib/contrast/framework/sinatra/patch/base.rb +12 -12
- data/lib/contrast/framework/sinatra/patch/support.rb +1 -1
- data/lib/contrast/framework/sinatra/support.rb +6 -6
- data/lib/contrast/funchook/funchook.rb +1 -1
- data/lib/contrast/logger/application.rb +13 -5
- data/lib/contrast/logger/format.rb +22 -9
- data/lib/contrast/logger/log.rb +17 -10
- data/lib/contrast/logger/request.rb +30 -0
- data/lib/contrast/tasks/config.rb +1 -1
- data/lib/contrast/tasks/service.rb +2 -2
- data/lib/contrast/utils/assess/sampling_util.rb +2 -2
- data/lib/contrast/utils/assess/tracking_util.rb +49 -4
- data/lib/contrast/utils/class_util.rb +2 -2
- data/lib/contrast/utils/duck_utils.rb +0 -10
- data/lib/contrast/utils/env_configuration_item.rb +2 -1
- data/lib/contrast/utils/hash_digest.rb +2 -1
- data/lib/contrast/utils/heap_dump_util.rb +2 -2
- data/lib/contrast/utils/invalid_configuration_util.rb +21 -22
- data/lib/contrast/utils/inventory_util.rb +3 -10
- data/lib/contrast/utils/io_util.rb +1 -1
- data/lib/contrast/utils/os.rb +1 -1
- data/lib/contrast/utils/ruby_ast_rewriter.rb +1 -1
- data/lib/contrast/utils/sha256_builder.rb +2 -14
- data/lib/contrast/utils/stack_trace_utils.rb +2 -2
- data/lib/contrast/utils/string_utils.rb +11 -6
- data/resources/assess/policy.json +31 -22
- data/ruby-agent.gemspec +21 -19
- data/service_executables/VERSION +1 -1
- data/service_executables/linux/contrast-service +0 -0
- data/service_executables/mac/contrast-service +0 -0
- metadata +73 -30
- data/lib/contrast/agent/assess/insulator.rb +0 -49
- data/lib/contrast/agent/require_state.rb +0 -61
- data/lib/contrast/extension/assess/assess_extension.rb +0 -147
- data/lib/contrast/utils/boolean_util.rb +0 -30
- data/lib/contrast/utils/freeze_util.rb +0 -32
- data/lib/contrast/utils/gemfile_reader.rb +0 -193
@@ -14,13 +14,15 @@ static VALUE rb_str_concat_literals_hook(size_t num, VALUE *strary) {
|
|
14
14
|
|
15
15
|
static int install_hooks() {
|
16
16
|
rb_str_concat_literals_original = rb_str_concat_literals;
|
17
|
-
patch_via_funchook(&rb_str_concat_literals_original,
|
17
|
+
patch_via_funchook(&rb_str_concat_literals_original,
|
18
|
+
&rb_str_concat_literals_hook);
|
18
19
|
|
19
20
|
return 0;
|
20
21
|
}
|
21
22
|
|
22
23
|
void Init_cs__assess_string_interpolation26(void) {
|
23
|
-
string_propagator =
|
24
|
+
string_propagator =
|
25
|
+
rb_define_class_under(core_assess, "StringPropagator", rb_cObject);
|
24
26
|
track_interpolation = rb_intern("track_interpolation");
|
25
27
|
install_hooks();
|
26
28
|
}
|
@@ -8,7 +8,7 @@
|
|
8
8
|
static VALUE rb_yield_hook(VALUE val, const VALUE self) {
|
9
9
|
VALUE method = rb_funcall(rb_mKernel, rb_sym_method, 0);
|
10
10
|
|
11
|
-
if(method == split_method && RB_TYPE_P(val, T_STRING)) {
|
11
|
+
if (method == split_method && RB_TYPE_P(val, T_STRING)) {
|
12
12
|
rb_funcall(split_class, propagate_yield, 1, val);
|
13
13
|
}
|
14
14
|
VALUE result = rb_yield_original(val);
|
@@ -25,6 +25,6 @@ void Init_cs__assess_yield_track(void) {
|
|
25
25
|
VALUE base = rb_define_class_under(assess_propagator, "Base", rb_cObject);
|
26
26
|
split_class = rb_define_class_under(assess_propagator, "Split", base);
|
27
27
|
propagate_yield = rb_intern("propagate_yield");
|
28
|
-
split_method =
|
28
|
+
split_method = ID2SYM(rb_intern("split"));
|
29
29
|
install_yield_hooks();
|
30
30
|
}
|
data/ext/cs__common/cs__common.c
CHANGED
@@ -2,8 +2,8 @@
|
|
2
2
|
* https://www.contrastsecurity.com/enduser-terms-0317a for more details. */
|
3
3
|
|
4
4
|
#include "cs__common.h"
|
5
|
-
#include <ruby.h>
|
6
5
|
#include <dlfcn.h>
|
6
|
+
#include <ruby.h>
|
7
7
|
|
8
8
|
/* Globals */
|
9
9
|
/* These are defined w/ `extern` in the header */
|
@@ -18,7 +18,7 @@ VALUE rb_sym_in_scope;
|
|
18
18
|
VALUE rb_sym_skip_contrast_analysis;
|
19
19
|
VALUE rb_sym_skip_assess_analysis;
|
20
20
|
VALUE rb_sym_method;
|
21
|
-
VALUE
|
21
|
+
VALUE rb_sym_hash_get, rb_sym_hash_set, rb_sym_hash_tracked;
|
22
22
|
/* end globals */
|
23
23
|
|
24
24
|
void patch_via_funchook(void *original_function, void *hook_function) {
|
@@ -27,19 +27,24 @@ void patch_via_funchook(void *original_function, void *hook_function) {
|
|
27
27
|
|
28
28
|
void *funchook_lib_handle;
|
29
29
|
void *funchook_reference, *(*funchook_create)(void);
|
30
|
-
int prepareResult, (*funchook_prepare)(void*, void**, void*);
|
31
|
-
int installResult, (*funchook_install)(void*, int);
|
30
|
+
int prepareResult, (*funchook_prepare)(void *, void **, void *);
|
31
|
+
int installResult, (*funchook_install)(void *, int);
|
32
32
|
|
33
|
-
funchook_lib_handle =
|
33
|
+
funchook_lib_handle =
|
34
|
+
dlopen(StringValueCStr(funchook_path), RTLD_NOW | RTLD_GLOBAL);
|
34
35
|
|
35
36
|
/* Load the funchook methods we need */
|
36
|
-
funchook_create
|
37
|
-
|
38
|
-
|
37
|
+
funchook_create =
|
38
|
+
(void *(*)(void))dlsym(funchook_lib_handle, "funchook_create");
|
39
|
+
funchook_prepare = (int (*)(void *, void **, void *))dlsym(
|
40
|
+
funchook_lib_handle, "funchook_prepare");
|
41
|
+
funchook_install =
|
42
|
+
(int (*)(void *, int))dlsym(funchook_lib_handle, "funchook_install");
|
39
43
|
|
40
|
-
funchook_reference = (void*)(*funchook_create)();
|
44
|
+
funchook_reference = (void *)(*funchook_create)();
|
41
45
|
|
42
|
-
prepareResult = (*funchook_prepare)(
|
46
|
+
prepareResult = (*funchook_prepare)(
|
47
|
+
funchook_reference, (void **)original_function, hook_function);
|
43
48
|
installResult = (*funchook_install)(funchook_reference, 0);
|
44
49
|
}
|
45
50
|
|
@@ -53,33 +58,32 @@ VALUE contrast_patcher() {
|
|
53
58
|
return patcher;
|
54
59
|
}
|
55
60
|
|
56
|
-
VALUE contrast_register_patch(const char *module_name,
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
+
VALUE contrast_register_patch(const char *module_name, const char *method_name,
|
62
|
+
VALUE(c_fn)(const int, const VALUE *,
|
63
|
+
const VALUE)) {
|
64
|
+
return _contrast_register_patch(module_name, method_name, c_fn,
|
65
|
+
IMPL_ALIAS_INSTANCE);
|
61
66
|
}
|
62
67
|
|
63
|
-
|
64
68
|
VALUE contrast_register_singleton_patch(const char *module_name,
|
65
69
|
const char *method_name,
|
66
|
-
VALUE(c_fn)(const int, const VALUE*,
|
67
|
-
|
68
|
-
|
70
|
+
VALUE(c_fn)(const int, const VALUE *,
|
71
|
+
const VALUE)) {
|
72
|
+
return _contrast_register_patch(module_name, method_name, c_fn,
|
73
|
+
IMPL_ALIAS_SINGLETON);
|
69
74
|
}
|
70
75
|
|
71
|
-
VALUE contrast_register_singleton_prepend_patch(
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
+
VALUE contrast_register_singleton_prepend_patch(
|
77
|
+
const char *module_name, const char *method_name,
|
78
|
+
VALUE(c_fn)(const int, const VALUE *, const VALUE)) {
|
79
|
+
return _contrast_register_patch(module_name, method_name, c_fn,
|
80
|
+
IMPL_PREPEND);
|
76
81
|
}
|
77
82
|
|
78
|
-
static VALUE
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
) {
|
83
|
+
static VALUE
|
84
|
+
_contrast_register_patch(const char *module_name, const char *method_name,
|
85
|
+
VALUE(c_fn)(const int, const VALUE *, const VALUE),
|
86
|
+
patch_impl patch) {
|
83
87
|
VALUE contrast_bind_module = rb_funcall(rb_cModule, rb_intern("new"), 0);
|
84
88
|
VALUE unbound_method = Qnil;
|
85
89
|
VALUE rb_str_module_name = rb_str_new_cstr(module_name);
|
@@ -105,24 +109,27 @@ static VALUE _contrast_register_patch(const char *module_name,
|
|
105
109
|
*/
|
106
110
|
rb_define_method(contrast_bind_module, method_name, (VALUE(*)())c_fn, -1);
|
107
111
|
VALUE rb_sym_instance_method = rb_intern("instance_method");
|
108
|
-
unbound_method = rb_funcall(contrast_bind_module, rb_sym_instance_method, 1,
|
112
|
+
unbound_method = rb_funcall(contrast_bind_module, rb_sym_instance_method, 1,
|
113
|
+
rb_str_method_name);
|
109
114
|
rb_undef_method(contrast_bind_module, method_name);
|
110
115
|
|
111
116
|
/* map impl enum -> ruby symbol */
|
112
117
|
VALUE impl = Qnil;
|
113
|
-
switch(patch) {
|
114
|
-
|
118
|
+
switch (patch) {
|
119
|
+
case IMPL_ALIAS_INSTANCE:
|
115
120
|
impl = ID2SYM(rb_sym_alias_instance);
|
116
121
|
break;
|
117
|
-
|
122
|
+
case IMPL_ALIAS_SINGLETON:
|
118
123
|
impl = ID2SYM(rb_sym_alias_singleton);
|
119
124
|
break;
|
120
|
-
|
125
|
+
case IMPL_PREPEND:
|
121
126
|
impl = ID2SYM(rb_sym_prepend);
|
122
127
|
break;
|
123
128
|
}
|
124
129
|
|
125
|
-
VALUE underlying_method_name =
|
130
|
+
VALUE underlying_method_name =
|
131
|
+
rb_funcall(contrast_patcher(), rb_sym_register_c_patch, 3,
|
132
|
+
rb_str_module_name, unbound_method, impl);
|
126
133
|
return SYM2ID(underlying_method_name);
|
127
134
|
}
|
128
135
|
|
@@ -137,13 +144,15 @@ void Init_cs__common(void) {
|
|
137
144
|
rb_sym_skip_contrast_analysis = rb_intern("skip_contrast_analysis?");
|
138
145
|
rb_sym_skip_assess_analysis = rb_intern("skip_assess_analysis?");
|
139
146
|
rb_sym_method = rb_intern("__method__");
|
140
|
-
|
147
|
+
rb_sym_hash_get = rb_intern("[]");
|
148
|
+
rb_sym_hash_set = rb_intern("[]=");
|
149
|
+
rb_sym_hash_tracked = rb_intern("tracked?");
|
141
150
|
|
142
151
|
/* Used for returning unbound C functions */
|
143
152
|
rb_sym_register_c_patch = rb_intern("register_c_patch");
|
144
|
-
rb_sym_alias_instance
|
145
|
-
rb_sym_alias_singleton
|
146
|
-
rb_sym_prepend
|
153
|
+
rb_sym_alias_instance = rb_intern("alias_instance");
|
154
|
+
rb_sym_alias_singleton = rb_intern("alias_singleton");
|
155
|
+
rb_sym_prepend = rb_intern("prepend");
|
147
156
|
|
148
157
|
/* Ensure definition of core Contrast instrumentation modules */
|
149
158
|
contrast = rb_define_module("Contrast");
|
data/ext/cs__common/cs__common.h
CHANGED
@@ -4,12 +4,11 @@
|
|
4
4
|
#include <ruby.h>
|
5
5
|
|
6
6
|
typedef enum {
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
IMPL_ALIAS_INSTANCE,
|
8
|
+
IMPL_ALIAS_SINGLETON,
|
9
|
+
IMPL_PREPEND
|
10
10
|
} patch_impl;
|
11
11
|
|
12
|
-
|
13
12
|
static VALUE cs__send_method;
|
14
13
|
static VALUE cs__alias_method_sym;
|
15
14
|
|
@@ -24,7 +23,7 @@ extern VALUE rb_sym_in_scope;
|
|
24
23
|
extern VALUE rb_sym_skip_contrast_analysis;
|
25
24
|
extern VALUE rb_sym_skip_assess_analysis;
|
26
25
|
extern VALUE rb_sym_method;
|
27
|
-
extern VALUE
|
26
|
+
extern VALUE rb_sym_hash_get, rb_sym_hash_set, rb_sym_hash_tracked;
|
28
27
|
|
29
28
|
static VALUE patcher;
|
30
29
|
static VALUE rb_sym_instance_method;
|
@@ -38,27 +37,23 @@ void patch_via_funchook(void *original_function, void *hook_function);
|
|
38
37
|
void contrast_alias_method(const VALUE target, const char *to,
|
39
38
|
const char *from);
|
40
39
|
|
41
|
-
VALUE contrast_register_patch(const char *module_name,
|
42
|
-
const
|
43
|
-
|
44
|
-
);
|
40
|
+
VALUE contrast_register_patch(const char *module_name, const char *method_name,
|
41
|
+
VALUE(c_fn)(const int, const VALUE *,
|
42
|
+
const VALUE));
|
45
43
|
|
46
44
|
VALUE contrast_register_singleton_patch(const char *module_name,
|
47
45
|
const char *method_name,
|
48
|
-
VALUE(c_fn)(const int, const VALUE*,
|
49
|
-
|
50
|
-
|
51
|
-
VALUE contrast_register_singleton_prepend_patch(const char *module_name,
|
52
|
-
const char *method_name,
|
53
|
-
VALUE(c_fn)(const int, const VALUE*, const VALUE)
|
54
|
-
);
|
46
|
+
VALUE(c_fn)(const int, const VALUE *,
|
47
|
+
const VALUE));
|
55
48
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
patch_impl patch_impl
|
60
|
-
);
|
49
|
+
VALUE contrast_register_singleton_prepend_patch(
|
50
|
+
const char *module_name, const char *method_name,
|
51
|
+
VALUE(c_fn)(const int, const VALUE *, const VALUE));
|
61
52
|
|
53
|
+
static VALUE
|
54
|
+
_contrast_register_patch(const char *module_name, const char *method_name,
|
55
|
+
VALUE(c_fn)(const int, const VALUE *, const VALUE),
|
56
|
+
patch_impl patch_impl);
|
62
57
|
|
63
58
|
VALUE contrast_patcher();
|
64
59
|
|
@@ -34,7 +34,6 @@ VALUE contrast_patch_call_original(const VALUE *args) {
|
|
34
34
|
method = args[3];
|
35
35
|
method_id = SYM2ID(method);
|
36
36
|
|
37
|
-
|
38
37
|
/* It looks like we can find the last Ruby block given so long as we don't
|
39
38
|
* change Ruby method scope (always call this function from C, not Ruby),
|
40
39
|
* which is the point of this C call.
|
@@ -111,7 +110,8 @@ VALUE contrast_call_super(const VALUE *args) {
|
|
111
110
|
}
|
112
111
|
|
113
112
|
VALUE contrast_run_patches(const VALUE *wrapped_args) {
|
114
|
-
VALUE impl, method, method_policy, object, original_args, original_ret,
|
113
|
+
VALUE impl, method, method_policy, object, original_args, original_ret,
|
114
|
+
preshift, transformed_ret;
|
115
115
|
int argc;
|
116
116
|
VALUE *argv;
|
117
117
|
VALUE rescue_args[6];
|
@@ -168,17 +168,17 @@ VALUE contrast_run_patches(const VALUE *wrapped_args) {
|
|
168
168
|
*/
|
169
169
|
|
170
170
|
switch (impl) {
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
171
|
+
case IMPL_ALIAS_INSTANCE:
|
172
|
+
case IMPL_ALIAS_SINGLETON:
|
173
|
+
original_ret = rb_rescue2(contrast_patch_call_original, original_args,
|
174
|
+
contrast_patch_call_rescue,
|
175
|
+
(VALUE)rescue_args, rb_eException, 0);
|
176
|
+
break;
|
177
|
+
case IMPL_PREPEND:
|
178
|
+
original_ret = rb_rescue2(contrast_call_super, original_args,
|
179
|
+
contrast_patch_call_rescue,
|
180
|
+
(VALUE)rescue_args, rb_eException, 0);
|
181
|
+
break;
|
182
182
|
};
|
183
183
|
|
184
184
|
/* If you're here, the original method did not throw an exception
|
@@ -302,16 +302,17 @@ VALUE contrast_patch_dispatch(const int argc, const VALUE *argv,
|
|
302
302
|
}
|
303
303
|
|
304
304
|
/* Otherwise, invoke Contrast analysis. */
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
return rb_ensure(contrast_run_patches, (VALUE)wrapped_args,
|
305
|
+
VALUE wrapped_args[7];
|
306
|
+
wrapped_args[0] = impl;
|
307
|
+
wrapped_args[1] = (VALUE)original_args;
|
308
|
+
wrapped_args[2] = method;
|
309
|
+
wrapped_args[3] = method_policy;
|
310
|
+
wrapped_args[4] = object;
|
311
|
+
wrapped_args[5] = INT2NUM(argc);
|
312
|
+
wrapped_args[6] = (VALUE)argv;
|
313
|
+
|
314
|
+
return rb_ensure(contrast_run_patches, (VALUE)wrapped_args,
|
315
|
+
contrast_ensure_function, method_policy);
|
315
316
|
|
316
317
|
call_original:
|
317
318
|
|
@@ -351,8 +352,9 @@ VALUE contrast_patch_define_method(const VALUE self, const VALUE clazz,
|
|
351
352
|
rb_funcall(method_policy, rb_sym_instance_method, 0);
|
352
353
|
char *cStr;
|
353
354
|
VALUE str;
|
354
|
-
rb_funcall(patch_status, rb_sym_set_info_for, 5, clazz,
|
355
|
-
method_policy, is_instance_method,
|
355
|
+
rb_funcall(patch_status, rb_sym_set_info_for, 5, clazz,
|
356
|
+
original_method_name, method_policy, is_instance_method,
|
357
|
+
cs_method);
|
356
358
|
|
357
359
|
/* Some methods we patch rely on a specific C level patch,
|
358
360
|
* in those cases we should still add the method to the info_for hash
|
@@ -1,5 +1,5 @@
|
|
1
|
-
#include <ruby.h>
|
2
1
|
#include "../cs__common/cs__common.h"
|
2
|
+
#include <ruby.h>
|
3
3
|
|
4
4
|
/* Calls to Contrast modules */
|
5
5
|
/* Contrast::Agent::Patching::Policy::PatchStatus */
|
@@ -140,8 +140,7 @@ VALUE contrast_call_super(const VALUE *args);
|
|
140
140
|
* instance (specifically for frozen sources)
|
141
141
|
*/
|
142
142
|
VALUE contrast_patch_dispatch(const int argc, const VALUE *argv,
|
143
|
-
const patch_impl impl,
|
144
|
-
const VALUE object);
|
143
|
+
const patch_impl impl, const VALUE object);
|
145
144
|
|
146
145
|
VALUE contrast_alias_instance_patch(const int argc, const VALUE *argv,
|
147
146
|
const VALUE object);
|
@@ -149,7 +148,6 @@ VALUE contrast_alias_instance_patch(const int argc, const VALUE *argv,
|
|
149
148
|
VALUE contrast_alias_singleton_patch(const int argc, const VALUE *argv,
|
150
149
|
const VALUE object);
|
151
150
|
|
152
|
-
|
153
151
|
VALUE contrast_prepend_patch(const int argc, const VALUE *argv,
|
154
152
|
const VALUE object);
|
155
153
|
|
@@ -169,12 +167,12 @@ VALUE contrast_prepend_patch(const int argc, const VALUE *argv,
|
|
169
167
|
* - prepending Foo with Foo::ContrastPrepend
|
170
168
|
*
|
171
169
|
* originalModule - Module; the actual Module being prepended
|
172
|
-
* methodPolicy - :MethodPolicy; the method policy that apply to the method
|
170
|
+
* methodPolicy - :MethodPolicy; the method policy that apply to the method
|
171
|
+
* being redefined
|
173
172
|
*
|
174
173
|
* return - Boolean; if the prepend occurred or not
|
175
174
|
*/
|
176
|
-
VALUE contrast_patch_prepend(const VALUE self,
|
177
|
-
const VALUE originalModule,
|
175
|
+
VALUE contrast_patch_prepend(const VALUE self, const VALUE originalModule,
|
178
176
|
const VALUE methodPolicy);
|
179
177
|
|
180
178
|
/*
|
@@ -10,7 +10,7 @@ static VALUE contrast_protect_fork(const int argc, const VALUE *argv,
|
|
10
10
|
VALUE ret;
|
11
11
|
if (rb_block_given_p()) {
|
12
12
|
/* We call our hook, but it's a little complicated.
|
13
|
-
* We wrap the fork block with our own lambda in
|
13
|
+
* We wrap the fork block with our own lambda in
|
14
14
|
* order to instrument it. There are no public
|
15
15
|
* methods in the Ruby C API to set the prevailing
|
16
16
|
* block, so we have to use rb_funcall_with_block.
|
@@ -20,12 +20,14 @@ static VALUE contrast_protect_fork(const int argc, const VALUE *argv,
|
|
20
20
|
*/
|
21
21
|
VALUE wrapper;
|
22
22
|
wrapper =
|
23
|
-
rb_funcall_with_block(kernel_protect, rb_sym_protect_kernel_wrapper,
|
24
|
-
|
25
|
-
rb_funcall(rb_mKernel, rb_intern("public"), 1,
|
23
|
+
rb_funcall_with_block(kernel_protect, rb_sym_protect_kernel_wrapper,
|
24
|
+
0, NULL, rb_block_proc());
|
25
|
+
rb_funcall(rb_mKernel, rb_intern("public"), 1,
|
26
|
+
ID2SYM(rb_sym_protect_kernel_fork));
|
26
27
|
ret = rb_funcall_with_block(self, rb_sym_protect_kernel_fork, argc,
|
27
28
|
argv, wrapper);
|
28
|
-
rb_funcall(rb_mKernel, rb_intern("private"), 1,
|
29
|
+
rb_funcall(rb_mKernel, rb_intern("private"), 1,
|
30
|
+
ID2SYM(rb_sym_protect_kernel_fork));
|
29
31
|
} else {
|
30
32
|
ret = rb_funcall2(self, rb_sym_protect_kernel_fork, argc, argv);
|
31
33
|
}
|
@@ -37,12 +39,9 @@ void Init_cs__protect_kernel(void) {
|
|
37
39
|
kernel_protect = rb_define_module_under(core_protect, "Kernel");
|
38
40
|
rb_sym_protect_kernel_wrapper = rb_intern("build_wrapper");
|
39
41
|
|
40
|
-
rb_sym_protect_kernel_fork =
|
41
|
-
|
42
|
-
&contrast_protect_fork);
|
43
|
-
|
44
|
-
rb_sym_protect_kernel_fork = contrast_register_singleton_patch("Kernel",
|
45
|
-
"fork",
|
46
|
-
&contrast_protect_fork);
|
42
|
+
rb_sym_protect_kernel_fork =
|
43
|
+
contrast_register_patch("Kernel", "fork", &contrast_protect_fork);
|
47
44
|
|
45
|
+
rb_sym_protect_kernel_fork = contrast_register_singleton_patch(
|
46
|
+
"Kernel", "fork", &contrast_protect_fork);
|
48
47
|
}
|
@@ -6,7 +6,7 @@ static VALUE kernel_protect;
|
|
6
6
|
static VALUE rb_sym_protect_kernel_fork;
|
7
7
|
static VALUE rb_sym_protect_kernel_wrapper;
|
8
8
|
|
9
|
-
static VALUE
|
10
|
-
|
9
|
+
static VALUE contrast_protect_fork(const int argc, const VALUE *argv,
|
10
|
+
const VALUE self);
|
11
11
|
|
12
12
|
void Init_cs__protect_kernel(void);
|
data/lib/contrast-agent.rb
CHANGED
data/lib/contrast.rb
CHANGED
@@ -1,6 +1,9 @@
|
|
1
1
|
# Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
|
+
# Used to prevent deprecation warnings from flooding stdout
|
5
|
+
ENV['PB_IGNORE_DEPRECATIONS'] = 'true'
|
6
|
+
|
4
7
|
# Top-level namespace for Contrast Security agent
|
5
8
|
module Contrast
|
6
9
|
end
|
@@ -22,38 +25,25 @@ class Object
|
|
22
25
|
alias_method :cs__singleton_class, :singleton_class
|
23
26
|
end
|
24
27
|
|
25
|
-
require_relative './contrast/agent/require_state' # rubocop:disable Kernel/RequireRelative
|
26
|
-
|
27
|
-
# We have to do this here BEOFRE we require anything b/c we can't use the
|
28
|
-
# Contrast require methods without it.
|
29
|
-
module Kernel
|
30
|
-
def cs__scoped_require path
|
31
|
-
Contrast::Agent::RequireStates.enter
|
32
|
-
require(path) # rubocop:disable Kernel/Require
|
33
|
-
ensure
|
34
|
-
Contrast::Agent::RequireStates.exit
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
28
|
# component interface for class creation
|
39
29
|
# config gets built as a consequence of this require
|
40
|
-
|
30
|
+
require 'contrast/components/interface'
|
41
31
|
|
42
32
|
# This needs to be required very early, after component interfaces, and before instrumentation attempts
|
43
|
-
|
33
|
+
require 'contrast/funchook/funchook'
|
44
34
|
|
45
35
|
# shared configuration support
|
46
|
-
|
47
|
-
|
36
|
+
require 'contrast/config'
|
37
|
+
require 'contrast/configuration'
|
48
38
|
|
49
|
-
|
39
|
+
require 'contrast/agent/version'
|
50
40
|
|
51
41
|
# errors and exceptions
|
52
|
-
|
42
|
+
require 'contrast/security_exception'
|
53
43
|
|
54
44
|
# shared utils
|
55
|
-
|
56
|
-
|
45
|
+
require 'contrast/utils/timer'
|
46
|
+
require 'contrast/utils/preflight_util'
|
57
47
|
|
58
|
-
|
59
|
-
|
48
|
+
require 'contrast/utils/assess/sampling_util'
|
49
|
+
require 'contrast/agent'
|