Sha256: c77d5a7b19eb0bd4464699bf21ca4303131e14851f2d98d21264a1beb74f0b64

Contents?: true

Size: 1.83 KB

Versions: 3

Compression:

Stored size: 1.83 KB

Contents

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

cs__scoped_require 'contrast/utils/service_sender_util'

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.
        class ActionControllerLiveBuffer
          class << self
            def send_messages
              return unless (context = Contrast::Agent::REQUEST_TRACKER.current)

              [context.server_activity, context.activity, context.observed_route].each do |msg|
                Contrast::Utils::ServiceSenderUtil.send_event_immediately(msg)
              end
            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

3 entries across 3 versions & 1 rubygems

Version Path
contrast-agent-3.12.2 lib/contrast/framework/rails/patch/action_controller_live_buffer.rb
contrast-agent-3.12.1 lib/contrast/framework/rails/patch/action_controller_live_buffer.rb
contrast-agent-3.12.0 lib/contrast/framework/rails/patch/action_controller_live_buffer.rb