Sha256: 5b9e949c9e3cd22000d8d4e986849b3f55f3fe81a786e8d75720f4579cbc309d

Contents?: true

Size: 1.95 KB

Versions: 6

Compression:

Stored size: 1.95 KB

Contents

# typed: true

require_relative '../request'

module Datadog
  module AppSec
    module Contrib
      module Rack
        module Reactive
          # Dispatch data from a Rack request to the WAF context
          module RequestBody
            def self.publish(op, request)
              catch(:block) do
                # params have been parsed from the request body
                op.publish('request.body', Rack::Request.form_hash(request))

                nil
              end
            end

            def self.subscribe(op, waf_context)
              addresses = [
                'request.body',
              ]

              op.subscribe(*addresses) do |*values|
                Datadog.logger.debug { "reacted to #{addresses.inspect}: #{values.inspect}" }
                body = values[0]

                waf_args = {
                  'server.request.body' => body,
                }

                waf_timeout = Datadog::AppSec.settings.waf_timeout
                result = waf_context.run(waf_args, waf_timeout)

                Datadog.logger.debug { "WAF TIMEOUT: #{result.inspect}" } if result.timeout

                case result.status
                when :match
                  Datadog.logger.debug { "WAF: #{result.inspect}" }

                  block = result.actions.include?('block')

                  yield [result, block]

                  throw(:block, [result, true]) if block
                when :ok
                  Datadog.logger.debug { "WAF OK: #{result.inspect}" }
                when :invalid_call
                  Datadog.logger.debug { "WAF CALL ERROR: #{result.inspect}" }
                when :invalid_rule, :invalid_flow, :no_rule
                  Datadog.logger.debug { "WAF RULE ERROR: #{result.inspect}" }
                else
                  Datadog.logger.debug { "WAF UNKNOWN: #{result.status.inspect} #{result.inspect}" }
                end
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
ddtrace-1.8.0 lib/datadog/appsec/contrib/rack/reactive/request_body.rb
ddtrace-1.7.0 lib/datadog/appsec/contrib/rack/reactive/request_body.rb
ddtrace-1.6.1 lib/datadog/appsec/contrib/rack/reactive/request_body.rb
ddtrace-1.6.0 lib/datadog/appsec/contrib/rack/reactive/request_body.rb
ddtrace-1.5.2 lib/datadog/appsec/contrib/rack/reactive/request_body.rb
ddtrace-1.5.1 lib/datadog/appsec/contrib/rack/reactive/request_body.rb