Class: Datadog::AppSec::Contrib::Rack::RequestMiddleware
- Inherits:
-
Object
- Object
- Datadog::AppSec::Contrib::Rack::RequestMiddleware
- Defined in:
- lib/datadog/appsec/contrib/rack/request_middleware.rb
Overview
Topmost Rack middleware for AppSec This should be inserted just below Datadog::Tracing::Contrib::Rack::TraceMiddleware
Instance Method Summary collapse
-
#call(env) ⇒ Object
rubocop:disable Metrics/MethodLength.
-
#initialize(app, opt = {}) ⇒ RequestMiddleware
constructor
A new instance of RequestMiddleware.
Constructor Details
#initialize(app, opt = {}) ⇒ RequestMiddleware
Returns a new instance of RequestMiddleware.
35 36 37 38 39 40 |
# File 'lib/datadog/appsec/contrib/rack/request_middleware.rb', line 35 def initialize(app, opt = {}) @app = app @oneshot_tags_sent = false @rack_headers = {} end |
Instance Method Details
#call(env) ⇒ Object
rubocop:disable Metrics/MethodLength
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 73 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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/datadog/appsec/contrib/rack/request_middleware.rb', line 43 def call(env) return @app.call(env) unless Datadog::AppSec.enabled? boot = Datadog::Core::Remote::Tie.boot Datadog::Core::Remote::Tie::Tracing.tag(boot, active_span) # For a given request, keep using the first Rack stack scope for # nested apps. Don't set `context` local variable so that on popping # out of this nested stack we don't finalize the parent's context return @app.call(env) if active_context(env) security_engine = Datadog::AppSec.security_engine # TODO: handle exceptions, except for @app.call return @app.call(env) unless security_engine ctx = Datadog::AppSec::Context.activate( Datadog::AppSec::Context.new(active_trace, active_span, security_engine.new_runner) ) env[Datadog::AppSec::Ext::CONTEXT_KEY] = ctx (ctx) (ctx, env) http_response = nil gateway_request = Gateway::Request.new(env) gateway_response = nil interrupt_params = catch(::Datadog::AppSec::Ext::INTERRUPT) do # TODO: This event should be renamed into `rack.request.start` to # reflect that it's the beginning of the request-cycle http_response, _gateway_request = Instrumentation.gateway.push('rack.request', gateway_request) do @app.call(env) end gateway_response = Gateway::Response.new( http_response[2], http_response[0], http_response[1], context: ctx ) Instrumentation.gateway.push('rack.request.finish', gateway_request) Instrumentation.gateway.push('rack.response', gateway_response) nil end if interrupt_params ctx.mark_as_interrupted! http_response = AppSec::Response.from_interrupt_params(interrupt_params, env['HTTP_ACCEPT']).to_rack end # NOTE: This is not optimal, but in the current implementation # `gateway_response` is a container to dispatch response event # and in case of interruption it suppose to be `nil`. # # `http_response` is a real response object in both cases, but # to save us some computations, we will use already pre-computed # `gateway_response` instead of re-creating it. # # WARNING: This part will be refactored. tmp_response = if interrupt_params Gateway::Response.new(http_response[2], http_response[0], http_response[1], context: ctx) else gateway_response end if AppSec::APISecurity.enabled? && AppSec::APISecurity.sample_trace?(ctx.trace) && AppSec::APISecurity.sample?(gateway_request.request, tmp_response.response) ctx.events.push( AppSec::SecurityEvent.new(ctx.extract_schema, trace: ctx.trace, span: ctx.span) ) end AppSec::Event.record(ctx, request: gateway_request, response: gateway_response) http_response ensure if ctx ctx.export_metrics ctx.export_request_telemetry Datadog::AppSec::Context.deactivate end end |