contrast-agent 4.12.0 → 4.14.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) 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 +5 -0
  6. data/ext/cs__common/cs__common.h +8 -0
  7. data/ext/cs__contrast_patch/cs__contrast_patch.c +16 -1
  8. data/ext/cs__os_information/cs__os_information.c +31 -0
  9. data/ext/cs__os_information/cs__os_information.h +7 -0
  10. data/ext/cs__os_information/extconf.rb +5 -0
  11. data/lib/contrast/agent/assess/policy/policy_node.rb +6 -6
  12. data/lib/contrast/agent/assess/policy/policy_scanner.rb +5 -0
  13. data/lib/contrast/agent/assess/policy/propagation_method.rb +2 -116
  14. data/lib/contrast/agent/assess/policy/propagation_node.rb +4 -4
  15. data/lib/contrast/agent/assess/policy/propagator/center.rb +1 -1
  16. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +2 -154
  17. data/lib/contrast/agent/assess/policy/source_method.rb +2 -71
  18. data/lib/contrast/agent/assess/policy/trigger_method.rb +45 -110
  19. data/lib/contrast/agent/assess/policy/trigger_node.rb +14 -6
  20. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +1 -1
  21. data/lib/contrast/agent/assess/property/tagged.rb +53 -185
  22. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +40 -6
  23. data/lib/contrast/agent/deadzone/policy/policy.rb +1 -1
  24. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +1 -0
  25. data/lib/contrast/agent/metric_telemetry_event.rb +26 -0
  26. data/lib/contrast/agent/middleware.rb +14 -62
  27. data/lib/contrast/agent/patching/policy/method_policy.rb +3 -89
  28. data/lib/contrast/agent/patching/policy/method_policy_extend.rb +111 -0
  29. data/lib/contrast/agent/patching/policy/patch.rb +28 -235
  30. data/lib/contrast/agent/patching/policy/patcher.rb +14 -49
  31. data/lib/contrast/agent/reporting/report.rb +21 -0
  32. data/lib/contrast/agent/reporting/reporter.rb +142 -0
  33. data/lib/contrast/agent/reporting/reporting_events/finding.rb +90 -0
  34. data/lib/contrast/agent/reporting/reporting_events/preflight.rb +25 -0
  35. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +56 -0
  36. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +37 -0
  37. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +127 -0
  38. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +168 -0
  39. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +66 -0
  40. data/lib/contrast/agent/request.rb +2 -81
  41. data/lib/contrast/agent/request_context.rb +4 -128
  42. data/lib/contrast/agent/request_context_extend.rb +138 -0
  43. data/lib/contrast/agent/request_handler.rb +7 -3
  44. data/lib/contrast/agent/response.rb +2 -73
  45. data/lib/contrast/agent/startup_metrics_telemetry_event.rb +94 -0
  46. data/lib/contrast/agent/static_analysis.rb +5 -3
  47. data/lib/contrast/agent/telemetry.rb +137 -0
  48. data/lib/contrast/agent/telemetry_event.rb +33 -0
  49. data/lib/contrast/agent/thread_watcher.rb +66 -11
  50. data/lib/contrast/agent/version.rb +1 -1
  51. data/lib/contrast/agent.rb +21 -0
  52. data/lib/contrast/api/communication/connection_status.rb +10 -7
  53. data/lib/contrast/api/communication/messaging_queue.rb +37 -3
  54. data/lib/contrast/api/communication/response_processor.rb +15 -8
  55. data/lib/contrast/api/communication/service_lifecycle.rb +13 -3
  56. data/lib/contrast/api/communication/socket.rb +6 -8
  57. data/lib/contrast/api/communication/socket_client.rb +29 -12
  58. data/lib/contrast/api/communication/speedracer.rb +37 -1
  59. data/lib/contrast/api/communication/tcp_socket.rb +4 -3
  60. data/lib/contrast/api/communication/unix_socket.rb +1 -0
  61. data/lib/contrast/api/decorators/finding.rb +45 -0
  62. data/lib/contrast/components/api.rb +90 -0
  63. data/lib/contrast/components/app_context.rb +10 -41
  64. data/lib/contrast/components/app_context_extend.rb +78 -0
  65. data/lib/contrast/components/base.rb +23 -0
  66. data/lib/contrast/components/config.rb +92 -13
  67. data/lib/contrast/components/contrast_service.rb +11 -0
  68. data/lib/contrast/components/sampling.rb +2 -2
  69. data/lib/contrast/config/agent_configuration.rb +1 -1
  70. data/lib/contrast/config/api_configuration.rb +27 -0
  71. data/lib/contrast/config/api_proxy_configuration.rb +14 -0
  72. data/lib/contrast/config/application_configuration.rb +2 -3
  73. data/lib/contrast/config/assess_configuration.rb +3 -3
  74. data/lib/contrast/config/base_configuration.rb +17 -28
  75. data/lib/contrast/config/certification_configuration.rb +15 -0
  76. data/lib/contrast/config/env_variables.rb +18 -0
  77. data/lib/contrast/config/heap_dump_configuration.rb +6 -6
  78. data/lib/contrast/config/inventory_configuration.rb +1 -5
  79. data/lib/contrast/config/protect_rule_configuration.rb +1 -1
  80. data/lib/contrast/config/request_audit_configuration.rb +18 -0
  81. data/lib/contrast/config/root_configuration.rb +1 -0
  82. data/lib/contrast/config/ruby_configuration.rb +6 -6
  83. data/lib/contrast/config/service_configuration.rb +2 -2
  84. data/lib/contrast/config.rb +1 -1
  85. data/lib/contrast/configuration.rb +4 -2
  86. data/lib/contrast/extension/assess/array.rb +5 -7
  87. data/lib/contrast/extension/thread.rb +31 -12
  88. data/lib/contrast/framework/manager.rb +22 -44
  89. data/lib/contrast/framework/manager_extend.rb +50 -0
  90. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +9 -6
  91. data/lib/contrast/framework/rails/patch/support.rb +31 -29
  92. data/lib/contrast/framework/rails/railtie.rb +1 -1
  93. data/lib/contrast/framework/sinatra/support.rb +2 -1
  94. data/lib/contrast/logger/application.rb +4 -0
  95. data/lib/contrast/logger/log.rb +8 -103
  96. data/lib/contrast/utils/assess/propagation_method_utils.rb +129 -0
  97. data/lib/contrast/utils/assess/property/tagged_utils.rb +165 -0
  98. data/lib/contrast/utils/assess/source_method_utils.rb +83 -0
  99. data/lib/contrast/utils/assess/tracking_util.rb +20 -15
  100. data/lib/contrast/utils/assess/trigger_method_utils.rb +138 -0
  101. data/lib/contrast/utils/class_util.rb +18 -14
  102. data/lib/contrast/utils/exclude_key.rb +20 -0
  103. data/lib/contrast/utils/findings.rb +62 -0
  104. data/lib/contrast/utils/hash_digest.rb +10 -73
  105. data/lib/contrast/utils/hash_digest_extend.rb +86 -0
  106. data/lib/contrast/utils/head_dump_utils_extend.rb +74 -0
  107. data/lib/contrast/utils/heap_dump_util.rb +2 -65
  108. data/lib/contrast/utils/invalid_configuration_util.rb +29 -0
  109. data/lib/contrast/utils/io_util.rb +1 -1
  110. data/lib/contrast/utils/log_utils.rb +108 -0
  111. data/lib/contrast/utils/metrics_hash.rb +59 -0
  112. data/lib/contrast/utils/middleware_utils.rb +87 -0
  113. data/lib/contrast/utils/net_http_base.rb +158 -0
  114. data/lib/contrast/utils/object_share.rb +1 -0
  115. data/lib/contrast/utils/os.rb +23 -0
  116. data/lib/contrast/utils/patching/policy/patch_utils.rb +232 -0
  117. data/lib/contrast/utils/patching/policy/patcher_utils.rb +54 -0
  118. data/lib/contrast/utils/request_utils.rb +88 -0
  119. data/lib/contrast/utils/response_utils.rb +97 -0
  120. data/lib/contrast/utils/substitution_utils.rb +167 -0
  121. data/lib/contrast/utils/tag_util.rb +9 -9
  122. data/lib/contrast/utils/telemetry.rb +79 -0
  123. data/lib/contrast/utils/telemetry_client.rb +90 -0
  124. data/lib/contrast/utils/telemetry_identifier.rb +130 -0
  125. data/lib/contrast.rb +18 -0
  126. data/ruby-agent.gemspec +7 -6
  127. data/service_executables/VERSION +1 -1
  128. data/service_executables/linux/contrast-service +0 -0
  129. data/service_executables/mac/contrast-service +0 -0
  130. metadata +69 -22
  131. data/lib/contrast/config/default_value.rb +0 -17
@@ -0,0 +1,138 @@
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 Utils
6
+ module Assess
7
+ # This module will include all methods for some internal validations/appliers in the TriggerMethod module
8
+ # and some other module methods from the same place, so we can ease the main module
9
+ module TriggerMethodUtils
10
+ # A request is reportable if it is not from ActionController::Live
11
+ #
12
+ # @param env [Hash] the env of the Request
13
+ # @return [Boolean]
14
+ def reportable? env
15
+ !(defined?(ActionController::Live) &&
16
+ env &&
17
+ env['action_controller.instance'].cs__class.included_modules.include?(ActionController::Live))
18
+ end
19
+
20
+ # Find the request for this finding. This assumes, for now, that if there is an active request, then that
21
+ # is the request to report. Otherwise, we'll use the first request found in the events of the
22
+ # source_object.
23
+ #
24
+ # @param source [Object,nil] some Object used as the source of a trigger event
25
+ # @return [Contrast::Agent::Request,nil] the request from which the dataflow on the request originated.
26
+ def find_request source
27
+ return Contrast::Agent::REQUEST_TRACKER.current.request if Contrast::Agent::REQUEST_TRACKER.current
28
+ return unless (properties = Contrast::Agent::Assess::Tracker.properties(source))
29
+
30
+ find_event_request(properties.event)
31
+ end
32
+
33
+ # Finds the first request along the left most tree of parent events
34
+ #
35
+ # @param event [Contrast::Agent::Assess::ContrastEvent|Contrast::Agent::Assess::Events::SourceEvent]
36
+ # @return [Contrast::Agent::Request, nil]
37
+ def find_event_request event
38
+ return event.request if event.cs__is_a?(Contrast::Agent::Assess::Events::SourceEvent)
39
+
40
+ idx = 0
41
+ while idx <= event.parent_events.length
42
+ found = find_event_request(event.parent_events[idx])
43
+ return found if found
44
+
45
+ idx += 1
46
+ return event.request if event.request
47
+ end
48
+ return unless event.cs__is_a?(Contrast::Agent::Assess::Events::SourceEvent)
49
+
50
+ event.request
51
+ end
52
+
53
+ # ===== APPLIERS =====
54
+ # This is our method that actually checks the taint on the object our trigger_node targets.
55
+ #
56
+ # @param trigger_node [Contrast::Agent::Assess::Policy::TriggerNode] the node to direct applying this
57
+ # trigger event
58
+ # @param source [Object] the source of the Trigger Event
59
+ # @param object [Object] the Object on which the method was invoked
60
+ # @param ret [Object] the Return of the invoked method
61
+ # @param args [Array<Object>] the Arguments with which the method was invoked
62
+ def apply_trigger trigger_node, source, object, ret, *args
63
+ return unless trigger_node
64
+ return if trigger_node.rule_disabled?
65
+ return if trigger_node.dataflow? && source.nil?
66
+
67
+ if trigger_node.regexp_rule?
68
+ apply_regexp_rule(trigger_node, source, object, ret, *args)
69
+ elsif trigger_node.custom_trigger?
70
+ trigger_node.apply_custom_trigger(trigger_node, source, object, ret, *args)
71
+ elsif trigger_node.dataflow?
72
+ apply_dataflow_rule(trigger_node, source, object, ret, *args)
73
+ else # trigger rule - just calling the method is dangerous
74
+ build_finding(trigger_node, source, object, ret, *args)
75
+ end
76
+ rescue StandardError => e
77
+ logger.warn('Unable to apply trigger', e, node_id: trigger_node.id)
78
+ end
79
+
80
+ # This is our method that actually checks the taint on the object our trigger_node targets for our Regexp
81
+ # based rules.
82
+ #
83
+ # @param trigger_node [Contrast::Agent::Assess::Policy::TriggerNode] the node to direct applying this
84
+ # trigger event
85
+ # @param source [Object] the source of the Trigger Event
86
+ # @param object [Object] the Object on which the method was invoked
87
+ # @param ret [Object] the Return of the invoked method
88
+ # @param args [Array<Object>] the Arguments with which the method was invoked
89
+ def apply_regexp_rule trigger_node, source, object, ret, *args
90
+ return unless source.is_a?(String)
91
+ return if trigger_node.good_value && source.match?(trigger_node.good_value)
92
+ return if trigger_node.bad_value && source !~ trigger_node.bad_value
93
+
94
+ build_finding(trigger_node, source, object, ret, *args)
95
+ end
96
+
97
+ # This is our method that actually checks the taint on the object our trigger_node targets for our Dataflow
98
+ # based rules.
99
+ #
100
+ # @param trigger_node [Contrast::Agent::Assess::Policy::TriggerNode] the node to direct applying this
101
+ # trigger event
102
+ # @param source [Object] the source of the Trigger Event
103
+ # @param object [Object] the Object on which the method was invoked
104
+ # @param ret [Object] the Return of the invoked method
105
+ # @param args [Array<Object>] the Arguments with which the method was invoked
106
+ def apply_dataflow_rule trigger_node, source, object, ret, *args # rubocop:disable Metrics/PerceivedComplexity
107
+ return unless source
108
+
109
+ if Contrast::Agent::Assess::Tracker.trackable?(source)
110
+ return unless Contrast::Agent::Assess::Tracker.tracked?(source)
111
+ return unless trigger_node.violated?(source)
112
+
113
+ build_finding(trigger_node, source, object, ret, *args)
114
+ elsif Contrast::Utils::DuckUtils.iterable_hash?(source)
115
+ return unless Contrast::Agent::Assess::Tracker.tracked?(source)
116
+
117
+ source.each_pair do |key, value|
118
+ apply_dataflow_rule(trigger_node, key, object, ret, *args)
119
+ apply_dataflow_rule(trigger_node, value, object, ret, *args)
120
+ end
121
+ elsif Contrast::Utils::DuckUtils.iterable_enumerable?(source)
122
+ return unless Contrast::Agent::Assess::Tracker.tracked?(source)
123
+
124
+ source.each do |value|
125
+ apply_dataflow_rule(trigger_node, value, object, ret, *args)
126
+ end
127
+ else
128
+ logger.debug('Trigger source is untrackable. Unable to inspect.',
129
+ node_id: trigger_node.id,
130
+ source_id: source.__id__,
131
+ source_type: source.cs__class.cs__name,
132
+ frozen: source.cs__frozen?)
133
+ end
134
+ end
135
+ end
136
+ end
137
+ end
138
+ end
@@ -54,7 +54,7 @@ module Contrast
54
54
  # Once we move to 2.7+, we can combine the caches using ID b/c the memory location stops being the id
55
55
  #
56
56
  # @param object [Object, nil] the entity to convert to a String
57
- # @return [String] the human readable form of the String, as defined by
57
+ # @return [String, Object] the human readable form of the String, as defined by
58
58
  # https://bitbucket.org/contrastsecurity/assess-specifications/src/master/vulnerability/capture-snapshot.md
59
59
  def to_contrast_string object
60
60
  # Only treat object like a string if it actually is a string+ some subclasses of String override string
@@ -66,19 +66,23 @@ module Contrast
66
66
  else
67
67
  return @lru_cache[object.__id__] if @lru_cache.key? object.__id__
68
68
 
69
- @lru_cache[object.__id__] = if object.nil?
70
- Contrast::Utils::ObjectShare::NIL_STRING
71
- elsif object.cs__is_a?(Symbol)
72
- ":#{ object }"
73
- elsif object.cs__is_a?(Module) || object.cs__is_a?(Class)
74
- "#{ object.cs__name }@#{ object.__id__ }"
75
- elsif object.cs__is_a?(Regexp)
76
- object.source
77
- elsif use_to_s?(object)
78
- object.to_s
79
- else
80
- "#{ object.cs__class.cs__name }@#{ object.__id__ }"
81
- end
69
+ @lru_cache[object.__id__] = convert_object object
70
+ end
71
+ end
72
+
73
+ def convert_object object
74
+ if object.nil?
75
+ Contrast::Utils::ObjectShare::NIL_STRING
76
+ elsif object.cs__is_a?(Symbol)
77
+ ":#{ object }"
78
+ elsif object.cs__is_a?(Module) || object.cs__is_a?(Class)
79
+ "#{ object.cs__name }@#{ object.__id__ }"
80
+ elsif object.cs__is_a?(Regexp)
81
+ object.source
82
+ elsif use_to_s?(object)
83
+ object.to_s
84
+ else
85
+ "#{ object.cs__class.cs__name }@#{ object.__id__ }"
82
86
  end
83
87
  end
84
88
 
@@ -0,0 +1,20 @@
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 Utils
6
+ # Determine if configuration keys is excluded from logging
7
+ module ExcludeKey
8
+ EXCLUDE_FROM_LOG = %w[api api_key url service_key user_name].cs__freeze
9
+ class << self
10
+ # Check if a config key can be logged or not
11
+ #
12
+ # @param key [String] key to check
13
+ # @return[Boolean] true | false
14
+ def excludable? key
15
+ EXCLUDE_FROM_LOG.any? { |exclude_key| key.include? exclude_key }
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,62 @@
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/logger'
5
+
6
+ module Contrast
7
+ module Utils
8
+ # Utility for saving raw findings for later
9
+ class Findings
10
+ include Contrast::Components::Logger::InstanceMethods
11
+
12
+ def initialize
13
+ @_collection = []
14
+ end
15
+
16
+ def collection
17
+ @_collection ||= []
18
+ end
19
+
20
+ def push trigger_node, source, object, ret, *args
21
+ return Contrast::Utils::ObjectShare::EMPTY_ARRAY unless trigger_node.collectable?
22
+
23
+ @_collection << { trigger_node: trigger_node, source: source, object: object, ret: ret, args: args }
24
+ end
25
+
26
+ # Some rules requires response to be available before validating them correctly,
27
+ # so we check if trigger_node.rule_id is collectable and then save them for
28
+ # later report, when we have the response.
29
+ #
30
+ # @param trigger_node [Contrast::Agent::Assess::Policy::TriggerNode] the node to direct applying this
31
+ # trigger event
32
+ # @param source [Object] the source of the Trigger Event
33
+ # @param object [Object] the Object on which the method was invoked
34
+ # @param ret [Object] the Return of the invoked method
35
+ # @param args [Array<Object>] the Arguments with which the method was invoked
36
+ def collect_finding trigger_node, source, object, ret, *args
37
+ push trigger_node, source, object, ret, args
38
+ logger.trace('Finding collected', node_id: trigger_node.id,
39
+ source_id: source.__id__,
40
+ rule: trigger_node.rule_id)
41
+ end
42
+
43
+ # Build and report all collected findings for the collectable rules.
44
+ #
45
+ # We make sure the content-type is present before reporting, because some
46
+ # findings do require it for validation.
47
+ def report_collected_findings
48
+ return if Contrast::Agent::REQUEST_TRACKER.current&.response&.content_type.nil?
49
+ return if @_collection.empty?
50
+
51
+ while @_collection.any?
52
+ finding = @_collection.pop
53
+ Contrast::Agent::Assess::Policy::TriggerMethod.build_finding finding[:trigger_node],
54
+ finding[:source],
55
+ finding[:object],
56
+ finding[:ret],
57
+ finding[:args]
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'digest'
5
+ require 'contrast/utils/hash_digest_extend'
5
6
 
6
7
  module Contrast
7
8
  module Utils
@@ -13,6 +14,7 @@ module Contrast
13
14
  # https://bitbucket.org/contrastsecurity/assess-specifications/src/master/vulnerability/preflight.md
14
15
  class HashDigest < Digest::Class
15
16
  include Digest::Instance
17
+ extend Contrast::Utils::HashDigestExtend
16
18
 
17
19
  CONTENT_LENGTH_HEADER = 'Content-Length'
18
20
  CRYPTO_RULES = %w[crypto-bad-ciphers crypto-bad-mac].cs__freeze
@@ -21,76 +23,6 @@ module Contrast
21
23
  CLASS_SOURCE_KEY = 'source'
22
24
  CLASS_CONSTANT_NAME_KEY = 'name'
23
25
  CLASS_LINE_NO_KEY = 'lineNo'
24
- class << self
25
- def generate_request_hash request
26
- hash = new
27
- hash.update(request.request_method)
28
- hash.update(request.normalized_uri)
29
- request.parameters.each_key do |name|
30
- hash.update(name)
31
- end
32
- cl = request.headers[CONTENT_LENGTH_HEADER]
33
- hash.update_on_content_length(cl) if cl
34
- hash.finish
35
- end
36
-
37
- def generate_event_hash finding, source, request
38
- return generate_dataflow_hash(finding, request) if finding.events.length.to_i > 1
39
-
40
- id = finding.rule_id
41
- return generate_crypto_hash(finding, source, request) if CRYPTO_RULES.include?(id)
42
-
43
- generate_trigger_hash(finding, request)
44
- end
45
-
46
- def generate_config_hash finding
47
- hash = new
48
- hash.update(finding.rule_id)
49
- path = finding.properties[CONFIG_PATH_KEY]
50
- hash.update(path)
51
- method = finding.properties[CONFIG_SESSION_ID_KEY]
52
- hash.update(method)
53
- hash.finish
54
- end
55
-
56
- def generate_class_scanning_hash finding
57
- hash = new
58
- hash.update(finding.rule_id)
59
- module_name = finding.properties[CLASS_SOURCE_KEY]
60
- hash.update(module_name)
61
- # We're not currently collecting this. 30/7/19 HM
62
- line_no = finding.properties[CLASS_LINE_NO_KEY]
63
- hash.update(line_no)
64
- field = finding.properties[CLASS_CONSTANT_NAME_KEY]
65
- hash.update(field)
66
- hash.finish
67
- end
68
-
69
- private
70
-
71
- def generate_crypto_hash finding, algorithm, request
72
- hash = new
73
- hash.update(finding.rule_id)
74
- hash.update(algorithm)
75
- hash.update_on_request(finding, request)
76
- hash.finish
77
- end
78
-
79
- def generate_dataflow_hash finding, request
80
- hash = new
81
- hash.update(finding.rule_id)
82
- hash.update_on_sources(finding.events)
83
- hash.update_on_request(finding, request)
84
- hash.finish
85
- end
86
-
87
- def generate_trigger_hash finding, request
88
- hash = new
89
- hash.update(finding.rule_id)
90
- hash.update_on_request(finding, request)
91
- hash.finish
92
- end
93
- end
94
26
 
95
27
  def update_on_request finding, request
96
28
  if (route = finding.routes[0])
@@ -106,9 +38,14 @@ module Contrast
106
38
  return unless events&.any?
107
39
 
108
40
  events.each do |event|
109
- event.event_sources.each do |source|
110
- update(source.type)
111
- update(source.name) # rubocop:disable Security/Module/Name -- API attribute.
41
+ if event.cs__is_a?(Contrast::Api::Dtm::TraceEvent)
42
+ event.event_sources&.each do |source|
43
+ update(source.type)
44
+ update(source.name) # rubocop:disable Security/Module/Name
45
+ end
46
+ elsif event.cs__is_a?(Contrast::Agent::Assess::Events::SourceEvent)
47
+ update(event.source_type)
48
+ update(event.source_name)
112
49
  end
113
50
  end
114
51
  end
@@ -0,0 +1,86 @@
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 'digest'
5
+ require 'contrast/utils/hash_digest'
6
+
7
+ module Contrast
8
+ module Utils
9
+ # We use this class to provide hashes for our Request and Finding objects
10
+ # based upon our definitions of uniqueness.
11
+ # While the uniqueness of the request object is something internal to the
12
+ # Ruby agent, the uniqueness of the Finding hash is defined by a
13
+ # specification shared across all agent teams. The spec can be found here:
14
+ # https://bitbucket.org/contrastsecurity/assess-specifications/src/master/vulnerability/preflight.md
15
+ module HashDigestExtend
16
+ def generate_request_hash request
17
+ hash = new
18
+ hash.update(request.request_method)
19
+ hash.update(request.normalized_uri)
20
+ request.parameters.each_key do |name|
21
+ hash.update(name)
22
+ end
23
+ cl = request.headers[Contrast::Utils::HashDigest::CONTENT_LENGTH_HEADER]
24
+ hash.update_on_content_length(cl) if cl
25
+ hash.finish
26
+ end
27
+
28
+ def generate_event_hash finding, source, request
29
+ return generate_dataflow_hash(finding, request) if finding.events.length.to_i > 1
30
+
31
+ id = finding.rule_id
32
+ return generate_crypto_hash(finding, source, request) if Contrast::Utils::HashDigest::CRYPTO_RULES.include?(id)
33
+
34
+ generate_trigger_hash(finding, request)
35
+ end
36
+
37
+ def generate_config_hash finding
38
+ hash = new
39
+ hash.update(finding.rule_id)
40
+ path = finding.properties[Contrast::Utils::HashDigest::CONFIG_PATH_KEY]
41
+ hash.update(path)
42
+ method = finding.properties[Contrast::Utils::HashDigest::CONFIG_SESSION_ID_KEY]
43
+ hash.update(method)
44
+ hash.finish
45
+ end
46
+
47
+ def generate_class_scanning_hash finding
48
+ hash = new
49
+ hash.update(finding.rule_id)
50
+ module_name = finding.properties[Contrast::Utils::HashDigest::CLASS_SOURCE_KEY]
51
+ hash.update(module_name)
52
+ # We're not currently collecting this. 30/7/19 HM
53
+ line_no = finding.properties[Contrast::Utils::HashDigest::CLASS_LINE_NO_KEY]
54
+ hash.update(line_no)
55
+ field = finding.properties[Contrast::Utils::HashDigest::CLASS_CONSTANT_NAME_KEY]
56
+ hash.update(field)
57
+ hash.finish
58
+ end
59
+
60
+ private
61
+
62
+ def generate_crypto_hash finding, algorithm, request
63
+ hash = new
64
+ hash.update(finding.rule_id)
65
+ hash.update(algorithm)
66
+ hash.update_on_request(finding, request)
67
+ hash.finish
68
+ end
69
+
70
+ def generate_dataflow_hash finding, request
71
+ hash = new
72
+ hash.update(finding.rule_id)
73
+ hash.update_on_sources(finding.events)
74
+ hash.update_on_request(finding, request)
75
+ hash.finish
76
+ end
77
+
78
+ def generate_trigger_hash finding, request
79
+ hash = new
80
+ hash.update(finding.rule_id)
81
+ hash.update_on_request(finding, request)
82
+ hash.finish
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,74 @@
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 Utils
6
+ # this module extends HeadDumpUtil
7
+ module HeadDumpExtend
8
+ def log_enabled_warning
9
+ control = Contrast::Utils::HeapDumpUtil.control
10
+ dir = control[:path]
11
+ window = control[:window]
12
+ count = control[:count]
13
+ delay = control[:delay]
14
+ clean = control[:clean]
15
+
16
+ logger.info <<~WARNING
17
+ *****************************************************
18
+ ******** HEAP DUMP HAS BEEN ENABLED ********
19
+ *** APPLICATION PROCESS WILL EXIT UPON COMPLETION ***
20
+ *****************************************************
21
+
22
+ Heap dump is a debugging tool that snapshots the entire
23
+ state of the Ruby VM. It is an exceptionally expensive
24
+ process, and should only be used to debug especially
25
+ pernicious errors.
26
+
27
+ It will write multiple memory snaphots, which are liable
28
+ to be multiple gigabytes in size.
29
+ They will be named "[unix timestamp]-heap.dump",
30
+ e.g.: 1020304050-heap.dump
31
+
32
+ It will then call Ruby `exit()`.
33
+
34
+ If this is not your specific intent, you can (and should)
35
+ disable this option in your Contrast config file.
36
+
37
+ HEAP DUMP PARAMETERS:
38
+ \t[write files to this directory] dir: #{ dir }
39
+ \t[wait this many seconds in between dumps] window: #{ window }
40
+ \t[heap dump this many times] count: #{ count }
41
+ \t[wait this many seconds into app lifetime] delay: #{ delay }
42
+ \t[perform gc pass before dump] clean: #{ clean }
43
+
44
+ *****************************************************
45
+ ******** YOU HAVE BEEN WARNED ********
46
+ *****************************************************
47
+ WARNING
48
+ end
49
+
50
+ def capture_heap_dump
51
+ control = Contrast::Utils::HeapDumpUtil.control
52
+ dir = control[:path]
53
+ window = control[:window]
54
+ count = control[:count]
55
+ clean = control[:clean]
56
+ logger.info('HEAP DUMP MAIN LOOP')
57
+ ObjectSpace.trace_object_allocations_start
58
+ count.times do |i|
59
+ logger.info('STARTING HEAP DUMP PASS', current_pass: i, max: count)
60
+ snapshot_heap(dir, clean)
61
+ logger.info('FINISHING HEAP DUMP PASS', current_pass: i, max: count)
62
+ sleep(window)
63
+ end
64
+ ensure
65
+ ObjectSpace.trace_object_allocations_stop
66
+ logger.info('*****************************************************')
67
+ logger.info('******** HEAP DUMP HAS CONCLUDED ********')
68
+ logger.info('*** APPLICATION PROCESS WILL EXIT SHORTLY ***')
69
+ logger.info('*****************************************************')
70
+ exit # rubocop:disable Rails/Exit We weren't kidding!
71
+ end
72
+ end
73
+ end
74
+ end
@@ -5,6 +5,7 @@ require 'objspace'
5
5
  require 'singleton'
6
6
  require 'contrast/components/heap_dump'
7
7
  require 'contrast/components/logger'
8
+ require 'contrast/utils/head_dump_utils_extend'
8
9
 
9
10
  module Contrast
10
11
  module Utils
@@ -13,6 +14,7 @@ module Contrast
13
14
  extend Contrast::Components::Logger::InstanceMethods
14
15
  include Contrast::Components::Logger::InstanceMethods
15
16
  extend Contrast::Components::HeapDump::InstanceMethods
17
+ include Contrast::Utils::HeadDumpExtend
16
18
 
17
19
  LOG_ERROR_DUMPS = 'Unable to generate heap dumps'
18
20
  FILE_WRITE_FLAGS = 'w'
@@ -47,71 +49,6 @@ module Contrast
47
49
  nil
48
50
  end
49
51
 
50
- def log_enabled_warning
51
- control = Contrast::Utils::HeapDumpUtil.control
52
- dir = control[:path]
53
- window = control[:window]
54
- count = control[:count]
55
- delay = control[:delay]
56
- clean = control[:clean]
57
-
58
- logger.info <<~WARNING
59
- *****************************************************
60
- ******** HEAP DUMP HAS BEEN ENABLED ********
61
- *** APPLICATION PROCESS WILL EXIT UPON COMPLETION ***
62
- *****************************************************
63
-
64
- Heap dump is a debugging tool that snapshots the entire
65
- state of the Ruby VM. It is an exceptionally expensive
66
- process, and should only be used to debug especially
67
- pernicious errors.
68
-
69
- It will write multiple memory snaphots, which are liable
70
- to be multiple gigabytes in size.
71
- They will be named "[unix timestamp]-heap.dump",
72
- e.g.: 1020304050-heap.dump
73
-
74
- It will then call Ruby `exit()`.
75
-
76
- If this is not your specific intent, you can (and should)
77
- disable this option in your Contrast config file.
78
-
79
- HEAP DUMP PARAMETERS:
80
- \t[write files to this directory] dir: #{ dir }
81
- \t[wait this many seconds in between dumps] window: #{ window }
82
- \t[heap dump this many times] count: #{ count }
83
- \t[wait this many seconds into app lifetime] delay: #{ delay }
84
- \t[perform gc pass before dump] clean: #{ clean }
85
-
86
- *****************************************************
87
- ******** YOU HAVE BEEN WARNED ********
88
- *****************************************************
89
- WARNING
90
- end
91
-
92
- def capture_heap_dump
93
- control = Contrast::Utils::HeapDumpUtil.control
94
- dir = control[:path]
95
- window = control[:window]
96
- count = control[:count]
97
- clean = control[:clean]
98
- logger.info('HEAP DUMP MAIN LOOP')
99
- ObjectSpace.trace_object_allocations_start
100
- count.times do |i|
101
- logger.info('STARTING HEAP DUMP PASS', current_pass: i, max: count)
102
- snapshot_heap(dir, clean)
103
- logger.info('FINISHING HEAP DUMP PASS', current_pass: i, max: count)
104
- sleep(window)
105
- end
106
- ensure
107
- ObjectSpace.trace_object_allocations_stop
108
- logger.info('*****************************************************')
109
- logger.info('******** HEAP DUMP HAS CONCLUDED ********')
110
- logger.info('*** APPLICATION PROCESS WILL EXIT SHORTLY ***')
111
- logger.info('*****************************************************')
112
- exit # rubocop:disable Rails/Exit We weren't kidding!
113
- end
114
-
115
52
  def snapshot_heap dir, clean
116
53
  output = "#{ Time.now.to_f }-heap.dump"
117
54
  output = File.join(dir, output)
@@ -34,11 +34,28 @@ module Contrast
34
34
  finding.hash_code = Contrast::Utils::StringUtils.force_utf8(hash)
35
35
  finding.preflight = Contrast::Utils::PreflightUtil.create_preflight(finding)
36
36
  Contrast::Agent::Assess::Policy::TriggerMethod.report_finding(finding)
37
+ if Contrast::Agent::Reporter.enabled?
38
+ cs__report_new_finding hash, rule_id, user_provided_options, call_location
39
+ end
37
40
  end
38
41
  rescue StandardError => e
39
42
  logger.error('Unable to build a finding', e, rule: rule_id)
40
43
  end
41
44
 
45
+ def cs__report_new_finding hash_code, rule_id, user_provided_options, call_location
46
+ new_preflight = Contrast::Agent::Reporting::Preflight.new
47
+ new_preflight_message = Contrast::Agent::Reporting::PreflightMessage.new
48
+ new_preflight_message.hash_code = hash_code
49
+ new_preflight_message.data = "#{ rule_id },#{ hash_code }"
50
+ new_preflight.messages << new_preflight_message
51
+
52
+ ruby_finding = Contrast::Agent::Reporting::Finding.new rule_id
53
+ ruby_finding.hash_code = hash_code
54
+ set_new_finding_properties(ruby_finding, user_provided_options, call_location)
55
+ Contrast::Agent.reporter_queue.send_event_immediately(new_preflight)
56
+ Contrast::Agent::Reporting::ReportingStorage[hash_code] = ruby_finding
57
+ end
58
+
42
59
  private
43
60
 
44
61
  # Set the properties needed to report and subsequently render this finding on the finding given.
@@ -76,6 +93,18 @@ module Contrast
76
93
  end
77
94
  call_location&.label&.dup
78
95
  end
96
+
97
+ def set_new_finding_properties finding, user_provided_options, call_location
98
+ path = call_location.path
99
+ # just get the file name, not the full path
100
+ path = path.split(Contrast::Utils::ObjectShare::SLASH).last
101
+ session_id = user_provided_options[:key].to_s if user_provided_options
102
+ finding.properties[CS__SESSION_ID] = session_id
103
+ finding.properties[CS__PATH] = path
104
+ file_path = call_location.absolute_path
105
+ snippet = file_snippet(file_path, call_location)
106
+ finding.properties[CS__SNIPPET] = snippet
107
+ end
79
108
  end
80
109
  end
81
110
  end