contrast-agent 6.2.0 → 6.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (209) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -3
  3. data/.simplecov +1 -0
  4. data/Rakefile +0 -27
  5. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +7 -5
  6. data/ext/cs__assess_kernel/cs__assess_kernel.c +14 -3
  7. data/ext/cs__assess_kernel/cs__assess_kernel.h +2 -0
  8. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +10 -3
  9. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.h +2 -1
  10. data/ext/cs__assess_regexp/cs__assess_regexp.c +9 -7
  11. data/ext/{cs__assess_string_interpolation26/cs__assess_string_interpolation26.c → cs__assess_string_interpolation/cs__assess_string_interpolation.c} +14 -3
  12. data/ext/{cs__assess_string_interpolation26/cs__assess_string_interpolation26.h → cs__assess_string_interpolation/cs__assess_string_interpolation.h} +1 -1
  13. data/ext/{cs__assess_string_interpolation26 → cs__assess_string_interpolation}/extconf.rb +0 -0
  14. data/ext/cs__common/cs__common.c +5 -4
  15. data/ext/cs__contrast_patch/cs__contrast_patch.c +17 -11
  16. data/lib/contrast/agent/assess/events/source_event.rb +16 -12
  17. data/lib/contrast/agent/assess/finalizers/hash.rb +1 -0
  18. data/lib/contrast/agent/assess/policy/policy_node.rb +6 -0
  19. data/lib/contrast/agent/assess/policy/propagation_method.rb +8 -42
  20. data/lib/contrast/agent/assess/policy/propagation_node.rb +8 -0
  21. data/lib/contrast/agent/assess/policy/propagator/base.rb +2 -0
  22. data/lib/contrast/agent/assess/policy/propagator/custom.rb +4 -0
  23. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +5 -0
  24. data/lib/contrast/agent/assess/policy/propagator/split.rb +3 -0
  25. data/lib/contrast/agent/assess/policy/source_method.rb +7 -47
  26. data/lib/contrast/agent/assess/policy/source_node.rb +1 -0
  27. data/lib/contrast/agent/assess/policy/trigger_method.rb +9 -3
  28. data/lib/contrast/agent/assess/policy/trigger_node.rb +8 -0
  29. data/lib/contrast/agent/assess/property/evented.rb +4 -18
  30. data/lib/contrast/agent/assess/tag.rb +19 -0
  31. data/lib/contrast/agent/assess/tracker.rb +12 -0
  32. data/lib/contrast/agent/at_exit_hook.rb +8 -8
  33. data/lib/contrast/agent/inventory/database_config.rb +6 -3
  34. data/lib/contrast/agent/inventory/dependency_analysis.rb +5 -4
  35. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +11 -11
  36. data/lib/contrast/agent/inventory/policy/datastores.rb +1 -1
  37. data/lib/contrast/agent/inventory/policy/policy.rb +1 -1
  38. data/lib/contrast/agent/middleware.rb +4 -0
  39. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +27 -2
  40. data/lib/contrast/agent/patching/policy/method_policy.rb +3 -3
  41. data/lib/contrast/agent/patching/policy/policy.rb +5 -0
  42. data/lib/contrast/agent/patching/policy/policy_node.rb +6 -0
  43. data/lib/contrast/agent/patching/policy/trigger_node.rb +3 -0
  44. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +3 -4
  45. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +1 -0
  46. data/lib/contrast/agent/protect/policy/rule_applicator.rb +2 -2
  47. data/lib/contrast/agent/protect/rule/base.rb +1 -0
  48. data/lib/contrast/agent/protect/rule/no_sqli.rb +2 -0
  49. data/lib/contrast/agent/reporting/reporter.rb +32 -7
  50. data/lib/contrast/agent/reporting/reporter_heartbeat.rb +22 -18
  51. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +17 -21
  52. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +1 -1
  53. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +26 -3
  54. data/lib/contrast/agent/reporting/reporting_events/application_update.rb +5 -24
  55. data/lib/contrast/agent/reporting/reporting_events/architecture_component.rb +8 -1
  56. data/lib/contrast/agent/reporting/reporting_events/discovered_route.rb +8 -1
  57. data/lib/contrast/agent/reporting/reporting_events/finding.rb +7 -1
  58. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +10 -1
  59. data/lib/contrast/agent/reporting/reporting_events/finding_event_object.rb +11 -1
  60. data/lib/contrast/agent/reporting/reporting_events/finding_event_parent_object.rb +11 -1
  61. data/lib/contrast/agent/reporting/reporting_events/finding_event_property.rb +12 -1
  62. data/lib/contrast/agent/reporting/reporting_events/finding_event_signature.rb +10 -1
  63. data/lib/contrast/agent/reporting/reporting_events/finding_event_source.rb +11 -1
  64. data/lib/contrast/agent/reporting/reporting_events/finding_event_stack.rb +11 -1
  65. data/lib/contrast/agent/reporting/reporting_events/finding_event_taint_range.rb +11 -1
  66. data/lib/contrast/agent/reporting/reporting_events/finding_request.rb +11 -1
  67. data/lib/contrast/agent/reporting/reporting_events/library_discovery.rb +29 -32
  68. data/lib/contrast/agent/reporting/reporting_events/library_usage_observation.rb +13 -1
  69. data/lib/contrast/agent/reporting/reporting_events/observed_library_usage.rb +11 -8
  70. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +12 -5
  71. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +8 -1
  72. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +9 -1
  73. data/lib/contrast/agent/reporting/reporting_events/route_discovery.rb +10 -1
  74. data/lib/contrast/agent/reporting/reporting_events/route_discovery_observation.rb +11 -4
  75. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +0 -8
  76. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +1 -4
  77. data/lib/contrast/agent/reporting/reporting_utilities/dtm_message.rb +0 -22
  78. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +1 -3
  79. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +1 -11
  80. data/lib/contrast/agent/request.rb +5 -7
  81. data/lib/contrast/agent/request_context.rb +16 -17
  82. data/lib/contrast/agent/request_context_extend.rb +8 -9
  83. data/lib/contrast/agent/request_handler.rb +9 -38
  84. data/lib/contrast/agent/rule_set.rb +4 -0
  85. data/lib/contrast/agent/service_heartbeat.rb +3 -4
  86. data/lib/contrast/agent/static_analysis.rb +7 -12
  87. data/lib/contrast/agent/telemetry/base.rb +35 -35
  88. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +2 -0
  89. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +2 -0
  90. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message.rb +5 -2
  91. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +3 -0
  92. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +3 -0
  93. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions.rb +0 -1
  94. data/lib/contrast/agent/thread_watcher.rb +1 -4
  95. data/lib/contrast/agent/version.rb +1 -1
  96. data/lib/contrast/agent/worker_thread.rb +10 -0
  97. data/lib/contrast/api/communication/socket.rb +1 -0
  98. data/lib/contrast/api/decorators/message.rb +0 -6
  99. data/lib/contrast/api/decorators.rb +0 -2
  100. data/lib/contrast/api/dtm.pb.rb +1 -1
  101. data/lib/contrast/api/settings.pb.rb +1 -1
  102. data/lib/contrast/components/agent.rb +51 -13
  103. data/lib/contrast/components/assess.rb +16 -6
  104. data/lib/contrast/components/config.rb +18 -2
  105. data/lib/contrast/components/contrast_service.rb +1 -1
  106. data/lib/contrast/components/heap_dump.rb +51 -1
  107. data/lib/contrast/components/inventory.rb +19 -13
  108. data/lib/contrast/components/logger.rb +18 -0
  109. data/lib/contrast/config/assess_configuration.rb +28 -0
  110. data/lib/contrast/config/base_configuration.rb +8 -15
  111. data/lib/contrast/config/root_configuration.rb +12 -8
  112. data/lib/contrast/config/ruby_configuration.rb +2 -9
  113. data/lib/contrast/config/service_configuration.rb +4 -4
  114. data/lib/contrast/config.rb +0 -6
  115. data/lib/contrast/configuration.rb +0 -2
  116. data/lib/contrast/extension/assess/eval_trigger.rb +0 -4
  117. data/lib/contrast/extension/assess/hash.rb +3 -2
  118. data/lib/contrast/extension/assess/kernel.rb +22 -0
  119. data/lib/contrast/extension/assess/marshal.rb +16 -0
  120. data/lib/contrast/extension/assess/string.rb +21 -20
  121. data/lib/contrast/extension/object.rb +19 -0
  122. data/lib/contrast/framework/base_support.rb +8 -0
  123. data/lib/contrast/framework/manager.rb +6 -20
  124. data/lib/contrast/framework/manager_extend.rb +0 -1
  125. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +11 -16
  126. data/lib/contrast/framework/rails/support.rb +4 -1
  127. data/lib/contrast/logger/aliased_logging.rb +2 -0
  128. data/lib/contrast/logger/log.rb +2 -1
  129. data/lib/contrast/utils/assess/event_limit_utils.rb +96 -0
  130. data/lib/contrast/utils/assess/propagation_method_utils.rb +27 -7
  131. data/lib/contrast/utils/assess/source_method_utils.rb +0 -9
  132. data/lib/contrast/utils/log_utils.rb +2 -2
  133. data/lib/contrast/utils/lru_cache.rb +3 -0
  134. data/lib/contrast/utils/middleware_utils.rb +2 -0
  135. data/lib/contrast/utils/patching/policy/patch_utils.rb +6 -23
  136. data/lib/contrast/utils/telemetry_client.rb +7 -7
  137. data/lib/contrast.rb +37 -18
  138. data/lib/protobuf/code_generator.rb +129 -0
  139. data/lib/protobuf/decoder.rb +28 -0
  140. data/lib/protobuf/deprecation.rb +117 -0
  141. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +79 -0
  142. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +360 -0
  143. data/lib/protobuf/descriptors.rb +3 -0
  144. data/lib/protobuf/encoder.rb +11 -0
  145. data/lib/protobuf/enum.rb +365 -0
  146. data/lib/protobuf/exceptions.rb +9 -0
  147. data/lib/protobuf/field/base_field.rb +380 -0
  148. data/lib/protobuf/field/base_field_object_definitions.rb +504 -0
  149. data/lib/protobuf/field/bool_field.rb +64 -0
  150. data/lib/protobuf/field/bytes_field.rb +67 -0
  151. data/lib/protobuf/field/double_field.rb +25 -0
  152. data/lib/protobuf/field/enum_field.rb +56 -0
  153. data/lib/protobuf/field/field_array.rb +102 -0
  154. data/lib/protobuf/field/field_hash.rb +122 -0
  155. data/lib/protobuf/field/fixed32_field.rb +25 -0
  156. data/lib/protobuf/field/fixed64_field.rb +28 -0
  157. data/lib/protobuf/field/float_field.rb +43 -0
  158. data/lib/protobuf/field/int32_field.rb +21 -0
  159. data/lib/protobuf/field/int64_field.rb +34 -0
  160. data/lib/protobuf/field/integer_field.rb +23 -0
  161. data/lib/protobuf/field/message_field.rb +51 -0
  162. data/lib/protobuf/field/sfixed32_field.rb +27 -0
  163. data/lib/protobuf/field/sfixed64_field.rb +28 -0
  164. data/lib/protobuf/field/signed_integer_field.rb +29 -0
  165. data/lib/protobuf/field/sint32_field.rb +21 -0
  166. data/lib/protobuf/field/sint64_field.rb +21 -0
  167. data/lib/protobuf/field/string_field.rb +51 -0
  168. data/lib/protobuf/field/uint32_field.rb +21 -0
  169. data/lib/protobuf/field/uint64_field.rb +21 -0
  170. data/lib/protobuf/field/varint_field.rb +77 -0
  171. data/lib/protobuf/field.rb +74 -0
  172. data/lib/protobuf/generators/base.rb +85 -0
  173. data/lib/protobuf/generators/enum_generator.rb +39 -0
  174. data/lib/protobuf/generators/extension_generator.rb +27 -0
  175. data/lib/protobuf/generators/field_generator.rb +193 -0
  176. data/lib/protobuf/generators/file_generator.rb +262 -0
  177. data/lib/protobuf/generators/group_generator.rb +122 -0
  178. data/lib/protobuf/generators/message_generator.rb +104 -0
  179. data/lib/protobuf/generators/option_generator.rb +17 -0
  180. data/lib/protobuf/generators/printable.rb +160 -0
  181. data/lib/protobuf/generators/service_generator.rb +50 -0
  182. data/lib/protobuf/lifecycle.rb +33 -0
  183. data/lib/protobuf/logging.rb +39 -0
  184. data/lib/protobuf/message/fields.rb +233 -0
  185. data/lib/protobuf/message/serialization.rb +85 -0
  186. data/lib/protobuf/message.rb +241 -0
  187. data/lib/protobuf/optionable.rb +72 -0
  188. data/lib/protobuf/tasks/compile.rake +80 -0
  189. data/lib/protobuf/tasks.rb +1 -0
  190. data/lib/protobuf/varint.rb +20 -0
  191. data/lib/protobuf/varint_pure.rb +31 -0
  192. data/lib/protobuf/version.rb +3 -0
  193. data/lib/protobuf/wire_type.rb +10 -0
  194. data/lib/protobuf.rb +91 -0
  195. data/proto/dynamic_discovery.proto +46 -0
  196. data/proto/google/protobuf/compiler/plugin.proto +183 -0
  197. data/proto/google/protobuf/descriptor.proto +911 -0
  198. data/proto/rpc.proto +71 -0
  199. data/resources/assess/policy.json +6 -23
  200. data/ruby-agent.gemspec +4 -2
  201. metadata +122 -33
  202. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions_report.rb +0 -30
  203. data/lib/contrast/api/decorators/application_update.rb +0 -44
  204. data/lib/contrast/api/decorators/library.rb +0 -56
  205. data/lib/contrast/config/agent_configuration.rb +0 -63
  206. data/lib/contrast/config/heap_dump_configuration.rb +0 -59
  207. data/lib/contrast/config/inventory_configuration.rb +0 -33
  208. data/lib/contrast/config/logger_configuration.rb +0 -26
  209. data/lib/contrast/framework/platform_version.rb +0 -22
@@ -6,6 +6,7 @@ require 'contrast/components/agent'
6
6
  require 'contrast/components/logger'
7
7
  require 'contrast/components/scope'
8
8
  require 'contrast/utils/thread_tracker'
9
+ require 'contrast/utils/assess/event_limit_utils'
9
10
  require 'contrast/utils/assess/split_utils'
10
11
  require 'contrast/agent/assess/events/event_data'
11
12
 
@@ -20,6 +21,7 @@ module Contrast
20
21
  extend Contrast::Components::Scope::InstanceMethods
21
22
  extend Contrast::Components::Logger::InstanceMethods
22
23
  extend Contrast::Utils::Assess::SplitUtils
24
+ extend Contrast::Utils::Assess::EventLimitUtils
23
25
 
24
26
  SPLIT_TRACKER = Contrast::Utils::ThreadTracker.new
25
27
 
@@ -42,6 +44,7 @@ module Contrast
42
44
  return unless (source_properties = Contrast::Agent::Assess::Tracker.properties(source))
43
45
 
44
46
  update_element_properties(propagation_node, target, preshift, source_properties)
47
+ increment_event_count(propagation_node)
45
48
  nil
46
49
  end
47
50
 
@@ -7,6 +7,7 @@ require 'contrast/components/logger'
7
7
  require 'contrast/utils/object_share'
8
8
  require 'contrast/utils/sha256_builder'
9
9
  require 'contrast/utils/assess/source_method_utils'
10
+ require 'contrast/utils/assess/event_limit_utils'
10
11
  require 'contrast/agent/assess/events/event_data'
11
12
 
12
13
  module Contrast
@@ -19,6 +20,7 @@ module Contrast
19
20
  module SourceMethod
20
21
  extend Contrast::Components::Logger::InstanceMethods
21
22
  extend Contrast::Utils::Assess::SourceMethodUtils
23
+ extend Contrast::Utils::Assess::EventLimitUtils
22
24
 
23
25
  PARAMETER_TYPE = 'PARAMETER'
24
26
  PARAMETER_KEY_TYPE = 'PARAMETER_KEY'
@@ -35,30 +37,18 @@ module Contrast
35
37
  # @param object [Object] the Object on which the method was invoked
36
38
  # @param ret [Object] the Return of the invoked method
37
39
  # @param args [Array<Object>] the Arguments with which the method was invoked
38
- # @return [Object, nil] the tracked Return or nil if no changes were made
39
40
  def apply_source method_policy, object, ret, args
40
41
  return unless analyze?(method_policy, object, ret, args)
42
+ return if event_limit?(method_policy)
41
43
  return unless (source_node = method_policy.source_node)
42
44
 
43
45
  # used to hold the object and ret
44
46
  source_data = Contrast::Agent::Assess::Events::EventData.new(nil, nil, object, ret, nil)
45
- return unless (target = determine_target(source_node, source_data, args))
46
-
47
- return_val = nil
48
- if target.cs__frozen? && !Contrast::Agent::Assess::Tracker.trackable?(target)
49
- return unless ::Contrast::ASSESS.track_frozen_sources?
50
- return unless source_node.targets[0] == Contrast::Utils::ObjectShare::RETURN_KEY
51
- return unless (dup = safe_dup(source_data.ret))
52
- return unless Contrast::Agent::Assess::Tracker.trackable?(dup)
53
47
 
54
- Contrast::Agent::Assess::Tracker.pre_freeze(dup)
55
- return_val = dup.cs__freeze
56
- target = dup
57
- end
48
+ return unless (target = determine_target(source_node, source_data, args))
49
+ return if target.cs__frozen? && !Contrast::Agent::Assess::Tracker.trackable?(target)
58
50
 
59
51
  process_source(source_node, target, source_data, source_node.type, nil, *args)
60
-
61
- return_val
62
52
  end
63
53
  Contrast::Components::Logger.add_trace_log_timing_for(SourceMethod, :apply_source)
64
54
 
@@ -79,6 +69,8 @@ module Contrast
79
69
  context = Contrast::Agent::REQUEST_TRACKER.current
80
70
  return unless context && source_node && target
81
71
 
72
+ increment_event_count(source_node)
73
+
82
74
  source_name ||= determine_source_name(source_node, source_data.object, source_data.ret, *args)
83
75
  # We know we only work on certain things.
84
76
  # Skip if this isn't one of them
@@ -108,42 +100,10 @@ module Contrast
108
100
  # map
109
101
  # @param args [Array<Object>] the Arguments with which the method was invoked
110
102
  def apply_hash_tags source_node, target, source_data, source_type, *args
111
- to_replace = []
112
103
  target.each_pair do |key, value|
113
- # We only do this for Strings b/c of the way Hash lookup works. To replace another object would break
114
- # hash lookup and, therefore, the application
115
- if replace_hash_key?(key, target)
116
- key = key.dup
117
- to_replace << key
118
- end
119
104
  process_source(source_node, key, source_data, key_type(source_type), key, *args)
120
105
  process_source(source_node, value, source_data, source_type, key, *args)
121
106
  end
122
- handle_hash_key(target, to_replace)
123
- end
124
-
125
- # Given an unfrozen hash, if the key is a String, we should replace it with one that we can finalize,
126
- # allowing us to track that key. This method handles checking if that replace can and should occur.
127
- #
128
- # @param key [Object] the key in the hash that may need replacing.
129
- # @param hash [Hash] the hash to which the key belongs.
130
- # @return [Boolean] whether replace the key in the hash or not.
131
- def replace_hash_key? key, hash
132
- ::Contrast::ASSESS.track_frozen_sources? &&
133
- !hash.cs__frozen? &&
134
- key.is_a?(String) &&
135
- !Contrast::Agent::Assess::Tracker.trackable?(key)
136
- end
137
-
138
- # Hash is designed to keep one instance of the string key in it. We need to remove the existing one and
139
- # replace it with our new tracked one.
140
- def handle_hash_key target, to_replace
141
- to_replace.each do |key|
142
- Contrast::Agent::Assess::Tracker.pre_freeze(key)
143
- key.cs__freeze
144
- value = target.delete(key)
145
- target[key] = value
146
- end
147
107
  end
148
108
 
149
109
  # @param source_node [Contrast::Agent::Assess::Policy::SourceNode] the node to direct applying this source
@@ -35,6 +35,7 @@ module Contrast
35
35
 
36
36
  # Standard validation + TS trace version two rules:
37
37
  # Must have source and type
38
+ # @raise[ArgumentError] raises if any of the required fields is missing or invalid
38
39
  def validate
39
40
  super
40
41
  raise(ArgumentError, "Source #{ id } did not have a proper target. Unable to create.") unless targets&.any?
@@ -25,6 +25,7 @@ module Contrast
25
25
  module TriggerMethod # rubocop:disable Metrics/ModuleLength
26
26
  extend Contrast::Components::Logger::InstanceMethods
27
27
  extend Contrast::Utils::Assess::TriggerMethodUtils
28
+ extend Contrast::Utils::Assess::EventLimitUtils
28
29
 
29
30
  # The level of TeamServer compliance our traces meet when in the abnormal condition of being dataflow rules
30
31
  # without routes.
@@ -48,6 +49,7 @@ module Contrast
48
49
  # @param args [Array<Object>] the Arguments with which the method was invoked
49
50
  def apply_trigger_rule trigger_node, object, ret, args
50
51
  return if trigger_node.nil?
52
+ return if event_limit_for_rule?(trigger_node.rule_id)
51
53
 
52
54
  context = Contrast::Agent::REQUEST_TRACKER.current
53
55
  # return if there is no context and the flag is set to default => false
@@ -102,6 +104,12 @@ module Contrast
102
104
  request = find_request(source)
103
105
  return unless reportable?(request&.env)
104
106
 
107
+ process_reportable_finding(trigger_node, source, object, ret, request, *args)
108
+ rescue StandardError => e
109
+ logger.error('Unable to build a finding', e, rule: trigger_node.rule_id, node_id: trigger_node.id)
110
+ end
111
+
112
+ def process_reportable_finding trigger_node, source, object, ret, request, *args
105
113
  if Contrast::Agent::Reporter.enabled?
106
114
  handle_new_finding(trigger_node, source, object, ret, request, *args)
107
115
  else # TODO: RUBY-1438 -- remove
@@ -112,8 +120,6 @@ module Contrast
112
120
  rule: trigger_node.rule_id)
113
121
  report_finding(finding, request)
114
122
  end
115
- rescue StandardError => e
116
- logger.error('Unable to build a finding', e, rule: trigger_node.rule_id, node_id: trigger_node.id)
117
123
  end
118
124
 
119
125
  # Given a finding, append it to an activity message and send it to the Service for processing. If an
@@ -211,7 +217,7 @@ module Contrast
211
217
 
212
218
  build_events(finding, properties.event) if properties.event
213
219
 
214
- # Google::Protobuf::Map doesn't support merge!, so we have to do this long form
220
+ # CSGoogle::Protobuf::Map doesn't support merge!, so we have to do this long form
215
221
  source_props = properties.properties
216
222
  return unless source_props
217
223
 
@@ -4,6 +4,7 @@
4
4
  require 'contrast/agent/assess/policy/trigger/reflected_xss'
5
5
  require 'contrast/agent/assess/policy/trigger/xpath'
6
6
  require 'contrast/api/decorators/trace_taint_range_tags'
7
+ require 'contrast/components/logger'
7
8
 
8
9
  module Contrast
9
10
  module Agent
@@ -15,6 +16,8 @@ module Contrast
15
16
  # vulnerability (indicate points in the application where uncontrolled
16
17
  # user input can do damage).
17
18
  class TriggerNode < PolicyNode # rubocop:disable Metrics/ClassLength
19
+ include Contrast::Components::Logger::InstanceMethods
20
+
18
21
  JSON_BAD_VALUE = 'bad_value'
19
22
  JSON_GOOD_VALUE = 'good_value'
20
23
  JSON_DISALLOWED_TAGS = 'disallowed_tags'
@@ -59,6 +62,9 @@ module Contrast
59
62
  @trigger_method = trigger_hash['trigger_method']
60
63
  @trigger_method = @trigger_method.to_sym if @trigger_method
61
64
  validate
65
+ rescue ArgumentError => e
66
+ logger.error('Trigger Node initialization failed with: ', e)
67
+ nil
62
68
  end
63
69
 
64
70
  def node_class
@@ -143,6 +149,7 @@ module Contrast
143
149
 
144
150
  # Standard validation + TS trace version two rules:
145
151
  # Must have source
152
+ # @raise[ArgumentError] raises if any of the required fields is invalid or missing
146
153
  def validate
147
154
  super
148
155
  # If this isn't a dataflow rule, it can't have a source
@@ -172,6 +179,7 @@ module Contrast
172
179
  @disallowed_tags << (VALIDATOR_START + loud_name)
173
180
  end
174
181
 
182
+ # @raise[ArgumentError] raises if any of the tags is invalid
175
183
  def validate_rule_tags tags
176
184
  return unless tags
177
185
 
@@ -42,27 +42,13 @@ module Contrast
42
42
  return unless event.source_type
43
43
  return unless (current_request = Contrast::Agent::REQUEST_TRACKER.current)
44
44
 
45
- append_to_dtm(current_request, event)
46
45
  append_to_ruby_object(current_request, event)
47
46
  end
48
47
 
49
- def append_to_dtm current_request, event
50
- if current_request.observed_route.sources.any? do |source|
51
- source.type == event.forced_source_type &&
52
- source.name == event.forced_source_name # rubocop:disable Security/Module/Name
53
- end
54
-
55
- return
56
- end
57
-
58
- event_source_dtm = event.build_event_source_dtm
59
- return unless event_source_dtm
60
-
61
- current_request.observed_route.sources << event_source_dtm
62
- end
63
-
48
+ # @param current_request [Contrast::Agent::RequestContext]
49
+ # @param event [Contrast::Agent::Assess::Events::ContrastEvent]
64
50
  def append_to_ruby_object current_request, event
65
- if current_request.new_observed_route.sources.any? do |source|
51
+ if current_request.observed_route.sources.any? do |source|
66
52
  source.type == event.forced_source_type &&
67
53
  source.name == event.forced_source_name # rubocop:disable Security/Module/Name
68
54
  end
@@ -73,7 +59,7 @@ module Contrast
73
59
  event_source = event.build_event_source
74
60
  return unless event_source
75
61
 
76
- current_request.new_observed_route.sources << event_source
62
+ current_request.observed_route.sources << event_source
77
63
  end
78
64
  end
79
65
  end
@@ -1,12 +1,16 @@
1
1
  # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'contrast/components/logger'
5
+
4
6
  module Contrast
5
7
  module Agent
6
8
  module Assess
7
9
  # A Tag represents a range in a given piece of data. It is used by the
8
10
  # Agent to determine if a vulnerable dataflow has occurred.
9
11
  class Tag
12
+ include Contrast::Components::Logger::InstanceMethods
13
+
10
14
  attr_reader :label, # the label of this tag
11
15
  :length, # length of tagged text within string
12
16
  :start_idx, # start of range
@@ -35,6 +39,8 @@ module Contrast
35
39
  def initialize label, length, start_idx = 0
36
40
  @label = label
37
41
  update_range(start_idx, start_idx + length)
42
+ rescue ArgumentError => e
43
+ logger.error('Range update for Tag failed with: ', e)
38
44
  end
39
45
 
40
46
  # Return true if the tag covers the given position in the string
@@ -74,22 +80,32 @@ module Contrast
74
80
 
75
81
  def shift idx
76
82
  update_range(@start_idx + idx, @end_idx + idx)
83
+ rescue ArgumentError => e
84
+ logger.error('Range update for Tag failed with: ', e)
77
85
  end
78
86
 
79
87
  def shift_end idx
80
88
  update_range(@start_idx, @end_idx + idx)
89
+ rescue ArgumentError => e
90
+ logger.error('Range update for Tag failed with: ', e)
81
91
  end
82
92
 
83
93
  def update_start start_idx
84
94
  update_range(start_idx, @end_idx)
95
+ rescue ArgumentError => e
96
+ logger.error('Range update for Tag failed with: ', e)
85
97
  end
86
98
 
87
99
  def update_end end_idx
88
100
  update_range(@start_idx, end_idx)
101
+ rescue ArgumentError => e
102
+ logger.error('Range update for Tag failed with: ', e)
89
103
  end
90
104
 
91
105
  def repurpose start_idx, end_idx
92
106
  update_range(start_idx, end_idx)
107
+ rescue ArgumentError => e
108
+ logger.error('Range update for Tag failed with: ', e)
93
109
  end
94
110
 
95
111
  # Given a tag, merge its ranges with this one
@@ -104,6 +120,8 @@ module Contrast
104
120
  start = other.start_idx < @start_idx ? other.start_idx : @start_idx
105
121
  finish = other.end_idx > @end_idx ? other.end_idx : @end_idx
106
122
  update_range(start, finish)
123
+ rescue ArgumentError => e
124
+ logger.error('Range update for Tag failed with: ', e)
107
125
  end
108
126
 
109
127
  # Modification to tracked String can change the position and length of the tracked tag
@@ -160,6 +178,7 @@ module Contrast
160
178
 
161
179
  private
162
180
 
181
+ # @raise[ArgumentError] raises if start_idx is negative or the end_idx is smaller than the start_idx
163
182
  def update_range start_idx, end_idx
164
183
  raise(ArgumentError, ERROR_NEGATIVE_START) if start_idx.negative?
165
184
  raise(ArgumentError, ERROR_END_BEFORE_START) if end_idx < start_idx
@@ -12,6 +12,7 @@ module Contrast
12
12
  # have tightly coupled dependencies on each other.
13
13
  class Tracker
14
14
  PROPERTIES_HASH = Contrast::Agent::Assess::Finalizers::Hash.new
15
+ KEEP_AGE = 600_000.cs__freeze # 10 minutes
15
16
 
16
17
  class << self
17
18
  # Retrieve the properties of the given Object, iff they exist.
@@ -55,6 +56,17 @@ module Contrast
55
56
  def copy source, target
56
57
  PROPERTIES_HASH[target] ||= properties(source).dup
57
58
  end
59
+
60
+ # Clean PROPERTIES_HASH of any values older than KEEP_AGE ms or
61
+ # have nil properties
62
+ def cleanup!
63
+ PROPERTIES_HASH.delete_if do |_k, properties|
64
+ return true if properties.nil?
65
+ return false unless (event = properties&.event)
66
+
67
+ KEEP_AGE <= (Contrast::Utils::Timer.now_ms - event.time)
68
+ end
69
+ end
58
70
  end
59
71
  end
60
72
  end
@@ -31,16 +31,16 @@ module Contrast
31
31
  context = Contrast::Agent::REQUEST_TRACKER.current
32
32
  return unless context
33
33
 
34
- Contrast::Agent.reporter.send_event_immediately(context.observed_library_usage)
34
+ [
35
+ Contrast::Agent::Inventory::DependencyUsageAnalysis.instance.generate_library_usage,
36
+ context.observed_route
37
+ ].compact.each do |event|
38
+ Contrast::Agent.reporter&.send_event_immediately(event)
39
+ end
35
40
 
36
41
  if Contrast::Agent::Reporter.enabled?
37
- [
38
- context.new_observed_route,
39
- Contrast::Agent::Reporting::DtmMessage.dtm_to_event(context.server_activity),
40
- Contrast::Agent::Reporting::DtmMessage.dtm_to_event(context.activity)
41
- ].each do |event|
42
- Contrast::Agent.reporter&.send_event_immediately(event)
43
- end
42
+ event = Contrast::Agent::Reporting::DtmMessage.dtm_to_event(context.activity)
43
+ Contrast::Agent.reporter&.send_event_immediately(event)
44
44
  else
45
45
  Contrast::Agent.messaging_queue&.send_event_immediately(context.activity)
46
46
  end
@@ -1,6 +1,7 @@
1
1
  # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'contrast/agent/reporting/reporting_events/architecture_component'
4
5
  require 'contrast/api/decorators/architecture_component'
5
6
  require 'contrast/components/logger'
6
7
  require 'contrast/utils/object_share'
@@ -25,10 +26,11 @@ module Contrast
25
26
 
26
27
  class << self
27
28
  # Append the available database connection information to the message being sent to TeamServer. This message
28
- # may be a Contrast::Api::Dtm::Activity or Contrast::Api::Dtm::ApplicationUpdate. Both report the same
29
+ # may be a Contrast::Api::Dtm::Activity or Contrast::Agent::Reporting::ApplicationUpdate.
30
+ # Both report the same
29
31
  # Contrast::Api::Dtm::ArchitectureComponent, but have different names for their fields.
30
32
  #
31
- # @param activity_or_update [Contrast::Api::Dtm::Activity, Contrast::Api::Dtm::ApplicationUpdate]
33
+ # @param activity_or_update [Contrast::Api::Dtm::Activity, Contrast::Agent::Reporting::ApplicationUpdate]
32
34
  # @param hash_or_str [Hash, String] the database connection information
33
35
  def append_db_config activity_or_update, hash_or_str = active_record_config
34
36
  arr = build_from_db_config(hash_or_str)
@@ -40,7 +42,8 @@ module Contrast
40
42
  if activity_or_update.is_a?(Contrast::Api::Dtm::Activity)
41
43
  activity_or_update.architectures << a
42
44
  else
43
- activity_or_update.components << a
45
+ converted_comp = Contrast::Agent::Reporting::ArchitectureComponent.convert(a)
46
+ activity_or_update.components << converted_comp
44
47
  end
45
48
  end
46
49
  rescue StandardError => e
@@ -1,6 +1,7 @@
1
1
  # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'contrast/agent/reporting/reporting_events/library_discovery'
4
5
  require 'contrast/agent/inventory/dependencies'
5
6
  require 'contrast/utils/object_share'
6
7
 
@@ -14,17 +15,17 @@ module Contrast
14
15
 
15
16
  # Report the dependencies of this application
16
17
  #
17
- # @return [Array<Contrast::Api::Dtm::Library>] protobuf form of the
18
+ # @return [Array<Contrast::Agent::Reporting::LibraryDiscovery>] direct report form of
18
19
  # Gem::Specification that have been loaded for this application.
19
20
  def library_pb_list
20
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless ::Contrast::INVENTORY.enabled?
21
- return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless ::Contrast::INVENTORY.analyze_libraries?
21
+ return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless ::Contrast::INVENTORY.enable
22
+ return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless ::Contrast::INVENTORY.analyze_libraries
22
23
 
23
24
  loaded_specs.each_with_object([]) do |(_name, spec), reported_lib_list|
24
25
  next unless spec
25
26
  next unless (digest = Contrast::Utils::Sha256Builder.instance.build_from_spec(spec))
26
27
 
27
- reported_lib_list << Contrast::Api::Dtm::Library.build(digest, spec)
28
+ reported_lib_list << Contrast::Agent::Reporting::LibraryDiscovery.new(digest, spec)
28
29
  end
29
30
  end
30
31
  end
@@ -52,7 +52,6 @@ module Contrast
52
52
  return unless enabled?
53
53
 
54
54
  spec_lookup_path = adjust_path_for_spec_lookup(path)
55
-
56
55
  spec = Gem::Specification.find_by_path(spec_lookup_path)
57
56
  unless spec
58
57
  logger.debug('Unable to resolve gem spec for path', path: path)
@@ -70,15 +69,13 @@ module Contrast
70
69
  logger.error('Unable to inventory file path', e, path: path)
71
70
  end
72
71
 
73
- # Populate the library_usages field of the Activity message using the data stored in the @gemdigest_cache.
72
+ # Populate the library_usages field of the Activity message using the data stored in the @gemdigest_cache. If
73
+ # no libraries had files loaded, or inventory analysis is disabled, return nil instead.
74
74
  #
75
- # TODO: RUBY-1355
76
- # TODO: RUBY-1438 -- change to just use EventMessage
77
- # @param observed_library_usage [Contrast::Agent::Reporting::ObservedLibraryUsage] the message to
78
- # which to append the usage data
79
- def generate_library_usage observed_library_usage
75
+ # @return [Contrast::Agent::Reporting::ObservedLibraryUsage, nil]
76
+ def generate_library_usage
80
77
  return unless enabled?
81
- return unless observed_library_usage
78
+ return unless @gemdigest_cache.any?
82
79
 
83
80
  # Disconnect gemdigest_cache and replace it with an empty one; synch so new libs cannot be added between the
84
81
  # assignment and the replace
@@ -87,12 +84,15 @@ module Contrast
87
84
  @gemdigest_cache = Hash.new { |hash, key| hash[key] = Set.new }
88
85
  hold
89
86
  end
87
+
88
+ observed_library_usage = Contrast::Agent::Reporting::ObservedLibraryUsage.new
90
89
  gem_spec_digest_to_files.each_pair do |digest, files|
91
- usage = Contrast::Agent::Reporting::LibraryUsageObservation.new(digest, files)
92
- next if usage.names.empty?
90
+ next unless files.any?
93
91
 
92
+ usage = Contrast::Agent::Reporting::LibraryUsageObservation.new(digest, files)
94
93
  observed_library_usage.observations << usage
95
94
  end
95
+ observed_library_usage.observations.any? ? observed_library_usage : nil
96
96
  rescue StandardError => e
97
97
  logger.error('Unable to generate library usage.', e)
98
98
  end
@@ -111,7 +111,7 @@ module Contrast
111
111
 
112
112
  # We only use this if inventory and library analysis are enabled
113
113
  def enabled?
114
- @_enabled = ::Contrast::INVENTORY.enabled? && ::Contrast::INVENTORY.analyze_libraries? if @_enabled.nil?
114
+ @_enabled = ::Contrast::INVENTORY.enable && ::Contrast::INVENTORY.analyze_libraries if @_enabled.nil?
115
115
  @_enabled
116
116
  end
117
117
  end
@@ -21,7 +21,7 @@ module Contrast
21
21
  DATA_STORE_MARKER = 'data_store'
22
22
 
23
23
  def report_data_store _method, _exception, properties, object, _args
24
- return unless ::Contrast::INVENTORY.enabled?
24
+ return unless ::Contrast::INVENTORY.enabled
25
25
 
26
26
  marker = properties[DATA_STORE_MARKER]
27
27
  return unless marker
@@ -19,7 +19,7 @@ module Contrast
19
19
  end
20
20
 
21
21
  def disabled_globally?
22
- !::Contrast::INVENTORY.enabled?
22
+ !::Contrast::INVENTORY.enable
23
23
  end
24
24
 
25
25
  def node_type
@@ -142,6 +142,8 @@ module Contrast
142
142
  #
143
143
  # @param context [Contrast::Agent::RequestContext]
144
144
  # @param request_handler [Contrast::Agent::RequestHandler]
145
+ # @raise [StandardError] raises an error if the exception is security concern
146
+ # which is being triggered when there is a failure within the pre-call with the agent
145
147
  def pre_call_with_agent context, request_handler
146
148
  with_contrast_scope do
147
149
  context.service_extract_request
@@ -160,6 +162,8 @@ module Contrast
160
162
  # this Request
161
163
  # @param request_handler [Contrast::Agent::RequestHandler]
162
164
  # @param response [Array,Rack::Response]
165
+ # @raise [StandardError] raises an error if the exception is security concern
166
+ # which is being triggered when there is a failure within the post-call with the agent
163
167
  def post_call_with_agent context, env, request_handler, response
164
168
  with_contrast_scope do
165
169
  context.extract_after(response) # update context with final response information
@@ -5,6 +5,8 @@ require 'contrast/agent/patching/policy/after_load_patch'
5
5
  require 'contrast/components/logger'
6
6
  require 'contrast/framework/manager'
7
7
  require 'contrast/extension/extension'
8
+ require 'contrast/extension/assess/kernel'
9
+ require 'contrast/extension/assess/marshal'
8
10
 
9
11
  module Contrast
10
12
  module Agent
@@ -29,22 +31,31 @@ module Contrast
29
31
  # there are no require time side effects of loading our core
30
32
  # extensions.
31
33
  def apply_direct_patches!
32
- @_apply_direct_patches ||= begin # TODO: RUBY-1541 - put 'kernel' back
34
+ @_apply_direct_patches ||= begin
33
35
  paths = %w[
34
36
  array
35
37
  basic_object
36
38
  module
37
39
  fiber_track
38
40
  hash
41
+ kernel
39
42
  marshal_module
40
43
  regexp
41
44
  string
42
- string_interpolation26
45
+ string_interpolation
43
46
  ].cs__freeze
44
47
  paths.each do |p|
45
48
  path_part = "cs__assess_#{ p }"
46
49
  Contrast::Extension::Assess::InstrumentHelper.instrument("#{ path_part }/#{ path_part }")
47
50
  end
51
+ # apply Kernel#exec alias patch:
52
+ unless Contrast::Agent::Assess.cs__object_method_prepended?(Kernel, :exec, false)
53
+ apply_kernel_exec_alias_patch
54
+ end
55
+ # apply Marshal load alias patch:
56
+ unless Contrast::Agent::Assess.cs__object_method_prepended?(Marshal, :load, false)
57
+ apply_marshal_load_alias_patch
58
+ end
48
59
  true
49
60
  end
50
61
  end
@@ -96,6 +107,20 @@ module Contrast
96
107
  patch.instrument!
97
108
  after_load_patches.delete_if(&:applied?)
98
109
  end
110
+
111
+ # Applies the Kernel#exec alias patch
112
+ def apply_kernel_exec_alias_patch
113
+ Kernel.extend(Contrast::Extension::Assess::ContrastKernel)
114
+ Marshal.alias_method(:cs__kernel_exec, :exec)
115
+ Marshal.alias_method(:exec, :cs__kernel_exec)
116
+ end
117
+
118
+ # Applies the Marshal#load alias patch
119
+ def apply_marshal_load_alias_patch
120
+ Marshal.extend(Contrast::Extension::Assess::ContrastMarshal)
121
+ Marshal.alias_method(:cs__marshal_load, :load)
122
+ Marshal.alias_method(:load, :cs__marshal_load)
123
+ end
99
124
  end
100
125
  end
101
126
  end
@@ -17,9 +17,9 @@ module Contrast
17
17
  #
18
18
  # @param method_policy [Hash]
19
19
  # {
20
- # source_node [ Contrast::Agent::Assesss::Policy::SourceNode ]
21
- # propagation_node [ Contrast::Agent::Assesss::Policy::PropagationNode ]
22
- # trigger_node [ Contrast::Agent::Assesss::Policy::TriggerNode ]
20
+ # source_node [ Contrast::Agent::Assess::Policy::SourceNode ]
21
+ # propagation_node [ Contrast::Agent::Assess::Policy::PropagationNode ]
22
+ # trigger_node [ Contrast::Agent::Assess::Policy::TriggerNode ]
23
23
  # inventory_node [ Contrast::Agent::Inventory::Policy::TriggerNode ]
24
24
  # protect_node [ Contrast::Agent::Protect::Policy::TriggerNode ]
25
25
  # deadzone_node [ Contrast::Agent::Deadzone::Policy::DeadzoneNode ]
@@ -35,16 +35,21 @@ module Contrast
35
35
  end
36
36
 
37
37
  # Indicates the folder in `resources` where this policy lives.
38
+ #
39
+ # @raise[NoMethodError] raises if any of the subclasses, extending this class is not implementing this method
38
40
  def self.policy_folder
39
41
  raise(NoMethodError, 'specify policy_folder for patching')
40
42
  end
41
43
 
42
44
  # Indicates is this feature has been disabled by the configuration, read at startup, and therefore can never
43
45
  # be enabled.
46
+ #
47
+ # @raise[NoMethodError] raises if any of the subclasses, extending this class is not implementing this method
44
48
  def disabled_globally?
45
49
  raise(NoMethodError, 'specify disabled_globally? conditions for patching')
46
50
  end
47
51
 
52
+ # @raise[NoMethodError] raises if any of the subclasses, extending this class is not implementing this method
48
53
  def node_type
49
54
  raise(NoMethodError, 'specify the concrete node type for this poilcy')
50
55
  end