Module: Datadog::AppSec::Contrib::Rack::Reactive::Request

Defined in:
lib/datadog/appsec/contrib/rack/reactive/request.rb

Overview

Dispatch data from a Rack request to the WAF context

Class Method Summary collapse

Class Method Details

.publish(op, gateway_request) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/datadog/appsec/contrib/rack/reactive/request.rb', line 20

def self.publish(op, gateway_request)
  catch(:block) do
    op.publish('request.query', gateway_request.query)
    op.publish('request.headers', gateway_request.headers)
    op.publish('request.uri.raw', gateway_request.fullpath)
    op.publish('request.cookies', gateway_request.cookies)
    op.publish('request.client_ip', gateway_request.client_ip)
    op.publish('server.request.method', gateway_request.method)

    nil
  end
end

.subscribe(op, waf_context) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/datadog/appsec/contrib/rack/reactive/request.rb', line 33

def self.subscribe(op, waf_context)
  op.subscribe(*ADDRESSES) do |*values|
    Datadog.logger.debug { "reacted to #{ADDRESSES.inspect}: #{values.inspect}" }
    headers = values[0]
    headers_no_cookies = headers.dup.tap { |h| h.delete('cookie') }
    uri_raw = values[1]
    query = values[2]
    cookies = values[3]
    client_ip = values[4]
    request_method = values[5]

    waf_args = {
      'server.request.cookies' => cookies,
      'server.request.query' => query,
      'server.request.uri.raw' => uri_raw,
      'server.request.headers' => headers,
      'server.request.headers.no_cookies' => headers_no_cookies,
      'http.client_ip' => client_ip,
      'server.request.method' => request_method,
    }

    waf_timeout = Datadog.configuration.appsec.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}" }

      yield result
      throw(:block, true) unless result.actions.empty?
    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