Sha256: 8467219518a6a726b82daf390899653e9f5d9de98f64df52e2bc0c21e47751e0

Contents?: true

Size: 1.24 KB

Versions: 26

Compression:

Stored size: 1.24 KB

Contents

# frozen_string_literal: true

require_relative 'gateway/request'
require_relative '../../instrumentation/gateway'
require_relative '../../response'

module Datadog
  module AppSec
    module Contrib
      module Rack
        # Rack request body middleware for AppSec
        # This should be inserted just below Rack::JSONBodyParser or
        # legacy Rack::PostBodyContentTypeParser from rack-contrib
        class RequestBodyMiddleware
          def initialize(app, opt = {})
            @app = app
          end

          def call(env)
            context = env[Datadog::AppSec::Ext::SCOPE_KEY]

            return @app.call(env) unless context

            # TODO: handle exceptions, except for @app.call

            request_return, request_response = Instrumentation.gateway.push(
              'rack.request.body',
              Gateway::Request.new(env)
            ) do
              @app.call(env)
            end

            if request_response
              blocked_event = request_response.find { |action, _event| action == :block }
              request_return = AppSec::Response.negotiate(env, blocked_event.last[:actions]).to_rack if blocked_event
            end

            request_return
          end
        end
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 2 rubygems

Version Path
datadog-2.8.0 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
datadog-2.7.1 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
datadog-2.7.0 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
datadog-2.6.0 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
datadog-2.5.0 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
datadog-2.4.0 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
datadog-2.3.0 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
datadog-2.2.0 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
ddtrace-1.23.3 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
ddtrace-1.23.2 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
datadog-2.1.0 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
datadog-2.0.0 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
ddtrace-1.23.1 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
datadog-2.0.0.beta2 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
ddtrace-1.22.0 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
datadog-2.0.0.beta1 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
ddtrace-1.21.1 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
ddtrace-1.21.0 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
ddtrace-1.20.0 lib/datadog/appsec/contrib/rack/request_body_middleware.rb
ddtrace-1.19.0 lib/datadog/appsec/contrib/rack/request_body_middleware.rb