Module: Datadog::AppSec::Contrib::Sinatra::Gateway::Watcher

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

Overview

Watcher for Sinatra gateway events

Class Method Summary collapse

Class Method Details

.watchObject



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

def watch
  gateway = Instrumentation.gateway

  watch_request_dispatch(gateway)
  watch_request_routed(gateway)
end

.watch_request_dispatch(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
# File 'lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb', line 24

def watch_request_dispatch(gateway = Instrumentation.gateway)
  gateway.watch('sinatra.request.dispatch', :appsec) do |stack, gateway_request|
    event = nil
    context = gateway_request.env[Datadog::AppSec::Ext::CONTEXT_KEY]
    engine = AppSec::Reactive::Engine.new

    Rack::Reactive::RequestBody.subscribe(engine, context) do |result|
      if result.status == :match
        # TODO: should this hash be an Event instance instead?
        event = {
          waf_result: result,
          trace: context.trace,
          span: context.span,
          request: gateway_request,
          actions: result.actions
        }

        # We want to keep the trace in case of security event
        context.trace.keep! if context.trace
        Datadog::AppSec::Event.tag_and_keep!(context, result)
        context.waf_runner.events << event
      end
    end

    block = Rack::Reactive::RequestBody.publish(engine, gateway_request)
    next [nil, [[:block, event]]] if block

    stack.call(gateway_request.request)
  end
end

.watch_request_routed(gateway = Instrumentation.gateway) ⇒ Object



55
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
81
82
83
84
# File 'lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb', line 55

def watch_request_routed(gateway = Instrumentation.gateway)
  gateway.watch('sinatra.request.routed', :appsec) do |stack, (gateway_request, gateway_route_params)|
    event = nil
    context = gateway_request.env[Datadog::AppSec::Ext::CONTEXT_KEY]
    engine = AppSec::Reactive::Engine.new

    Sinatra::Reactive::Routed.subscribe(engine, context) do |result|
      if result.status == :match
        # TODO: should this hash be an Event instance instead?
        event = {
          waf_result: result,
          trace: context.trace,
          span: context.span,
          request: gateway_request,
          actions: result.actions
        }

        # We want to keep the trace in case of security event
        context.trace.keep! if context.trace
        Datadog::AppSec::Event.tag_and_keep!(context, result)
        context.waf_runner.events << event
      end
    end

    block = Sinatra::Reactive::Routed.publish(engine, [gateway_request, gateway_route_params])
    next [nil, [[:block, event]]] if block

    stack.call(gateway_request.request)
  end
end