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



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

def watch
  gateway = Instrumentation.gateway

  watch_request_dispatch(gateway)
  watch_request_routed(gateway)
  watch_response_body_json(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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb', line 24

def watch_request_dispatch(gateway = Instrumentation.gateway)
  gateway.watch("sinatra.request.dispatch") do |stack, gateway_request|
    context = gateway_request.env[AppSec::Ext::CONTEXT_KEY] # : Context

    context.state[:web_framework] = "sinatra"

    request = gateway_request.request
    next stack.call(request) unless gateway_request.collectable_body?

    # NOTE: A limit of 0 disables request body collection entirely.
    limit = Datadog.configuration.appsec.body_parsing_size_limit
    next stack.call(request) if limit.zero?

    persistent_data = {}
    byte_length = gateway_request.body_bytesize(limit)

    if byte_length
      persistent_data["server.request.body.byte_length"] = byte_length

      if byte_length <= limit
        body = gateway_request.form_hash
        persistent_data["server.request.body"] = body if body
      end
    # NOTE: Body was parsed before measurement, keep byte_length unset
    elsif gateway_request.env.key?("rack.request.form_hash")
      body = gateway_request.env["rack.request.form_hash"]
      persistent_data["server.request.body"] = body if body
    end

    next stack.call(request) if persistent_data.empty?

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

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

    if result.match?
      AppSec::Event.tag(context, result)
      TraceKeeper.keep!(context.trace) if result.keep?

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

    stack.call(gateway_request.request)
  end
end

.watch_request_routed(gateway = Instrumentation.gateway) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb', line 74

def watch_request_routed(gateway = Instrumentation.gateway)
  gateway.watch("sinatra.request.routed") do |stack, args|
    gateway_request, gateway_route_params = args # : [Gateway::Request, Gateway::RouteParams]
    context = gateway_request.env[AppSec::Ext::CONTEXT_KEY] # : Context

    persistent_data = {
      "server.request.path_params" => gateway_route_params.params,
    }

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

    if result.match?
      AppSec::Event.tag(context, result)
      TraceKeeper.keep!(context.trace) if result.keep?

      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_response_body_json(gateway = Instrumentation.gateway) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb', line 100

def watch_response_body_json(gateway = Instrumentation.gateway)
  gateway.watch("sinatra.response.body.json") do |stack, container|
    context = container.context # : Context

    persistent_data = {
      "server.response.body" => container.data,
    }
    result = context.run_waf(persistent_data, {}, Datadog.configuration.appsec.waf_timeout)

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

      AppSec::Event.tag(context, result)
      TraceKeeper.keep!(context.trace) if result.keep?

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

    stack.call(container)
  end
end