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



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

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



24
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
55
56
# File 'lib/datadog/appsec/contrib/rack/gateway/watcher.rb', line 24

def watch_request(gateway = Instrumentation.gateway)
  gateway.watch('rack.request', :appsec) do |stack, gateway_request|
    context = gateway_request.env[Datadog::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?
      Datadog::AppSec::Event.tag_and_keep!(context, result)

      context.events << {
        waf_result: result,
        trace: context.trace,
        span: context.span,
        request: gateway_request,
        actions: result.actions
      }

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

    stack.call(gateway_request.request)
  end
end

.watch_request_body(gateway = Instrumentation.gateway) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/datadog/appsec/contrib/rack/gateway/watcher.rb', line 88

def watch_request_body(gateway = Instrumentation.gateway)
  gateway.watch('rack.request.body', :appsec) do |stack, gateway_request|
    context = gateway_request.env[Datadog::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?
      Datadog::AppSec::Event.tag_and_keep!(context, result)

      context.events << {
        waf_result: result,
        trace: context.trace,
        span: context.span,
        request: gateway_request,
        actions: result.actions
      }

      Datadog::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



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/datadog/appsec/contrib/rack/gateway/watcher.rb', line 120

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



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

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?
      Datadog::AppSec::Event.tag_and_keep!(context, result)

      context.events << {
        waf_result: result,
        trace: context.trace,
        span: context.span,
        response: gateway_response,
        actions: result.actions
      }

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

    stack.call(gateway_response.response)
  end
end