# Copyright (c) 2023 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true require 'English' # This must precede other Contrast C extensions require 'cs__common/cs__common' # This must precede any patching we do as we log patches and we shouldn't cause # requires to happen during that process. require 'contrast/components/logger' # defining instrumentation, this must precede core extensions # because they need to register their patches require 'contrast/agent/patching/policy/patcher' require 'contrast/agent/patching/policy/patch' # core extensions require 'contrast/extension/assess' require 'contrast/extension/delegator' require 'contrast/extension/inventory' require 'contrast/extension/module' require 'contrast/extension/protect' require 'contrast/utils/object_share' require 'contrast/utils/string_utils' require 'contrast/utils/io_util' require 'contrast/utils/os' require 'contrast/utils/hash_digest' require 'contrast/utils/invalid_configuration_util' # Collect findings require 'contrast/utils/findings' # Collect Exploites and Attacks require 'contrast/agent/protect/exploitable_collection' # scoping require 'contrast/agent/scope/scope' require 'contrast/utils/thread_tracker' # Framework support require 'contrast/framework/manager' require 'contrast/agent/thread/thread_watcher' require 'contrast/utils/silence_maker' module Contrast # Top namespace of the Agent section. Holds tracking contexts that will be # accessed throughout the Agent. module Agent # build a map for tracking the context of the current request REQUEST_TRACKER = Contrast::Utils::ThreadTracker.new FINDINGS = Contrast::Utils::Findings.new EXPLOITS = Contrast::Agent::Protect::ExploitableCollection.new # @return [Contrast::Framework::Manager] def self.framework_manager reinitialize_with_log @_framework_manager ||= Contrast::Framework::Manager.new end # @return [nil, Contrast::Utils::HeapDumpUtil] def self.heapdump_util thread_watcher.heapdump_util end # @return [nil, Contrast::Agent::Telemetry::Base] def self.telemetry_queue thread_watcher.telemetry_queue end # @return [Contrast::Agent::Reporter] def self.reporter thread_watcher.reporter end # @return [Contrast::Agent::Protect::WorthWatchingAnalyzer] def self.worth_watching_analyzer thread_watcher.worth_watching_analyzer end # @return [Contrast::Agent::ThreadWatcher] def self.thread_watcher @_thread_watcher ||= Contrast::Agent::ThreadWatcher.new end # Apparently by some unknown reason - if we have already some instance for the AgentLib - we can set the # logger with options. That's why in rspec it started passing - because when we set the const in # protect_spec_helper in the with AgentLib context - we've already set the AGENT_LIB constant. # # So that leads to this methods logic here, which somehow works def self.reinitialize_with_log return if Contrast::AGENT_LIB.enable_log # Silence warning about reinit AgentLib Contrast::Utils::SilenceMaker.hush do Contrast.cs__const_set(:AGENT_LIB, Contrast::AgentLib::Interface.new(true, 2, nil)) end end end end require 'contrast/utils/resource_loader' require 'contrast/utils/duck_utils' require 'contrast/agent/hooks/tracepoint_hook' require 'contrast/agent/hooks/at_exit_hook' require 'contrast/agent/excluder/exclusion_matcher' # threads that handle contrast scoper require 'contrast/agent/thread/thread' # keep track of attacks require 'contrast/agent/request/request_context' require 'contrast/agent/assess/assess' # protect rules require 'contrast/agent/protect/rule' # application libraries and technologies require 'contrast/agent/inventory/inventory' # rack event monitoring require 'contrast/agent/middleware/middleware' # Install the patches we need before the application has a chance to initialize Contrast::Agent.framework_manager.before_load_patches!