contrast-agent 3.10.0 → 3.12.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.flayignore +1 -0
- data/.simplecov +5 -2
- data/ext/build_funchook.rb +12 -7
- data/ext/cs__assess_active_record_named/cs__active_record_named.c +12 -14
- data/ext/cs__assess_active_record_named/cs__active_record_named.h +1 -0
- data/ext/cs__assess_active_record_named/extconf.rb +3 -0
- data/ext/cs__assess_array/cs__assess_array.c +5 -6
- data/ext/cs__assess_array/cs__assess_array.h +1 -0
- data/ext/cs__assess_array/extconf.rb +3 -0
- data/ext/cs__assess_basic_object/cs__assess_basic_object.c +13 -11
- data/ext/cs__assess_basic_object/cs__assess_basic_object.h +2 -1
- data/ext/cs__assess_basic_object/extconf.rb +3 -0
- data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +4 -3
- data/ext/cs__assess_fiber_track/cs__assess_fiber_track.h +3 -3
- data/ext/cs__assess_fiber_track/extconf.rb +3 -0
- data/ext/cs__assess_hash/cs__assess_hash.c +40 -17
- data/ext/cs__assess_hash/cs__assess_hash.h +4 -6
- data/ext/cs__assess_hash/extconf.rb +3 -0
- data/ext/cs__assess_kernel/cs__assess_kernel.c +11 -9
- data/ext/cs__assess_kernel/cs__assess_kernel.h +1 -0
- data/ext/cs__assess_kernel/extconf.rb +3 -0
- data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +3 -6
- data/ext/cs__assess_marshal_module/extconf.rb +3 -0
- data/ext/cs__assess_module/cs__assess_module.c +16 -14
- data/ext/cs__assess_module/cs__assess_module.h +3 -0
- data/ext/cs__assess_module/extconf.rb +3 -0
- data/ext/cs__assess_regexp/cs__assess_regexp.c +13 -9
- data/ext/cs__assess_regexp/cs__assess_regexp.h +1 -0
- data/ext/cs__assess_regexp/extconf.rb +3 -0
- data/ext/cs__assess_string/cs__assess_string.c +5 -8
- data/ext/cs__assess_string/cs__assess_string.h +2 -1
- data/ext/cs__assess_string/extconf.rb +3 -0
- data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +2 -2
- data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.h +3 -3
- data/ext/cs__assess_string_interpolation26/extconf.rb +3 -0
- data/ext/cs__assess_yield_track/cs__assess_yield_track.h +1 -1
- data/ext/cs__assess_yield_track/extconf.rb +3 -0
- data/ext/cs__common/cs__common.c +80 -1
- data/ext/cs__common/cs__common.h +34 -0
- data/ext/cs__common/extconf.rb +9 -8
- data/ext/cs__contrast_patch/cs__contrast_patch.h +1 -6
- data/ext/cs__contrast_patch/extconf.rb +3 -0
- data/ext/cs__protect_kernel/cs__protect_kernel.c +23 -12
- data/ext/cs__protect_kernel/cs__protect_kernel.h +1 -0
- data/ext/cs__protect_kernel/extconf.rb +3 -0
- data/ext/extconf_common.rb +10 -8
- data/funchook/autom4te.cache/requests +45 -45
- data/funchook/config.log +4 -4
- data/lib/contrast.rb +1 -1
- data/lib/contrast/agent.rb +32 -29
- data/lib/contrast/agent/assess.rb +1 -11
- data/lib/contrast/agent/assess/adjusted_span.rb +3 -1
- data/lib/contrast/agent/assess/contrast_event.rb +16 -62
- data/lib/contrast/agent/assess/events/event_factory.rb +25 -0
- data/lib/contrast/agent/assess/events/source_event.rb +83 -0
- data/lib/contrast/agent/assess/insulator.rb +0 -4
- data/lib/contrast/agent/assess/policy/patcher.rb +6 -2
- data/lib/contrast/agent/assess/policy/policy_node.rb +1 -8
- data/lib/contrast/agent/assess/policy/policy_scanner.rb +2 -2
- data/lib/contrast/agent/assess/policy/preshift.rb +1 -1
- data/lib/contrast/agent/assess/policy/propagation_method.rb +68 -33
- data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -1
- data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
- data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
- data/lib/contrast/agent/assess/policy/propagator/database_write.rb +1 -3
- data/lib/contrast/agent/assess/policy/propagator/match_data.rb +80 -0
- data/lib/contrast/agent/assess/policy/propagator/select.rb +35 -22
- data/lib/contrast/agent/assess/policy/propagator/split.rb +26 -6
- data/lib/contrast/agent/assess/policy/propagator/substitution.rb +2 -0
- data/lib/contrast/agent/assess/policy/rewriter_patch.rb +37 -26
- data/lib/contrast/agent/assess/policy/source_method.rb +20 -20
- data/lib/contrast/agent/assess/policy/source_node.rb +0 -15
- data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +90 -0
- data/lib/contrast/agent/assess/policy/trigger/xpath.rb +57 -0
- data/lib/contrast/agent/assess/policy/trigger_method.rb +30 -45
- data/lib/contrast/agent/assess/policy/trigger_node.rb +7 -7
- data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -31
- data/lib/contrast/agent/assess/properties.rb +5 -3
- data/lib/contrast/agent/assess/rule/base.rb +1 -20
- data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +23 -6
- data/lib/contrast/agent/assess/rule/redos.rb +4 -5
- data/lib/contrast/agent/assess/tag.rb +24 -14
- data/lib/contrast/agent/at_exit_hook.rb +16 -13
- data/lib/contrast/agent/class_reopener.rb +23 -8
- data/lib/contrast/agent/deadzone/policy/policy.rb +2 -2
- data/lib/contrast/agent/disable_reaction.rb +3 -4
- data/lib/contrast/agent/exclusion_matcher.rb +7 -48
- data/lib/contrast/agent/inventory/policy/datastores.rb +54 -0
- data/lib/contrast/agent/inventory/policy/policy.rb +1 -1
- data/lib/contrast/agent/middleware.rb +101 -260
- data/lib/contrast/agent/module_data.rb +2 -1
- data/lib/contrast/agent/patching/policy/after_load_patch.rb +13 -3
- data/lib/contrast/agent/patching/policy/after_load_patcher.rb +59 -47
- data/lib/contrast/agent/patching/policy/method_policy.rb +3 -3
- data/lib/contrast/agent/patching/policy/module_policy.rb +0 -25
- data/lib/contrast/agent/patching/policy/patch.rb +97 -23
- data/lib/contrast/agent/patching/policy/patcher.rb +28 -30
- data/lib/contrast/agent/patching/policy/policy.rb +7 -7
- data/lib/contrast/agent/patching/policy/policy_node.rb +3 -11
- data/lib/contrast/agent/patching/policy/trigger_node.rb +2 -5
- data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +63 -0
- data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +52 -0
- data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +68 -0
- data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +117 -0
- data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +54 -0
- data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +129 -0
- data/lib/contrast/agent/protect/policy/policy.rb +6 -6
- data/lib/contrast/agent/protect/policy/rule_applicator.rb +51 -0
- data/lib/contrast/agent/protect/rule.rb +0 -5
- data/lib/contrast/agent/protect/rule/base.rb +19 -37
- data/lib/contrast/agent/protect/rule/base_service.rb +3 -1
- data/lib/contrast/agent/protect/rule/cmd_injection.rb +12 -15
- data/lib/contrast/agent/protect/rule/default_scanner.rb +0 -13
- data/lib/contrast/agent/protect/rule/deserialization.rb +2 -0
- data/lib/contrast/agent/protect/rule/http_method_tampering.rb +2 -2
- data/lib/contrast/agent/protect/rule/no_sqli.rb +4 -4
- data/lib/contrast/agent/protect/rule/path_traversal.rb +6 -10
- data/lib/contrast/agent/protect/rule/sqli.rb +5 -4
- data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +2 -0
- data/lib/contrast/agent/protect/rule/xss.rb +2 -0
- data/lib/contrast/agent/protect/rule/xxe.rb +10 -4
- data/lib/contrast/agent/railtie.rb +3 -8
- data/lib/contrast/agent/reaction_processor.rb +5 -5
- data/lib/contrast/agent/request.rb +11 -18
- data/lib/contrast/agent/request_context.rb +16 -19
- data/lib/contrast/agent/request_handler.rb +35 -0
- data/lib/contrast/agent/response.rb +39 -86
- data/lib/contrast/agent/rewriter.rb +22 -10
- data/lib/contrast/agent/rule_set.rb +49 -0
- data/lib/contrast/agent/scope.rb +0 -6
- data/lib/contrast/agent/service_heartbeat.rb +3 -4
- data/lib/contrast/agent/socket_client.rb +25 -19
- data/lib/contrast/agent/static_analysis.rb +41 -0
- data/lib/contrast/agent/thread.rb +1 -1
- data/lib/contrast/agent/tracepoint_hook.rb +1 -5
- data/lib/contrast/agent/version.rb +1 -1
- data/lib/contrast/api.rb +1 -1
- data/lib/contrast/api/decorators.rb +14 -0
- data/lib/contrast/api/decorators/application_settings.rb +37 -0
- data/lib/contrast/api/decorators/application_update.rb +66 -0
- data/lib/contrast/api/decorators/input_analysis.rb +17 -0
- data/lib/contrast/api/decorators/server_features.rb +24 -0
- data/lib/contrast/api/speedracer.rb +28 -24
- data/lib/contrast/api/tcp_socket.rb +0 -2
- data/lib/contrast/components/agent.rb +34 -24
- data/lib/contrast/components/app_context.rb +45 -38
- data/lib/contrast/components/assess.rb +25 -15
- data/lib/contrast/components/config.rb +7 -5
- data/lib/contrast/components/contrast_service.rb +23 -71
- data/lib/contrast/components/heap_dump.rb +12 -8
- data/lib/contrast/components/interface.rb +15 -22
- data/lib/contrast/components/inventory.rb +5 -1
- data/lib/contrast/components/logger.rb +3 -68
- data/lib/contrast/components/protect.rb +40 -4
- data/lib/contrast/components/sampling.rb +22 -11
- data/lib/contrast/components/scope.rb +2 -52
- data/lib/contrast/components/settings.rb +42 -23
- data/lib/contrast/config/base_configuration.rb +1 -0
- data/lib/contrast/config/default_value.rb +1 -0
- data/lib/contrast/config/protect_rule_configuration.rb +0 -14
- data/lib/contrast/config/protect_rules_configuration.rb +0 -1
- data/lib/contrast/configuration.rb +2 -2
- data/lib/contrast/{extensions/ruby_core → extension}/assess.rb +12 -15
- data/lib/contrast/extension/assess/array.rb +77 -0
- data/lib/contrast/{extensions/ruby_core → extension}/assess/assess_extension.rb +29 -24
- data/lib/contrast/{extensions/ruby_core → extension}/assess/erb.rb +0 -8
- data/lib/contrast/extension/assess/eval_trigger.rb +78 -0
- data/lib/contrast/{extensions/ruby_core → extension}/assess/exec_trigger.rb +7 -9
- data/lib/contrast/extension/assess/fiber.rb +113 -0
- data/lib/contrast/extension/assess/hash.rb +39 -0
- data/lib/contrast/extension/assess/kernel.rb +110 -0
- data/lib/contrast/extension/assess/regexp.rb +84 -0
- data/lib/contrast/{extensions/ruby_core → extension}/assess/string.rb +18 -10
- data/lib/contrast/{extensions/ruby_core → extension}/delegator.rb +0 -0
- data/lib/contrast/{extensions/ruby_core → extension}/inventory.rb +2 -2
- data/lib/contrast/extension/kernel.rb +54 -0
- data/lib/contrast/{extensions/ruby_core → extension}/module.rb +0 -0
- data/lib/contrast/{extensions/ruby_core → extension}/protect.rb +2 -2
- data/lib/contrast/extension/protect/kernel.rb +44 -0
- data/lib/contrast/{extensions/ruby_core → extension}/protect/psych.rb +1 -1
- data/lib/contrast/{extensions/ruby_core → extension}/thread.rb +0 -0
- data/lib/contrast/framework/base_support.rb +32 -0
- data/lib/contrast/framework/manager.rb +59 -8
- data/lib/contrast/framework/platform_version.rb +1 -0
- data/lib/contrast/framework/rack/patch/session_cookie.rb +126 -0
- data/lib/contrast/framework/rack/patch/support.rb +24 -0
- data/lib/contrast/framework/rack/support.rb +22 -0
- data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +43 -0
- data/lib/contrast/framework/rails/patch/assess_configuration.rb +103 -0
- data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +31 -0
- data/lib/contrast/framework/rails/patch/support.rb +67 -0
- data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +34 -0
- data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +39 -0
- data/lib/contrast/framework/rails/rewrite/active_record_named.rb +73 -0
- data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +33 -0
- data/lib/contrast/framework/rails/support.rb +115 -0
- data/lib/contrast/framework/sinatra/application_helper.rb +51 -0
- data/lib/contrast/framework/sinatra/patch/base.rb +83 -0
- data/lib/contrast/framework/sinatra/patch/support.rb +27 -0
- data/lib/contrast/framework/sinatra/support.rb +109 -0
- data/lib/contrast/framework/view_technologies_descriptor.rb +1 -0
- data/lib/contrast/logger/application.rb +80 -0
- data/lib/contrast/logger/log.rb +143 -0
- data/lib/contrast/logger/time.rb +50 -0
- data/lib/contrast/tasks/config.rb +54 -0
- data/lib/contrast/tasks/service.rb +3 -13
- data/lib/contrast/utils/assess/sampling_util.rb +4 -9
- data/lib/contrast/utils/assess/tracking_util.rb +7 -1
- data/lib/contrast/utils/boolean_util.rb +2 -2
- data/lib/contrast/utils/cache.rb +0 -11
- data/lib/contrast/utils/class_util.rb +21 -2
- data/lib/contrast/utils/gemfile_reader.rb +7 -5
- data/lib/contrast/utils/hash_digest.rb +2 -11
- data/lib/contrast/utils/heap_dump_util.rb +12 -11
- data/lib/contrast/utils/invalid_configuration_util.rb +4 -4
- data/lib/contrast/utils/inventory_util.rb +2 -2
- data/lib/contrast/utils/io_util.rb +1 -11
- data/lib/contrast/utils/job_servers_running.rb +6 -4
- data/lib/contrast/utils/object_share.rb +1 -28
- data/lib/contrast/utils/os.rb +1 -25
- data/lib/contrast/utils/service_response_util.rb +36 -60
- data/lib/contrast/utils/service_sender_util.rb +84 -23
- data/lib/contrast/utils/sinatra_helper.rb +0 -6
- data/lib/contrast/utils/stack_trace_utils.rb +86 -182
- data/lib/contrast/utils/string_utils.rb +18 -2
- data/lib/contrast/utils/tag_util.rb +11 -1
- data/lib/contrast/utils/thread_tracker.rb +2 -2
- data/lib/contrast/utils/timer.rb +0 -40
- data/resources/assess/policy.json +42 -71
- data/resources/inventory/policy.json +2 -2
- data/resources/protect/policy.json +15 -15
- data/ruby-agent.gemspec +11 -4
- data/service_executables/VERSION +1 -1
- data/service_executables/linux/contrast-service +0 -0
- data/service_executables/mac/contrast-service +0 -0
- metadata +122 -111
- data/ext/cs__assess_regexp_track/cs__assess_regexp_track.c +0 -63
- data/ext/cs__assess_regexp_track/cs__assess_regexp_track.h +0 -29
- data/ext/cs__assess_regexp_track/extconf.rb +0 -2
- data/lib/contrast/agent/assess/frozen_properties.rb +0 -41
- data/lib/contrast/agent/assess/rule/csrf.rb +0 -66
- data/lib/contrast/agent/assess/rule/csrf/csrf_action.rb +0 -28
- data/lib/contrast/agent/assess/rule/csrf/csrf_applicator.rb +0 -73
- data/lib/contrast/agent/assess/rule/csrf/csrf_watcher.rb +0 -132
- data/lib/contrast/agent/assess/rule/response_scanning_rule.rb +0 -47
- data/lib/contrast/agent/assess/rule/response_watcher.rb +0 -36
- data/lib/contrast/agent/assess/rule/watcher.rb +0 -36
- data/lib/contrast/agent/feature_state.rb +0 -376
- data/lib/contrast/agent/logger_manager.rb +0 -116
- data/lib/contrast/agent/protect/rule/csrf.rb +0 -118
- data/lib/contrast/agent/protect/rule/csrf/csrf_evaluator.rb +0 -103
- data/lib/contrast/agent/protect/rule/csrf/csrf_token_injector.rb +0 -85
- data/lib/contrast/agent/settings_state.rb +0 -152
- data/lib/contrast/delegators.rb +0 -9
- data/lib/contrast/delegators/application_update.rb +0 -32
- data/lib/contrast/extensions/framework/rack/cookie.rb +0 -24
- data/lib/contrast/extensions/framework/rack/request.rb +0 -24
- data/lib/contrast/extensions/framework/rack/response.rb +0 -23
- data/lib/contrast/extensions/framework/rails/action_controller_railties_helper_inherited.rb +0 -20
- data/lib/contrast/extensions/framework/rails/active_record.rb +0 -26
- data/lib/contrast/extensions/framework/rails/active_record_named.rb +0 -53
- data/lib/contrast/extensions/framework/rails/active_record_time_zone_inherited.rb +0 -21
- data/lib/contrast/extensions/framework/rails/buffer.rb +0 -28
- data/lib/contrast/extensions/framework/rails/configuration.rb +0 -27
- data/lib/contrast/extensions/framework/sinatra/base.rb +0 -59
- data/lib/contrast/extensions/ruby_core/assess/array.rb +0 -59
- data/lib/contrast/extensions/ruby_core/assess/basic_object.rb +0 -15
- data/lib/contrast/extensions/ruby_core/assess/fiber.rb +0 -124
- data/lib/contrast/extensions/ruby_core/assess/hash.rb +0 -22
- data/lib/contrast/extensions/ruby_core/assess/kernel.rb +0 -95
- data/lib/contrast/extensions/ruby_core/assess/module.rb +0 -14
- data/lib/contrast/extensions/ruby_core/assess/regexp.rb +0 -206
- data/lib/contrast/extensions/ruby_core/assess/tilt_template_trigger.rb +0 -73
- data/lib/contrast/extensions/ruby_core/assess/xpath_library_trigger.rb +0 -40
- data/lib/contrast/extensions/ruby_core/eval_trigger.rb +0 -52
- data/lib/contrast/extensions/ruby_core/inventory/datastores.rb +0 -37
- data/lib/contrast/extensions/ruby_core/protect/applies_command_injection_rule.rb +0 -72
- data/lib/contrast/extensions/ruby_core/protect/applies_deserialization_rule.rb +0 -60
- data/lib/contrast/extensions/ruby_core/protect/applies_no_sqli_rule.rb +0 -83
- data/lib/contrast/extensions/ruby_core/protect/applies_path_traversal_rule.rb +0 -123
- data/lib/contrast/extensions/ruby_core/protect/applies_sqli_rule.rb +0 -65
- data/lib/contrast/extensions/ruby_core/protect/applies_xxe_rule.rb +0 -143
- data/lib/contrast/extensions/ruby_core/protect/kernel.rb +0 -30
- data/lib/contrast/framework/rails_support.rb +0 -88
- data/lib/contrast/framework/sinatra_application_helper.rb +0 -49
- data/lib/contrast/framework/sinatra_support.rb +0 -94
- data/lib/contrast/utils/comment_range.rb +0 -19
- data/lib/contrast/utils/data_store_util.rb +0 -23
- data/lib/contrast/utils/environment_util.rb +0 -81
- data/lib/contrast/utils/performs_logging.rb +0 -152
- data/lib/contrast/utils/rack_assess_session_cookie.rb +0 -104
- data/lib/contrast/utils/rails_assess_configuration.rb +0 -95
- data/lib/contrast/utils/random_util.rb +0 -22
- data/resources/csrf/inject.js +0 -44
- data/resources/factory-bot-spec/spec_helper.rb +0 -30
- data/resources/rubocops/kernel/catch_cop.rb +0 -37
- data/resources/rubocops/kernel/require_cop.rb +0 -37
- data/resources/rubocops/kernel/require_relative_cop.rb +0 -33
- data/resources/rubocops/module/autoload_cop.rb +0 -37
- data/resources/rubocops/module/const_defined_cop.rb +0 -37
- data/resources/rubocops/module/const_get_cop.rb +0 -37
- data/resources/rubocops/module/const_set_cop.rb +0 -37
- data/resources/rubocops/module/constants_cop.rb +0 -37
- data/resources/rubocops/module/name_cop.rb +0 -37
- data/resources/rubocops/object/class_cop.rb +0 -37
- data/resources/rubocops/object/freeze_cop.rb +0 -37
- data/resources/rubocops/object/frozen_cop.rb +0 -37
- data/resources/rubocops/object/is_a_cop.rb +0 -37
- data/resources/rubocops/object/method_cop.rb +0 -37
- data/resources/rubocops/object/respond_to_cop.rb +0 -37
- data/resources/rubocops/object/singleton_class_cop.rb +0 -37
- data/resources/rubocops/regexp/spelling_cop.rb +0 -44
- data/resources/rubocops/thread/new_cop.rb +0 -39
- data/resources/ruby-spec/ancestors_spec.rb +0 -70
- data/resources/ruby-spec/modulo_spec.rb +0 -831
- data/resources/ruby-spec/parameters_spec.rb +0 -261
- data/resources/ruby-spec/ruby_spec_spec_helper.rb +0 -35
@@ -6,7 +6,8 @@ module Contrast
|
|
6
6
|
# A simple wrapper around a Module and a call to its name, used to avoid
|
7
7
|
# calling the Module#name method and generating extra Strings
|
8
8
|
class ModuleData
|
9
|
-
|
9
|
+
attr_reader :mod, :name
|
10
|
+
|
10
11
|
def initialize mod, name = nil
|
11
12
|
@mod = mod
|
12
13
|
@name = name || mod.cs__name
|
@@ -1,6 +1,8 @@
|
|
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
|
+
cs__scoped_require 'contrast/components/interface'
|
5
|
+
cs__scoped_require 'contrast/extension/module'
|
4
6
|
cs__scoped_require 'contrast/utils/class_util'
|
5
7
|
|
6
8
|
module Contrast
|
@@ -9,16 +11,20 @@ module Contrast
|
|
9
11
|
module Policy
|
10
12
|
# Used to handle tracking patches that need to apply special instrumentation when a module is loaded
|
11
13
|
class AfterLoadPatch
|
12
|
-
|
13
|
-
|
14
|
+
include Contrast::Components::Interface
|
15
|
+
access_component :scope
|
16
|
+
attr_reader :applied, :module_name, :instrumentation_file_path, :method_to_instrument, :instrumenting_module
|
17
|
+
|
18
|
+
def initialize module_name, instrumentation_file_path, method_to_instrument: nil, instrumenting_module:
|
14
19
|
@applied = false
|
15
20
|
@module_name = module_name
|
16
21
|
@method_to_instrument = method_to_instrument
|
17
22
|
@instrumentation_file_path = instrumentation_file_path
|
23
|
+
@instrumenting_module = instrumenting_module
|
18
24
|
end
|
19
25
|
|
20
26
|
def applied?
|
21
|
-
|
27
|
+
applied
|
22
28
|
end
|
23
29
|
|
24
30
|
# Modules can be re-opened, so the first load may not
|
@@ -54,6 +60,10 @@ module Contrast
|
|
54
60
|
|
55
61
|
def instrument!
|
56
62
|
cs__scoped_require instrumentation_file_path
|
63
|
+
if instrumenting_module
|
64
|
+
mod = Module.cs__const_get(instrumenting_module)
|
65
|
+
with_contrast_scope { mod.instrument } if mod
|
66
|
+
end
|
57
67
|
@applied = true
|
58
68
|
end
|
59
69
|
|
@@ -3,6 +3,7 @@
|
|
3
3
|
|
4
4
|
cs__scoped_require 'contrast/components/interface'
|
5
5
|
cs__scoped_require 'contrast/agent/patching/policy/after_load_patch'
|
6
|
+
cs__scoped_require 'contrast/framework/manager'
|
6
7
|
|
7
8
|
module Contrast
|
8
9
|
module Agent
|
@@ -12,69 +13,80 @@ module Contrast
|
|
12
13
|
# after they've been loaded
|
13
14
|
module AfterLoadPatcher
|
14
15
|
include Contrast::Components::Interface
|
15
|
-
access_component :
|
16
|
-
|
17
|
-
AFTER_LOAD_PATCHES = Set.new([
|
18
|
-
Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
|
19
|
-
'ActiveRecord::AttributeMethods::TimeZoneConversion::ClassMethods',
|
20
|
-
'contrast/extensions/framework/rails/active_record_time_zone_inherited',
|
21
|
-
method_to_instrument: :inherited),
|
22
|
-
Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
|
23
|
-
'ActionController::Railties::Helper::ClassMethods',
|
24
|
-
'contrast/extensions/framework/rails/action_controller_railties_helper_inherited',
|
25
|
-
method_to_instrument: :inherited),
|
26
|
-
Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
|
27
|
-
'ActiveRecord::Scoping::Named::ClassMethods',
|
28
|
-
'contrast/extensions/framework/rails/active_record_named'),
|
29
|
-
Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
|
30
|
-
'ActiveRecord::AttributeMethods::Read::ClassMethods',
|
31
|
-
'contrast/extensions/framework/rails/active_record'),
|
32
|
-
Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
|
33
|
-
'Sinatra::Base',
|
34
|
-
'contrast/extensions/framework/sinatra/base'),
|
35
|
-
Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
|
36
|
-
'Rails::Application::Configuration',
|
37
|
-
'contrast/extensions/framework/rails/configuration',
|
38
|
-
method_to_instrument: :session_store),
|
39
|
-
Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
|
40
|
-
'Rack::Request',
|
41
|
-
'contrast/extensions/framework/rack/request'),
|
42
|
-
Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
|
43
|
-
'Rack::Response',
|
44
|
-
'contrast/extensions/framework/rack/response'),
|
45
|
-
Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
|
46
|
-
'ActionController::Live::Buffer',
|
47
|
-
'contrast/extensions/framework/rails/buffer'),
|
48
|
-
Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
|
49
|
-
'Rack::Session::Cookie',
|
50
|
-
'contrast/extensions/framework/rack/cookie')
|
51
|
-
])
|
16
|
+
access_component :agent, :logging
|
52
17
|
|
53
18
|
# After initialization run a catchup check to instrument any already loaded modules we care about
|
54
19
|
def catchup_after_load_patches
|
55
|
-
|
56
|
-
|
20
|
+
apply_require_patches!
|
21
|
+
apply_direct_patches!
|
22
|
+
apply_load_patches!
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
# These patches need to be applied directly, not from our policy, so
|
28
|
+
# do so and do so only once. This should be the new standard so that
|
29
|
+
# there are no require time side effects of loading our core
|
30
|
+
# extensions.
|
31
|
+
def apply_direct_patches!
|
32
|
+
@_apply_direct_patches ||= begin
|
33
|
+
Contrast::Extension::Assess::ArrayPropagator.instrument_array_track
|
34
|
+
Contrast::Extension::Assess::EvalTrigger.instrument_basic_object_track
|
35
|
+
Contrast::Extension::Assess::EvalTrigger.instrument_module_track
|
36
|
+
Contrast::Extension::Assess::FiberPropagator.instrument_fiber_track
|
37
|
+
Contrast::Extension::Assess::HashPropagator.instrument_hash_track
|
38
|
+
Contrast::Extension::Assess::KernelPropagator.instrument_kernel_track
|
39
|
+
Contrast::Extension::Assess::RegexpPropagator.instrument_regexp_track
|
40
|
+
Contrast::Extension::Assess::StringPropagator.instrument_string
|
41
|
+
Contrast::Extension::Assess::StringPropagator.instrument_string_interpolation
|
42
|
+
|
43
|
+
Contrast::Extension::Protect::Kernel.instrument
|
44
|
+
true
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def apply_load_patches!
|
49
|
+
after_load_patches.each do |after_load_patch|
|
57
50
|
next unless after_load_patch.target_defined?
|
58
51
|
next if AGENT.skip_instrumentation?(after_load_patch.module_name)
|
59
52
|
|
60
|
-
logger.
|
53
|
+
logger.trace(
|
54
|
+
'Catching up on already loaded afterload patch - applying instrumentation',
|
55
|
+
module: after_load_patch.module_name)
|
61
56
|
after_load_patch.instrument!
|
62
|
-
applied << after_load_patch
|
63
57
|
end
|
64
|
-
|
58
|
+
after_load_patches.delete_if(&:applied?)
|
59
|
+
end
|
60
|
+
|
61
|
+
# These patches need to be applied directly, not from our policy, and
|
62
|
+
# are applied as a result of requiring the file as they alias methods
|
63
|
+
# directly, allowing us to control things like scope and exception
|
64
|
+
# handling
|
65
|
+
def apply_require_patches!
|
66
|
+
@_apply_require_patches ||= begin
|
67
|
+
cs__scoped_require 'contrast/extension/thread'
|
68
|
+
cs__scoped_require 'contrast/extension/kernel'
|
69
|
+
true
|
70
|
+
rescue LoadError, StandardError => e
|
71
|
+
logger.error('failed instrumenting apply_require_patches!', e)
|
72
|
+
false
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def after_load_patches
|
77
|
+
@_after_load_patches ||= Contrast::Agent.framework_manager.find_after_load_patches
|
65
78
|
end
|
66
79
|
|
67
80
|
# Use for any checks after we've initialized
|
68
81
|
def load_patches_for_module module_name
|
69
|
-
return if
|
82
|
+
return if after_load_patches.empty?
|
70
83
|
|
71
|
-
patch =
|
84
|
+
patch = after_load_patches.find { |after_load_patch| after_load_patch.applies?(module_name) }
|
72
85
|
return unless patch
|
73
86
|
|
74
|
-
logger.
|
75
|
-
|
87
|
+
logger.trace('Detected loading of afterload patch - applying instrumentation', module: module_name)
|
76
88
|
patch.instrument!
|
77
|
-
|
89
|
+
after_load_patches.delete_if(&:applied?)
|
78
90
|
end
|
79
91
|
end
|
80
92
|
end
|
@@ -7,9 +7,9 @@ module Contrast
|
|
7
7
|
module Policy
|
8
8
|
# This class is used to map each method to the trigger node that applies to it
|
9
9
|
class MethodPolicy
|
10
|
-
|
11
|
-
|
12
|
-
|
10
|
+
attr_reader :deadzone_node, :inventory_node, :propagation_node, :protect_node, :trigger_node
|
11
|
+
attr_accessor :source_node, :method_name, :method_visibility, :instance_method
|
12
|
+
|
13
13
|
def initialize(source_node: nil, propagation_node: nil,
|
14
14
|
trigger_node: nil, inventory_node: nil,
|
15
15
|
protect_node: nil, deadzone_node: nil,
|
@@ -73,31 +73,6 @@ module Contrast
|
|
73
73
|
end
|
74
74
|
end
|
75
75
|
|
76
|
-
def collect_class_nodes nodes, clazz_name
|
77
|
-
return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless nodes
|
78
|
-
|
79
|
-
nodes.select { |policy_node| clazz_name == policy_node.class_name }
|
80
|
-
end
|
81
|
-
|
82
|
-
def collect_method_nodes nodes, method, instance_method
|
83
|
-
return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless nodes
|
84
|
-
|
85
|
-
nodes.select do |node|
|
86
|
-
node.instance_method? == instance_method && node.method_name == method
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
def create_method_policy_subset method, instance_method
|
91
|
-
method_policy = ModulePolicy.new
|
92
|
-
method_policy.source_nodes = collect_method_nodes(source_nodes, method, instance_method)
|
93
|
-
method_policy.propagator_nodes = collect_method_nodes(propagator_nodes, method, instance_method)
|
94
|
-
method_policy.trigger_nodes = collect_method_nodes(trigger_nodes, method, instance_method)
|
95
|
-
method_policy.inventory_nodes = collect_method_nodes(inventory_nodes, method, instance_method)
|
96
|
-
method_policy.protect_nodes = collect_method_nodes(protect_nodes, method, instance_method)
|
97
|
-
method_policy.deadzone_nodes = collect_method_nodes(deadzone_nodes, method, instance_method)
|
98
|
-
method_policy
|
99
|
-
end
|
100
|
-
|
101
76
|
private
|
102
77
|
|
103
78
|
def sort_method_names nodes, instance_methods, singleton_methods
|
@@ -2,28 +2,25 @@
|
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
cs__scoped_require 'monitor'
|
5
|
-
|
6
|
-
|
5
|
+
cs__scoped_require 'contrast/components/interface'
|
6
|
+
|
7
|
+
cs__scoped_require 'contrast/agent'
|
8
|
+
cs__scoped_require 'contrast/logger/log'
|
7
9
|
cs__scoped_require 'contrast/agent/patching/policy/method_policy'
|
8
10
|
cs__scoped_require 'contrast/agent/patching/policy/patch_status'
|
9
11
|
cs__scoped_require 'contrast/agent/patching/policy/trigger_node'
|
10
|
-
cs__scoped_require 'contrast/components/interface'
|
11
12
|
|
12
13
|
# assess
|
13
14
|
cs__scoped_require 'contrast/agent/assess/policy/policy'
|
14
15
|
cs__scoped_require 'contrast/agent/assess/policy/propagation_method'
|
15
16
|
cs__scoped_require 'contrast/agent/assess/policy/source_method'
|
16
17
|
cs__scoped_require 'contrast/agent/assess/policy/trigger_method'
|
17
|
-
cs__scoped_require 'contrast/extensions/ruby_core/assess'
|
18
|
-
|
19
|
-
# inventory
|
20
|
-
cs__scoped_require 'contrast/agent/inventory/policy/policy'
|
21
|
-
cs__scoped_require 'contrast/extensions/ruby_core/inventory'
|
22
18
|
|
23
19
|
# protect
|
24
20
|
cs__scoped_require 'contrast/agent/protect/policy/policy'
|
25
|
-
|
26
|
-
|
21
|
+
|
22
|
+
# inventory
|
23
|
+
cs__scoped_require 'contrast/agent/inventory/policy/policy'
|
27
24
|
|
28
25
|
module Contrast
|
29
26
|
module Agent
|
@@ -40,7 +37,7 @@ module Contrast
|
|
40
37
|
include Contrast::Agent::Assess::Policy::TriggerMethod
|
41
38
|
|
42
39
|
include Contrast::Components::Interface
|
43
|
-
access_component :
|
40
|
+
access_component :agent, :analysis, :logging, :scope
|
44
41
|
|
45
42
|
POLICIES = [
|
46
43
|
Contrast::Agent::Assess::Policy::Policy,
|
@@ -86,7 +83,7 @@ module Contrast
|
|
86
83
|
rescue StandardError => e
|
87
84
|
# Anything else was our bad and we gotta catch that to allow for
|
88
85
|
# normal application flow
|
89
|
-
logger.error(
|
86
|
+
logger.error('Unable to apply pre patch to method.', e)
|
90
87
|
rescue Exception => e # rubocop:disable Lint/RescueException
|
91
88
|
# This is something like NoMemoryError that we can't
|
92
89
|
# hope to handle. Nonetheless, shouldn't leak scope.
|
@@ -115,7 +112,7 @@ module Contrast
|
|
115
112
|
def apply_post_patch method_policy, preshift, object, ret, args, block
|
116
113
|
apply_assess(method_policy, preshift, object, ret, args, block)
|
117
114
|
rescue StandardError => e
|
118
|
-
logger.error(
|
115
|
+
logger.error('Unable to apply post patch to method.', e)
|
119
116
|
end
|
120
117
|
|
121
118
|
# Apply the Protect patch which applies to the given method.
|
@@ -130,6 +127,7 @@ module Contrast
|
|
130
127
|
# @param args [Array<Object>] The arguments passed to the method
|
131
128
|
# being invoked.
|
132
129
|
def apply_protect method_policy, method, exception, object, args
|
130
|
+
return unless AGENT.enabled?
|
133
131
|
return unless PROTECT.enabled?
|
134
132
|
|
135
133
|
apply_trigger_only(method_policy&.protect_node,
|
@@ -184,14 +182,12 @@ module Contrast
|
|
184
182
|
|
185
183
|
trigger_node = method_policy.trigger_node
|
186
184
|
Contrast::Agent::Assess::Policy::TriggerMethod.apply_trigger_rule(trigger_node, object, ret, args) if trigger_node
|
187
|
-
|
188
185
|
if method_policy.source_node
|
189
186
|
# If we were given a frozen return, and it was the target of a
|
190
187
|
# source, and we have frozen sources enabled, we'll need to
|
191
188
|
# replace the return. Note, this is not the default case.
|
192
189
|
source_ret = Contrast::Agent::Assess::Policy::SourceMethod.source_patchers(method_policy, object, ret, args)
|
193
190
|
end
|
194
|
-
|
195
191
|
if method_policy.propagation_node
|
196
192
|
propagated_ret = Contrast::Agent::Assess::Policy::PropagationMethod.apply_propagation(
|
197
193
|
method_policy,
|
@@ -201,13 +197,12 @@ module Contrast
|
|
201
197
|
args,
|
202
198
|
block)
|
203
199
|
end
|
204
|
-
|
205
200
|
handle_return(propagated_ret, source_ret, ret)
|
206
201
|
rescue StandardError => e
|
207
|
-
logger.error(
|
202
|
+
logger.error('Unable to assess method call.', e)
|
208
203
|
handle_return(propagated_ret, source_ret, ret)
|
209
204
|
rescue Exception => e # rubocop:disable Lint/RescueException
|
210
|
-
logger.error(
|
205
|
+
logger.error('Unable to assess method call.', e)
|
211
206
|
handle_return(propagated_ret, source_ret, ret)
|
212
207
|
raise e
|
213
208
|
end
|
@@ -237,7 +232,6 @@ module Contrast
|
|
237
232
|
# resembling CUSTOM - they all have a common interface but their
|
238
233
|
# own logic based on what's in the method(s) they've been patched
|
239
234
|
# into.
|
240
|
-
applicator = trigger_node.applicator
|
241
235
|
# Each patch also knows the method of its applicator. Some
|
242
236
|
# things, like AppliesXxeRule, have different methods depending
|
243
237
|
# on the library patched. This lets us handle the boilerplate of
|
@@ -247,7 +241,7 @@ module Contrast
|
|
247
241
|
# By calling send like this, we can reuse all the patching.
|
248
242
|
# We `send` to the given method of the given class
|
249
243
|
# (applicator) since they all accept the same inputs
|
250
|
-
applicator.send(applicator_method, method, exception, trigger_node.properties, object, args)
|
244
|
+
trigger_node.applicator.send(applicator_method, method, exception, trigger_node.properties, object, args)
|
251
245
|
end
|
252
246
|
|
253
247
|
# Method to choose which replaced return from the post_patch to
|
@@ -279,6 +273,15 @@ module Contrast
|
|
279
273
|
patched_method.to_s).to_sym
|
280
274
|
end
|
281
275
|
|
276
|
+
# Given a method, return a symbol in the format
|
277
|
+
# <method_start>_unbound_<method_name>
|
278
|
+
def build_unbound_method_name patcher_method
|
279
|
+
(Contrast::Utils::ObjectShare::CONTRAST_PATCHED_METHOD_START +
|
280
|
+
'unbound' +
|
281
|
+
Contrast::Utils::ObjectShare::UNDERSCORE +
|
282
|
+
patcher_method.to_s).to_sym
|
283
|
+
end
|
284
|
+
|
282
285
|
# @param mod [Module] the module in which the patch should be
|
283
286
|
# placed.
|
284
287
|
# @param methods [Array(Symbol)] all the instance or singleton
|
@@ -297,10 +300,11 @@ module Contrast
|
|
297
300
|
rescue NameError => e
|
298
301
|
# This shouldn't happen anymore, but just in case calling alias
|
299
302
|
# results in a NameError, we'll be safe here.
|
300
|
-
logger.
|
303
|
+
logger.error(
|
304
|
+
'Attempted to alias a method on a Module that doesn\'t respond to it.',
|
301
305
|
e,
|
302
|
-
|
303
|
-
|
306
|
+
module: mod.cs__name,
|
307
|
+
method: method_policy.method_name)
|
304
308
|
return false
|
305
309
|
end
|
306
310
|
true
|
@@ -316,9 +320,79 @@ module Contrast
|
|
316
320
|
contrast_prepend_method(mod, method_policy)
|
317
321
|
end
|
318
322
|
|
323
|
+
# @param unbound_method [UnboundMethod] An unbound method, that doesn't reference its binding.
|
324
|
+
# This method executes C hooking code.
|
325
|
+
def register_c_hook unbound_method
|
326
|
+
# current binding is as meaningless as any other. but we need something
|
327
|
+
unbound_method.bind_call(self)
|
328
|
+
end
|
329
|
+
|
330
|
+
# @param target_module_name [String] Fully-qualified module name, as string, to which the C patch applies.
|
331
|
+
# @param unbound_method [UnboundMethod] An unbound method, to be patched into target_module.
|
332
|
+
# @param impl [Symbol] Strategy for applying the patch: { :alias_instance, :alias_singleton, or :prepend }:
|
333
|
+
# :alias_instance -> alias instance method of module
|
334
|
+
# :alias_singleton -> alias instance method of singleton class of module
|
335
|
+
# (equivalent to :alias, where `module = module.singleton class`)
|
336
|
+
# (this is a.k.a. "class-method patch")
|
337
|
+
# :prepend -> prepend instance method of module
|
338
|
+
# [prepending singleton is easily supported too, just not implemented yet.]
|
339
|
+
# @return [Symbol] new alias for the underlying method (presumably, so the patched method can call it)
|
340
|
+
def register_c_patch target_module_name, unbound_method, impl = :alias_instance
|
341
|
+
# These could be set as AfterLoadPatches.
|
342
|
+
method_name = unbound_method.name.to_sym
|
343
|
+
underlying_method_name = build_unbound_method_name(method_name).to_sym
|
344
|
+
|
345
|
+
target_module = Module.cs__const_get(target_module_name)
|
346
|
+
|
347
|
+
target_module = target_module.cs__singleton_class if %i[alias_singleton prepend].include? impl
|
348
|
+
|
349
|
+
visibility = if target_module.private_instance_methods(false).include?(method_name)
|
350
|
+
:private
|
351
|
+
elsif target_module.protected_instance_methods(false).include?(method_name)
|
352
|
+
:protected
|
353
|
+
elsif target_module.public_instance_methods(false).include?(method_name)
|
354
|
+
:public
|
355
|
+
else
|
356
|
+
raise NotImplementedError,
|
357
|
+
<<~ERR
|
358
|
+
Tried to register a C-defined #{ impl } patch for \
|
359
|
+
#{ target_module_name }##{ method_name }, but can't find :#{ method_name }.
|
360
|
+
ERR
|
361
|
+
end
|
362
|
+
|
363
|
+
case impl
|
364
|
+
when :alias_instance, :alias_singleton
|
365
|
+
unless target_module.instance_methods(false).include? underlying_method_name
|
366
|
+
# alias_method may be private
|
367
|
+
target_module.send(:alias_method, underlying_method_name, method_name)
|
368
|
+
target_module.send(:define_method, method_name, unbound_method.bind(target_module))
|
369
|
+
end
|
370
|
+
target_module.send(visibility, method_name) # e.g., module.private(:my_method)
|
371
|
+
when :prepend
|
372
|
+
prepending_module = Module.new
|
373
|
+
prepending_module.send(:define_method, method_name, unbound_method.bind(target_module))
|
374
|
+
prepending_module.send(visibility, :method_name)
|
375
|
+
# This prepends to the singleton class (it patches a class method)
|
376
|
+
target_module.prepend prepending_module
|
377
|
+
end
|
378
|
+
# Ougai::Logger.create_item_with_2args calls Hash#[]=, so we
|
379
|
+
# can't invoke this logging method or we'll seg fault as we'd
|
380
|
+
# change the method definition mid-call
|
381
|
+
# if method_name != :[]=
|
382
|
+
# logger.trace(
|
383
|
+
# 'Registered C-defined patch',
|
384
|
+
# implementation: impl,
|
385
|
+
# module: target_module_name,
|
386
|
+
# method: method_name,
|
387
|
+
# visibility: visibility)
|
388
|
+
# end
|
389
|
+
underlying_method_name.to_sym
|
390
|
+
end
|
391
|
+
|
319
392
|
# @return [Boolean]
|
320
393
|
def skip_contrast_analysis?
|
321
394
|
return true if in_contrast_scope?
|
395
|
+
return true unless defined?(Contrast::Agent::REQUEST_TRACKER)
|
322
396
|
return true unless Contrast::Agent::REQUEST_TRACKER.current&.analyze_request?
|
323
397
|
|
324
398
|
false
|