contrast-agent 6.1.0 → 6.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (214) hide show
  1. checksums.yaml +4 -4
  2. data/.simplecov +1 -1
  3. data/Rakefile +1 -1
  4. data/ext/build_funchook.rb +3 -3
  5. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +5 -1
  6. data/ext/extconf_common.rb +1 -1
  7. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -2
  8. data/lib/contrast/agent/assess/policy/policy.rb +9 -10
  9. data/lib/contrast/agent/assess/policy/policy_node.rb +9 -10
  10. data/lib/contrast/agent/assess/policy/propagation_method.rb +3 -3
  11. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -3
  12. data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
  13. data/lib/contrast/agent/assess/policy/propagator/buffer.rb +2 -1
  14. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +1 -1
  15. data/lib/contrast/agent/assess/policy/propagator/splat.rb +1 -1
  16. data/lib/contrast/agent/assess/policy/propagator/split.rb +2 -2
  17. data/lib/contrast/agent/assess/policy/propagator/trim.rb +1 -1
  18. data/lib/contrast/agent/assess/policy/source_node.rb +1 -1
  19. data/lib/contrast/agent/assess/policy/trigger_method.rb +7 -7
  20. data/lib/contrast/agent/assess/policy/trigger_node.rb +16 -16
  21. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +1 -1
  22. data/lib/contrast/agent/assess/property/evented.rb +2 -2
  23. data/lib/contrast/agent/assess/property/tagged.rb +2 -2
  24. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +6 -8
  25. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +6 -7
  26. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +5 -5
  27. data/lib/contrast/agent/assess/rule/response/base_rule.rb +2 -3
  28. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +8 -9
  29. data/lib/contrast/agent/assess/rule/response/click_jacking_header_rule.rb +4 -4
  30. data/lib/contrast/agent/assess/rule/response/csp_header_insecure_rule.rb +6 -6
  31. data/lib/contrast/agent/assess/rule/response/csp_header_missing_rule.rb +4 -4
  32. data/lib/contrast/agent/assess/rule/response/hsts_header_rule.rb +4 -4
  33. data/lib/contrast/agent/assess/rule/response/x_content_type_header_rule.rb +4 -4
  34. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +3 -4
  35. data/lib/contrast/agent/assess/tag.rb +13 -14
  36. data/lib/contrast/agent/at_exit_hook.rb +13 -1
  37. data/lib/contrast/agent/inventory/database_config.rb +12 -4
  38. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +9 -5
  39. data/lib/contrast/agent/middleware.rb +6 -3
  40. data/lib/contrast/agent/patching/policy/after_load_patch.rb +3 -3
  41. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +2 -2
  42. data/lib/contrast/agent/patching/policy/method_policy_extend.rb +4 -4
  43. data/lib/contrast/agent/patching/policy/patch.rb +9 -9
  44. data/lib/contrast/agent/patching/policy/patch_status.rb +10 -3
  45. data/lib/contrast/agent/patching/policy/policy.rb +13 -15
  46. data/lib/contrast/agent/patching/policy/policy_node.rb +19 -21
  47. data/lib/contrast/agent/patching/policy/trigger_node.rb +1 -1
  48. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +125 -125
  49. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -2
  50. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +1 -1
  51. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +1 -1
  52. data/lib/contrast/agent/protect/policy/rule_applicator.rb +4 -4
  53. data/lib/contrast/agent/protect/rule/base.rb +30 -18
  54. data/lib/contrast/agent/protect/rule/base_service.rb +31 -14
  55. data/lib/contrast/agent/protect/rule/cmd_injection.rb +16 -9
  56. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +3 -3
  57. data/lib/contrast/agent/protect/rule/default_scanner.rb +2 -1
  58. data/lib/contrast/agent/protect/rule/deserialization.rb +18 -7
  59. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +74 -74
  60. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +71 -53
  61. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +3 -3
  62. data/lib/contrast/agent/protect/rule/no_sqli.rb +15 -16
  63. data/lib/contrast/agent/protect/rule/path_traversal.rb +13 -3
  64. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +2 -2
  65. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +1 -1
  66. data/lib/contrast/agent/protect/rule/sqli.rb +16 -23
  67. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +61 -61
  68. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +29 -29
  69. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +32 -32
  70. data/lib/contrast/agent/protect/rule/xss.rb +21 -0
  71. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +14 -13
  72. data/lib/contrast/agent/protect/rule/xxe.rb +25 -3
  73. data/lib/contrast/agent/reaction_processor.rb +1 -1
  74. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +36 -36
  75. data/lib/contrast/agent/reporting/masker/masker.rb +10 -10
  76. data/lib/contrast/agent/reporting/masker/masker_utils.rb +2 -2
  77. data/lib/contrast/agent/reporting/reporter.rb +2 -11
  78. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +8 -10
  79. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +53 -5
  80. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +25 -19
  81. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +129 -17
  82. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +20 -21
  83. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_stack.rb +22 -0
  84. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +26 -12
  85. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +7 -22
  86. data/lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb +7 -5
  87. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +4 -10
  88. data/lib/contrast/agent/reporting/reporting_events/discovered_route.rb +75 -15
  89. data/lib/contrast/agent/reporting/reporting_events/finding.rb +2 -2
  90. data/lib/contrast/agent/reporting/reporting_events/library_usage_observation.rb +5 -19
  91. data/lib/contrast/agent/reporting/reporting_events/observed_library_usage.rb +6 -22
  92. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +1 -1
  93. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +2 -3
  94. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +1 -3
  95. data/lib/contrast/agent/reporting/reporting_events/route_coverage.rb +9 -0
  96. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +1 -2
  97. data/lib/contrast/agent/reporting/reporting_utilities/dtm_message.rb +0 -10
  98. data/lib/contrast/agent/reporting/reporting_utilities/headers.rb +1 -2
  99. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +6 -7
  100. data/lib/contrast/agent/reporting/reporting_utilities/response.rb +61 -3
  101. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +33 -11
  102. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +8 -8
  103. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +64 -32
  104. data/lib/contrast/agent/reporting/settings/application_settings.rb +8 -23
  105. data/lib/contrast/agent/reporting/settings/assess.rb +5 -5
  106. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +28 -34
  107. data/lib/contrast/agent/reporting/settings/bot_blocker.rb +68 -0
  108. data/lib/contrast/agent/reporting/settings/code_exclusion.rb +27 -0
  109. data/lib/contrast/agent/reporting/settings/exclusion_base.rb +33 -0
  110. data/lib/contrast/agent/reporting/settings/exclusions.rb +39 -57
  111. data/lib/contrast/agent/reporting/settings/helpers.rb +56 -0
  112. data/lib/contrast/agent/reporting/settings/input_exclusion.rb +37 -0
  113. data/lib/contrast/agent/reporting/settings/ip_filter.rb +35 -0
  114. data/lib/contrast/agent/reporting/settings/keyword.rb +74 -0
  115. data/lib/contrast/agent/reporting/settings/log_enhancer.rb +65 -0
  116. data/lib/contrast/agent/reporting/settings/protect.rb +22 -5
  117. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +62 -115
  118. data/lib/contrast/agent/reporting/settings/reaction.rb +13 -4
  119. data/lib/contrast/agent/reporting/settings/rule_definition.rb +63 -0
  120. data/lib/contrast/agent/reporting/settings/sampling.rb +10 -0
  121. data/lib/contrast/agent/reporting/settings/sanitizer.rb +38 -0
  122. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +11 -3
  123. data/lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb +9 -2
  124. data/lib/contrast/agent/reporting/settings/server_features.rb +10 -2
  125. data/lib/contrast/agent/reporting/settings/syslog.rb +176 -0
  126. data/lib/contrast/agent/reporting/settings/url_exclusion.rb +42 -0
  127. data/lib/contrast/agent/reporting/settings/validator.rb +17 -0
  128. data/lib/contrast/agent/request.rb +2 -2
  129. data/lib/contrast/agent/request_context.rb +27 -19
  130. data/lib/contrast/agent/request_context_extend.rb +10 -23
  131. data/lib/contrast/agent/request_handler.rb +9 -5
  132. data/lib/contrast/agent/rule_set.rb +2 -2
  133. data/lib/contrast/agent/scope.rb +1 -1
  134. data/lib/contrast/agent/static_analysis.rb +4 -8
  135. data/lib/contrast/agent/telemetry/base.rb +9 -5
  136. data/lib/contrast/agent/telemetry/events/exceptions/obfuscate.rb +119 -0
  137. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +2 -2
  138. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +1 -1
  139. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +1 -1
  140. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions_report.rb +16 -18
  141. data/lib/contrast/agent/telemetry/events/startup_metrics_event.rb +2 -2
  142. data/lib/contrast/agent/thread_watcher.rb +4 -5
  143. data/lib/contrast/agent/version.rb +1 -1
  144. data/lib/contrast/agent.rb +1 -3
  145. data/lib/contrast/api/communication/messaging_queue.rb +1 -1
  146. data/lib/contrast/api/communication/service_lifecycle.rb +1 -1
  147. data/lib/contrast/api/communication/socket.rb +1 -1
  148. data/lib/contrast/api/communication/socket_client.rb +1 -1
  149. data/lib/contrast/api/communication/speedracer.rb +2 -2
  150. data/lib/contrast/api/decorators/agent_startup.rb +10 -9
  151. data/lib/contrast/api/decorators/application_settings.rb +1 -1
  152. data/lib/contrast/api/decorators/application_startup.rb +4 -4
  153. data/lib/contrast/api/decorators/application_update.rb +0 -8
  154. data/lib/contrast/api/decorators/response_type.rb +4 -17
  155. data/lib/contrast/api/decorators.rb +0 -1
  156. data/lib/contrast/components/agent.rb +1 -1
  157. data/lib/contrast/components/base.rb +1 -1
  158. data/lib/contrast/components/config.rb +6 -6
  159. data/lib/contrast/components/contrast_service.rb +4 -1
  160. data/lib/contrast/components/sampling.rb +1 -1
  161. data/lib/contrast/components/settings.rb +52 -28
  162. data/lib/contrast/config/assess_rules_configuration.rb +1 -1
  163. data/lib/contrast/config/protect_rules_configuration.rb +1 -1
  164. data/lib/contrast/config/root_configuration.rb +1 -1
  165. data/lib/contrast/configuration.rb +4 -4
  166. data/lib/contrast/extension/assess/array.rb +1 -1
  167. data/lib/contrast/extension/assess/erb.rb +1 -1
  168. data/lib/contrast/extension/assess/marshal.rb +1 -1
  169. data/lib/contrast/extension/assess/string.rb +1 -1
  170. data/lib/contrast/extension/extension.rb +2 -2
  171. data/lib/contrast/framework/base_support.rb +13 -12
  172. data/lib/contrast/framework/grape/support.rb +9 -9
  173. data/lib/contrast/framework/manager.rb +7 -9
  174. data/lib/contrast/framework/manager_extend.rb +2 -1
  175. data/lib/contrast/framework/rack/patch/session_cookie.rb +1 -1
  176. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +15 -3
  177. data/lib/contrast/framework/rails/patch/assess_configuration.rb +3 -3
  178. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +1 -1
  179. data/lib/contrast/framework/rails/patch/support.rb +1 -1
  180. data/lib/contrast/framework/rails/support.rb +11 -4
  181. data/lib/contrast/framework/sinatra/support.rb +4 -3
  182. data/lib/contrast/logger/aliased_logging.rb +27 -15
  183. data/lib/contrast/logger/cef_log.rb +14 -14
  184. data/lib/contrast/logger/format.rb +1 -1
  185. data/lib/contrast/logger/log.rb +8 -8
  186. data/lib/contrast/tasks/config.rb +12 -12
  187. data/lib/contrast/tasks/service.rb +2 -2
  188. data/lib/contrast/utils/assess/tracking_util.rb +4 -4
  189. data/lib/contrast/utils/class_util.rb +4 -4
  190. data/lib/contrast/utils/findings.rb +3 -3
  191. data/lib/contrast/utils/hash_digest.rb +6 -7
  192. data/lib/contrast/utils/head_dump_utils_extend.rb +1 -1
  193. data/lib/contrast/utils/invalid_configuration_util.rb +1 -1
  194. data/lib/contrast/utils/log_utils.rb +4 -4
  195. data/lib/contrast/utils/lru_cache.rb +1 -1
  196. data/lib/contrast/utils/metrics_hash.rb +1 -1
  197. data/lib/contrast/utils/middleware_utils.rb +5 -5
  198. data/lib/contrast/utils/net_http_base.rb +4 -4
  199. data/lib/contrast/utils/os.rb +1 -1
  200. data/lib/contrast/utils/patching/policy/patch_utils.rb +2 -2
  201. data/lib/contrast/utils/request_utils.rb +2 -2
  202. data/lib/contrast/utils/response_utils.rb +14 -1
  203. data/lib/contrast/utils/sha256_builder.rb +4 -4
  204. data/lib/contrast/utils/stack_trace_utils.rb +31 -13
  205. data/lib/contrast/utils/telemetry.rb +15 -9
  206. data/lib/contrast/utils/telemetry_client.rb +5 -5
  207. data/lib/contrast/utils/telemetry_hash.rb +37 -13
  208. data/lib/contrast/utils/telemetry_identifier.rb +10 -2
  209. data/lib/contrast/utils/thread_tracker.rb +26 -9
  210. data/lib/contrast/utils/timer.rb +7 -2
  211. data/lib/contrast.rb +1 -3
  212. data/resources/assess/policy.json +1 -1
  213. metadata +28 -14
  214. data/lib/contrast/api/decorators/library_usage_update.rb +0 -31
@@ -34,7 +34,7 @@ module Contrast
34
34
  # @return [nil, String] the content of the body
35
35
  def extract_body body
36
36
  return unless body
37
- return if defined?(Rack::File) && body.is_a?(Rack::File)
37
+ return if body_is_file?(body)
38
38
 
39
39
  return handle_rack_body_proxy(body) if body.is_a?(Rack::BodyProxy)
40
40
  return extract_body(body.body) if sub_extractable?(body)
@@ -77,6 +77,19 @@ module Contrast
77
77
  obj.to_s
78
78
  end
79
79
  end
80
+
81
+ # After Rack 3.1 gets live - line 88 (Rack::File) will be removed.
82
+ # In 3.1 version, they drop the support for File class and will only support Files class
83
+ #
84
+ # @param body [String, Rack::File, Rack::BodyProxy,
85
+ # ActionDispatch::Response::RackBody, Rack::Response] Something that
86
+ # holds, wraps, or is the body of the Response
87
+ def body_is_file? body
88
+ return true if defined?(Rack::File) && (body.is_a?(Rack::File) || body.is_a?(Rack::File::Iterator))
89
+ return true if defined?(Rack::Files) && (body.is_a?(Rack::Files) || body.is_a?(Rack::Files::Iterator))
90
+
91
+ false
92
+ end
80
93
  end
81
94
  end
82
95
  end
@@ -9,16 +9,16 @@ module Contrast
9
9
  # Utility methods for building and caching hashes from strings
10
10
  class Sha256Builder
11
11
  include Singleton
12
+ # Return a list of "source" files. Ignore metadata files as well as files
13
+ # in the spec, test or exe directories. Could potentially miss important
14
+ # files but good enough for the time being.
15
+ PATHS = '{lib,bin,ext}/**/*.{rb,c,h,c++,cpp,java,rs}'
12
16
 
13
17
  def initialize
14
18
  @sha256_cache = {}
15
19
  @gem_hash_cache = {}
16
20
  end
17
21
 
18
- # Return a list of "source" files. Ignore metadata files as well as files
19
- # in the spec, test or exe directories. Could potentially miss important
20
- # files but good enough for the time being.
21
- PATHS = '{lib,bin,ext}/**/*.{rb,c,h,c++,cpp,java,rs}'
22
22
  def files path
23
23
  if Dir.exist?(path)
24
24
  Dir.glob(File.join(path, PATHS))
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/utils/object_share'
5
+ require 'contrast/agent/reporting/reporting_events/application_defend_attack_sample_stack'
5
6
  require 'contrast/api'
6
7
 
7
8
  module Contrast
@@ -35,21 +36,18 @@ module Contrast
35
36
  # StackTraceElement for TeamServer to display, excluding any Contrast
36
37
  # code found.
37
38
  #
38
- # @return [Array<Contrast::Api::Dtm::StackTraceElement]
39
+ # @return [Array<Contrast::Api::Dtm::StackTraceElement>]
39
40
  def build_protect_stack_array
40
- stack = caller(2, 20)
41
- return [] unless stack
41
+ build_protect_stack(Contrast::Api::Dtm::StackTraceElement)
42
+ end
42
43
 
43
- stack = reject_caller_entries(stack)
44
- i = 0
45
- stack.map! do |entry|
46
- element = Contrast::Api::Dtm::StackTraceElement.new
47
- element = fill_protect_element(element, entry, i)
48
- i += 1
49
- element
50
- end
51
- stack.compact!
52
- stack
44
+ # Call and translate a caller_locations array to an array of
45
+ # StackTraceElement for TeamServer to display, excluding any Contrast
46
+ # code found.
47
+ #
48
+ # @return [Array<Contrast::Agent::Reporting::ApplicationDefendAttackSampleStack>]
49
+ def build_protect_report_stack_array
50
+ build_protect_stack(Contrast::Agent::Reporting::ApplicationDefendAttackSampleStack)
53
51
  end
54
52
 
55
53
  # Translate a caller array to an array of TraceStacks for TeamServer to
@@ -86,6 +84,26 @@ module Contrast
86
84
  end
87
85
  end
88
86
 
87
+ # @param clazz [Class] Contrast::Api::Dtm::StackTraceElement or
88
+ # Contrast::Agent::Reporting::ApplicationDefendAttackSampleStack
89
+ # @return [Array<Contrast::Agent::Reporting::ApplicationDefendAttackSampleStack|
90
+ # Contrast::Api::Dtm::StackTraceElement>]
91
+ def build_protect_stack clazz
92
+ stack = caller(3, 21)
93
+ return [] unless stack
94
+
95
+ stack = reject_caller_entries(stack)
96
+ i = 0
97
+ stack.map! do |entry|
98
+ element = clazz.new
99
+ element = fill_protect_element(element, entry, i)
100
+ i += 1
101
+ element
102
+ end
103
+ stack.compact!
104
+ stack
105
+ end
106
+
89
107
  # In Contrast UI - there are many ways to render the stacktraces. For
90
108
  # Protect, there are two ways we're concerned about, one for the first
91
109
  # element and then all others.
@@ -31,18 +31,21 @@ module Contrast
31
31
  # @return[Boolean, nil] true if file is created, false if file already exist
32
32
  # and nil if Telemetry is disabled or on unsupported OS.
33
33
  def self.create_telemetry_file
34
- write_mark_file DIR, FILE, CONFIG_DIR
34
+ write_mark_file(DIR, FILE, CONFIG_DIR)
35
35
  end
36
36
 
37
+ # The telemetry disclaimer message, set if needed.
38
+ #
39
+ # @return [String]
37
40
  def self.disclaimer
38
41
  @_disclaimer = MESSAGE[:disclaimer]
39
42
  end
40
43
 
44
+ # Determine if telemetry has been explicitly disabled by opt out or has been left running.
45
+ #
46
+ # @return [Boolean]
41
47
  def self.exceptions_enabled?
42
- # TODO: RUBY-1643 Obfuscate path
43
- # Enabled this once the masking of stack frames is done.
44
- # @_exceptions_enabled = telemetry_exceptions_enabled? if @_exceptions_enabled.nil?
45
- @_exceptions_enabled = false if @_exceptions_enabled.nil?
48
+ @_exceptions_enabled = telemetry_exceptions_enabled? if @_exceptions_enabled.nil?
46
49
  @_exceptions_enabled
47
50
  end
48
51
 
@@ -67,7 +70,7 @@ module Contrast
67
70
  # nor can we write to the "root" directory ( / ). This results in Errno::EROFS exception.
68
71
  # So for the MacOS we would use the config directory of the instrumented application.
69
72
  @dir = config_dir if Contrast::Utils::OS.mac?
70
- return false if File.file? @dir + file
73
+ return false if File.file?(@dir + file)
71
74
  return true if touch_marker(@dir, file)
72
75
 
73
76
  # If we don't have permission to write to root directory use config instead
@@ -78,13 +81,16 @@ module Contrast
78
81
  #
79
82
  # @return[Boolean] true if success, false if fails
80
83
  def touch_marker dir, file
81
- FileUtils.mkdir_p dir unless Dir.exist? dir
82
- FileUtils.touch dir + file
83
- File.file? dir + file
84
+ FileUtils.mkdir_p(dir) unless Dir.exist?(dir)
85
+ FileUtils.touch(dir + file)
86
+ File.file?(dir + file)
84
87
  rescue StandardError => _e
85
88
  false
86
89
  end
87
90
 
91
+ # Determine if telemetry has been explicitly disabled by opt out or has been left running.
92
+ #
93
+ # @return [Boolean]
88
94
  def telemetry_exceptions_enabled?
89
95
  opts_out_telemetry = return_value(:telemetry_opt_outs).to_s
90
96
  return false if opts_out_telemetry.casecmp?('true') || opts_out_telemetry == '1'
@@ -12,10 +12,10 @@ module Contrast
12
12
  module Utils
13
13
  # This module creates a Net::HTTP client and initiates a connection to the provided result
14
14
  class TelemetryClient < NetHttpBase
15
+ include Contrast::Components::Logger::InstanceMethods
15
16
  ENDPOINT = 'api/v1/telemetry/metrics' # /TelemetryEvent.path
16
17
  EXCEPTIONS = 'api/v1/telemetry/exceptions' # /TelemetryExceptions::Event.path
17
18
  SERVICE_NAME = 'Telemetry'
18
- include Contrast::Components::Logger::InstanceMethods
19
19
  # This method initializes the Net::HTTP client we'll need. it will validate
20
20
  # the connection and make the first request. If connection is valid and response
21
21
  # is available then the open connection is returned.
@@ -32,7 +32,7 @@ module Contrast
32
32
  # @param event [Contrast::Agent::Telemetry::Event, Array<Contrast::Agent::Telemetry::TelemetryException::Event>]
33
33
  # @return [Net::HTTP::Post]
34
34
  def build_request event
35
- return unless valid_event? event
35
+ return unless valid_event?(event)
36
36
 
37
37
  string_body = if Array(event).all?(Contrast::Agent::Telemetry::TelemetryException::Event)
38
38
  event.map(&:to_controlled_hash).flatten!
@@ -54,10 +54,10 @@ module Contrast
54
54
  # @param connection[Net::HTTP]
55
55
  def send_request event, connection
56
56
  return if connection.nil? || event.nil?
57
- return unless valid_event? event
57
+ return unless valid_event?(event)
58
58
 
59
- req = build_request event
60
- connection.request req
59
+ req = build_request(event)
60
+ connection.request(req)
61
61
  end
62
62
 
63
63
  # This method will handle the response from the tenant
@@ -2,34 +2,58 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/components/logger'
5
+ require 'contrast/agent/telemetry/events/exceptions/telemetry_exception_event'
5
6
 
6
7
  module Contrast
7
8
  module Utils
8
- # This is the TelemetryHash, which will take data type, so we can push freely, without worrying about
9
- # validating the event before that
9
+ # This is the TelemetryHash, which will store Contrast::Agent::Telemetry::TelemetryException::Event, so we can push
10
+ # freely, without worrying about validating the event before that. TelemetryHash has a max size of events,
11
+ # default is 10 events
10
12
  class TelemetryHash < Hash
11
13
  include Contrast::Components::Logger::InstanceMethods
14
+ HASH_SIZE_LIMIT = 10
12
15
 
13
- attr_reader :data_type
14
-
15
- def initialize data_type, *_several_variants
16
- super()
17
- @data_type = data_type
18
- end
19
-
16
+ # Wrapper to set a value in this TelemetryHash only if the provided value is of the data_type for this
17
+ # TelemetryHash or the hash has not reached its limit of for unique keys
18
+ #
19
+ # @param key [Object] the key to which to associate the value
20
+ # @param value [Object] the value to store
21
+ # @return [Object, nil] echo back out the value as the Hash#[]= method does, or nil if not of the expected
22
+ # data_type
20
23
  def []= key, value
21
- return unless valid_value? value
24
+ return if exception_limit?
25
+ return unless valid_value?(value)
22
26
 
23
27
  super(key, value)
24
28
  end
25
29
 
30
+ # Increment the occurrences for the exception object contained in this TelemetryHash
31
+ #
32
+ # @param key [Object] the key to check for the exception stored in this TelemetryHash
26
33
  def increment key
27
- self[key].exceptions[0].increment_occurrences
28
- :incremented!
34
+ value = self[key]
35
+ return unless value&.exceptions&.any?
36
+
37
+ value.exceptions[0].increment_occurrences
29
38
  end
30
39
 
40
+ # Determine if hash has reached exception event limit
41
+ # @return [Boolean]
42
+ def exception_limit?
43
+ unless size < HASH_SIZE_LIMIT
44
+ logger.debug("Number of TelemetryExceptions exceeds limit of #{ HASH_SIZE_LIMIT }")
45
+ return true
46
+ end
47
+ false
48
+ end
49
+
50
+ private
51
+
52
+ # Determine if the given value is valid based on the datatype which this TelemetryHash contains
53
+ # @param value [Object]
54
+ # @return boolean
31
55
  def valid_value? value
32
- unless value.cs__is_a?(data_type)
56
+ unless value.cs__is_a?(Contrast::Agent::Telemetry::TelemetryException::Event)
33
57
  logger.debug('The following key will be omitted', value: value)
34
58
  return false
35
59
  end
@@ -38,6 +38,10 @@ module Contrast
38
38
  @_mac = find_mac(primary) || find_mac
39
39
  end
40
40
 
41
+ # Set and return a Sha256 hash representing this application, based on the mac identifier of this machine and
42
+ # the application name or a Secure UUID if one of them cannot be determined.
43
+ #
44
+ # @return [String]
41
45
  def self.application_id
42
46
  @_application_id ||= begin
43
47
  id = nil
@@ -48,6 +52,10 @@ module Contrast
48
52
  end
49
53
  end
50
54
 
55
+ # Set and return a Sha256 hash representing this agent run, based on the mac identifier of this machine or a
56
+ # Secure UUID if one cannot be determined.
57
+ #
58
+ # @return [String]
51
59
  def self.instance_id
52
60
  @_instance_id ||= Digest::SHA2.new(256).hexdigest(Contrast::Utils::Telemetry::Identifier.mac ||
53
61
  "_#{ SecureRandom.uuid }")
@@ -76,10 +84,10 @@ module Contrast
76
84
  next if primary && !name.include?(primary)
77
85
 
78
86
  # retrieving MAC address from primary network interface or first available
79
- mac = retrieve_mac addr
87
+ mac = retrieve_mac(addr)
80
88
  next unless mac
81
89
 
82
- result = mac if mac && (mac.match? MAC_REGEX)
90
+ result = mac if mac&.match?(MAC_REGEX)
83
91
  break if result
84
92
  end
85
93
  result
@@ -3,36 +3,53 @@
3
3
 
4
4
  module Contrast
5
5
  module Utils
6
- # ThreadTracker allows tracking of singleton objects across threads
6
+ # ThreadTracker allows tracking of singleton objects across threads. It acts on Thread#[] as that call is
7
+ # fiber-local where as Thread#thread_variables is not.
7
8
  class ThreadTracker
8
- def initialize; end
9
-
10
- # Note about Ruby -- thread#[] is fiber-local,
11
- # #thread_variables is not.
12
-
9
+ # Get the given key to given value in Thread#[] or return default.
10
+ #
11
+ # @param key [Object] key used to reference the value
12
+ # @param default [Object] value to return if not present in Thread#[]
13
+ # @return [Object]
13
14
  def get key, default = nil
14
15
  Thread.current[key] || default
15
16
  end
16
17
 
18
+ # Set the given key to given value in Thread#[].
19
+ #
20
+ # @param key [Object] key used to reference the value
21
+ # @param value [Object] value to store
17
22
  def set key, value
18
23
  Thread.current[key] = value
19
24
  end
20
25
 
26
+ # Remove the given key from the current Thread#[] by setting it to nil.
27
+ #
28
+ # @param key [Object] key of the value to delete
21
29
  def delete key
22
30
  Thread.current[key] = nil
23
31
  end
24
32
 
25
- def lifespan obj
26
- set(:current_context, obj)
27
- response = yield(obj)
33
+ # Wrap the block given with a RequestContext by setting it beforehand and deleting it after.
34
+ #
35
+ # @param context [Contrast::Agent::RequestContext]
36
+ def lifespan context
37
+ set(:current_context, context)
38
+ response = yield(context)
28
39
  delete(:current_context)
29
40
  response
30
41
  end
31
42
 
43
+ # Retrieve the Thread#[] RequestContext
44
+ #
45
+ # @return [Contrast::Agent::RequestContext]
32
46
  def current
33
47
  get(:current_context)
34
48
  end
35
49
 
50
+ # Set the Thread#[] context to the one provided.
51
+ #
52
+ # @param context [Contrast::Agent::RequestContext]
36
53
  def update_current_context context
37
54
  set(:current_context, context)
38
55
  end
@@ -4,16 +4,21 @@
4
4
  module Contrast
5
5
  module Utils
6
6
  # Timer is class that can track state about when an event starts and how long it takes
7
- # Also containes utility methods to get time values in milliseconds
7
+ # Also contains utility methods to get time values in milliseconds
8
8
  class Timer
9
- attr_reader :start_ms, :events
9
+ # @return [Integer] the ms of the Time that this instance represents
10
+ attr_reader :start_ms
10
11
 
12
+ # Create a wrapper for the current time
13
+ #
14
+ # @param time [Time]
11
15
  def initialize time = Time.now
12
16
  @start_at = time
13
17
  @start_ms = (@start_at.to_f * 1000).to_i
14
18
  @events = {}
15
19
  end
16
20
 
21
+ # @return [Integer] time, in ms
17
22
  def self.now_ms
18
23
  (Time.now.to_f * 1000).to_i
19
24
  end
data/lib/contrast.rb CHANGED
@@ -66,9 +66,7 @@ module Contrast
66
66
  end
67
67
 
68
68
  module Contrast
69
- TELEMETRY_EXCEPTIONS = if Contrast::Utils::Telemetry.exceptions_enabled?
70
- Contrast::Utils::TelemetryHash.new(Contrast::Agent::Telemetry::TelemetryException::Event)
71
- end
69
+ TELEMETRY_EXCEPTIONS = (Contrast::Utils::TelemetryHash.new if Contrast::Utils::Telemetry.exceptions_enabled?)
72
70
  end
73
71
 
74
72
  # This needs to be required very early, after component interfaces, and before instrumentation attempts
@@ -1716,7 +1716,7 @@
1716
1716
  "source":"P0"
1717
1717
  }, {
1718
1718
  "class_name": "BasicObject",
1719
- "instance_method": false,
1719
+ "instance_method": true,
1720
1720
  "method_visibility": "public",
1721
1721
  "method_name":"instance_eval",
1722
1722
  "source":"P0",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: contrast-agent
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.1.0
4
+ version: 6.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - galen.palmer@contrastsecurity.com
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: exe
15
15
  cert_chain: []
16
- date: 2022-05-05 00:00:00.000000000 Z
16
+ date: 2022-05-27 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: bundler
@@ -631,22 +631,22 @@ executables:
631
631
  - contrast_service
632
632
  extensions:
633
633
  - ext/cs__common/extconf.rb
634
- - ext/cs__assess_hash/extconf.rb
635
- - ext/cs__assess_array/extconf.rb
636
- - ext/cs__assess_test/extconf.rb
637
- - ext/cs__os_information/extconf.rb
638
- - ext/cs__assess_yield_track/extconf.rb
639
- - ext/cs__assess_kernel/extconf.rb
640
- - ext/cs__assess_module/extconf.rb
641
634
  - ext/cs__assess_string_interpolation26/extconf.rb
642
- - ext/cs__assess_regexp/extconf.rb
635
+ - ext/cs__assess_array/extconf.rb
636
+ - ext/cs__assess_basic_object/extconf.rb
643
637
  - ext/cs__assess_fiber_track/extconf.rb
644
- - ext/cs__scope/extconf.rb
638
+ - ext/cs__assess_hash/extconf.rb
645
639
  - ext/cs__assess_marshal_module/extconf.rb
640
+ - ext/cs__assess_yield_track/extconf.rb
646
641
  - ext/cs__assess_string/extconf.rb
647
- - ext/cs__assess_basic_object/extconf.rb
648
- - ext/cs__contrast_patch/extconf.rb
642
+ - ext/cs__assess_regexp/extconf.rb
643
+ - ext/cs__os_information/extconf.rb
644
+ - ext/cs__scope/extconf.rb
645
+ - ext/cs__assess_test/extconf.rb
649
646
  - ext/cs__tests/extconf.rb
647
+ - ext/cs__assess_kernel/extconf.rb
648
+ - ext/cs__assess_module/extconf.rb
649
+ - ext/cs__contrast_patch/extconf.rb
650
650
  extra_rdoc_files: []
651
651
  files:
652
652
  - ".clang-format"
@@ -1007,6 +1007,7 @@ files:
1007
1007
  - lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb
1008
1008
  - lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb
1009
1009
  - lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb
1010
+ - lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_stack.rb
1010
1011
  - lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb
1011
1012
  - lib/contrast/agent/reporting/reporting_events/application_inventory.rb
1012
1013
  - lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb
@@ -1056,14 +1057,27 @@ files:
1056
1057
  - lib/contrast/agent/reporting/settings/application_settings.rb
1057
1058
  - lib/contrast/agent/reporting/settings/assess.rb
1058
1059
  - lib/contrast/agent/reporting/settings/assess_server_feature.rb
1060
+ - lib/contrast/agent/reporting/settings/bot_blocker.rb
1061
+ - lib/contrast/agent/reporting/settings/code_exclusion.rb
1062
+ - lib/contrast/agent/reporting/settings/exclusion_base.rb
1059
1063
  - lib/contrast/agent/reporting/settings/exclusions.rb
1064
+ - lib/contrast/agent/reporting/settings/helpers.rb
1065
+ - lib/contrast/agent/reporting/settings/input_exclusion.rb
1066
+ - lib/contrast/agent/reporting/settings/ip_filter.rb
1067
+ - lib/contrast/agent/reporting/settings/keyword.rb
1068
+ - lib/contrast/agent/reporting/settings/log_enhancer.rb
1060
1069
  - lib/contrast/agent/reporting/settings/protect.rb
1061
1070
  - lib/contrast/agent/reporting/settings/protect_server_feature.rb
1062
1071
  - lib/contrast/agent/reporting/settings/reaction.rb
1072
+ - lib/contrast/agent/reporting/settings/rule_definition.rb
1063
1073
  - lib/contrast/agent/reporting/settings/sampling.rb
1074
+ - lib/contrast/agent/reporting/settings/sanitizer.rb
1064
1075
  - lib/contrast/agent/reporting/settings/sensitive_data_masking.rb
1065
1076
  - lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb
1066
1077
  - lib/contrast/agent/reporting/settings/server_features.rb
1078
+ - lib/contrast/agent/reporting/settings/syslog.rb
1079
+ - lib/contrast/agent/reporting/settings/url_exclusion.rb
1080
+ - lib/contrast/agent/reporting/settings/validator.rb
1067
1081
  - lib/contrast/agent/request.rb
1068
1082
  - lib/contrast/agent/request_context.rb
1069
1083
  - lib/contrast/agent/request_context_extend.rb
@@ -1075,6 +1089,7 @@ files:
1075
1089
  - lib/contrast/agent/static_analysis.rb
1076
1090
  - lib/contrast/agent/telemetry/base.rb
1077
1091
  - lib/contrast/agent/telemetry/events/event.rb
1092
+ - lib/contrast/agent/telemetry/events/exceptions/obfuscate.rb
1078
1093
  - lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb
1079
1094
  - lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb
1080
1095
  - lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message.rb
@@ -1116,7 +1131,6 @@ files:
1116
1131
  - lib/contrast/api/decorators/instrumentation_mode.rb
1117
1132
  - lib/contrast/api/decorators/ip_denylist.rb
1118
1133
  - lib/contrast/api/decorators/library.rb
1119
- - lib/contrast/api/decorators/library_usage_update.rb
1120
1134
  - lib/contrast/api/decorators/message.rb
1121
1135
  - lib/contrast/api/decorators/rasp_rule_sample.rb
1122
1136
  - lib/contrast/api/decorators/response_type.rb
@@ -1,31 +0,0 @@
1
- # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
- # frozen_string_literal: true
3
-
4
- require 'contrast/utils/string_utils'
5
-
6
- module Contrast
7
- module Api
8
- module Decorators
9
- # Used to decorate the LibraryUsageUpdate protobuf
10
- module LibraryUsageUpdate
11
- def self.included klass
12
- klass.extend(ClassMethods)
13
- end
14
-
15
- # Used to add class methods to the LibraryUsageUpdate class on inclusion of the decorator
16
- module ClassMethods
17
- def build digest, files
18
- msg = new
19
- msg.hash_code = Contrast::Utils::StringUtils.force_utf8(digest)
20
- files.each do |required_file|
21
- msg.class_names[required_file] = true
22
- end
23
- msg
24
- end
25
- end
26
- end
27
- end
28
- end
29
- end
30
-
31
- Contrast::Api::Dtm::LibraryUsageUpdate.include(Contrast::Api::Decorators::LibraryUsageUpdate)