# Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true require 'contrast/components/logger' require 'contrast/components/scope' module Contrast module Agent # This module is used to apply instrumentation to classes as they are required module TracePointHook extend Contrast::Components::Logger::InstanceMethods extend Contrast::Components::Scope::InstanceMethods class << self def enable! @require_hook ||= TracePoint.new(:end) do |tracepoint_event| process(tracepoint_event) end @require_hook.enable logger.debug('Enabled :end event tracing') end def disable instance_variable_defined?(:@require_hook) && @require_hook.disable end private def process tracepoint_event with_contrast_scope do logger.trace('Received TracePoint end event', module: tracepoint_event.self.to_s) loaded_module = tracepoint_event.self path = tracepoint_event.path return if path&.include?('contrast') Contrast::Agent.framework_manager.register_late_framework(loaded_module) Contrast::Agent::Inventory::DependencyUsageAnalysis.instance.associate_file(path) if path Contrast::Agent::Patching::Policy::Patcher.patch_specific_module(loaded_module) if RUBY_VERSION < '2.6.0' # TODO: RUBY-714 remove guard w/ EOL of 2.5 Contrast::Agent::Assess::Policy::RewriterPatch.rewrite_interpolation(loaded_module) end Contrast::Agent::Assess::Policy::PolicyScanner.scan(tracepoint_event) rescue StandardError => e logger.error('Unable to complete TracePoint analysis', e, module: loaded_module) end end end end end end