contrast-agent 4.4.1 → 4.9.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +6 -1
- data/.gitmodules +1 -1
- data/.simplecov +2 -1
- data/Gemfile +1 -1
- data/LICENSE.txt +1 -1
- data/Rakefile +2 -3
- data/exe/contrast_service +1 -1
- data/ext/build_funchook.rb +4 -4
- data/ext/cs__assess_active_record_named/cs__active_record_named.c +1 -1
- data/ext/cs__assess_active_record_named/extconf.rb +1 -1
- data/ext/cs__assess_array/cs__assess_array.c +1 -1
- data/ext/cs__assess_array/extconf.rb +1 -1
- data/ext/cs__assess_basic_object/cs__assess_basic_object.c +1 -1
- data/ext/cs__assess_basic_object/extconf.rb +1 -1
- data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +1 -1
- data/ext/cs__assess_fiber_track/extconf.rb +1 -1
- data/ext/cs__assess_hash/cs__assess_hash.c +4 -2
- data/ext/cs__assess_hash/extconf.rb +1 -1
- data/ext/cs__assess_kernel/cs__assess_kernel.c +1 -1
- data/ext/cs__assess_kernel/extconf.rb +1 -1
- data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +1 -1
- data/ext/cs__assess_marshal_module/extconf.rb +1 -1
- data/ext/cs__assess_module/cs__assess_module.c +1 -1
- data/ext/cs__assess_module/extconf.rb +1 -1
- data/ext/cs__assess_regexp/cs__assess_regexp.c +1 -1
- data/ext/cs__assess_regexp/extconf.rb +1 -1
- data/ext/cs__assess_string/cs__assess_string.c +1 -1
- data/ext/cs__assess_string/extconf.rb +1 -1
- data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +1 -1
- data/ext/cs__assess_string_interpolation26/extconf.rb +1 -1
- data/ext/cs__assess_yield_track/cs__assess_yield_track.c +1 -1
- data/ext/cs__assess_yield_track/extconf.rb +1 -1
- data/ext/cs__common/cs__common.c +5 -5
- data/ext/cs__common/cs__common.h +4 -4
- data/ext/cs__common/extconf.rb +1 -1
- data/ext/cs__contrast_patch/cs__contrast_patch.c +22 -25
- data/ext/cs__contrast_patch/extconf.rb +1 -1
- data/ext/cs__protect_kernel/cs__protect_kernel.c +1 -1
- data/ext/cs__protect_kernel/extconf.rb +1 -1
- data/ext/extconf_common.rb +2 -6
- data/lib/contrast-agent.rb +1 -1
- data/lib/contrast.rb +44 -15
- data/lib/contrast/agent.rb +1 -3
- data/lib/contrast/agent/assess.rb +2 -2
- data/lib/contrast/agent/assess/contrast_event.rb +54 -72
- data/lib/contrast/agent/assess/contrast_object.rb +3 -3
- data/lib/contrast/agent/assess/events/event_factory.rb +3 -2
- data/lib/contrast/agent/assess/events/source_event.rb +7 -2
- data/lib/contrast/agent/assess/finalizers/freeze.rb +1 -1
- data/lib/contrast/agent/assess/finalizers/hash.rb +28 -38
- data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +24 -20
- data/lib/contrast/agent/assess/policy/patcher.rb +17 -22
- data/lib/contrast/agent/assess/policy/policy.rb +2 -2
- data/lib/contrast/agent/assess/policy/policy_node.rb +26 -34
- data/lib/contrast/agent/assess/policy/policy_scanner.rb +4 -6
- data/lib/contrast/agent/assess/policy/preshift.rb +8 -6
- data/lib/contrast/agent/assess/policy/propagation_method.rb +12 -25
- data/lib/contrast/agent/assess/policy/propagation_node.rb +20 -9
- data/lib/contrast/agent/assess/policy/propagator.rb +2 -1
- data/lib/contrast/agent/assess/policy/propagator/append.rb +1 -1
- data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
- data/lib/contrast/agent/assess/policy/propagator/center.rb +3 -2
- data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
- data/lib/contrast/agent/assess/policy/propagator/database_write.rb +4 -7
- data/lib/contrast/agent/assess/policy/propagator/insert.rb +4 -2
- data/lib/contrast/agent/assess/policy/propagator/keep.rb +1 -1
- data/lib/contrast/agent/assess/policy/propagator/match_data.rb +3 -2
- data/lib/contrast/agent/assess/policy/propagator/next.rb +1 -1
- data/lib/contrast/agent/assess/policy/propagator/prepend.rb +1 -1
- data/lib/contrast/agent/assess/policy/propagator/rack_protection.rb +73 -0
- data/lib/contrast/agent/assess/policy/propagator/remove.rb +23 -19
- data/lib/contrast/agent/assess/policy/propagator/replace.rb +1 -1
- data/lib/contrast/agent/assess/policy/propagator/reverse.rb +1 -1
- data/lib/contrast/agent/assess/policy/propagator/select.rb +3 -13
- data/lib/contrast/agent/assess/policy/propagator/splat.rb +1 -1
- data/lib/contrast/agent/assess/policy/propagator/split.rb +13 -14
- data/lib/contrast/agent/assess/policy/propagator/substitution.rb +4 -11
- data/lib/contrast/agent/assess/policy/propagator/trim.rb +64 -45
- data/lib/contrast/agent/assess/policy/rewriter_patch.rb +14 -11
- data/lib/contrast/agent/assess/policy/source_method.rb +97 -86
- data/lib/contrast/agent/assess/policy/source_node.rb +1 -1
- data/lib/contrast/agent/assess/policy/source_validation/cross_site_validator.rb +8 -6
- data/lib/contrast/agent/assess/policy/source_validation/source_validation.rb +2 -4
- data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +7 -3
- data/lib/contrast/agent/assess/policy/trigger/xpath.rb +7 -11
- data/lib/contrast/agent/assess/policy/trigger_method.rb +104 -77
- data/lib/contrast/agent/assess/policy/trigger_node.rb +6 -5
- data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +5 -4
- data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -3
- data/lib/contrast/agent/assess/policy/trigger_validation/trigger_validation.rb +1 -1
- data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +2 -9
- data/lib/contrast/agent/assess/properties.rb +1 -1
- data/lib/contrast/agent/assess/property/evented.rb +9 -6
- data/lib/contrast/agent/assess/property/tagged.rb +1 -1
- data/lib/contrast/agent/assess/property/updated.rb +1 -1
- data/lib/contrast/agent/assess/rule/provider.rb +1 -1
- data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +12 -6
- data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +5 -2
- data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +8 -10
- data/lib/contrast/agent/assess/tag.rb +1 -1
- data/lib/contrast/agent/assess/tracker.rb +1 -1
- data/lib/contrast/agent/at_exit_hook.rb +4 -4
- data/lib/contrast/agent/class_reopener.rb +10 -7
- data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +1 -1
- data/lib/contrast/agent/deadzone/policy/policy.rb +7 -3
- data/lib/contrast/agent/disable_reaction.rb +5 -8
- data/lib/contrast/agent/exclusion_matcher.rb +8 -15
- data/lib/contrast/agent/inventory.rb +1 -2
- data/lib/contrast/agent/inventory/dependencies.rb +3 -1
- data/lib/contrast/agent/inventory/dependency_analysis.rb +3 -7
- data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +38 -28
- data/lib/contrast/agent/inventory/policy/datastores.rb +4 -5
- data/lib/contrast/agent/inventory/policy/policy.rb +2 -2
- data/lib/contrast/agent/inventory/policy/trigger_node.rb +1 -1
- data/lib/contrast/agent/middleware.rb +52 -80
- data/lib/contrast/agent/module_data.rb +4 -4
- data/lib/contrast/agent/patching/policy/after_load_patch.rb +4 -4
- data/lib/contrast/agent/patching/policy/after_load_patcher.rb +10 -10
- data/lib/contrast/agent/patching/policy/method_policy.rb +7 -3
- data/lib/contrast/agent/patching/policy/module_policy.rb +15 -8
- data/lib/contrast/agent/patching/policy/patch.rb +32 -38
- data/lib/contrast/agent/patching/policy/patch_status.rb +7 -8
- data/lib/contrast/agent/patching/policy/patcher.rb +29 -28
- data/lib/contrast/agent/patching/policy/policy.rb +16 -25
- data/lib/contrast/agent/patching/policy/policy_node.rb +17 -8
- data/lib/contrast/agent/patching/policy/trigger_node.rb +22 -9
- data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +2 -2
- 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 +3 -4
- data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +2 -2
- data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +6 -10
- data/lib/contrast/agent/protect/policy/policy.rb +2 -2
- data/lib/contrast/agent/protect/policy/rule_applicator.rb +8 -10
- data/lib/contrast/agent/protect/policy/trigger_node.rb +1 -1
- data/lib/contrast/agent/protect/rule.rb +1 -1
- data/lib/contrast/agent/protect/rule/base.rb +26 -40
- data/lib/contrast/agent/protect/rule/base_service.rb +10 -6
- data/lib/contrast/agent/protect/rule/cmd_injection.rb +19 -24
- data/lib/contrast/agent/protect/rule/default_scanner.rb +1 -1
- data/lib/contrast/agent/protect/rule/deserialization.rb +7 -14
- data/lib/contrast/agent/protect/rule/http_method_tampering.rb +4 -15
- data/lib/contrast/agent/protect/rule/no_sqli.rb +7 -3
- data/lib/contrast/agent/protect/rule/no_sqli/mongo_no_sql_scanner.rb +2 -4
- data/lib/contrast/agent/protect/rule/path_traversal.rb +7 -11
- data/lib/contrast/agent/protect/rule/sqli.rb +3 -3
- data/lib/contrast/agent/protect/rule/sqli/default_sql_scanner.rb +1 -1
- data/lib/contrast/agent/protect/rule/sqli/mysql_sql_scanner.rb +1 -1
- data/lib/contrast/agent/protect/rule/sqli/postgres_sql_scanner.rb +2 -2
- data/lib/contrast/agent/protect/rule/sqli/sqlite_sql_scanner.rb +1 -1
- data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +2 -2
- data/lib/contrast/agent/protect/rule/xss.rb +2 -2
- data/lib/contrast/agent/protect/rule/xxe.rb +6 -13
- data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +2 -3
- data/lib/contrast/agent/reaction_processor.rb +14 -14
- data/lib/contrast/agent/request.rb +29 -27
- data/lib/contrast/agent/request_context.rb +20 -30
- data/lib/contrast/agent/request_handler.rb +6 -4
- data/lib/contrast/agent/response.rb +3 -4
- data/lib/contrast/agent/rewriter.rb +10 -7
- data/lib/contrast/agent/rule_set.rb +6 -5
- data/lib/contrast/agent/scope.rb +1 -1
- data/lib/contrast/agent/service_heartbeat.rb +5 -7
- data/lib/contrast/agent/static_analysis.rb +7 -6
- data/lib/contrast/agent/thread.rb +3 -5
- data/lib/contrast/agent/thread_watcher.rb +4 -5
- data/lib/contrast/agent/tracepoint_hook.rb +6 -6
- data/lib/contrast/agent/version.rb +2 -2
- data/lib/contrast/agent/worker_thread.rb +1 -1
- data/lib/contrast/api.rb +1 -1
- data/lib/contrast/api/communication.rb +1 -1
- data/lib/contrast/api/communication/connection_status.rb +1 -1
- data/lib/contrast/api/communication/messaging_queue.rb +5 -6
- data/lib/contrast/api/communication/response_processor.rb +13 -15
- data/lib/contrast/api/communication/service_lifecycle.rb +10 -7
- data/lib/contrast/api/communication/socket.rb +1 -1
- data/lib/contrast/api/communication/socket_client.rb +23 -32
- data/lib/contrast/api/communication/speedracer.rb +10 -15
- 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 +1 -1
- data/lib/contrast/api/decorators/address.rb +3 -4
- data/lib/contrast/api/decorators/agent_startup.rb +8 -10
- data/lib/contrast/api/decorators/application_settings.rb +1 -1
- data/lib/contrast/api/decorators/application_startup.rb +14 -10
- data/lib/contrast/api/decorators/application_update.rb +1 -5
- data/lib/contrast/api/decorators/http_request.rb +4 -8
- data/lib/contrast/api/decorators/input_analysis.rb +1 -1
- data/lib/contrast/api/decorators/instrumentation_mode.rb +35 -0
- data/lib/contrast/api/decorators/library.rb +9 -7
- data/lib/contrast/api/decorators/library_usage_update.rb +1 -1
- data/lib/contrast/api/decorators/message.rb +10 -10
- data/lib/contrast/api/decorators/rasp_rule_sample.rb +1 -1
- data/lib/contrast/api/decorators/route_coverage.rb +1 -1
- data/lib/contrast/api/decorators/server_features.rb +1 -1
- data/lib/contrast/api/decorators/trace_event.rb +4 -2
- data/lib/contrast/api/decorators/trace_event_object.rb +4 -7
- data/lib/contrast/api/decorators/trace_event_signature.rb +1 -1
- data/lib/contrast/api/decorators/trace_taint_range.rb +1 -1
- data/lib/contrast/api/decorators/trace_taint_range_tags.rb +2 -7
- data/lib/contrast/api/decorators/user_input.rb +1 -1
- data/lib/contrast/components/agent.rb +20 -26
- data/lib/contrast/components/app_context.rb +12 -16
- data/lib/contrast/components/assess.rb +20 -25
- data/lib/contrast/components/base.rb +40 -0
- data/lib/contrast/components/config.rb +3 -4
- data/lib/contrast/components/contrast_service.rb +13 -19
- data/lib/contrast/components/heap_dump.rb +6 -5
- data/lib/contrast/components/inventory.rb +3 -8
- data/lib/contrast/components/logger.rb +2 -3
- data/lib/contrast/components/protect.rb +14 -20
- data/lib/contrast/components/sampling.rb +14 -8
- data/lib/contrast/components/scope.rb +2 -5
- data/lib/contrast/components/settings.rb +28 -103
- data/lib/contrast/config.rb +1 -1
- data/lib/contrast/config/agent_configuration.rb +1 -1
- data/lib/contrast/config/application_configuration.rb +1 -1
- data/lib/contrast/config/assess_configuration.rb +1 -1
- data/lib/contrast/config/assess_rules_configuration.rb +2 -4
- data/lib/contrast/config/base_configuration.rb +5 -6
- data/lib/contrast/config/default_value.rb +1 -1
- data/lib/contrast/config/exception_configuration.rb +2 -6
- data/lib/contrast/config/heap_dump_configuration.rb +13 -7
- data/lib/contrast/config/inventory_configuration.rb +1 -1
- data/lib/contrast/config/logger_configuration.rb +2 -6
- data/lib/contrast/config/protect_configuration.rb +1 -1
- data/lib/contrast/config/protect_rule_configuration.rb +23 -1
- data/lib/contrast/config/protect_rules_configuration.rb +1 -1
- data/lib/contrast/config/root_configuration.rb +1 -1
- data/lib/contrast/config/ruby_configuration.rb +1 -1
- data/lib/contrast/config/sampling_configuration.rb +1 -1
- data/lib/contrast/config/server_configuration.rb +1 -1
- data/lib/contrast/config/service_configuration.rb +1 -1
- data/lib/contrast/configuration.rb +7 -19
- data/lib/contrast/extension/assess.rb +1 -1
- data/lib/contrast/extension/assess/array.rb +4 -11
- data/lib/contrast/extension/assess/erb.rb +2 -8
- data/lib/contrast/extension/assess/eval_trigger.rb +5 -14
- data/lib/contrast/extension/assess/exec_trigger.rb +4 -14
- data/lib/contrast/extension/assess/fiber.rb +9 -18
- data/lib/contrast/extension/assess/hash.rb +4 -4
- data/lib/contrast/extension/assess/kernel.rb +5 -14
- data/lib/contrast/extension/assess/marshal.rb +7 -15
- data/lib/contrast/extension/assess/regexp.rb +7 -11
- data/lib/contrast/extension/assess/string.rb +9 -7
- data/lib/contrast/extension/delegator.rb +1 -1
- data/lib/contrast/extension/inventory.rb +1 -1
- data/lib/contrast/extension/kernel.rb +3 -3
- data/lib/contrast/extension/module.rb +1 -1
- data/lib/contrast/extension/protect.rb +1 -1
- data/lib/contrast/extension/protect/kernel.rb +1 -6
- data/lib/contrast/extension/protect/psych.rb +1 -1
- data/lib/contrast/extension/thread.rb +1 -1
- data/lib/contrast/framework/base_support.rb +1 -1
- data/lib/contrast/framework/manager.rb +7 -12
- data/lib/contrast/framework/platform_version.rb +1 -1
- data/lib/contrast/framework/rack/patch/session_cookie.rb +12 -25
- data/lib/contrast/framework/rack/patch/support.rb +7 -5
- data/lib/contrast/framework/rack/support.rb +1 -1
- data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +1 -1
- data/lib/contrast/framework/rails/patch/assess_configuration.rb +13 -10
- data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +4 -4
- data/lib/contrast/framework/rails/patch/support.rb +42 -36
- data/lib/contrast/framework/rails/railtie.rb +34 -0
- data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +5 -2
- data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +3 -1
- data/lib/contrast/framework/rails/rewrite/active_record_named.rb +6 -5
- data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +3 -1
- data/lib/contrast/framework/rails/support.rb +3 -3
- data/lib/contrast/framework/sinatra/support.rb +4 -2
- data/lib/contrast/funchook/funchook.rb +6 -9
- data/lib/contrast/logger/application.rb +14 -16
- data/lib/contrast/logger/format.rb +3 -6
- data/lib/contrast/logger/log.rb +27 -10
- data/lib/contrast/logger/request.rb +2 -7
- data/lib/contrast/logger/time.rb +1 -1
- data/lib/contrast/security_exception.rb +2 -2
- data/lib/contrast/tasks/config.rb +1 -1
- data/lib/contrast/tasks/service.rb +7 -8
- data/lib/contrast/utils/assess/sampling_util.rb +3 -4
- data/lib/contrast/utils/assess/tracking_util.rb +4 -7
- data/lib/contrast/utils/class_util.rb +15 -11
- data/lib/contrast/utils/duck_utils.rb +1 -1
- data/lib/contrast/utils/env_configuration_item.rb +1 -1
- data/lib/contrast/utils/hash_digest.rb +16 -24
- data/lib/contrast/utils/heap_dump_util.rb +6 -4
- data/lib/contrast/utils/invalid_configuration_util.rb +5 -4
- data/lib/contrast/utils/inventory_util.rb +3 -4
- data/lib/contrast/utils/io_util.rb +4 -6
- data/lib/contrast/utils/job_servers_running.rb +14 -8
- data/lib/contrast/utils/object_share.rb +1 -1
- data/lib/contrast/utils/os.rb +5 -5
- data/lib/contrast/utils/preflight_util.rb +1 -1
- data/lib/contrast/utils/resource_loader.rb +1 -1
- data/lib/contrast/utils/ruby_ast_rewriter.rb +3 -2
- data/lib/contrast/utils/sha256_builder.rb +1 -1
- data/lib/contrast/utils/stack_trace_utils.rb +1 -1
- data/lib/contrast/utils/string_utils.rb +3 -4
- data/lib/contrast/utils/tag_util.rb +26 -20
- data/lib/contrast/utils/thread_tracker.rb +1 -1
- data/lib/contrast/utils/timer.rb +1 -1
- data/resources/assess/policy.json +60 -2
- data/resources/deadzone/policy.json +7 -17
- data/ruby-agent.gemspec +25 -21
- data/service_executables/VERSION +1 -1
- data/service_executables/linux/contrast-service +0 -0
- data/service_executables/mac/contrast-service +0 -0
- data/sonar-project.properties +9 -0
- metadata +108 -51
- data/lib/contrast/agent/inventory/gemfile_digest_cache.rb +0 -38
- data/lib/contrast/agent/railtie.rb +0 -31
- data/lib/contrast/common_agent_configuration.rb +0 -87
- data/lib/contrast/components/interface.rb +0 -195
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c)
|
1
|
+
# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
$TO_MAKE = File.basename(__dir__)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c)
|
1
|
+
# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
$TO_MAKE = File.basename(__dir__)
|
data/ext/extconf_common.rb
CHANGED
@@ -1,15 +1,11 @@
|
|
1
|
-
# Copyright (c)
|
1
|
+
# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
require 'mkmf'
|
5
5
|
require_relative '../lib/contrast/agent/version'
|
6
6
|
|
7
|
-
def name
|
8
|
-
$TO_MAKE
|
9
|
-
end
|
10
|
-
|
11
7
|
def make!
|
12
|
-
create_makefile "#{
|
8
|
+
create_makefile "#{ $TO_MAKE }/#{ $TO_MAKE }"
|
13
9
|
end
|
14
10
|
|
15
11
|
def ext_path
|
data/lib/contrast-agent.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c)
|
1
|
+
# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
# We need this to make bundler happy since it's the name of the gem, unless we
|
data/lib/contrast.rb
CHANGED
@@ -1,13 +1,9 @@
|
|
1
|
-
# Copyright (c)
|
1
|
+
# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
# Used to prevent deprecation warnings from flooding stdout
|
5
5
|
ENV['PB_IGNORE_DEPRECATIONS'] = 'true'
|
6
6
|
|
7
|
-
# Top-level namespace for Contrast Security agent
|
8
|
-
module Contrast
|
9
|
-
end
|
10
|
-
|
11
7
|
# Some developers override various methods on Object, which can often involve
|
12
8
|
# changing expected method parity/behavior which in turn prevents us from being
|
13
9
|
# able to reliably use affected methods.
|
@@ -25,25 +21,58 @@ class Object
|
|
25
21
|
alias_method :cs__singleton_class, :singleton_class
|
26
22
|
end
|
27
23
|
|
28
|
-
|
29
|
-
#
|
30
|
-
|
24
|
+
if RUBY_VERSION >= '3.0.0'
|
25
|
+
# This fixes Ruby 3.0 issues with Module#(some instance method) patching by preventing the prepending of
|
26
|
+
# a JSON helper on protobuf load. String.instance_method(:+) is one of the most noticable.
|
27
|
+
# TODO: RUBY-1132 Remove this once Ruby 3 is fixed.
|
28
|
+
# See bug here: https://bugs.ruby-lang.org/issues/17725
|
29
|
+
class Class
|
30
|
+
alias_method(:cs__orig_prepend, :prepend)
|
31
|
+
def prepend other
|
32
|
+
include(other)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
require 'contrast/components/agent'
|
38
|
+
require 'contrast/components/app_context'
|
39
|
+
require 'contrast/components/assess'
|
40
|
+
require 'contrast/components/config'
|
41
|
+
require 'contrast/components/contrast_service'
|
42
|
+
require 'contrast/components/inventory'
|
43
|
+
require 'contrast/components/logger'
|
44
|
+
require 'contrast/components/protect'
|
45
|
+
require 'contrast/components/sampling'
|
46
|
+
require 'contrast/components/scope'
|
47
|
+
require 'contrast/components/settings'
|
48
|
+
|
49
|
+
module Contrast
|
50
|
+
SCOPE = Contrast::Components::Scope::Interface.new
|
51
|
+
CONFIG = Contrast::Components::Config::Interface.new
|
52
|
+
SETTINGS = Contrast::Components::Settings::Interface.new
|
53
|
+
ASSESS = Contrast::Components::Assess::Interface.new
|
54
|
+
PROTECT = Contrast::Components::Protect::Interface.new
|
55
|
+
INVENTORY = Contrast::Components::Inventory::Interface.new
|
56
|
+
LOGGER = Contrast::Components::Logger::Interface.new
|
57
|
+
AGENT = Contrast::Components::Agent::Interface.new
|
58
|
+
CONTRAST_SERVICE = Contrast::Components::ContrastService::Interface.new
|
59
|
+
APP_CONTEXT = Contrast::Components::AppContext::Interface.new
|
60
|
+
end
|
31
61
|
|
32
62
|
# This needs to be required very early, after component interfaces, and before instrumentation attempts
|
33
63
|
require 'contrast/funchook/funchook'
|
34
64
|
|
35
|
-
# shared configuration support
|
36
|
-
require 'contrast/config'
|
37
|
-
require 'contrast/configuration'
|
38
|
-
|
39
65
|
require 'contrast/agent/version'
|
40
66
|
|
41
|
-
# errors and exceptions
|
42
|
-
require 'contrast/security_exception'
|
43
|
-
|
44
67
|
# shared utils
|
45
68
|
require 'contrast/utils/timer'
|
46
69
|
require 'contrast/utils/preflight_util'
|
47
70
|
|
48
71
|
require 'contrast/utils/assess/sampling_util'
|
49
72
|
require 'contrast/agent'
|
73
|
+
|
74
|
+
if RUBY_VERSION >= '3.0.0'
|
75
|
+
# Put prepend back as it was.
|
76
|
+
Class.alias_method(:prepend, :cs__orig_prepend)
|
77
|
+
Class.remove_method(:cs__orig_prepend)
|
78
|
+
end
|
data/lib/contrast/agent.rb
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c)
|
1
|
+
# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
require 'English'
|
@@ -27,8 +27,6 @@ require 'contrast/utils/string_utils'
|
|
27
27
|
require 'contrast/utils/io_util'
|
28
28
|
require 'contrast/utils/os'
|
29
29
|
|
30
|
-
require 'contrast/common_agent_configuration'
|
31
|
-
|
32
30
|
require 'contrast/utils/hash_digest'
|
33
31
|
require 'contrast/utils/invalid_configuration_util'
|
34
32
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c)
|
1
|
+
# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
module Contrast
|
@@ -10,7 +10,7 @@ module Contrast
|
|
10
10
|
module Assess
|
11
11
|
require 'contrast/agent/assess/tracker'
|
12
12
|
require 'contrast/agent/module_data'
|
13
|
-
require 'contrast/agent/rewriter'
|
13
|
+
require 'contrast/agent/rewriter' if RUBY_VERSION < '2.6.0' # TODO: RUBY-714 remove guard w/ EOL of 2.5
|
14
14
|
require 'contrast/agent/assess/policy/preshift'
|
15
15
|
|
16
16
|
# Dynamic Sources
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c)
|
1
|
+
# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
require 'contrast/utils/assess/tracking_util'
|
@@ -8,43 +8,30 @@ require 'contrast/utils/object_share'
|
|
8
8
|
require 'contrast/utils/stack_trace_utils'
|
9
9
|
require 'contrast/utils/string_utils'
|
10
10
|
require 'contrast/utils/timer'
|
11
|
-
require 'contrast/components/interface'
|
12
11
|
require 'contrast/agent/assess/contrast_object'
|
13
12
|
|
14
13
|
module Contrast
|
15
14
|
module Agent
|
16
15
|
module Assess
|
17
|
-
# This class holds the data about an event in the application
|
18
|
-
#
|
19
|
-
# the object to which this event belongs ends in a trigger.
|
16
|
+
# This class holds the data about an event in the application We'll use it to build an event that TeamServer can
|
17
|
+
# consume if the object to which this event belongs ends in a trigger.
|
20
18
|
#
|
21
19
|
# @attr_reader event_id [Integer] the atomic id of this event
|
22
|
-
# @attr_reader policy_node [Contrast::Agent::Assess::Policy::PolicyNode]
|
23
|
-
#
|
24
|
-
# @attr_reader
|
25
|
-
#
|
26
|
-
# @attr_reader
|
27
|
-
#
|
28
|
-
# @attr_reader
|
29
|
-
#
|
30
|
-
# @attr_reader
|
31
|
-
#
|
32
|
-
# @attr_reader ret [Contrast::Agent::Assess::ContrastObject] the safe
|
33
|
-
# representation of the Return of the invoked method
|
34
|
-
# @attr_reader args [Array<Contrast::Agent::Assess::ContrastObject>] the
|
35
|
-
# safe representation of the Arguments with which the method was invoked
|
20
|
+
# @attr_reader policy_node [Contrast::Agent::Assess::Policy::PolicyNode] the node that governs this event.
|
21
|
+
# @attr_reader stack_trace [Array<String>] the execution stack at the time the method for this event was invoked
|
22
|
+
# @attr_reader time [Integer] the time, in epoch ms, when this event was created
|
23
|
+
# @attr_reader thread [Integer] the object id of the thread on which this event was generated
|
24
|
+
# @attr_reader object [Contrast::Agent::Assess::ContrastObject] the safe representation of the Object on which
|
25
|
+
# the method was invoked
|
26
|
+
# @attr_reader ret [Contrast::Agent::Assess::ContrastObject] the safe representation of the Return of the invoked
|
27
|
+
# method
|
28
|
+
# @attr_reader args [Array<Contrast::Agent::Assess::ContrastObject>] the safe representation of the Arguments
|
29
|
+
# with which the method was invoked
|
36
30
|
class ContrastEvent
|
37
|
-
include Contrast::Components::Interface
|
38
|
-
access_component :analysis
|
39
31
|
|
40
|
-
attr_reader :event_id, :policy_node, :stack_trace, :time, :thread,
|
41
|
-
:object,
|
42
|
-
:ret,
|
43
|
-
:args,
|
44
|
-
:tags
|
32
|
+
attr_reader :event_id, :policy_node, :stack_trace, :time, :thread, :object, :ret, :args, :tags
|
45
33
|
|
46
|
-
# We need this to track the parent id's of events to build up a flow
|
47
|
-
# chart of the finding
|
34
|
+
# We need this to track the parent id's of events to build up a flow chart of the finding
|
48
35
|
@atomic_id = 0
|
49
36
|
@atomic_mutex = Mutex.new
|
50
37
|
def self.next_atomic_id
|
@@ -56,26 +43,13 @@ module Contrast
|
|
56
43
|
end
|
57
44
|
end
|
58
45
|
|
59
|
-
# @param policy_node [Contrast::Agent::Assess::Policy::PolicyNode]
|
60
|
-
# the node that governs this event.
|
46
|
+
# @param policy_node [Contrast::Agent::Assess::Policy::PolicyNode] the node that governs this event.
|
61
47
|
# @param tagged [Object] the Target to which this event pertains.
|
62
48
|
# @param object [Object] the Object on which the method was invoked
|
63
49
|
# @param ret [Object] the Return of the invoked method
|
64
|
-
# @param args [Array<Object>] the Arguments with which the method
|
65
|
-
# was invoked
|
50
|
+
# @param args [Array<Object>] the Arguments with which the method was invoked
|
66
51
|
def initialize policy_node, tagged, object, ret, args
|
67
52
|
@policy_node = policy_node
|
68
|
-
|
69
|
-
# Capture stacktraces only as configured.
|
70
|
-
#
|
71
|
-
# So long as this event is built in a factory, we know Contrast Code
|
72
|
-
# will be the first three events
|
73
|
-
@stack_trace = if ASSESS.capture_stacktrace?(policy_node)
|
74
|
-
caller(3, 20)
|
75
|
-
else
|
76
|
-
Contrast::Utils::ObjectShare::EMPTY_ARRAY
|
77
|
-
end
|
78
|
-
|
79
53
|
@time = Contrast::Utils::Timer.now_ms
|
80
54
|
@thread = Thread.current.object_id
|
81
55
|
|
@@ -84,17 +58,17 @@ module Contrast
|
|
84
58
|
@tags = Contrast::Agent::Assess::Tracker.properties(tagged)&.tags
|
85
59
|
find_parent_events!(policy_node, object, ret, args)
|
86
60
|
snapshot!(object, ret, args)
|
61
|
+
capture_stacktrace!
|
87
62
|
end
|
88
63
|
|
89
64
|
def parent_events
|
90
65
|
@_parent_events ||= []
|
91
66
|
end
|
92
67
|
|
93
|
-
# We have to do a little work to figure out what our TS appropriate
|
94
|
-
#
|
95
|
-
# 1) If my policy_node has a target, work on targets. Else, work on sources.
|
96
|
-
#
|
97
|
-
# The only type of policy_node w/o targets is a Trigger, but that may
|
68
|
+
# We have to do a little work to figure out what our TS appropriate target is. To break this down, the logic is
|
69
|
+
# as follows:
|
70
|
+
# 1) If my policy_node has a target, work on targets. Else, work on sources. Per TS law, each policy_node must
|
71
|
+
# have at least a source or a target. The only type of policy_node w/o targets is a Trigger, but that may
|
98
72
|
# change.
|
99
73
|
# 2) I'll set the event's source and target to TS values.
|
100
74
|
# 3) Return the first source/target as the taint target.
|
@@ -117,21 +91,17 @@ module Contrast
|
|
117
91
|
|
118
92
|
private
|
119
93
|
|
120
|
-
# Parent events are the events of all the sources of this event which
|
121
|
-
#
|
122
|
-
# any of the sources were tracked, there may be more than one parent.
|
94
|
+
# Parent events are the events of all the sources of this event which were tracked prior to this event
|
95
|
+
# occurring. Depending on which, if any of the sources were tracked, there may be more than one parent.
|
123
96
|
#
|
124
|
-
# All events except for [Contrast::Agent::Assess::Events::SourceEvent]
|
125
|
-
# will have at least one parent.
|
97
|
+
# All events except for [Contrast::Agent::Assess::Events::SourceEvent] will have at least one parent.
|
126
98
|
#
|
127
99
|
# We set those events to this event's instance variables.
|
128
100
|
#
|
129
|
-
# @param policy_node [Contrast::Agent::Assess::Policy::PolicyNode]
|
130
|
-
# the node that governs this event.
|
101
|
+
# @param policy_node [Contrast::Agent::Assess::Policy::PolicyNode] the node that governs this event.
|
131
102
|
# @param object [Object] the Object on which the method was invoked
|
132
103
|
# @param ret [Object] the Return of the invoked method
|
133
|
-
# @param args [Array<Object>] the Arguments with which the method
|
134
|
-
# was invoked
|
104
|
+
# @param args [Array<Object>] the Arguments with which the method was invoked
|
135
105
|
def find_parent_events! policy_node, object, ret, args
|
136
106
|
policy_node.sources.each do |source_marker|
|
137
107
|
source = value_of_source(source_marker, object, ret, args)
|
@@ -145,10 +115,8 @@ module Contrast
|
|
145
115
|
# @param source [String] the marker for the source type
|
146
116
|
# @param object [Object] the Object on which the method was invoked
|
147
117
|
# @param ret [Object] the Return of the invoked method
|
148
|
-
# @param args [Array<Object>] the Arguments with which the method
|
149
|
-
#
|
150
|
-
# @return [Object,nil] the literal value of the source indicated by the
|
151
|
-
# given marker
|
118
|
+
# @param args [Array<Object>] the Arguments with which the method was invoked
|
119
|
+
# @return [Object,nil] the literal value of the source indicated by the given marker
|
152
120
|
def value_of_source source, object, ret, args
|
153
121
|
case source
|
154
122
|
when Contrast::Utils::ObjectShare::OBJECT_KEY
|
@@ -160,16 +128,13 @@ module Contrast
|
|
160
128
|
end
|
161
129
|
end
|
162
130
|
|
163
|
-
# Everything* is mutable in Ruby. As such, to ensure we can accurately
|
164
|
-
#
|
165
|
-
#
|
166
|
-
# them for our later use. We set those safe values to this event's
|
167
|
-
# instance variables.
|
131
|
+
# Everything* is mutable in Ruby. As such, to ensure we can accurately report the application state at the time
|
132
|
+
# of this method's invocation, we have to snapshot the given values, making safe representations of them for
|
133
|
+
# our later use. We set those safe values to this event's instance variables.
|
168
134
|
#
|
169
135
|
# @param object [Object] the Object on which the method was invoked
|
170
136
|
# @param ret [Object] the Return of the invoked method
|
171
|
-
# @param args [Array<Object>] the Arguments with which the method
|
172
|
-
# was invoked
|
137
|
+
# @param args [Array<Object>] the Arguments with which the method was invoked
|
173
138
|
def snapshot! object, ret, args
|
174
139
|
@object = Contrast::Agent::Assess::ContrastObject.new(object) if object
|
175
140
|
@ret = Contrast::Agent::Assess::ContrastObject.new(ret) if ret
|
@@ -177,18 +142,35 @@ module Contrast
|
|
177
142
|
self
|
178
143
|
end
|
179
144
|
|
180
|
-
# Given an array of arguments, copy them into a safe, meaning String,
|
181
|
-
#
|
145
|
+
# Given an array of arguments, copy them into a safe, meaning String, format that we can use to send to SR and
|
146
|
+
# TS for rendering.
|
182
147
|
#
|
183
148
|
# @param args [Array<Object>] the arguments to translate
|
184
|
-
# @return [Array<Contrast::Agent::Assess::ContrastObject>] the String forms of those Objects, as
|
185
|
-
#
|
149
|
+
# @return [Array<Contrast::Agent::Assess::ContrastObject>] the String forms of those Objects, as determined by
|
150
|
+
# Contrast::Utils::ClassUtil.to_contrast_string
|
186
151
|
def safe_args_representation args
|
187
152
|
return unless args
|
188
153
|
return Contrast::Utils::ObjectShare::EMPTY_ARRAY if args.empty?
|
189
154
|
|
190
155
|
args.map { |arg| arg ? Contrast::Agent::Assess::ContrastObject.new(arg) : nil }
|
191
156
|
end
|
157
|
+
|
158
|
+
# Capture stack traces only as configured. We'll use this to grab the start of the call stack as if the
|
159
|
+
# instrumented method were the caller. This means we'll start at the entry just after the first block of
|
160
|
+
# Contrast code.
|
161
|
+
def capture_stacktrace!
|
162
|
+
# If we're configured to not capture the stacktrace, usually for performance reasons, then don't and return an
|
163
|
+
# empty array instead
|
164
|
+
unless ::Contrast::ASSESS.capture_stacktrace?(policy_node)
|
165
|
+
@stack_trace = Contrast::Utils::ObjectShare::EMPTY_ARRAY
|
166
|
+
return
|
167
|
+
end
|
168
|
+
|
169
|
+
# Otherwise, find where in the stack the application / Ruby code starts
|
170
|
+
start = caller(0, 20)&.find_index { |stack| !stack.include?('/lib/contrast') }
|
171
|
+
# And then use that to build out the reported stacktrace, or a fallback if we couldn't find it.
|
172
|
+
@stack_trace = start ? caller(start + 1, 20) : caller(20, 20)
|
173
|
+
end
|
192
174
|
end
|
193
175
|
end
|
194
176
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c)
|
1
|
+
# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
require 'contrast/utils/class_util'
|
@@ -34,14 +34,14 @@ module Contrast
|
|
34
34
|
def initialize object
|
35
35
|
if object
|
36
36
|
@object = Contrast::Utils::ClassUtil.to_contrast_string(object)
|
37
|
-
@object_type = object.cs__class.
|
37
|
+
@object_type = object.cs__class.cs__name
|
38
38
|
# TODO: RUBY-1084 determine if we need to copy these tags to
|
39
39
|
# restore immutability. For instance, if these tags were on a
|
40
40
|
# String that was then #reverse!'d, would our tags be wrong?
|
41
41
|
@tags = Contrast::Agent::Assess::Tracker.properties(object)&.tags
|
42
42
|
else
|
43
43
|
@object = Contrast::Utils::ObjectShare::NIL_STRING
|
44
|
-
@object_type = nil.cs__class.
|
44
|
+
@object_type = nil.cs__class.cs__name
|
45
45
|
end
|
46
46
|
end
|
47
47
|
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c)
|
1
|
+
# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
require 'contrast/agent/assess/contrast_event'
|
@@ -13,7 +13,8 @@ module Contrast
|
|
13
13
|
def self.build policy_node, tagged, object, ret, args, source_type = nil, source_name = nil
|
14
14
|
case policy_node
|
15
15
|
when Contrast::Agent::Assess::Policy::SourceNode
|
16
|
-
Contrast::Agent::Assess::Events::SourceEvent.new(policy_node, tagged, object, ret, args, source_type,
|
16
|
+
Contrast::Agent::Assess::Events::SourceEvent.new(policy_node, tagged, object, ret, args, source_type,
|
17
|
+
source_name)
|
17
18
|
when Contrast::Agent::Assess::Policy::PolicyNode
|
18
19
|
Contrast::Agent::Assess::ContrastEvent.new(policy_node, tagged, object, ret, args)
|
19
20
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c)
|
1
|
+
# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
require 'contrast/agent/assess/contrast_event'
|
@@ -11,8 +11,13 @@ module Contrast
|
|
11
11
|
# This class holds the data about an event in the application
|
12
12
|
# We'll use it to build an event that TeamServer can consume if
|
13
13
|
# the object to which this event belongs ends in a trigger.
|
14
|
+
#
|
15
|
+
# @attr_reader request [Contrast::Agent::Request] our wrapper around the Rack::Request at the time this source
|
16
|
+
# was created
|
17
|
+
# @attr_reader source_name [String] the name of the source if it comes from a map-like entity
|
18
|
+
# @attr_reader source_type [String] the TeamServer understood type of source; i.e. parameter
|
14
19
|
class SourceEvent < Contrast::Agent::Assess::ContrastEvent
|
15
|
-
attr_reader :
|
20
|
+
attr_reader :request, :source_name, :source_type
|
16
21
|
|
17
22
|
def initialize policy_node, tagged, object, ret, args, source_type = nil, source_name = nil
|
18
23
|
super(policy_node, tagged, object, ret, args)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c)
|
1
|
+
# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
require 'contrast/agent/assess/tracker'
|
@@ -1,4 +1,4 @@
|
|
1
|
-
# Copyright (c)
|
1
|
+
# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
|
2
2
|
# frozen_string_literal: true
|
3
3
|
|
4
4
|
require 'contrast/utils/duck_utils'
|
@@ -7,25 +7,20 @@ module Contrast
|
|
7
7
|
module Agent
|
8
8
|
module Assess
|
9
9
|
module Finalizers
|
10
|
-
# An extension of Hash that doesn't impact GC of the object being
|
11
|
-
#
|
12
|
-
# finalizer on the object to remove its entry from the Hash immediately
|
13
|
-
# after it's GC'd.
|
10
|
+
# An extension of Hash that doesn't impact GC of the object being stored by storing its ID as a Key to lookup
|
11
|
+
# and registering a finalizer on the object to remove its entry from the Hash immediately after it's GC'd.
|
14
12
|
class Hash < Hash
|
15
|
-
include Contrast::Components::Interface
|
16
|
-
access_component :agent, :analysis
|
17
13
|
|
18
14
|
FROZEN_FINALIZED_IDS = Set.new
|
19
15
|
|
20
16
|
def []= key, obj
|
21
|
-
return unless AGENT.enabled? && ASSESS.enabled?
|
17
|
+
return unless ::Contrast::AGENT.enabled? && ::Contrast::ASSESS.enabled?
|
22
18
|
|
23
|
-
# We can't finalize frozen things, so only act on those that went
|
24
|
-
# through .pre_freeze
|
19
|
+
# We can't finalize frozen things, so only act on those that went through .pre_freeze
|
25
20
|
if key.cs__frozen?
|
26
21
|
return unless FROZEN_FINALIZED_IDS.include?(key.__id__)
|
27
22
|
else
|
28
|
-
ObjectSpace.define_finalizer(key,
|
23
|
+
ObjectSpace.define_finalizer(key, finalizing_proc)
|
29
24
|
end
|
30
25
|
super key.__id__, obj
|
31
26
|
end
|
@@ -34,8 +29,8 @@ module Contrast
|
|
34
29
|
super key.__id__
|
35
30
|
end
|
36
31
|
|
37
|
-
# Something is trackable if it is not a collection and either not
|
38
|
-
#
|
32
|
+
# Something is trackable if it is not a collection and either not frozen or it was frozen after we put a
|
33
|
+
# finalizer on it.
|
39
34
|
#
|
40
35
|
# @param key [Object] the thing to determine if trackable
|
41
36
|
# @return [Boolean]
|
@@ -47,51 +42,46 @@ module Contrast
|
|
47
42
|
# If it's not frozen, we can finalize/ track it.
|
48
43
|
return true unless key.cs__frozen?
|
49
44
|
|
50
|
-
# Otherwise, we can only track it if we've finalized it in our
|
51
|
-
# freeze patch.
|
45
|
+
# Otherwise, we can only track it if we've finalized it in our freeze patch.
|
52
46
|
FROZEN_FINALIZED_IDS.include?(key.__id__)
|
53
47
|
end
|
54
48
|
|
55
|
-
# Determine if the given Object is tracked, meaning it has a known
|
56
|
-
#
|
49
|
+
# Determine if the given Object is tracked, meaning it has a known set of properties and those properties are
|
50
|
+
# tracked.
|
57
51
|
#
|
58
|
-
# @param key [Object] the Object whose properties, by id, we want to
|
59
|
-
# check for tracked status
|
52
|
+
# @param key [Object] the Object whose properties, by id, we want to check for tracked status
|
60
53
|
# @return [Boolean]
|
61
54
|
def tracked? key
|
62
55
|
key?(key.__id__) && fetch(key.__id__, nil)&.tracked?
|
63
56
|
end
|
64
57
|
|
65
|
-
#
|
66
|
-
#
|
67
|
-
#
|
68
|
-
# not the Object itself. You CANNOT pass the Object to this as a
|
69
|
-
# finalizer cannot hold reference to the Object being finalized; that
|
70
|
-
# prevents GC, which introduces a memory leak and defeats the entire
|
71
|
-
# purpose of this.
|
58
|
+
# Create a Proc to remove the given key from our frozen and properties tracking during finalization of the
|
59
|
+
# Object to which the given key_id pertains. The ObjectSpace's finalizer mechanism will handle passing this
|
60
|
+
# ID in, so we only need to define the Proc once.
|
72
61
|
#
|
73
|
-
#
|
74
|
-
#
|
75
|
-
|
76
|
-
|
62
|
+
# NOTE: by necessity, this is the only method which takes the __id__, not the Object itself. You CANNOT pass
|
63
|
+
# the Object to this as a finalizer cannot hold reference to the Object being finalized; that prevents GC,
|
64
|
+
# which introduces a memory leak and defeats the entire purpose of this.
|
65
|
+
#
|
66
|
+
# @return [Proc] the Proc to remove references to the ID of the GC'd object from our Hash
|
67
|
+
def finalizing_proc
|
68
|
+
@_finalizing_proc ||= proc do |key_id|
|
77
69
|
FROZEN_FINALIZED_IDS.delete(key_id)
|
78
70
|
delete(key_id)
|
79
71
|
end
|
80
72
|
end
|
81
73
|
|
82
|
-
# Frozen things cannot be finalized. To avoid any issue here, we
|
83
|
-
#
|
84
|
-
#
|
85
|
-
# our tracking.
|
74
|
+
# Frozen things cannot be finalized. To avoid any issue here, we intercept the #freeze call and set
|
75
|
+
# finalizers on the Object. To ensure later we know it's been pre-finalized, we add it's __id__ to our
|
76
|
+
# tracking.
|
86
77
|
#
|
87
|
-
# @param key [Object] the Object on which we need to pre-define
|
88
|
-
# finalizers
|
78
|
+
# @param key [Object] the Object on which we need to pre-define finalizers
|
89
79
|
def pre_freeze key
|
90
|
-
return unless AGENT.enabled? && ASSESS.enabled?
|
80
|
+
return unless ::Contrast::AGENT.enabled? && ::Contrast::ASSESS.enabled?
|
91
81
|
return if key.cs__frozen?
|
92
82
|
return if FROZEN_FINALIZED_IDS.include?(key.__id__)
|
93
83
|
|
94
|
-
ObjectSpace.define_finalizer(key,
|
84
|
+
ObjectSpace.define_finalizer(key, finalizing_proc)
|
95
85
|
|
96
86
|
FROZEN_FINALIZED_IDS << key.__id__
|
97
87
|
rescue StandardError => _e
|