Module: Datadog::AppSec::Contrib::Rack::Reactive::Response

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

Overview

Dispatch data from a Rack response to the WAF context

Class Method Summary collapse

Class Method Details

.publish(op, gateway_response) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/datadog/appsec/contrib/rack/reactive/response.rb', line 16

def self.publish(op, gateway_response)
  catch(:block) do
    op.publish('response.status', gateway_response.status)
    op.publish('response.headers', gateway_response.headers)

    nil
  end
end

.subscribe(op, waf_context) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/datadog/appsec/contrib/rack/reactive/response.rb', line 25

def self.subscribe(op, waf_context)
  op.subscribe(*ADDRESSES) do |*values|
    Datadog.logger.debug { "reacted to #{ADDRESSES.inspect}: #{values.inspect}" }

    response_status = values[0]
    response_headers = values[1]
    response_headers_no_cookies = response_headers.dup.tap { |h| h.delete('set-cookie') }

    persistent_data = {
      'server.response.status' => response_status.to_s,
      'server.response.headers' => response_headers,
      'server.response.headers.no_cookies' => response_headers_no_cookies,
    }

    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