Module: Datadog::AppSec::Contrib::Faraday::Patcher

Defined in:
lib/datadog/appsec/contrib/faraday/patcher.rb

Overview

Patcher for Faraday

Class Method Summary collapse

Class Method Details

.configure_default_faraday_connectionObject



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/datadog/appsec/contrib/faraday/patcher.rb', line 30

def configure_default_faraday_connection
  if target_version >= Gem::Version.new('1.0.0')
    # Patch the default connection (e.g. +Faraday.get+)
    ::Faraday.default_connection.use(:datadog_appsec)

    # Patch new connection instances (e.g. +Faraday.new+)
    ::Faraday::Connection.prepend(ConnectionPatch)
  else
    # Patch the default connection (e.g. +Faraday.get+)
    #
    # We insert our middleware before the 'adapter', which is
    # always the last handler.
    idx = ::Faraday.default_connection.builder.handlers.size - 1
    ::Faraday.default_connection.builder.insert(idx, SSRFDetectionMiddleware)

    # Patch new connection instances (e.g. +Faraday.new+)
    ::Faraday::RackBuilder.prepend(RackBuilderPatch)
  end
end

.patchObject



19
20
21
22
23
24
25
26
27
28
# File 'lib/datadog/appsec/contrib/faraday/patcher.rb', line 19

def patch
  require_relative 'ssrf_detection_middleware'
  require_relative 'connection_patch'
  require_relative 'rack_builder_patch'

  ::Faraday::Middleware.register_middleware(datadog_appsec: SSRFDetectionMiddleware)
  configure_default_faraday_connection

  Patcher.instance_variable_set(:@patched, true)
end

.patched?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/datadog/appsec/contrib/faraday/patcher.rb', line 11

def patched?
  Patcher.instance_variable_get(:@patched)
end

.target_versionObject



15
16
17
# File 'lib/datadog/appsec/contrib/faraday/patcher.rb', line 15

def target_version
  Integration.version
end