contrast-agent 4.1.0 → 4.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (139) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +1 -0
  3. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +22 -10
  4. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.h +4 -3
  5. data/lib/contrast/agent.rb +5 -1
  6. data/lib/contrast/agent/assess.rb +0 -9
  7. data/lib/contrast/agent/assess/contrast_event.rb +49 -132
  8. data/lib/contrast/agent/assess/contrast_object.rb +54 -0
  9. data/lib/contrast/agent/assess/events/source_event.rb +4 -9
  10. data/lib/contrast/agent/assess/finalizers/hash.rb +7 -0
  11. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +17 -3
  12. data/lib/contrast/agent/assess/policy/patcher.rb +4 -3
  13. data/lib/contrast/agent/assess/policy/policy_node.rb +31 -59
  14. data/lib/contrast/agent/assess/policy/preshift.rb +3 -3
  15. data/lib/contrast/agent/assess/policy/propagation_method.rb +41 -32
  16. data/lib/contrast/agent/assess/policy/propagation_node.rb +12 -24
  17. data/lib/contrast/agent/assess/policy/propagator/append.rb +29 -15
  18. data/lib/contrast/agent/assess/policy/propagator/center.rb +1 -2
  19. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  20. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +21 -18
  21. data/lib/contrast/agent/assess/policy/propagator/insert.rb +1 -2
  22. data/lib/contrast/agent/assess/policy/propagator/keep.rb +1 -2
  23. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +3 -2
  24. data/lib/contrast/agent/assess/policy/propagator/next.rb +1 -2
  25. data/lib/contrast/agent/assess/policy/propagator/prepend.rb +1 -2
  26. data/lib/contrast/agent/assess/policy/propagator/remove.rb +2 -4
  27. data/lib/contrast/agent/assess/policy/propagator/replace.rb +1 -2
  28. data/lib/contrast/agent/assess/policy/propagator/reverse.rb +1 -2
  29. data/lib/contrast/agent/assess/policy/propagator/select.rb +3 -4
  30. data/lib/contrast/agent/assess/policy/propagator/splat.rb +25 -17
  31. data/lib/contrast/agent/assess/policy/propagator/split.rb +83 -120
  32. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +41 -25
  33. data/lib/contrast/agent/assess/policy/propagator/trim.rb +3 -7
  34. data/lib/contrast/agent/assess/policy/source_method.rb +2 -14
  35. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +5 -8
  36. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +1 -1
  37. data/lib/contrast/agent/assess/policy/trigger_method.rb +13 -8
  38. data/lib/contrast/agent/assess/policy/trigger_node.rb +28 -7
  39. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +59 -0
  40. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -3
  41. data/lib/contrast/agent/assess/policy/trigger_validation/trigger_validation.rb +6 -4
  42. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +2 -4
  43. data/lib/contrast/agent/assess/properties.rb +0 -2
  44. data/lib/contrast/agent/assess/property/tagged.rb +56 -32
  45. data/lib/contrast/agent/assess/tracker.rb +16 -18
  46. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +7 -0
  47. data/lib/contrast/agent/middleware.rb +134 -55
  48. data/lib/contrast/agent/patching/policy/method_policy.rb +1 -1
  49. data/lib/contrast/agent/patching/policy/patch.rb +6 -0
  50. data/lib/contrast/agent/patching/policy/patch_status.rb +1 -1
  51. data/lib/contrast/agent/patching/policy/patcher.rb +51 -44
  52. data/lib/contrast/agent/patching/policy/trigger_node.rb +5 -2
  53. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +47 -1
  54. data/lib/contrast/agent/protect/policy/rule_applicator.rb +53 -0
  55. data/lib/contrast/agent/protect/rule/base.rb +63 -14
  56. data/lib/contrast/agent/protect/rule/cmd_injection.rb +12 -28
  57. data/lib/contrast/agent/protect/rule/default_scanner.rb +1 -4
  58. data/lib/contrast/agent/protect/rule/deserialization.rb +4 -1
  59. data/lib/contrast/agent/protect/rule/no_sqli.rb +3 -3
  60. data/lib/contrast/agent/protect/rule/sqli.rb +20 -14
  61. data/lib/contrast/agent/protect/rule/xxe.rb +32 -11
  62. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +10 -6
  63. data/lib/contrast/agent/reaction_processor.rb +1 -1
  64. data/lib/contrast/agent/request_context.rb +12 -0
  65. data/lib/contrast/agent/response.rb +5 -5
  66. data/lib/contrast/agent/rewriter.rb +3 -3
  67. data/lib/contrast/agent/scope.rb +81 -55
  68. data/lib/contrast/agent/static_analysis.rb +13 -7
  69. data/lib/contrast/agent/thread.rb +1 -1
  70. data/lib/contrast/agent/thread_watcher.rb +20 -5
  71. data/lib/contrast/agent/version.rb +1 -1
  72. data/lib/contrast/api/communication/messaging_queue.rb +18 -21
  73. data/lib/contrast/api/communication/response_processor.rb +8 -1
  74. data/lib/contrast/api/communication/socket_client.rb +22 -14
  75. data/lib/contrast/api/decorators.rb +2 -0
  76. data/lib/contrast/api/decorators/agent_startup.rb +58 -0
  77. data/lib/contrast/api/decorators/application_startup.rb +51 -0
  78. data/lib/contrast/api/decorators/library.rb +1 -0
  79. data/lib/contrast/api/decorators/library_usage_update.rb +1 -0
  80. data/lib/contrast/api/decorators/route_coverage.rb +15 -5
  81. data/lib/contrast/api/decorators/trace_event.rb +58 -42
  82. data/lib/contrast/api/decorators/trace_event_object.rb +11 -3
  83. data/lib/contrast/api/decorators/trace_event_signature.rb +27 -5
  84. data/lib/contrast/api/decorators/user_input.rb +2 -1
  85. data/lib/contrast/common_agent_configuration.rb +2 -1
  86. data/lib/contrast/components/agent.rb +2 -0
  87. data/lib/contrast/components/app_context.rb +4 -22
  88. data/lib/contrast/components/assess.rb +36 -0
  89. data/lib/contrast/components/interface.rb +5 -3
  90. data/lib/contrast/components/sampling.rb +48 -6
  91. data/lib/contrast/components/scope.rb +72 -6
  92. data/lib/contrast/components/settings.rb +11 -7
  93. data/lib/contrast/config/assess_configuration.rb +2 -1
  94. data/lib/contrast/extension/assess/array.rb +2 -3
  95. data/lib/contrast/extension/assess/erb.rb +1 -3
  96. data/lib/contrast/extension/assess/exec_trigger.rb +1 -4
  97. data/lib/contrast/extension/assess/fiber.rb +2 -3
  98. data/lib/contrast/extension/assess/hash.rb +4 -2
  99. data/lib/contrast/extension/assess/kernel.rb +1 -2
  100. data/lib/contrast/extension/assess/marshal.rb +34 -26
  101. data/lib/contrast/extension/assess/regexp.rb +3 -8
  102. data/lib/contrast/extension/assess/string.rb +1 -2
  103. data/lib/contrast/framework/base_support.rb +51 -53
  104. data/lib/contrast/framework/manager.rb +16 -14
  105. data/lib/contrast/framework/rack/patch/session_cookie.rb +1 -1
  106. data/lib/contrast/framework/rack/support.rb +2 -1
  107. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +1 -1
  108. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +1 -1
  109. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +1 -1
  110. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +1 -1
  111. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +1 -1
  112. data/lib/contrast/framework/rails/support.rb +44 -44
  113. data/lib/contrast/framework/sinatra/support.rb +102 -42
  114. data/lib/contrast/logger/application.rb +0 -3
  115. data/lib/contrast/logger/log.rb +31 -15
  116. data/lib/contrast/utils/class_util.rb +3 -1
  117. data/lib/contrast/utils/duck_utils.rb +1 -1
  118. data/lib/contrast/utils/heap_dump_util.rb +103 -87
  119. data/lib/contrast/utils/invalid_configuration_util.rb +21 -12
  120. data/lib/contrast/utils/object_share.rb +3 -3
  121. data/lib/contrast/utils/preflight_util.rb +1 -1
  122. data/lib/contrast/utils/resource_loader.rb +1 -1
  123. data/lib/contrast/utils/sha256_builder.rb +2 -2
  124. data/lib/contrast/utils/string_utils.rb +1 -1
  125. data/lib/contrast/utils/tag_util.rb +9 -13
  126. data/resources/assess/policy.json +12 -18
  127. data/resources/deadzone/policy.json +156 -0
  128. data/resources/protect/policy.json +12 -0
  129. data/ruby-agent.gemspec +61 -19
  130. data/service_executables/VERSION +1 -1
  131. data/service_executables/linux/contrast-service +0 -0
  132. data/service_executables/mac/contrast-service +0 -0
  133. metadata +126 -113
  134. data/lib/contrast/agent/assess/rule.rb +0 -18
  135. data/lib/contrast/agent/assess/rule/base.rb +0 -52
  136. data/lib/contrast/agent/assess/rule/redos.rb +0 -67
  137. data/lib/contrast/framework/sinatra/patch/base.rb +0 -83
  138. data/lib/contrast/framework/sinatra/patch/support.rb +0 -27
  139. data/lib/contrast/utils/prevent_serialization.rb +0 -52
@@ -17,14 +17,9 @@ module Contrast
17
17
  # report the already-loaded gems.
18
18
  def catchup
19
19
  @_catchup ||= begin
20
- with_contrast_scope do
21
- Contrast::Agent::Inventory::DependencyUsageAnalysis.instance.catchup
22
- send_inventory_message
23
- true
24
- end
20
+ threaded_analysis!
21
+ true
25
22
  end
26
- rescue StandardError => e
27
- logger.warn('Unable to run post-initialization static analysis', e)
28
23
  end
29
24
 
30
25
  def send_inventory_message
@@ -35,6 +30,17 @@ module Contrast
35
30
  Contrast::Utils::InventoryUtil.append_db_config(app_update_msg)
36
31
  Contrast::Agent.messaging_queue.send_event_eventually(app_update_msg)
37
32
  end
33
+
34
+ private
35
+
36
+ def threaded_analysis!
37
+ Contrast::Agent::Thread.new do
38
+ Contrast::Agent::Inventory::DependencyUsageAnalysis.instance.catchup
39
+ send_inventory_message
40
+ rescue StandardError => e
41
+ logger.warn('Unable to run post-initialization static analysis', e)
42
+ end
43
+ end
38
44
  end
39
45
  end
40
46
  end
@@ -5,7 +5,7 @@ require 'contrast/components/interface'
5
5
 
6
6
  module Contrast
7
7
  module Agent
8
- # Threads used by Contrast.
8
+ # Threads used by Contrast. Any long running thread should be created and managed by our ThreadWatcher class.
9
9
  class Thread < ::Thread
10
10
  include Contrast::Components::Interface
11
11
 
@@ -3,22 +3,31 @@
3
3
 
4
4
  require 'contrast/components/interface'
5
5
  require 'contrast/agent/service_heartbeat'
6
+ require 'contrast/api/communication/messaging_queue'
6
7
 
7
8
  module Contrast
8
9
  module Agent
9
10
  # This class used to ensure that our worker threads are running in multi-process environments
11
+ #
12
+ # @attr_reader heapdump_util [Contrast::Utils::HeapDumpUtil]
13
+ # @attr_reader heartbeat [Contrast::Agent::ServiceHeartbeat]
14
+ # @attr_reader messaging_queue [Contrast::Api::Communication::MessagingQueue]
10
15
  class ThreadWatcher
11
16
  include Contrast::Components::Interface
12
- access_component :logging
17
+ access_component :agent, :logging
13
18
 
14
- attr_reader :heartbeat
19
+ attr_reader :heapdump_util, :heartbeat, :messaging_queue
15
20
 
16
21
  def initialize
17
22
  @pids = {}
23
+ @heapdump_util = Contrast::Utils::HeapDumpUtil.new
18
24
  @heartbeat = Contrast::Agent::ServiceHeartbeat.new
25
+ @messaging_queue = Contrast::Api::Communication::MessagingQueue.new
19
26
  end
20
27
 
21
28
  def startup!
29
+ return unless AGENT.enabled?
30
+
22
31
  unless heartbeat.running?
23
32
  logger.debug('Attempting to start heartbeat thread')
24
33
  heartbeat.start_thread!
@@ -26,11 +35,11 @@ module Contrast
26
35
  heartbeat_result = heartbeat.running?
27
36
  logger.debug('Heartbeat thread status', alive: heartbeat_result)
28
37
 
29
- unless Contrast::Agent.messaging_queue.running?
38
+ unless messaging_queue.running?
30
39
  logger.debug('Attempting to start messaging queue thread')
31
- Contrast::Agent.messaging_queue.start_thread!
40
+ messaging_queue.start_thread!
32
41
  end
33
- messaging_result = Contrast::Agent.messaging_queue.running?
42
+ messaging_result = messaging_queue.running?
34
43
  logger.debug('Messaging thread status', alive: messaging_result)
35
44
 
36
45
  logger.debug('ThreadWatcher started threads')
@@ -44,6 +53,12 @@ module Contrast
44
53
  logger.debug('ThreadWatcher - threads not running')
45
54
  startup!
46
55
  end
56
+
57
+ def shutdown!
58
+ heartbeat.stop!
59
+ messaging_queue.stop!
60
+ heapdump_util.stop!
61
+ end
47
62
  end
48
63
  end
49
64
  end
@@ -3,6 +3,6 @@
3
3
 
4
4
  module Contrast
5
5
  module Agent
6
- VERSION = '4.1.0'
6
+ VERSION = '4.4.0'
7
7
  end
8
8
  end
@@ -10,7 +10,7 @@ module Contrast
10
10
  # Top level gateway to messaging with speedracer
11
11
  class MessagingQueue < Contrast::Agent::WorkerThread
12
12
  include Contrast::Components::Interface
13
- access_component :analysis, :logging, :settings
13
+ access_component :agent, :analysis, :logging, :settings
14
14
 
15
15
  attr_reader :queue, :speedracer
16
16
 
@@ -22,11 +22,19 @@ module Contrast
22
22
 
23
23
  # Use this to bypass the messaging queue and leave response processing to the caller
24
24
  def send_event_immediately event
25
- send_event(event, true)
25
+ if AGENT.disabled?
26
+ logger.warn('Attempted to send event immediately with Agent disabled', caller: caller, event: event)
27
+ return
28
+ end
29
+ speedracer.return_response(event)
26
30
  end
27
31
 
28
32
  # Use this to add a message to the queue and process the response internally
29
33
  def send_event_eventually event
34
+ if AGENT.disabled?
35
+ logger.warn('Attempted to queue event with Agent disabled', caller: caller, event: event)
36
+ return
37
+ end
30
38
  logger.debug('Enqueued event for sending', event_type: event.cs__class)
31
39
  queue << event if event
32
40
  end
@@ -35,13 +43,14 @@ module Contrast
35
43
  speedracer.ensure_startup!
36
44
  return if running?
37
45
 
46
+ @queue ||= Queue.new
38
47
  @_thread = Contrast::Agent::Thread.new do
39
48
  loop do
40
49
  event = queue.pop
41
50
 
42
51
  begin
43
52
  logger.debug('Dequeued event for sending', event_type: event.cs__class)
44
- send_event(event)
53
+ speedracer.process_internally(event)
45
54
  rescue StandardError => e
46
55
  logger.error('Could not send message to service from messaging queue thread.', e)
47
56
  end
@@ -50,25 +59,13 @@ module Contrast
50
59
  logger.debug('Started background sending thread.')
51
60
  end
52
61
 
53
- private
62
+ def stop!
63
+ return unless running?
54
64
 
55
- # return_response is used to determine if we want to return the response to the caller or process it internally
56
- def send_event event, return_response = false
57
- preprocess_event(event)
58
- if return_response
59
- speedracer.return_response(event)
60
- else
61
- speedracer.process_internally(event)
62
- end
63
- end
64
-
65
- # For now this only handles appending assess tags
66
- # eventually we could break out preprocessors for every event type
67
- def preprocess_event event
68
- return unless event.is_a?(Contrast::Api::Dtm::Activity)
69
-
70
- # See if they're even enabled
71
- event.findings.delete_if { |finding| ASSESS.rule_disabled?(finding.rule_id) }
65
+ super
66
+ @queue&.clear
67
+ @queue&.close
68
+ @queue = nil
72
69
  end
73
70
  end
74
71
  end
@@ -11,6 +11,7 @@ module Contrast
11
11
  include Contrast::Components::Interface
12
12
  access_component :agent, :analysis, :logging, :settings
13
13
 
14
+ # @param response [Contrast::Api::Settings::AgentSettings]
14
15
  def process response
15
16
  logger.debug('Received a response', sent_ms: response&.sent_ms)
16
17
 
@@ -31,8 +32,10 @@ module Contrast
31
32
 
32
33
  private
33
34
 
34
- # Given some protobuf messages, update settings.
35
+ # Given some protobuf messages, update server features.
35
36
  # This is the bridge between Contrast Service <-> Settings.
37
+ #
38
+ # @param response [Contrast::Api::Settings::AgentSettings]
36
39
  def process_server_response response
37
40
  server_features = response&.server_features
38
41
  return unless server_features
@@ -44,6 +47,10 @@ module Contrast
44
47
  server_features
45
48
  end
46
49
 
50
+ # Given some protobuf messages, update application settings.
51
+ # This is the bridge between Contrast Service <-> Settings.
52
+ #
53
+ # @param response [Contrast::Api::Settings::AgentSettings]
47
54
  def process_application_response response
48
55
  app_settings = response&.application_settings
49
56
  return unless app_settings
@@ -26,6 +26,7 @@ module Contrast
26
26
  #
27
27
  # @param event [Contrast::Api::Dtm] One of the DTMs valid for the event field of
28
28
  # Contrast::Api::Dtm::Message
29
+ # @return [Contrast::Api::Settings::AgentSettings]
29
30
  def send_one event
30
31
  msg = Contrast::Api::Dtm::Message.build(event)
31
32
  send_message(msg)
@@ -58,24 +59,31 @@ module Contrast
58
59
  end
59
60
 
60
61
  # Or something is not set.
61
- msg = if CONFIG.root.agent.service.host
62
- 'Missing a required connection value to the Contrast Service. ' \
63
- '`agent.service.port` is not set. ' \
64
- 'Falling back to default TCP socket port.'
65
- elsif CONFIG.root.agent.service.port
66
- 'Missing a required connection value to the Contrast Service. ' \
67
- '`agent.service.host` is not set. ' \
68
- 'Falling back to default TCP socket host.'
69
- else
70
- 'Missing a required connection value to the Contrast Service. ' \
71
- 'Neither `agent.service.socket` nor the pair of `agent.service.host` and `agent.service.port` are set. '\
72
- 'Falling back to default TCP socket.'
73
- end
74
- logger.warn(msg,
62
+ logger.warn(log_connection_error_msg,
75
63
  host: CONTRAST_SERVICE.host,
76
64
  port: CONTRAST_SERVICE.port)
77
65
  end
78
66
 
67
+ # If our connection isn't built properly, we need to warn the user. This builds out the context specific
68
+ # message to provide that warning
69
+ #
70
+ # @return [String]
71
+ def log_connection_error_msg
72
+ if CONFIG.root.agent.service.host
73
+ 'Missing a required connection value to the Contrast Service. ' \
74
+ '`agent.service.port` is not set. ' \
75
+ 'Falling back to default TCP socket port.'
76
+ elsif CONFIG.root.agent.service.port
77
+ 'Missing a required connection value to the Contrast Service. ' \
78
+ '`agent.service.host` is not set. ' \
79
+ 'Falling back to default TCP socket host.'
80
+ else
81
+ 'Missing a required connection value to the Contrast Service. ' \
82
+ 'Neither `agent.service.socket` nor the pair of `agent.service.host` and `agent.service.port` are set. '\
83
+ 'Falling back to default TCP socket.'
84
+ end
85
+ end
86
+
79
87
  def send_message msg
80
88
  return unless msg
81
89
 
@@ -10,6 +10,8 @@ module Contrast
10
10
  end
11
11
 
12
12
  require 'contrast/api/decorators/message'
13
+ require 'contrast/api/decorators/agent_startup'
14
+ require 'contrast/api/decorators/application_startup'
13
15
  require 'contrast/api/decorators/application_update'
14
16
  require 'contrast/api/decorators/input_analysis'
15
17
  require 'contrast/api/decorators/application_settings'
@@ -0,0 +1,58 @@
1
+ # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/api/dtm.pb'
5
+ require 'contrast/components/interface'
6
+ require 'contrast/utils/string_utils'
7
+
8
+ module Contrast
9
+ module Api
10
+ module Decorators
11
+ # Used to decorate the AgentStartup protobuf model to handle reporting Agent process start
12
+ module AgentStartup
13
+ include Contrast::Components::ComponentBase
14
+ include Contrast::Components::Interface
15
+ access_component :analysis, :config
16
+
17
+ def self.included klass
18
+ klass.extend(ClassMethods)
19
+ end
20
+
21
+ # Used to add class methods to the AgentStartup class on inclusion of the decorator
22
+ module ClassMethods
23
+ # Return a new DTM with the values from the configuration and Agent discovery
24
+ #
25
+ # @parma name [String] the Hostname of this Server, or overridden value, used to identify this process
26
+ # @parma name [String] the Hostname of this Server, or overridden value, used to identify this process
27
+ # @parma name [String] the Hostname of this Server, or overridden value, used to identify this process
28
+ # @return [Contrast::Api::Dtm::AgentStartup]
29
+ def build name, path, type
30
+ msg = new
31
+ msg.server_version = Contrast::Agent::VERSION
32
+ msg.server_name = Contrast::Utils::StringUtils.protobuf_format name
33
+ msg.server_path = Contrast::Utils::StringUtils.protobuf_format path
34
+ msg.server_type = Contrast::Utils::StringUtils.protobuf_format type
35
+ config!(msg)
36
+ msg.finding_tags = Contrast::Utils::StringUtils.protobuf_format ASSESS.tags
37
+ msg
38
+ end
39
+
40
+ private
41
+
42
+ # set the configuration driven values for this AgentStartup msg
43
+ #
44
+ # @param msg [Contrast::Api::Dtm::AgentStartup]
45
+ def config! msg
46
+ msg.version = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.server.version
47
+ msg.environment = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.server.environment
48
+ msg.server_tags = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.server.tags
49
+ msg.application_tags = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.application.tags
50
+ msg.library_tags = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.inventory.tags
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ Contrast::Api::Dtm::AgentStartup.include(Contrast::Api::Decorators::AgentStartup)
@@ -0,0 +1,51 @@
1
+ # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/api/dtm.pb'
5
+ require 'contrast/components/interface'
6
+ require 'contrast/utils/string_utils'
7
+
8
+ module Contrast
9
+ module Api
10
+ module Decorators
11
+ # Used to decorate the ApplicationCreate protobuf model to handle reporting Agent process start
12
+ module ApplicationStartup
13
+ include Contrast::Components::ComponentBase
14
+ include Contrast::Components::Interface
15
+ access_component :config
16
+
17
+ def self.included klass
18
+ klass.extend(ClassMethods)
19
+ end
20
+
21
+ # Used to add class methods to the AgentStartup class on inclusion of the decorator
22
+ module ClassMethods
23
+ # Return a new DTM with the values from the configuration
24
+ #
25
+ # @return [Contrast::Api::Dtm::ApplicationCreate]
26
+ def build
27
+ msg = new
28
+ msg.group = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.application.group
29
+ msg.app_version = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.application.version.to_s
30
+ msg.code = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.application.code
31
+ msg.metadata = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.application.metadata
32
+ session!(msg)
33
+ msg
34
+ end
35
+
36
+ private
37
+
38
+ # Set the session metadata for this ApplicationCreate msg
39
+ #
40
+ # @param msg [Contrast::Api::Dtm::ApplicationCreate]
41
+ def session! msg
42
+ msg.session_id = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.application.session_id, truncate: false
43
+ msg.session_metadata = Contrast::Utils::StringUtils.protobuf_format CONFIG.root.application.session_metadata, truncate: false
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+
51
+ Contrast::Api::Dtm::ApplicationCreate.include(Contrast::Api::Decorators::ApplicationStartup)
@@ -13,6 +13,7 @@ module Contrast
13
13
  def self.included klass
14
14
  klass.extend(ClassMethods)
15
15
  end
16
+
16
17
  # Used to add class methods to the Library class on inclusion of the decorator
17
18
  module ClassMethods
18
19
  def build digest, gem_specification
@@ -11,6 +11,7 @@ module Contrast
11
11
  def self.included klass
12
12
  klass.extend(ClassMethods)
13
13
  end
14
+
14
15
  # Used to add class methods to the LibraryUsageUpdate class on inclusion of the decorator
15
16
  module ClassMethods
16
17
  def build digest, files
@@ -26,27 +26,37 @@ module Contrast
26
26
  end
27
27
 
28
28
  # Convert ActionDispatch::Journey::Route to Contrast::Api::Dtm::RouteCoverage
29
+ #
29
30
  # @param journey_obj [ActionDispatch::Journey::Route] a rails route
31
+ # @param url [String, nil] use url from string instead of journey object.
30
32
  # @return [Contrast::Api::Dtm::RouteCoverage]
31
- def from_action_dispatch_journey journey_obj
33
+ def from_action_dispatch_journey journey_obj, url = nil
32
34
  msg = new
33
35
  msg.route = "#{ journey_obj.defaults[:controller] }##{ journey_obj.defaults[:action] }"
34
36
 
35
37
  verb = source_or_string(journey_obj.verb)
36
38
  msg.verb = Contrast::Utils::StringUtils.force_utf8(verb)
37
39
 
38
- url = source_or_string(journey_obj.path.spec)
40
+ url ||= source_or_string(journey_obj.path.spec)
39
41
  msg.url = Contrast::Utils::StringUtils.force_utf8(url)
40
42
  msg
41
43
  end
42
44
 
43
- def from_sinatra_route clazz, method, pattern
45
+ # Convert Sinatra route data to dtm message.
46
+ #
47
+ # @param controller [::Sinatra::Base] the route's final controller.
48
+ # @param method [String] GET, PUT, POST, etc...
49
+ # @param method [::Mustermann::Sinatra] the pattern that was matched in routing.
50
+ # @param url [String, nil] use url from string instead matched pattern.
51
+ # @return [Contrast::Api::Dtm::RouteCoverage]
52
+ def from_sinatra_route controller, method, pattern, url = nil
44
53
  safe_pattern = source_or_string(pattern)
54
+ safe_url = source_or_string(url || pattern)
45
55
 
46
56
  msg = new
47
- msg.route = "#{ clazz }##{ method } #{ safe_pattern }"
57
+ msg.route = "#{ controller }##{ method } #{ safe_pattern }"
48
58
  msg.verb = Contrast::Utils::StringUtils.force_utf8(method)
49
- msg.url = Contrast::Utils::StringUtils.force_utf8(safe_pattern)
59
+ msg.url = Contrast::Utils::StringUtils.force_utf8(safe_url)
50
60
  msg
51
61
  end
52
62
  end