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
# 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]

    persistent_data = {
      '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(persistent_data, {}, waf_timeout)

    next if result.status != :match

    yield result
    throw(:block, true) unless result.actions.empty?
  end
end