Sha256: 1ef65b080aa4e1645effc09c0e91d16fc795db669517233517bcc2497285b38b

Contents?: true

Size: 1.9 KB

Versions: 31

Compression:

Stored size: 1.9 KB

Contents

require_relative "../blocked_app"
require_relative "../errors"

module Immunio
  # Rack middleware trigger proper hook whenever an exception is raised in the app.
  class ExceptionHandler
    def initialize(app)
      @app = app
    end

    def call(env)
      @app.call(env)
    rescue RequestBlocked
      Request.time "plugin", "#{Module.nesting[0]}::#{__method__}[RequestBlocked]" do
        # Avoid bubbling exception to logging middlewares.
        Immunio.blocked_app.call(env)
      end
    rescue OverrideResponse => override
      status, headers, body = Immunio.override_response.call(env, override)

      Immunio.run_hook "http_tracker", "http_response_start",
        status: status, headers: headers
      [status, headers, body]
    rescue StandardError => e
      Request.time "plugin", "#{Module.nesting[0]}::#{__method__}[#{e.class}]" do
        # Check for template, DB errors and such generated by the agent.
        original_exception = unwrap_exception(e)
        if RequestBlocked === original_exception || original_exception.message =~ /^Immunio::RequestBlocked:/
          return Immunio.blocked_app.call(env)
        end
        if OverrideResponse === original_exception  # We can't do anything with just the message:
          status, headers, body = Immunio.override_response.call(env, original_exception)

          Immunio.run_hook "http_tracker", "http_response_start",
            status: status, headers: headers
          return [status, headers, body]
        end

        # A real app exception, not generated by the agent.
        Immunio.run_hook! "exception_handler", "exception", exception: "#{e.class.name}: #{e}"

        # Re-raise
        raise
      end
    end

    # Unwrap the innermost original exception.
    def unwrap_exception(e)
      while e.respond_to?(:original_exception) && e.original_exception.is_a?(Exception)
        e = e.original_exception
      end
      e
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
immunio-1.1.13 lib/immunio/plugins/exception_handler.rb
immunio-1.1.11 lib/immunio/plugins/exception_handler.rb
immunio-1.1.10 lib/immunio/plugins/exception_handler.rb
immunio-1.1.7 lib/immunio/plugins/exception_handler.rb
immunio-1.1.6 lib/immunio/plugins/exception_handler.rb
immunio-1.1.5 lib/immunio/plugins/exception_handler.rb
immunio-1.1.2 lib/immunio/plugins/exception_handler.rb
immunio-1.1.1 lib/immunio/plugins/exception_handler.rb
immunio-1.1.0 lib/immunio/plugins/exception_handler.rb
immunio-1.0.22 lib/immunio/plugins/exception_handler.rb
immunio-1.0.19 lib/immunio/plugins/exception_handler.rb
immunio-1.0.17 lib/immunio/plugins/exception_handler.rb
immunio-1.0.15 lib/immunio/plugins/exception_handler.rb
immunio-1.0.14 lib/immunio/plugins/exception_handler.rb
immunio-1.0.13 lib/immunio/plugins/exception_handler.rb
immunio-1.0.12 lib/immunio/plugins/exception_handler.rb
immunio-1.0.11 lib/immunio/plugins/exception_handler.rb
immunio-1.0.10 lib/immunio/plugins/exception_handler.rb
immunio-1.0.9 lib/immunio/plugins/exception_handler.rb
immunio-1.0.8 lib/immunio/plugins/exception_handler.rb