# Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details. # frozen_string_literal: true # Used to prevent deprecation warnings from flooding stdout ENV['PB_IGNORE_DEPRECATIONS'] = 'true' # Some developers override various methods on Object, which can often involve # changing expected method parity/behavior which in turn prevents us from being # able to reliably use affected methods. # We alias these method so that we always have access to them. # # Because we use these methods in constructing classes (e.g., calling #freeze # on constants within class definitions) we do this aliasing ASAP. class Object alias_method :cs__class, :class alias_method :cs__freeze, :freeze alias_method :cs__frozen?, :frozen? alias_method :cs__is_a?, :is_a? alias_method :cs__method, :method alias_method :cs__respond_to?, :respond_to? alias_method :cs__singleton_class, :singleton_class end # ActiveRecord gives access to the `String#blank?` method, which we've started using. We need to make sure that method # actually exists. # From https://github.com/rails/rails/blob/main/activesupport/lib/active_support/core_ext/object/blank.rb class String unless cs__respond_to?(:blank?) CS__BLANK_RE = /\A[[:space:]]*\z/.cs__freeze # A string is blank if it's empty or contains whitespaces only: # # ''.blank? # => true # ' '.blank? # => true # "\t\n\r".blank? # => true # ' blah '.blank? # => false # # Unicode whitespace is supported: # # "\u00a0".blank? # => true # # @return [true, false] def blank? # The regexp that matches blank strings is expensive. For the case of empty # strings we can speed up this method (~3.5x) with an empty? call. The # penalty for the rest of strings is marginal. empty? || begin CS__BLANK_RE.match?(self) rescue Encoding::CompatibilityError false end end end end if RUBY_VERSION >= '3.0.0' && RUBY_VERSION < '3.1.0' # This fixes Ruby 3.0 issues with Module#(some instance method) patching by preventing the prepending of # a JSON helper on protobuf load. String.instance_method(:+) is one of the most noticeable. # This is fixed in Ruby 3.1.0 # TODO: RUBY-1132 Remove this once Ruby 3 support is dropped. # See bug here: https://bugs.ruby-lang.org/issues/17725 class Class alias_method(:cs__orig_prepend, :prepend) def prepend other include(other) end end end require 'contrast/components/agent' require 'contrast/components/api' require 'contrast/components/app_context' require 'contrast/components/assess' require 'contrast/components/config' require 'contrast/components/contrast_service' require 'contrast/components/inventory' require 'contrast/components/logger' require 'contrast/components/protect' require 'contrast/components/sampling' require 'contrast/components/scope' require 'contrast/components/settings' require 'contrast/utils/telemetry_hash' require 'contrast/utils/telemetry' require 'contrast/agent/telemetry/events/exceptions/telemetry_exception_event' require 'protobuf' # TODO: RUBY-1438 module Contrast API = Contrast::Components::Api::Interface.new SCOPE = Contrast::Components::Scope::Interface.new CONFIG = Contrast::Components::Config::Interface.new SETTINGS = Contrast::Components::Settings::Interface.new ASSESS = Contrast::Components::Assess::Interface.new PROTECT = Contrast::Components::Protect::Interface.new INVENTORY = Contrast::Components::Inventory::Interface.new LOGGER = Contrast::Components::Logger::Interface.new AGENT = Contrast::Components::Agent::Interface.new CONTRAST_SERVICE = Contrast::Components::ContrastService::Interface.new APP_CONTEXT = Contrast::Components::AppContext::Interface.new end module Contrast TELEMETRY_EXCEPTIONS = (Contrast::Utils::TelemetryHash.new if Contrast::Utils::Telemetry.exceptions_enabled?) end # This needs to be required very early, after component interfaces, and before instrumentation attempts require 'contrast/funchook/funchook' require 'contrast/agent/version' # shared utils require 'contrast/utils/timer' require 'contrast/utils/preflight_util' require 'contrast/utils/assess/sampling_util' require 'contrast/agent' if RUBY_VERSION >= '3.0.0' && RUBY_VERSION < '3.1.0' # Put prepend back as it was. Class.alias_method(:prepend, :cs__orig_prepend) Class.remove_method(:cs__orig_prepend) end if RUBY_VERSION < '3.0.0' # Better handles ancestors for older ruby versions. # This is called from C, tread lightly. class Module @_included_in = [] # Returns array with modules including this instance def included_in @_included_in ||= [] unless cs__frozen? end def self.included_in @_included_in ||= [] unless cs__frozen? end end end