Module: Datadog::AppSec::Contrib::Rack::Gateway::Watcher

Defined in:
lib/datadog/appsec/contrib/rack/gateway/watcher.rb

Overview

Watcher for Rack gateway events

Class Method Summary collapse

Class Method Details

.watchObject



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

def watch
  gateway = Instrumentation.gateway

  watch_request(gateway)
  watch_response(gateway)
  watch_request_body(gateway)
  watch_request_finish(gateway)
end

.watch_request(gateway = Instrumentation.gateway) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/datadog/appsec/contrib/rack/gateway/watcher.rb', line 25

def watch_request(gateway = Instrumentation.gateway)
  gateway.watch('rack.request', :appsec) do |stack, gateway_request|
    context = gateway_request.env[AppSec::Ext::CONTEXT_KEY]

    persistent_data = {
      'server.request.cookies' => gateway_request.cookies,
      'server.request.query' => gateway_request.query,
      'server.request.uri.raw' => gateway_request.fullpath,
      'server.request.headers' => gateway_request.headers,
      'server.request.headers.no_cookies' => gateway_request.headers.dup.tap { |h| h.delete('cookie') },
      'http.client_ip' => gateway_request.client_ip,
      'server.request.method' => gateway_request.method
    }

    result = context.run_waf(persistent_data, {}, Datadog.configuration.appsec.waf_timeout)

    if result.match? || !result.derivatives.empty?
      context.events.push(
        AppSec::SecurityEvent.new(result, trace: context.trace, span: context.span)
      )
    end

    if result.match?
      AppSec::Event.tag_and_keep!(context, result)
      AppSec::ActionsHandler.handle(result.actions)
    end

    stack.call(gateway_request.request)
  end
end

.watch_request_body(gateway = Instrumentation.gateway) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/datadog/appsec/contrib/rack/gateway/watcher.rb', line 82

def watch_request_body(gateway = Instrumentation.gateway)
  gateway.watch('rack.request.body', :appsec) do |stack, gateway_request|
    context = gateway_request.env[AppSec::Ext::CONTEXT_KEY]

    persistent_data = {
      'server.request.body' => gateway_request.form_hash
    }

    result = context.run_waf(persistent_data, {}, Datadog.configuration.appsec.waf_timeout)

    if result.match?
      AppSec::Event.tag_and_keep!(context, result)

      context.events.push(
        AppSec::SecurityEvent.new(result, trace: context.trace, span: context.span)
      )

      AppSec::ActionsHandler.handle(result.actions)
    end

    stack.call(gateway_request.request)
  end
end

.watch_request_finish(gateway = Instrumentation.gateway) ⇒ Object

NOTE: In the current state we unable to substibe twice to the same

event within the same group. Ideally this code should live
somewhere closer to identity related monitor.

WARNING: The Gateway is a subject of refactoring



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/datadog/appsec/contrib/rack/gateway/watcher.rb', line 110

def watch_request_finish(gateway = Instrumentation.gateway)
  gateway.watch('rack.request.finish', :appsec) do |stack, gateway_request|
    context = gateway_request.env[AppSec::Ext::CONTEXT_KEY]

    if context.span.nil? || !gateway.pushed?('appsec.events.user_lifecycle')
      next stack.call(gateway_request.request)
    end

    gateway_request.headers.each do |name, value|
      if !Ext::COLLECTABLE_REQUEST_HEADERS.include?(name) &&
          !Ext::IDENTITY_COLLECTABLE_REQUEST_HEADERS.include?(name)
        next
      end

      context.span["http.request.headers.#{name}"] ||= value
    end

    stack.call(gateway_request.request)
  end
end

.watch_response(gateway = Instrumentation.gateway) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/datadog/appsec/contrib/rack/gateway/watcher.rb', line 56

def watch_response(gateway = Instrumentation.gateway)
  gateway.watch('rack.response', :appsec) do |stack, gateway_response|
    context = gateway_response.context

    persistent_data = {
      'server.response.status' => gateway_response.status.to_s,
      'server.response.headers' => gateway_response.headers,
      'server.response.headers.no_cookies' => gateway_response.headers.dup.tap { |h| h.delete('set-cookie') }
    }

    result = context.run_waf(persistent_data, {}, Datadog.configuration.appsec.waf_timeout)

    if result.match?
      AppSec::Event.tag_and_keep!(context, result)

      context.events.push(
        AppSec::SecurityEvent.new(result, trace: context.trace, span: context.span)
      )

      AppSec::ActionsHandler.handle(result.actions)
    end

    stack.call(gateway_response.response)
  end
end