Sha256: 6643964df8dedc4f895daaf476769e47a7e9b472a1cdad5c419680a71c364525

Contents?: true

Size: 1.67 KB

Versions: 6

Compression:

Stored size: 1.67 KB

Contents

# Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
# frozen_string_literal: true

require 'contrast/agent/inventory/dependency_usage_analysis'

module Contrast
  module Framework
    module Rails
      module Patch
        # This class acts as our patch into the ActionController::Live::Buffer class, allowing us to track the close
        # event on streamed responses.
        module ActionControllerLiveBuffer
          class << self
            def send_messages
              return unless (context = Contrast::Agent::REQUEST_TRACKER.current)

              [
                context.observed_route
              ].each do |event|
                Contrast::Agent.reporter&.send_event_immediately(event)
              end

              Contrast::Agent.reporter&.send_event_immediately(context.activity)
            end

            def instrument
              @_instrument ||= begin
                ::ActionController::Live::Buffer.class_eval do
                  # normally pre->in->post filters are applied however, in a streamed response we can run into a case
                  # where it's pre -> in -> post -> more infilters in order to submit anything found during the
                  # infilters after the response has been written we need to explicitly send them
                  alias_method(:cs__close, :close)
                  def close
                    Contrast::Framework::Rails::Patch::ActionControllerLiveBuffer.send_messages
                    cs__close
                  end
                end
                true
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
contrast-agent-6.11.0 lib/contrast/framework/rails/patch/action_controller_live_buffer.rb
contrast-agent-6.10.0 lib/contrast/framework/rails/patch/action_controller_live_buffer.rb
contrast-agent-6.9.0 lib/contrast/framework/rails/patch/action_controller_live_buffer.rb
contrast-agent-6.8.0 lib/contrast/framework/rails/patch/action_controller_live_buffer.rb
contrast-agent-6.7.0 lib/contrast/framework/rails/patch/action_controller_live_buffer.rb
contrast-agent-6.6.5 lib/contrast/framework/rails/patch/action_controller_live_buffer.rb