contrast-agent 4.11.0 → 4.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +4 -4
  2. data/.simplecov +1 -0
  3. data/ext/cs__assess_module/cs__assess_module.c +48 -0
  4. data/ext/cs__assess_module/cs__assess_module.h +7 -0
  5. data/ext/cs__common/cs__common.c +24 -7
  6. data/ext/cs__common/cs__common.h +12 -2
  7. data/ext/cs__contrast_patch/cs__contrast_patch.c +48 -11
  8. data/ext/cs__contrast_patch/cs__contrast_patch.h +5 -2
  9. data/ext/cs__os_information/cs__os_information.c +31 -0
  10. data/ext/cs__os_information/cs__os_information.h +7 -0
  11. data/ext/{cs__protect_kernel → cs__os_information}/extconf.rb +0 -0
  12. data/lib/contrast/agent/assess/contrast_event.rb +1 -1
  13. data/lib/contrast/agent/assess/contrast_object.rb +1 -1
  14. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +2 -0
  15. data/lib/contrast/agent/assess/policy/policy_node.rb +6 -6
  16. data/lib/contrast/agent/assess/policy/policy_scanner.rb +5 -0
  17. data/lib/contrast/agent/assess/policy/preshift.rb +19 -6
  18. data/lib/contrast/agent/assess/policy/propagation_method.rb +2 -116
  19. data/lib/contrast/agent/assess/policy/propagation_node.rb +4 -4
  20. data/lib/contrast/agent/assess/policy/propagator/center.rb +1 -1
  21. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +2 -0
  22. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +2 -154
  23. data/lib/contrast/agent/assess/policy/source_method.rb +2 -71
  24. data/lib/contrast/agent/assess/policy/trigger_method.rb +45 -110
  25. data/lib/contrast/agent/assess/policy/trigger_node.rb +62 -21
  26. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +1 -1
  27. data/lib/contrast/agent/assess/property/tagged.rb +66 -189
  28. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +40 -6
  29. data/lib/contrast/agent/deadzone/policy/policy.rb +6 -0
  30. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +1 -0
  31. data/lib/contrast/agent/metric_telemetry_event.rb +26 -0
  32. data/lib/contrast/agent/middleware.rb +14 -62
  33. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +0 -1
  34. data/lib/contrast/agent/patching/policy/method_policy.rb +3 -44
  35. data/lib/contrast/agent/patching/policy/method_policy_extend.rb +111 -0
  36. data/lib/contrast/agent/patching/policy/patch.rb +37 -238
  37. data/lib/contrast/agent/patching/policy/patcher.rb +15 -50
  38. data/lib/contrast/agent/reporting/report.rb +21 -0
  39. data/lib/contrast/agent/reporting/reporter.rb +142 -0
  40. data/lib/contrast/agent/reporting/reporting_events/finding.rb +90 -0
  41. data/lib/contrast/agent/reporting/reporting_events/preflight.rb +25 -0
  42. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +56 -0
  43. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +37 -0
  44. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +127 -0
  45. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +168 -0
  46. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +66 -0
  47. data/lib/contrast/agent/request.rb +2 -81
  48. data/lib/contrast/agent/request_context.rb +18 -126
  49. data/lib/contrast/agent/request_context_extend.rb +138 -0
  50. data/lib/contrast/agent/request_handler.rb +7 -3
  51. data/lib/contrast/agent/response.rb +2 -73
  52. data/lib/contrast/agent/rule_set.rb +2 -4
  53. data/lib/contrast/agent/startup_metrics_telemetry_event.rb +94 -0
  54. data/lib/contrast/agent/static_analysis.rb +5 -3
  55. data/lib/contrast/agent/telemetry.rb +137 -0
  56. data/lib/contrast/agent/telemetry_event.rb +33 -0
  57. data/lib/contrast/agent/thread_watcher.rb +66 -11
  58. data/lib/contrast/agent/version.rb +1 -1
  59. data/lib/contrast/agent.rb +21 -1
  60. data/lib/contrast/api/communication/connection_status.rb +10 -7
  61. data/lib/contrast/api/communication/messaging_queue.rb +37 -3
  62. data/lib/contrast/api/communication/response_processor.rb +15 -8
  63. data/lib/contrast/api/communication/service_lifecycle.rb +13 -3
  64. data/lib/contrast/api/communication/socket.rb +6 -8
  65. data/lib/contrast/api/communication/socket_client.rb +29 -12
  66. data/lib/contrast/api/communication/speedracer.rb +37 -1
  67. data/lib/contrast/api/communication/tcp_socket.rb +4 -3
  68. data/lib/contrast/api/communication/unix_socket.rb +1 -0
  69. data/lib/contrast/api/decorators/finding.rb +45 -0
  70. data/lib/contrast/components/api.rb +90 -0
  71. data/lib/contrast/components/app_context.rb +10 -41
  72. data/lib/contrast/components/app_context_extend.rb +78 -0
  73. data/lib/contrast/components/assess.rb +7 -0
  74. data/lib/contrast/components/base.rb +23 -0
  75. data/lib/contrast/components/config.rb +92 -13
  76. data/lib/contrast/components/contrast_service.rb +11 -0
  77. data/lib/contrast/components/sampling.rb +2 -2
  78. data/lib/contrast/config/agent_configuration.rb +1 -1
  79. data/lib/contrast/config/api_configuration.rb +27 -0
  80. data/lib/contrast/config/api_proxy_configuration.rb +14 -0
  81. data/lib/contrast/config/application_configuration.rb +2 -3
  82. data/lib/contrast/config/assess_configuration.rb +3 -2
  83. data/lib/contrast/config/base_configuration.rb +17 -28
  84. data/lib/contrast/config/certification_configuration.rb +15 -0
  85. data/lib/contrast/config/env_variables.rb +18 -0
  86. data/lib/contrast/config/heap_dump_configuration.rb +6 -6
  87. data/lib/contrast/config/inventory_configuration.rb +1 -5
  88. data/lib/contrast/config/protect_rule_configuration.rb +1 -1
  89. data/lib/contrast/config/request_audit_configuration.rb +18 -0
  90. data/lib/contrast/config/root_configuration.rb +1 -0
  91. data/lib/contrast/config/ruby_configuration.rb +6 -6
  92. data/lib/contrast/config/service_configuration.rb +2 -2
  93. data/lib/contrast/config.rb +1 -1
  94. data/lib/contrast/configuration.rb +4 -2
  95. data/lib/contrast/extension/assess/array.rb +5 -7
  96. data/lib/contrast/framework/manager.rb +22 -44
  97. data/lib/contrast/framework/manager_extend.rb +50 -0
  98. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +9 -6
  99. data/lib/contrast/framework/rails/patch/support.rb +31 -29
  100. data/lib/contrast/framework/rails/railtie.rb +1 -1
  101. data/lib/contrast/framework/sinatra/support.rb +2 -1
  102. data/lib/contrast/logger/application.rb +4 -0
  103. data/lib/contrast/logger/log.rb +8 -103
  104. data/lib/contrast/utils/assess/propagation_method_utils.rb +129 -0
  105. data/lib/contrast/utils/assess/property/tagged_utils.rb +165 -0
  106. data/lib/contrast/utils/assess/source_method_utils.rb +83 -0
  107. data/lib/contrast/utils/assess/tracking_util.rb +20 -15
  108. data/lib/contrast/utils/assess/trigger_method_utils.rb +138 -0
  109. data/lib/contrast/utils/class_util.rb +65 -54
  110. data/lib/contrast/utils/exclude_key.rb +20 -0
  111. data/lib/contrast/utils/findings.rb +62 -0
  112. data/lib/contrast/utils/hash_digest.rb +10 -73
  113. data/lib/contrast/utils/hash_digest_extend.rb +86 -0
  114. data/lib/contrast/utils/head_dump_utils_extend.rb +74 -0
  115. data/lib/contrast/utils/heap_dump_util.rb +2 -65
  116. data/lib/contrast/utils/invalid_configuration_util.rb +29 -0
  117. data/lib/contrast/utils/io_util.rb +1 -1
  118. data/lib/contrast/utils/log_utils.rb +108 -0
  119. data/lib/contrast/utils/lru_cache.rb +4 -2
  120. data/lib/contrast/utils/metrics_hash.rb +59 -0
  121. data/lib/contrast/utils/middleware_utils.rb +87 -0
  122. data/lib/contrast/utils/net_http_base.rb +158 -0
  123. data/lib/contrast/utils/object_share.rb +1 -0
  124. data/lib/contrast/utils/os.rb +23 -0
  125. data/lib/contrast/utils/patching/policy/patch_utils.rb +232 -0
  126. data/lib/contrast/utils/patching/policy/patcher_utils.rb +54 -0
  127. data/lib/contrast/utils/request_utils.rb +88 -0
  128. data/lib/contrast/utils/response_utils.rb +97 -0
  129. data/lib/contrast/utils/substitution_utils.rb +167 -0
  130. data/lib/contrast/utils/tag_util.rb +9 -9
  131. data/lib/contrast/utils/telemetry.rb +79 -0
  132. data/lib/contrast/utils/telemetry_client.rb +90 -0
  133. data/lib/contrast/utils/telemetry_identifier.rb +130 -0
  134. data/lib/contrast.rb +19 -1
  135. data/resources/assess/policy.json +12 -6
  136. data/resources/deadzone/policy.json +86 -5
  137. data/ruby-agent.gemspec +7 -6
  138. data/service_executables/VERSION +1 -1
  139. data/service_executables/linux/contrast-service +0 -0
  140. data/service_executables/mac/contrast-service +0 -0
  141. metadata +68 -26
  142. data/ext/cs__protect_kernel/cs__protect_kernel.c +0 -47
  143. data/ext/cs__protect_kernel/cs__protect_kernel.h +0 -12
  144. data/lib/contrast/config/default_value.rb +0 -17
  145. data/lib/contrast/extension/protect/kernel.rb +0 -29
@@ -13,6 +13,10 @@ module Contrast
13
13
 
14
14
  private
15
15
 
16
+ # Attempt to start up a local process with SpeedRacer. This will ensure the process isn't just a zombie and is
17
+ # a real functioning process.
18
+ #
19
+ # @return [Boolean] Did the SpeedRacer process start?
16
20
  def attempt_local_service_startup
17
21
  zombie_check
18
22
  service_starter_thread.join(5)
@@ -25,8 +29,8 @@ module Contrast
25
29
  is_service_started
26
30
  end
27
31
 
28
- # check if there's a zombie service that exists, and wait on it if so. currently, this only happens when trying
29
- # to initialize speedracer
32
+ # Check if there's a zombie service that exists, and wait on it if so. Currently, this only happens when trying
33
+ # to initialize SpeedRacer. If there is a zombie, we'll wait until the zombie completes.
30
34
  def zombie_check
31
35
  zombie_pid_list = Contrast::Utils::OS.zombie_pids
32
36
  zombie_pid_list.each do |pid|
@@ -36,6 +40,10 @@ module Contrast
36
40
  end
37
41
  end
38
42
 
43
+ # Determine the options used to set up the SpeedRacer process. Specifically, this handles if we need to adjust
44
+ # where the process needs to log.
45
+ #
46
+ # @return [Hash] options used to spawn the SpeedRacer process
39
47
  def determine_startup_options
40
48
  return { out: :out, err: :out } if ::Contrast::CONTRAST_SERVICE.logger_path == 'STDOUT'
41
49
  return { out: :err, err: :err } if ::Contrast::CONTRAST_SERVICE.logger_path == 'STDERR'
@@ -43,13 +51,15 @@ module Contrast
43
51
  { out: File::NULL, err: File::NULL }
44
52
  end
45
53
 
46
- # This is a separate method so we can overwrite it globally in specs
54
+ # Create a new process for the SpeedRacer. This is a separate method so we can overwrite it globally in specs.
47
55
  def spawn_service
48
56
  options = determine_startup_options
49
57
  logger.debug('Spawning service')
50
58
  spawn 'contrast_service', options
51
59
  end
52
60
 
61
+ # Create a thread to start the SpeedRacer, making calls to spawn until one starts successfully. As soon as the
62
+ # SpeedRacer process starts, this thread terminates.
53
63
  def service_starter_thread
54
64
  Contrast::Agent::Thread.new do
55
65
  # Always check to see if it already started
@@ -4,16 +4,15 @@
4
4
  module Contrast
5
5
  module Api
6
6
  module Communication
7
- # Behavior common to all sockets used
8
- # to communicate with the Contrast Service.
7
+ # Behavior common to all sockets used to communicate with the Contrast Service.
9
8
  module Socket
10
9
  SOCKET_MONITOR = Monitor.new
11
10
 
12
11
  # Send a message across the socket and read back the response.
13
- # @param marshaled [String] some marshaled form of data to be sent across
14
- # the socket. Typically an encoded form of Contrast::Api::Dtm::Message.
15
- # @return [String] some marshalled form of data returned by the socket.
16
- # Typically an encoded form of Contrast::Api::Settings::AgentSettings.
12
+ # @param marshaled [String] some marshaled form of data to be sent across the socket. Typically an encoded form
13
+ # of Contrast::Api::Dtm::Message.
14
+ # @return [String] some marshalled form of data returned by the socket. Typically an encoded form of
15
+ # Contrast::Api::Settings::AgentSettings.
17
16
  def send_marshaled marshaled
18
17
  SOCKET_MONITOR.synchronize do
19
18
  socket = new_socket
@@ -34,8 +33,7 @@ module Contrast
34
33
  end
35
34
  end
36
35
 
37
- # Override this method to return a socket.
38
- # Should be interface compatible with TCPSocket, UNIXSocket, etc.
36
+ # Override this method to return a socket. Should be interface compatible with TCPSocket, UNIXSocket, etc.
39
37
  def new_socket
40
38
  raise NoMethodError, 'This is abstract, override it.'
41
39
  end
@@ -11,8 +11,8 @@ require 'contrast/components/logger'
11
11
  module Contrast
12
12
  module Api
13
13
  module Communication
14
- # SocketClient acts as a interface between the agent and the service. It instantiates a
15
- # service proxy and tracks the state of that proxy.
14
+ # SocketClient acts as a interface between the agent and the service. It instantiates a service proxy and tracks
15
+ # the state of that proxy.
16
16
  class SocketClient
17
17
  include Contrast::Components::Logger::InstanceMethods
18
18
 
@@ -20,12 +20,13 @@ module Contrast
20
20
  @socket = init_connection
21
21
  end
22
22
 
23
- # Wrap the given DTM in a Contrast::Api::Dtm::Message and send it to the
24
- # Service for processing
23
+ # Wrap the given DTM in a Contrast::Api::Dtm::Message and send it to the SpeedRacer for processing. The
24
+ # Message is the top level object required to communicate to SpeedRacer as it encompasses the information
25
+ # needed to find this process' context.
25
26
  #
26
27
  # @param event [Contrast::Api::Dtm] One of the DTMs valid for the event field of
27
28
  # Contrast::Api::Dtm::Message
28
- # @return [Contrast::Api::Settings::AgentSettings]
29
+ # @return [Contrast::Api::Settings::AgentSettings, nil]
29
30
  def send_one event
30
31
  msg = Contrast::Api::Dtm::Message.build(event)
31
32
  send_message(msg)
@@ -33,6 +34,12 @@ module Contrast
33
34
 
34
35
  private
35
36
 
37
+ # Initialize the connection to the SpeedRacer process based on the configuration provided by the user. This can
38
+ # be either TCP or UDP. Note that unlike the Go and the Node Agents, we cannot use the GRPC communication
39
+ # option as we cannot use Google's protobuf gems; they do not compile reliably and result in segmentation
40
+ # faults in customer environments.
41
+ #
42
+ # @return [Contrast::Api::Communication::TcpSocket, Contrast::Api::Communication::UnixSocket]
36
43
  def init_connection
37
44
  log_connection
38
45
  if ::Contrast::CONTRAST_SERVICE.use_tcp?
@@ -43,6 +50,7 @@ module Contrast
43
50
  end
44
51
  end
45
52
 
53
+ # Log information about the connection being used to communicate between the Agent and SpeedRacer.
46
54
  def log_connection
47
55
  # The socket is set,
48
56
  if ::Contrast::CONFIG.root.agent.service.socket
@@ -72,19 +80,24 @@ module Contrast
72
80
  def log_connection_error_msg
73
81
  if ::Contrast::CONFIG.root.agent.service.host
74
82
  'Missing a required connection value to the Contrast Service. ' \
75
- '`agent.service.port` is not set. ' \
76
- 'Falling back to default TCP socket port.'
83
+ '`agent.service.port` is not set. ' \
84
+ 'Falling back to default TCP socket port.'
77
85
  elsif ::Contrast::CONFIG.root.agent.service.port
78
86
  'Missing a required connection value to the Contrast Service. ' \
79
- '`agent.service.host` is not set. ' \
80
- 'Falling back to default TCP socket host.'
87
+ '`agent.service.host` is not set. ' \
88
+ 'Falling back to default TCP socket host.'
81
89
  else
82
90
  'Missing a required connection value to the Contrast Service. ' \
83
- 'Neither `agent.service.socket` nor the pair of `agent.service.host` and `agent.service.port` are set. '\
84
- 'Falling back to default TCP socket.'
91
+ 'Neither `agent.service.socket` nor the pair of `agent.service.host` and `agent.service.port` are set. '\
92
+ 'Falling back to default TCP socket.'
85
93
  end
86
94
  end
87
95
 
96
+ # Send the given message to SpeedRacer and return the response from it.
97
+ #
98
+ # @param msg [Contrast::Api::Dtm::Message] the packaged message to send to SpeedRacer
99
+ # @return [Contrast::Api::Settings::AgentSettings, nil]
100
+ # @raise [StandardError] if unable to send a message to SpeedRacer
88
101
  def send_message msg
89
102
  return unless msg
90
103
 
@@ -98,9 +111,13 @@ module Contrast
98
111
  rescue StandardError => e
99
112
  logger.error('Sending failed for message.', e, msg_id: msg.__id__, p_id: msg.pid,
100
113
  msg_count: msg.message_count, response_id: response&.__id__)
101
- raise e # reraise to let Speedracer manage the connection
114
+ raise e # reraise to let SpeedRacer manage the connection
102
115
  end
103
116
 
117
+ # Send the marshaled Contrast::Api::Dtm::Message across the socket used to talk to SpeedRacer
118
+ #
119
+ # @param marshaled [String]
120
+ # @return [String] the marshaled from of Contrast::Api::Settings::AgentSettings
104
121
  def send_marshaled marshaled
105
122
  @socket.send_marshaled(marshaled)
106
123
  end
@@ -6,7 +6,10 @@ require 'contrast/components/logger'
6
6
  module Contrast
7
7
  module Api
8
8
  module Communication
9
- # Wraps all connection data to speedracer
9
+ # Wraps all connection data to SpeedRacer. SpeedRacer, also known as the Contrast Service, is a standalone
10
+ # executable that sits between the Agent and TeamServer. It handles converting our Protobuf messages into a
11
+ # format consumable by TeamServer. The Agent requires a SpeedRacer process to be running somewhere to which it
12
+ # can connect, as specified by the user configuration, in order to function.
10
13
  class Speedracer
11
14
  include Contrast::Api::Communication::ServiceLifecycle
12
15
  include Contrast::Components::Logger::InstanceMethods
@@ -20,6 +23,14 @@ module Contrast
20
23
  @ensure_running = Mutex.new
21
24
  end
22
25
 
26
+ # If there is not a SpeedRacer at the location specified by the configuration of this Agent and the Agent is
27
+ # set such that it should manage one, start up a new child process to run the SpeedRacer executable. If a
28
+ # connection has not already been made to that process, after starting it, this method will also send the
29
+ # messages necessary to create a context for this Agent process in the SpeedRacer as well as trigger
30
+ # SpeedRacer's sending of startup messages which will return features and settings from TeamServer.
31
+ #
32
+ # This operation is synchronous and blocking, so it will only happen one at a time per process and will halt
33
+ # Thread execution here until completion.
23
34
  def ensure_startup!
24
35
  return if status.connected?
25
36
 
@@ -40,12 +51,24 @@ module Contrast
40
51
  end
41
52
  end
42
53
 
54
+ # Send the given Event to SpeedRacer, returning the response from it. This response will either be new settings
55
+ # if anything's changed in TeamServer meaning the Agent needs to replace its current settings or there is
56
+ # Protect analysis information or nil if the current Agent settings do not need updating.
57
+ #
58
+ # @param event [Contrast::Api::Dtm] One of the DTMs valid for the event field of
59
+ # Contrast::Api::Dtm::Message|Contrast::Api::Dtm::Activity
60
+ # @return [Contrast::Api::Settings::AgentSettings,nil]
43
61
  def return_response event
44
62
  send_to_speedracer(event) do |response|
45
63
  return response
46
64
  end
47
65
  end
48
66
 
67
+ # Send the given Event to SpeedRacer and pass the result to our Contrast::Api::Communication::ResponseProcessor
68
+ # as there is no immediate action required from sending this Event.
69
+ #
70
+ # @param event [Contrast::Api::Dtm] One of the DTMs valid for the event field of
71
+ # Contrast::Api::Dtm::Message|Contrast::Api::Dtm::Activity
49
72
  def process_internally event
50
73
  send_to_speedracer(event) do |response|
51
74
  response_processor.process(response)
@@ -54,6 +77,12 @@ module Contrast
54
77
 
55
78
  private
56
79
 
80
+ # Ensure there is a running SpeedRacer and then send the given Event to it. It is necessary to ensure the
81
+ # SpeedRacer is running as, if the process has crashed or restarted, we must rebuild our context there.
82
+ #
83
+ # @param event [Contrast::Api::Dtm] One of the DTMs valid for the event field of
84
+ # Contrast::Api::Dtm::Message|Contrast::Api::Dtm::Activity
85
+ # @return [Contrast::Api::Settings::AgentSettings, nil]
57
86
  def send_to_speedracer event
58
87
  ensure_startup!
59
88
 
@@ -68,6 +97,9 @@ module Contrast
68
97
  nil
69
98
  end
70
99
 
100
+ # Send those messages which are required to build a context for this Agent process on SpeedRacer as well as
101
+ # report server and application startup to TeamServer. With these messages, the SpeedRacer will be able to
102
+ # retrieve settings from TeamServer and provide those for the Agent to complete its initialization.
71
103
  def send_initialization_messages
72
104
  agent_startup_msg = ::Contrast::APP_CONTEXT.build_agent_startup_message
73
105
 
@@ -97,6 +129,10 @@ module Contrast
97
129
  nil
98
130
  end
99
131
 
132
+ # Log the startup message we're sending to SpeedRacer
133
+ #
134
+ # @param event [Contrast::Api::Dtm] One of the DTMs valid for the event field of
135
+ # Contrast::Api::Dtm::Message|Contrast::Api::Dtm::Activity
100
136
  def log_send_event event
101
137
  logger.debug('Immediately sending event.', event_id: event.__id__, event_type: event.cs__class.cs__name)
102
138
  end
@@ -6,15 +6,15 @@ require 'contrast/api/communication/socket'
6
6
  module Contrast
7
7
  module Api
8
8
  module Communication
9
- # This class allows us to create a TCP Socket to communicate to the Service
10
- # (Speed Racer). Either it or the Unix Socket will be used, as determined
11
- # by the configuration options set for Service communication.
9
+ # This class allows us to create a TCP Socket to communicate to the Service (Speed Racer). Either it or the Unix
10
+ # Socket will be used, as determined by the configuration options set for Service communication.
12
11
  class TcpSocket
13
12
  include Contrast::Api::Communication::Socket
14
13
 
15
14
  attr_reader :host, :port
16
15
 
17
16
  # Create the socket
17
+ #
18
18
  # @param host [String] socket target hostname or IP address
19
19
  # @param port [String,Integer] socket target port
20
20
  def initialize host, port
@@ -22,6 +22,7 @@ module Contrast
22
22
  @port = port.to_i
23
23
  end
24
24
 
25
+ # @return [::TCPSocket]
25
26
  def new_socket
26
27
  ::TCPSocket.new(host, port)
27
28
  end
@@ -18,6 +18,7 @@ module Contrast
18
18
  @path = path
19
19
  end
20
20
 
21
+ # @return [::UNIXSocket]
21
22
  def new_socket
22
23
  ::UNIXSocket.new(path)
23
24
  end
@@ -0,0 +1,45 @@
1
+ # Copyright (c) 2021 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/utils/string_utils'
6
+ require 'contrast/components/base'
7
+
8
+ module Contrast
9
+ module Api
10
+ module Decorators
11
+ # Used to decorate the {Contrast::Api::Dtm::Finding} protobuf
12
+ # model so it can own the request which its data is for.
13
+ module Finding
14
+ def self.included klass
15
+ klass.extend(ClassMethods)
16
+ end
17
+
18
+ # Used to add class methods to the AgentStartup class on inclusion of the decorator
19
+ module ClassMethods
20
+ def build
21
+ new
22
+ end
23
+
24
+ def to_controlled_hash finding, *_args
25
+ {
26
+ hash_code: finding.hash_code,
27
+ platform: finding.platform,
28
+ rule_id: finding.rule_id,
29
+ evidence: finding.evidence,
30
+ properties: finding.properties,
31
+ events: finding.events,
32
+ tags: finding.tags,
33
+ version: finding.version,
34
+ routes: finding.routes,
35
+ session_id: finding.session_id,
36
+ teamserver_url: ::Contrast::API.api_url
37
+ }
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
44
+
45
+ Contrast::Api::Dtm::Finding.include(Contrast::Api::Decorators::Finding)
@@ -0,0 +1,90 @@
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/components/base'
5
+ require 'contrast/components/config'
6
+
7
+ module Contrast
8
+ module Components
9
+ module Api
10
+ # A wrapper build around the Common Agent Configuration project to allow
11
+ # for Api keys to be mapped with their values contained in their
12
+ # parent_configuration_spec.yaml.
13
+ class Interface
14
+ include Contrast::Components::ComponentBase
15
+
16
+ def api_url
17
+ @_api_url ||= ::Contrast::CONFIG.root.api.url
18
+ end
19
+
20
+ def api_key
21
+ @_api_key ||= ::Contrast::CONFIG.root.api.api_key
22
+ end
23
+
24
+ def service_key
25
+ @_service_key ||= ::Contrast::CONFIG.root.api.service_key
26
+ end
27
+
28
+ def username
29
+ @_username ||= ::Contrast::CONFIG.root.api.user_name
30
+ end
31
+
32
+ def proxy_enabled?
33
+ @_proxy_enabled = true?(::Contrast::CONFIG.root.api.proxy.enable) if @_proxy_enabled.nil?
34
+ @_proxy_enabled
35
+ end
36
+
37
+ def proxy_url
38
+ @_proxy_url ||= ::Contrast::CONFIG.root.api.proxy.url
39
+ end
40
+
41
+ def request_audit_enable
42
+ @_request_audit_enable ||= true?(::Contrast::CONFIG.root.api.request_audit.enable)
43
+ end
44
+
45
+ def request_audit_requests
46
+ return @_request_audit_requests unless @_request_audit_requests.nil?
47
+
48
+ @_request_audit_requests = true?(::Contrast::CONFIG.root.api.request_audit.requests)
49
+ end
50
+
51
+ def request_audit_responses
52
+ return @_request_audit_responses unless @_request_audit_responses.nil?
53
+
54
+ @_request_audit_responses = true?(::Contrast::CONFIG.root.api.request_audit.responses)
55
+ end
56
+
57
+ def request_audit_path
58
+ @_request_audit_path ||= ::Contrast::CONFIG.root.api.request_audit.path.to_s
59
+ end
60
+
61
+ def certification_enabled?
62
+ @_certification_enabled ||= certification_truly_enabled?(::Contrast::CONFIG.root.api.certificate)
63
+ end
64
+
65
+ def certification_ca_file
66
+ @_certification_ca_file ||= ::Contrast::CONFIG.root.api.certificate.ca_file
67
+ end
68
+
69
+ def certification_cert_file
70
+ @_certification_cert_file ||= ::Contrast::CONFIG.root.api.certificate.cert_file
71
+ end
72
+
73
+ def certification_key_file
74
+ @_certification_key_file ||= ::Contrast::CONFIG.root.api.certificate.key_file
75
+ end
76
+
77
+ private
78
+
79
+ def certification_truly_enabled? config_path
80
+ return false unless true?(config_path.enable)
81
+ return true if file_exists?(certification_ca_file) && valid_cert?(certification_ca_file)
82
+ return true if file_exists?(certification_cert_file) && valid_cert?(certification_cert_file)
83
+ return true if file_exists?(certification_key_file) && valid_cert?(certification_key_file)
84
+
85
+ false
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -5,6 +5,7 @@ require 'rubygems/version'
5
5
  require 'contrast/api/decorators/agent_startup'
6
6
  require 'contrast/api/decorators/application_startup'
7
7
  require 'contrast/utils/object_share'
8
+ require 'contrast/components/app_context_extend'
8
9
 
9
10
  module Contrast
10
11
  module Components
@@ -15,6 +16,7 @@ module Contrast
15
16
  # Specifically, this allows for querying the state of the Application,
16
17
  # including the Client, Process, and Server information.
17
18
  class Interface
19
+ include Contrast::Components::AppContextExtend
18
20
  include Contrast::Components::ComponentBase
19
21
  include Contrast::Components::Logger::InstanceMethods
20
22
 
@@ -46,6 +48,14 @@ module Contrast
46
48
  end
47
49
  end
48
50
 
51
+ def session_id
52
+ @_session_id ||= build_app_startup_message.session_id
53
+ end
54
+
55
+ def app_version
56
+ @_app_version ||= Contrast::CONFIG.root.application.version
57
+ end
58
+
49
59
  def path
50
60
  @_path ||= begin
51
61
  tmp = ::Contrast::CONFIG.root.application.path
@@ -76,47 +86,6 @@ module Contrast
76
86
  end
77
87
  end
78
88
 
79
- def build_app_startup_message
80
- Contrast::Api::Dtm::ApplicationCreate.build
81
- end
82
-
83
- def build_agent_startup_message
84
- msg = Contrast::Api::Dtm::AgentStartup.build(server_name, server_path, server_type)
85
- logger.info('Application context',
86
- server_name: msg.server_name,
87
- server_path: msg.server_path,
88
- server_type: msg.server_type,
89
- application_name: app_name,
90
- application_path: path,
91
- application_language: Contrast::Utils::ObjectShare::RUBY)
92
-
93
- msg
94
- end
95
-
96
- def pid
97
- Process.pid
98
- end
99
-
100
- def ppid
101
- Process.ppid
102
- end
103
-
104
- def pgid
105
- Process.getpgid(pid)
106
- end
107
-
108
- def client_id
109
- @_client_id ||= [app_name, pgid].join('-')
110
- end
111
-
112
- def instrument_middleware_stack?
113
- !Contrast::Utils::JobServersRunning.job_servers_running?
114
- end
115
-
116
- def disabled_agent_rake_tasks
117
- ::Contrast::CONFIG.root.agent.ruby.disabled_agent_rake_tasks
118
- end
119
-
120
89
  # Determines if the Process we're currently in matches that of the
121
90
  # Process in which the App Context instance was created.
122
91
  # If it doesn't, that indicates the running context is in a new
@@ -0,0 +1,78 @@
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ module Contrast
5
+ module Components
6
+ # A wrapper build around the Common Agent Configuration project to allow
7
+ # for access of the values contained in its
8
+ # parent_configuration_spec.yaml.
9
+ # Specifically, this allows for querying the state of the Application,
10
+ # including the Client, Process, and Server information.
11
+ module AppContextExtend
12
+ SUPPORTED_FRAMEWORKS = %w[rails sinatra grape rack].cs__freeze
13
+ SUPPORTED_SERVERS = %w[passenger puma thin unicorn].cs__freeze
14
+
15
+ def build_app_startup_message
16
+ @_build_app_startup_message ||= Contrast::Api::Dtm::ApplicationCreate.build
17
+ end
18
+
19
+ def build_agent_startup_message
20
+ msg = Contrast::Api::Dtm::AgentStartup.build(server_name, server_path, server_type)
21
+ logger.info('Application context',
22
+ server_name: msg.server_name,
23
+ server_path: msg.server_path,
24
+ server_type: msg.server_type,
25
+ application_name: app_name,
26
+ application_path: path,
27
+ application_language: Contrast::Utils::ObjectShare::RUBY)
28
+
29
+ msg
30
+ end
31
+
32
+ def pid
33
+ Process.pid
34
+ end
35
+
36
+ def ppid
37
+ Process.ppid
38
+ end
39
+
40
+ def pgid
41
+ Process.getpgid(pid)
42
+ end
43
+
44
+ def client_id
45
+ @_client_id ||= [app_name, pgid].join('-')
46
+ end
47
+
48
+ def app_and_server_information
49
+ {
50
+ application_info: find_gem_information(SUPPORTED_FRAMEWORKS),
51
+ server_info: find_gem_information(SUPPORTED_SERVERS)
52
+ }
53
+ end
54
+
55
+ def find_gem_information arr
56
+ arr.each do |framework|
57
+ next unless Gem.loaded_specs.key?(framework)
58
+
59
+ loaded = Gem.loaded_specs[framework]
60
+ next unless loaded
61
+
62
+ name = loaded.instance_variable_get(:@name)
63
+ version = loaded.instance_variable_get(:@version).to_s
64
+ return [name, version].join(' ')
65
+ end
66
+ nil
67
+ end
68
+
69
+ def instrument_middleware_stack?
70
+ !Contrast::Utils::JobServersRunning.job_servers_running?
71
+ end
72
+
73
+ def disabled_agent_rake_tasks
74
+ ::Contrast::CONFIG.root.agent.ruby.disabled_agent_rake_tasks
75
+ end
76
+ end
77
+ end
78
+ end
@@ -88,6 +88,13 @@ module Contrast
88
88
  @_require_scan
89
89
  end
90
90
 
91
+ def require_dynamic_sources?
92
+ if @_require_dynamic_sources.nil?
93
+ @_require_dynamic_sources = !false?(::Contrast::CONFIG.root.assess.enable_dynamic_sources)
94
+ end
95
+ @_require_dynamic_sources
96
+ end
97
+
91
98
  def tags
92
99
  ::Contrast::CONFIG.root.assess&.tags
93
100
  end
@@ -35,6 +35,29 @@ module Contrast
35
35
 
36
36
  config_param.downcase == Contrast::Utils::ObjectShare::TRUE
37
37
  end
38
+
39
+ # this method will check if a path could be possibly used
40
+ # So for example if we pass a path to a file - we'll check
41
+ # if there is actually that file and if it's with certain extension
42
+ #
43
+ # @param config_path [String,nil]
44
+ # @return [Boolean]
45
+ def valid_cert? config_path
46
+ return false if config_path.nil?
47
+
48
+ exts = %w[.pem .crt .cer].cs__freeze
49
+ return false unless exts.include?(File.extname(config_path))
50
+
51
+ true
52
+ end
53
+
54
+ # check if file exists at all
55
+ # @param path [String,nil]
56
+ def file_exists? path
57
+ return false unless path
58
+
59
+ File.exist? path
60
+ end
38
61
  end
39
62
  end
40
63
  end