contrast-agent 6.0.0 → 6.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (260) 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/cs__assess_regexp/cs__assess_regexp.c +15 -2
  7. data/ext/cs__assess_regexp/cs__assess_regexp.h +2 -0
  8. data/ext/cs__assess_string/cs__assess_string.c +8 -0
  9. data/ext/cs__assess_test/cs__assess_test.h +9 -0
  10. data/ext/cs__assess_test/cs__assess_tests.c +22 -0
  11. data/ext/cs__assess_test/extconf.rb +5 -0
  12. data/ext/cs__common/cs__common.c +101 -0
  13. data/ext/cs__common/cs__common.h +29 -5
  14. data/ext/cs__contrast_patch/cs__contrast_patch.c +1 -1
  15. data/ext/cs__tests/cs__tests.c +12 -0
  16. data/ext/cs__tests/cs__tests.h +3 -0
  17. data/ext/cs__tests/extconf.rb +5 -0
  18. data/ext/extconf_common.rb +1 -1
  19. data/lib/contrast/agent/assess/contrast_object.rb +16 -16
  20. data/lib/contrast/agent/assess/events/source_event.rb +17 -19
  21. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -2
  22. data/lib/contrast/agent/assess/policy/policy.rb +9 -10
  23. data/lib/contrast/agent/assess/policy/policy_node.rb +9 -10
  24. data/lib/contrast/agent/assess/policy/policy_scanner.rb +2 -16
  25. data/lib/contrast/agent/assess/policy/propagation_method.rb +3 -3
  26. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -3
  27. data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
  28. data/lib/contrast/agent/assess/policy/propagator/buffer.rb +2 -1
  29. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +1 -1
  30. data/lib/contrast/agent/assess/policy/propagator/splat.rb +1 -1
  31. data/lib/contrast/agent/assess/policy/propagator/split.rb +17 -21
  32. data/lib/contrast/agent/assess/policy/propagator/trim.rb +1 -1
  33. data/lib/contrast/agent/assess/policy/source_node.rb +1 -1
  34. data/lib/contrast/agent/assess/policy/trigger_method.rb +10 -18
  35. data/lib/contrast/agent/assess/policy/trigger_node.rb +16 -16
  36. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +1 -1
  37. data/lib/contrast/agent/assess/property/evented.rb +2 -2
  38. data/lib/contrast/agent/assess/property/tagged.rb +2 -2
  39. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +6 -8
  40. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +6 -7
  41. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +12 -7
  42. data/lib/contrast/agent/assess/rule/response/base_rule.rb +13 -6
  43. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +66 -43
  44. data/lib/contrast/agent/assess/rule/response/click_jacking_header_rule.rb +4 -4
  45. data/lib/contrast/agent/assess/rule/response/csp_header_insecure_rule.rb +6 -6
  46. data/lib/contrast/agent/assess/rule/response/csp_header_missing_rule.rb +4 -4
  47. data/lib/contrast/agent/assess/rule/response/hsts_header_rule.rb +4 -4
  48. data/lib/contrast/agent/assess/rule/response/x_content_type_header_rule.rb +4 -4
  49. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +3 -4
  50. data/lib/contrast/agent/assess/tag.rb +13 -14
  51. data/lib/contrast/agent/at_exit_hook.rb +12 -1
  52. data/lib/contrast/agent/inventory/database_config.rb +22 -7
  53. data/lib/contrast/agent/middleware.rb +9 -6
  54. data/lib/contrast/agent/patching/policy/after_load_patch.rb +3 -5
  55. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +2 -2
  56. data/lib/contrast/agent/patching/policy/method_policy_extend.rb +4 -4
  57. data/lib/contrast/agent/patching/policy/patch.rb +20 -19
  58. data/lib/contrast/agent/patching/policy/patch_status.rb +10 -3
  59. data/lib/contrast/agent/patching/policy/patcher.rb +1 -1
  60. data/lib/contrast/agent/patching/policy/policy.rb +13 -15
  61. data/lib/contrast/agent/patching/policy/policy_node.rb +19 -21
  62. data/lib/contrast/agent/patching/policy/trigger_node.rb +1 -1
  63. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +126 -122
  64. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -2
  65. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +1 -1
  66. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +1 -1
  67. data/lib/contrast/agent/protect/policy/rule_applicator.rb +4 -4
  68. data/lib/contrast/agent/protect/rule/base.rb +30 -18
  69. data/lib/contrast/agent/protect/rule/base_service.rb +31 -14
  70. data/lib/contrast/agent/protect/rule/cmd_injection.rb +16 -9
  71. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +3 -3
  72. data/lib/contrast/agent/protect/rule/default_scanner.rb +2 -1
  73. data/lib/contrast/agent/protect/rule/deserialization.rb +18 -7
  74. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +74 -74
  75. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +71 -53
  76. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +3 -3
  77. data/lib/contrast/agent/protect/rule/no_sqli.rb +15 -16
  78. data/lib/contrast/agent/protect/rule/path_traversal.rb +13 -3
  79. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +2 -2
  80. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +1 -1
  81. data/lib/contrast/agent/protect/rule/sqli.rb +16 -23
  82. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +61 -61
  83. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +29 -29
  84. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +32 -32
  85. data/lib/contrast/agent/protect/rule/xss.rb +17 -0
  86. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +14 -13
  87. data/lib/contrast/agent/protect/rule/xxe.rb +25 -3
  88. data/lib/contrast/agent/reaction_processor.rb +1 -1
  89. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +36 -36
  90. data/lib/contrast/agent/reporting/masker/masker.rb +18 -21
  91. data/lib/contrast/agent/reporting/masker/masker_utils.rb +10 -6
  92. data/lib/contrast/agent/reporting/reporter.rb +11 -16
  93. data/lib/contrast/agent/reporting/reporter_heartbeat.rb +49 -0
  94. data/lib/contrast/agent/reporting/reporting_events/agent_startup.rb +6 -2
  95. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +51 -0
  96. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +96 -0
  97. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +70 -0
  98. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +182 -0
  99. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +56 -0
  100. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_stack.rb +22 -0
  101. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +70 -0
  102. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +6 -2
  103. data/lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb +60 -0
  104. data/lib/contrast/agent/reporting/reporting_events/application_reporting_event.rb +27 -0
  105. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +15 -11
  106. data/lib/contrast/agent/reporting/reporting_events/application_update.rb +7 -12
  107. data/lib/contrast/agent/reporting/reporting_events/discovered_route.rb +1 -1
  108. data/lib/contrast/agent/reporting/reporting_events/finding.rb +9 -3
  109. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +2 -4
  110. data/lib/contrast/agent/reporting/reporting_events/finding_event_object.rb +3 -3
  111. data/lib/contrast/agent/reporting/reporting_events/observed_library_usage.rb +6 -2
  112. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +7 -3
  113. data/lib/contrast/agent/reporting/reporting_events/poll.rb +6 -2
  114. data/lib/contrast/agent/reporting/reporting_events/preflight.rb +10 -8
  115. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +6 -10
  116. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +12 -20
  117. data/lib/contrast/agent/reporting/reporting_events/server_reporting_event.rb +27 -0
  118. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +17 -27
  119. data/lib/contrast/agent/reporting/reporting_utilities/build_preflight.rb +38 -0
  120. data/lib/contrast/agent/reporting/reporting_utilities/dtm_message.rb +8 -0
  121. data/lib/contrast/agent/reporting/reporting_utilities/endpoints.rb +6 -0
  122. data/lib/contrast/agent/reporting/reporting_utilities/headers.rb +1 -2
  123. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +15 -10
  124. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +54 -67
  125. data/lib/contrast/agent/reporting/reporting_utilities/response.rb +17 -7
  126. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +9 -6
  127. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +17 -17
  128. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +47 -32
  129. data/lib/contrast/agent/reporting/settings/application_settings.rb +1 -1
  130. data/lib/contrast/agent/reporting/settings/assess.rb +5 -5
  131. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +3 -3
  132. data/lib/contrast/agent/reporting/settings/exclusions.rb +3 -3
  133. data/lib/contrast/agent/reporting/settings/protect.rb +21 -6
  134. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +6 -6
  135. data/lib/contrast/agent/reporting/settings/reaction.rb +3 -3
  136. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +2 -2
  137. data/lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb +2 -2
  138. data/lib/contrast/agent/reporting/settings/server_features.rb +2 -2
  139. data/lib/contrast/agent/request.rb +5 -5
  140. data/lib/contrast/agent/request_context.rb +23 -19
  141. data/lib/contrast/agent/request_context_extend.rb +11 -24
  142. data/lib/contrast/agent/request_handler.rb +4 -4
  143. data/lib/contrast/agent/response.rb +2 -0
  144. data/lib/contrast/agent/rule_set.rb +2 -2
  145. data/lib/contrast/agent/scope.rb +1 -1
  146. data/lib/contrast/agent/service_heartbeat.rb +6 -48
  147. data/lib/contrast/agent/static_analysis.rb +1 -1
  148. data/lib/contrast/agent/telemetry/base.rb +155 -0
  149. data/lib/contrast/agent/telemetry/events/event.rb +35 -0
  150. data/lib/contrast/agent/telemetry/events/exceptions/obfuscate.rb +119 -0
  151. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +44 -36
  152. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +29 -21
  153. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message.rb +91 -73
  154. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +62 -44
  155. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +50 -33
  156. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions.rb +20 -0
  157. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions_report.rb +30 -0
  158. data/lib/contrast/agent/telemetry/events/metric_event.rb +28 -0
  159. data/lib/contrast/agent/telemetry/events/startup_metrics_event.rb +123 -0
  160. data/lib/contrast/agent/thread_watcher.rb +52 -68
  161. data/lib/contrast/agent/version.rb +1 -1
  162. data/lib/contrast/agent/worker_thread.rb +8 -0
  163. data/lib/contrast/agent.rb +1 -3
  164. data/lib/contrast/api/communication/messaging_queue.rb +29 -12
  165. data/lib/contrast/api/communication/response_processor.rb +7 -10
  166. data/lib/contrast/api/communication/service_lifecycle.rb +1 -1
  167. data/lib/contrast/api/communication/socket.rb +1 -1
  168. data/lib/contrast/api/communication/socket_client.rb +1 -1
  169. data/lib/contrast/api/communication/speedracer.rb +3 -3
  170. data/lib/contrast/api/decorators/activity.rb +33 -0
  171. data/lib/contrast/api/decorators/agent_startup.rb +10 -9
  172. data/lib/contrast/api/decorators/application_settings.rb +1 -1
  173. data/lib/contrast/api/decorators/application_startup.rb +4 -4
  174. data/lib/contrast/api/decorators/http_request.rb +1 -1
  175. data/lib/contrast/api/decorators/response_type.rb +4 -17
  176. data/lib/contrast/components/agent.rb +1 -1
  177. data/lib/contrast/components/base.rb +1 -1
  178. data/lib/contrast/components/config.rb +19 -28
  179. data/lib/contrast/components/contrast_service.rb +13 -1
  180. data/lib/contrast/components/sampling.rb +1 -1
  181. data/lib/contrast/components/settings.rb +58 -24
  182. data/lib/contrast/config/agent_configuration.rb +21 -11
  183. data/lib/contrast/config/api_configuration.rb +12 -8
  184. data/lib/contrast/config/api_proxy_configuration.rb +7 -3
  185. data/lib/contrast/config/application_configuration.rb +15 -11
  186. data/lib/contrast/config/assess_configuration.rb +13 -9
  187. data/lib/contrast/config/assess_rules_configuration.rb +6 -2
  188. data/lib/contrast/config/base_configuration.rb +3 -35
  189. data/lib/contrast/config/certification_configuration.rb +9 -5
  190. data/lib/contrast/config/exception_configuration.rb +10 -7
  191. data/lib/contrast/config/heap_dump_configuration.rb +13 -9
  192. data/lib/contrast/config/inventory_configuration.rb +9 -6
  193. data/lib/contrast/config/logger_configuration.rb +9 -6
  194. data/lib/contrast/config/protect_configuration.rb +9 -6
  195. data/lib/contrast/config/protect_rule_configuration.rb +12 -8
  196. data/lib/contrast/config/protect_rules_configuration.rb +19 -18
  197. data/lib/contrast/config/request_audit_configuration.rb +10 -7
  198. data/lib/contrast/config/root_configuration.rb +29 -12
  199. data/lib/contrast/config/ruby_configuration.rb +14 -11
  200. data/lib/contrast/config/sampling_configuration.rb +11 -8
  201. data/lib/contrast/config/server_configuration.rb +13 -9
  202. data/lib/contrast/config/service_configuration.rb +14 -11
  203. data/lib/contrast/configuration.rb +23 -14
  204. data/lib/contrast/extension/assess/array.rb +1 -1
  205. data/lib/contrast/extension/assess/erb.rb +1 -1
  206. data/lib/contrast/extension/assess/marshal.rb +1 -1
  207. data/lib/contrast/extension/assess/string.rb +1 -1
  208. data/lib/contrast/extension/extension.rb +2 -2
  209. data/lib/contrast/framework/base_support.rb +8 -8
  210. data/lib/contrast/framework/grape/support.rb +3 -3
  211. data/lib/contrast/framework/manager.rb +5 -5
  212. data/lib/contrast/framework/manager_extend.rb +1 -1
  213. data/lib/contrast/framework/rack/patch/session_cookie.rb +1 -1
  214. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +14 -3
  215. data/lib/contrast/framework/rails/patch/assess_configuration.rb +3 -3
  216. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +1 -1
  217. data/lib/contrast/framework/rails/patch/support.rb +14 -46
  218. data/lib/contrast/framework/rails/support.rb +2 -2
  219. data/lib/contrast/framework/sinatra/support.rb +1 -1
  220. data/lib/contrast/logger/aliased_logging.rb +94 -0
  221. data/lib/contrast/logger/application.rb +0 -4
  222. data/lib/contrast/logger/cef_log.rb +14 -14
  223. data/lib/contrast/logger/format.rb +1 -1
  224. data/lib/contrast/logger/log.rb +8 -8
  225. data/lib/contrast/tasks/config.rb +30 -21
  226. data/lib/contrast/tasks/service.rb +2 -2
  227. data/lib/contrast/utils/assess/tracking_util.rb +4 -4
  228. data/lib/contrast/utils/class_util.rb +6 -10
  229. data/lib/contrast/utils/findings.rb +3 -3
  230. data/lib/contrast/utils/hash_digest.rb +6 -7
  231. data/lib/contrast/utils/head_dump_utils_extend.rb +1 -1
  232. data/lib/contrast/utils/invalid_configuration_util.rb +2 -2
  233. data/lib/contrast/utils/log_utils.rb +6 -4
  234. data/lib/contrast/utils/lru_cache.rb +1 -1
  235. data/lib/contrast/utils/metrics_hash.rb +1 -1
  236. data/lib/contrast/utils/middleware_utils.rb +6 -6
  237. data/lib/contrast/utils/net_http_base.rb +4 -4
  238. data/lib/contrast/utils/object_share.rb +1 -1
  239. data/lib/contrast/utils/os.rb +1 -1
  240. data/lib/contrast/utils/patching/policy/patch_utils.rb +2 -2
  241. data/lib/contrast/utils/request_utils.rb +2 -2
  242. data/lib/contrast/utils/sha256_builder.rb +4 -4
  243. data/lib/contrast/utils/stack_trace_utils.rb +31 -13
  244. data/lib/contrast/utils/telemetry.rb +22 -7
  245. data/lib/contrast/utils/telemetry_client.rb +27 -15
  246. data/lib/contrast/utils/telemetry_hash.rb +41 -0
  247. data/lib/contrast/utils/telemetry_identifier.rb +18 -3
  248. data/lib/contrast/utils/timer.rb +1 -1
  249. data/lib/contrast.rb +9 -0
  250. data/resources/assess/policy.json +1 -1
  251. data/ruby-agent.gemspec +1 -1
  252. data/service_executables/VERSION +1 -1
  253. data/service_executables/linux/contrast-service +0 -0
  254. data/service_executables/mac/contrast-service +0 -0
  255. metadata +41 -16
  256. data/lib/contrast/agent/telemetry/events/metric_telemetry_event.rb +0 -26
  257. data/lib/contrast/agent/telemetry/events/startup_metrics_telemetry_event.rb +0 -121
  258. data/lib/contrast/agent/telemetry/events/telemetry_event.rb +0 -33
  259. data/lib/contrast/agent/telemetry/telemetry.rb +0 -150
  260. data/lib/contrast/utils/exclude_key.rb +0 -20
@@ -21,18 +21,23 @@ module Contrast
21
21
  'Contrast Security Protect Rule Triggered. Response blocked.'
22
22
  end
23
23
 
24
+ # @param context [Contrast::Agent::RequestContext]
24
25
  def infilter? context
25
- return false unless context&.speedracer_input_analysis&.results
26
26
  return false unless enabled?
27
+ return false unless context&.speedracer_input_analysis&.results&.any? do |result|
28
+ result.rule_id == rule_name
29
+ end
30
+
27
31
  return false if protect_excluded_by_code?
28
32
 
29
33
  true
30
34
  end
31
35
 
32
36
  # Override for rules that need the response
33
- # Currently postfilter can be applied to streamed responses,
34
- # if any logic within postfilter changes to modify the response
35
- # streamed responses will break
37
+ # Currently postfilter can be applied to streamed responses, if any logic within postfilter changes to modify
38
+ # the response streamed responses will break
39
+ # @param context [Contrast::Agent::RequestContext]
40
+ # @raise [Contrast::SecurityException]
36
41
  def postfilter context
37
42
  return unless enabled? && POSTFILTER_MODES.include?(mode)
38
43
  if mode == Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION ||
@@ -44,27 +49,38 @@ module Contrast
44
49
  result = find_postfilter_attacker(context, nil)
45
50
  return unless result&.samples&.any?
46
51
 
47
- cef_logging result
52
+ cef_logging(result)
48
53
  append_to_activity(context, result)
49
54
  return unless result.response == :BLOCKED
50
55
 
51
- raise Contrast::SecurityException.new(self, "#{ rule_name } triggered in postfilter. Response blocked.")
56
+ raise(Contrast::SecurityException.new(self, "#{ rule_name } triggered in postfilter. Response blocked."))
52
57
  end
53
58
 
54
59
  protected
55
60
 
61
+ # @param context [Contrast::Agent::RequestContext]
62
+ # @return [Array<Contrast::Api::Settings::InputAnalysis>]
56
63
  def gather_ia_results context
57
64
  context.speedracer_input_analysis.results.select do |ia_result|
58
65
  ia_result.rule_id == rule_name
59
66
  end
60
67
  end
61
68
 
69
+ # @param context [Contrast::Agent::RequestContext]
70
+ # @param potential_attack_string [String, nil]
71
+ # @param **kwargs
72
+ # @return [Contrast::Api::Dtm::AttackResult]
62
73
  def find_attacker context, potential_attack_string, **kwargs
63
74
  ia_results = gather_ia_results(context)
64
75
  find_attacker_with_results(context, potential_attack_string, ia_results, **kwargs)
65
76
  end
66
77
 
67
78
  # Allows for the InputAnalysis from service to be extracted early
79
+ # @param context [Contrast::Agent::RequestContext]
80
+ # @param potential_attack_string [String, nil]
81
+ # @param ia_results [Array<Contrast::Api::Settings::InputAnalysis>]
82
+ # @param **kwargs
83
+ # @return [Contrast::Api::Dtm::AttackResult, nil]
68
84
  def find_attacker_with_results context, potential_attack_string, ia_results, **kwargs
69
85
  logger.trace('Checking vectors for attacks', rule: rule_name, input: potential_attack_string)
70
86
 
@@ -84,17 +100,18 @@ module Contrast
84
100
 
85
101
  private
86
102
 
103
+ # @param context [Contrast::Agent::RequestContext]
104
+ # @param potential_attack_string [String, nil]
105
+ # @return [Contrast::Api::Dtm::AttackResult, nil]
87
106
  def find_postfilter_attacker context, potential_attack_string, **kwargs
88
107
  ia_results = gather_ia_results(context)
89
108
  ia_results.select! do |ia_result|
90
- ia_result.score_level == if ia_result.rule_id == Contrast::Agent::Protect::Rule::Sqli::NAME ||
91
- ia_result.rule_id == Contrast::Agent::Protect::Rule::CmdInjection::NAME
92
-
93
- Contrast::Agent::Reporting::ScoreLevel::WORTHWATCHING
94
- else
95
- # legacy implementation for DEFINITEATATACK
96
- Contrast::Api::Settings::InputAnalysisResult::ScoreLevel::DEFINITEATTACK
97
- end
109
+ required_level = if ia_result.cs__is_a?(Contrast::Api::Settings::InputAnalysisResult)
110
+ Contrast::Api::Settings::InputAnalysisResult::ScoreLevel::DEFINITEATTACK
111
+ else
112
+ Contrast::Agent::Reporting::ScoreLevel::DEFINITEATTACK
113
+ end
114
+ ia_result.score_level == required_level
98
115
  end
99
116
  find_attacker_with_results(context, potential_attack_string, ia_results, **kwargs)
100
117
  end
@@ -24,6 +24,18 @@ module Contrast
24
24
  MULTIPART_FIELD_NAME, XML_VALUE, DWR_VALUE
25
25
  ].cs__freeze
26
26
 
27
+ class << self
28
+ # @param attack_sample [Contrast::Api::Dtm::RaspRuleSample]
29
+ # @return [Hash] the details for this specific rule
30
+ def extract_details attack_sample
31
+ {
32
+ command: attack_sample.cmdi.command,
33
+ startIndex: attack_sample.cmdi.start_idx,
34
+ endIndex: attack_sample.cmdi.end_idx
35
+ }
36
+ end
37
+ end
38
+
27
39
  def rule_name
28
40
  NAME
29
41
  end
@@ -46,13 +58,13 @@ module Contrast
46
58
  return unless result
47
59
 
48
60
  append_to_activity(context, result)
49
- cef_logging result, :successful_attack
61
+ cef_logging(result, :successful_attack)
50
62
 
51
63
  return unless blocked?
52
64
 
53
- raise Contrast::SecurityException.new(self,
65
+ raise(Contrast::SecurityException.new(self,
54
66
  'Command Injection rule triggered. '\
55
- "Call to #{ classname }.#{ method } blocked.")
67
+ "Call to #{ classname }.#{ method } blocked."))
56
68
  end
57
69
 
58
70
  def build_attack_with_match context, input_analysis_result, result, candidate_string, **kwargs
@@ -90,6 +102,7 @@ module Contrast
90
102
  command = candidate_string || input_analysis_result.value
91
103
  command = Contrast::Utils::StringUtils.protobuf_safe_string(command)
92
104
  sample.cmdi.command = command
105
+ sample.cmdi.end_idx = command.length
93
106
 
94
107
  # This is a special case where the user input is UNKNOWN_USER_INPUT but
95
108
  # we want to send the attack value
@@ -134,12 +147,6 @@ module Contrast
134
147
  def report_any_command_execution?
135
148
  ::Contrast::PROTECT.report_any_command_execution?
136
149
  end
137
-
138
- def gather_ia_results context
139
- context.agent_input_analysis.results.select do |ia_result|
140
- ia_result.rule_id == rule_name
141
- end
142
- end
143
150
  end
144
151
  end
145
152
  end
@@ -64,15 +64,15 @@ module Contrast
64
64
  #
65
65
  # @return res [Contrast::Agent::Reporting::InputAnalysisResult]
66
66
  def cmdi_create_new_input_result request, rule_id, input_type, value
67
- ia_result = new_ia_result rule_id, input_type, request.path, value
68
- if cmdi_worth_watching? value
67
+ ia_result = new_ia_result(rule_id, input_type, request.path, value)
68
+ if cmdi_worth_watching?(value)
69
69
  ia_result.score_level = WORTHWATCHING
70
70
  ia_result.ids << WORTHWATCHING_MATCH
71
71
  else
72
72
  ia_result.score_level = IGNORE
73
73
  end
74
74
 
75
- add_needed_key request, ia_result, input_type, value if CMDI_KEYS_NEEDED.include? input_type
75
+ add_needed_key(request, ia_result, input_type, value) if CMDI_KEYS_NEEDED.include?(input_type)
76
76
  ia_result
77
77
  end
78
78
  end
@@ -7,6 +7,8 @@
7
7
  # @deprecated RUBY-356: this class and those that extend it are being phased out
8
8
  # in favor of the more performant code in the Service.
9
9
  class Contrast::Agent::Protect::Rule::DefaultScanner # rubocop:disable Style/ClassAndModuleChildren
10
+ OPERATOR_PATTERN = %r{[+=*^/%><!-]}.cs__freeze
11
+
10
12
  # Potential states
11
13
  # :STATE_INSIDE_TOKEN
12
14
  # :STATE_INSIDE_NUMBER
@@ -220,7 +222,6 @@ class Contrast::Agent::Protect::Rule::DefaultScanner # rubocop:disable Style/Cla
220
222
  idx
221
223
  end
222
224
 
223
- OPERATOR_PATTERN = %r{[+=*^/%><!-]}.cs__freeze
224
225
  def operator? char
225
226
  char.match?(OPERATOR_PATTERN)
226
227
  end
@@ -13,6 +13,9 @@ module Contrast
13
13
  class Deserialization < Contrast::Agent::Protect::Rule::Base
14
14
  # The TeamServer recognized name of this rule
15
15
  include Contrast::Components::Logger::InstanceMethods
16
+ # Used to name this input since input analysis isn't done for this
17
+ # rule
18
+ INPUT_NAME = 'Serialized Gadget'
16
19
 
17
20
  NAME = 'untrusted-deserialization'
18
21
 
@@ -39,6 +42,17 @@ module Contrast
39
42
  # Used to indicate to TeamServer the gadget is an Arel module
40
43
  AREL = 'Arel'
41
44
 
45
+ class << self
46
+ # @param attack_sample [Contrast::Api::Dtm::RaspRuleSample]
47
+ # @return [Hash] the details for this specific rule
48
+ def extract_details attack_sample
49
+ {
50
+ command: attack_sample.untrusted_deserialization.command,
51
+ deserializer: attack_sample.untrusted_deserialization.deserializer
52
+ }
53
+ end
54
+ end
55
+
42
56
  # Return the TeamServer understood id / name of this rule.
43
57
  # @return [String] the TeamServer understood id / name of this rule.
44
58
  def rule_name
@@ -81,9 +95,9 @@ module Contrast
81
95
  result = build_attack_with_match(context, ia_result, nil, serialized_input, **kwargs)
82
96
  append_to_activity(context, result)
83
97
 
84
- cef_logging result, :successful_attack
98
+ cef_logging(result, :successful_attack)
85
99
 
86
- raise Contrast::SecurityException.new(self, block_message) if blocked?
100
+ raise(Contrast::SecurityException.new(self, block_message)) if blocked?
87
101
  end
88
102
 
89
103
  # Determine if the issued command was called while we're
@@ -102,8 +116,8 @@ module Contrast
102
116
  ia_result = build_evaluation(gadget_command)
103
117
  result = build_attack_with_match(context, ia_result, nil, gadget_command, **kwargs)
104
118
  append_to_activity(context, result)
105
- cef_logging result, :successful_attack, gadget_command
106
- raise Contrast::SecurityException.new(self, BLOCK_MESSAGE) if blocked?
119
+ cef_logging(result, :successful_attack, gadget_command)
120
+ raise(Contrast::SecurityException.new(self, BLOCK_MESSAGE)) if blocked?
107
121
  end
108
122
 
109
123
  protected
@@ -134,9 +148,6 @@ module Contrast
134
148
 
135
149
  private
136
150
 
137
- # Used to name this input since input analysis isn't done for this
138
- # rule
139
- INPUT_NAME = 'Serialized Gadget'
140
151
  # We know that this attack happened, so the result is always matched
141
152
  # and the level is always critical. Only variable is the Gadget
142
153
  # supplied by the attacker.
@@ -15,80 +15,80 @@ module Contrast
15
15
  # as a result input would be marked as DEFINETEATTACK or IGNORE,
16
16
  # to be analyzed at the sink level.
17
17
  module HttpMethodTamperingInputClassification
18
- class << self
19
- include InputClassificationBase
20
-
21
- # This method will determine actually if the user input is DEFINITEATTACK or IGNORE
22
- #
23
- # @param input_type [Contrast::Agent::Reporting::InputType] the type of the user input
24
- # @param input_analysis [Contrast::Agent::Reporting::InputAnalysis] Holds all the results from the input
25
- # analysis from the current request.
26
- def classify input_type, input_analysis
27
- return unless input_analysis.request
28
- return unless input_type == METHOD
29
-
30
- rule_id = Contrast::Agent::Protect::Rule::HttpMethodTampering::NAME
31
-
32
- ia_result = method_tampering_new_input_analysis(input_analysis.request, rule_id, input_type)
33
- input_analysis.results << ia_result
34
-
35
- return input_analysis if ia_result.score_level == IGNORE
36
-
37
- attack_result = build_attack_result ia_result, rule_id
38
-
39
- if Contrast::Api::Settings::ProtectionRule::Mode::BLOCK != Contrast::PROTECT.rule_mode(rule_id)
40
- attack_result.response = :EXPLOITED
41
- Contrast::Agent::EXPLOITS.push attack_result
42
- return input_analysis
43
- end
44
-
45
- attack_result.response = :BLOCKED
46
- context.activity.results << attack_result
47
- raise Contrast::SecurityException.new(self,
48
- 'HTTP Method Tampering rule triggered. '\
49
- "Call to #{ input_analysis.request.path } with " \
50
- "#{ input_analysis.request.request_method } blocked.")
51
- end
52
-
53
- private
54
-
55
- # @param request [Contrast::Agent::Request] the current request context.
56
- def method_tampering_exploited? request
57
- !Contrast::Agent::Protect::Rule::HttpMethodTampering::APPLICABLE_METHODS_INPUTS.include?(request.request_method) # rubocop:disable Layout/LineLength
58
- end
59
-
60
- # This methods checks if input is tagged DEFINITEATTACK or IGNORE matches value with it's
61
- # key if needed and Creates new instance of InputAnalysisResult.
62
- #
63
- # @param request [Contrast::Agent::Request] the current request context.
64
- # @param rule_id [String] The name of the Protect Rule.
65
- # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
66
- #
67
- # @return res [Contrast::Agent::Reporting::InputAnalysisResult]
68
- def method_tampering_new_input_analysis request, rule_id, input_type
69
- ia_result = new_ia_result rule_id, input_type, request.path
70
- if method_tampering_exploited? request
71
- ia_result.score_level = DEFINITEATTACK
72
- ia_result.ids << rule_id
73
- else
74
- ia_result.score_level = IGNORE
75
- end
76
-
77
- ia_result
78
- end
79
-
80
- def build_attack_result ia_result, rule_id
81
- rasp_rule_sample = Contrast::Agent::Reporting::RaspRuleSample.new.build context, ia_result
82
- result = Contrast::Agent::Reporting::AttackResult.new
83
- result.rule_id = rule_id
84
- result.samples << rasp_rule_sample
85
- result
86
- end
87
-
88
- def context
89
- Contrast::Agent::REQUEST_TRACKER.current
90
- end
91
- end
18
+ # class << self
19
+ # include InputClassificationBase
20
+ #
21
+ # # This method will determine actually if the user input is DEFINITEATTACK or IGNORE
22
+ # #
23
+ # # @param input_type [Contrast::Agent::Reporting::InputType] the type of the user input
24
+ # # @param input_analysis [Contrast::Agent::Reporting::InputAnalysis] Holds all the results from the input
25
+ # # analysis from the current request.
26
+ # def classify input_type, input_analysis
27
+ # return unless input_analysis.request
28
+ # return unless input_type == METHOD
29
+ #
30
+ # rule_id = Contrast::Agent::Protect::Rule::HttpMethodTampering::NAME
31
+ #
32
+ # ia_result = method_tampering_new_input_analysis(input_analysis.request, rule_id, input_type)
33
+ # input_analysis.results << ia_result
34
+ #
35
+ # return input_analysis if ia_result.score_level == IGNORE
36
+ #
37
+ # attack_result = build_attack_result ia_result, rule_id
38
+ #
39
+ # if Contrast::Api::Settings::ProtectionRule::Mode::BLOCK != Contrast::PROTECT.rule_mode(rule_id)
40
+ # attack_result.response = :EXPLOITED
41
+ # Contrast::Agent::EXPLOITS.push attack_result
42
+ # return input_analysis
43
+ # end
44
+ #
45
+ # attack_result.response = :BLOCKED
46
+ # context.activity.results << attack_result
47
+ # raise Contrast::SecurityException.new(self,
48
+ # 'HTTP Method Tampering rule triggered. '\
49
+ # "Call to #{ input_analysis.request.path } with " \
50
+ # "#{ input_analysis.request.request_method } blocked.")
51
+ # end
52
+ #
53
+ # private
54
+ #
55
+ # # @param request [Contrast::Agent::Request] the current request context.
56
+ # def method_tampering_exploited? request
57
+ # !Contrast::Agent::Protect::Rule::HttpMethodTampering::APPLICABLE_METHODS_INPUTS.include?(request.request_method) # rubocop:disable Layout/LineLength
58
+ # end
59
+ #
60
+ # # This methods checks if input is tagged DEFINITEATTACK or IGNORE matches value with it's
61
+ # # key if needed and Creates new instance of InputAnalysisResult.
62
+ # #
63
+ # # @param request [Contrast::Agent::Request] the current request context.
64
+ # # @param rule_id [String] The name of the Protect Rule.
65
+ # # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
66
+ # #
67
+ # # @return res [Contrast::Agent::Reporting::InputAnalysisResult]
68
+ # def method_tampering_new_input_analysis request, rule_id, input_type
69
+ # ia_result = new_ia_result rule_id, input_type, request.path
70
+ # if method_tampering_exploited? request
71
+ # ia_result.score_level = DEFINITEATTACK
72
+ # ia_result.ids << rule_id
73
+ # else
74
+ # ia_result.score_level = IGNORE
75
+ # end
76
+ #
77
+ # ia_result
78
+ # end
79
+ #
80
+ # def build_attack_result ia_result, rule_id
81
+ # rasp_rule_sample = Contrast::Agent::Reporting::RaspRuleSample.new.build context, ia_result
82
+ # result = Contrast::Agent::Reporting::AttackResult.new
83
+ # result.rule_id = rule_id
84
+ # result.samples << rasp_rule_sample
85
+ # result
86
+ # end
87
+ #
88
+ # def context
89
+ # Contrast::Agent::REQUEST_TRACKER.current
90
+ # end
91
+ # end
92
92
  end
93
93
  end
94
94
  end
@@ -10,65 +10,83 @@ module Contrast
10
10
  # The Ruby implementation of the Protect HTTP Method Tampering rule.
11
11
  class HttpMethodTampering < Contrast::Agent::Protect::Rule::BaseService
12
12
  NAME = 'method-tampering'
13
- STANDARD_METHODS = %w[GET HEAD POST PUT DELETE CONNECT OPTIONS TRACE PATCH].cs__freeze
13
+ # STANDARD_METHODS = %w[GET HEAD POST PUT DELETE CONNECT OPTIONS TRACE PATCH].cs__freeze
14
+ #
15
+ # APPLICABLE_METHODS_INPUTS = %w[
16
+ # ACL BASELINE-CONTROL CHECKIN CHECKOUT CONNECT COPY
17
+ # DELETE GET HEAD LABEL LOCK MERGE MKACTIVITY MKCALENDAR
18
+ # MKCOL MKWORKSPACE MOVE OPTIONS ORDERPATCH PATCH POST
19
+ # PROPFIND PROPPATCH PUT REPORT SEARCH TRACE UNCHECKOUT
20
+ # UNLOCK UPDATE VERSION-CONTROL
21
+ # ].cs__freeze
14
22
 
15
- APPLICABLE_METHODS_INPUTS = %w[
16
- ACL BASELINE-CONTROL CHECKIN CHECKOUT CONNECT COPY
17
- DELETE GET HEAD LABEL LOCK MERGE MKACTIVITY MKCALENDAR
18
- MKCOL MKWORKSPACE MOVE OPTIONS ORDERPATCH PATCH POST
19
- PROPFIND PROPPATCH PUT REPORT SEARCH TRACE UNCHECKOUT
20
- UNLOCK UPDATE VERSION-CONTROL
21
- ].cs__freeze
23
+ class << self
24
+ # @param attack_sample [Contrast::Api::Dtm::RaspRuleSample]
25
+ # @return [Hash] the details for this specific rule
26
+ def extract_details attack_sample
27
+ {
28
+ method: attack_sample.method_tampering.method, # rubocop:disable Security/Object/Method
29
+ responseCode: attack_sample.method_tampering.response_code
30
+ }
31
+ end
32
+ end
22
33
 
23
34
  def rule_name
24
35
  NAME
25
36
  end
26
37
 
27
- def postfilter context
28
- return unless enabled? && POSTFILTER_MODES.include?(mode)
29
- return if normal_request?(context)
30
-
31
- # The only way to be here in postfilter with a result is if the rule mode was MONITOR
32
- ia_results = gather_ia_results(context)
33
- return if ia_results.empty?
34
-
35
- # does the status code start with 4 or 5? Rails responds with 404 (but java is checking 501)
36
- response_code = context&.response&.response_code
37
- return unless response_code
38
-
39
- method = ia_results.first.value
40
- result = if response_code.to_s.start_with?('4', '5')
41
- build_attack_without_match(context, nil, nil, method: method, response_code: response_code)
42
- else
43
- build_attack_with_match(context, nil, nil, nil, method: method, response_code: response_code)
44
- end
45
-
46
- return unless result
47
-
48
- append_to_activity(context, result)
49
- cef_logging result, :ineffective_attack
50
- end
51
-
52
- protected
53
-
54
- def build_sample context, evaluation, _candidate_string, **kwargs
55
- sample = build_base_sample(context, evaluation)
56
- sample.user_input.value = kwargs[:method]
57
- sample.user_input.input_type = :METHOD
58
-
59
- sample.method_tampering = Contrast::Api::Dtm::HttpMethodTamperingDetails.new
60
- sample.method_tampering.method = Contrast::Utils::StringUtils.protobuf_safe_string(kwargs[:method])
61
- code = kwargs[:response_code] || -1
62
- sample.method_tampering.response_code = code.to_i
63
- sample
64
- end
65
-
66
- private
67
-
68
- def normal_request? context
69
- method = context.request.request_method
70
- context.request.static? || method.nil? || STANDARD_METHODS.include?(method.upcase)
71
- end
38
+ # This rule is solely based on input analysis, which the Service handles. When we move from the Service to the
39
+ # agent with protect library, we should re-enable these tests and that rule.
40
+ # TODO: RUBY-1574
41
+ # def enabled?
42
+ # super && false
43
+ # end
44
+ #
45
+ # def postfilter context
46
+ # return unless enabled? && POSTFILTER_MODES.include?(mode)
47
+ # return if normal_request?(context)
48
+ #
49
+ # # The only way to be here in postfilter with a result is if the rule mode was MONITOR
50
+ # ia_results = gather_ia_results(context)
51
+ # return if ia_results.empty?
52
+ #
53
+ # # does the status code start with 4 or 5? Rails responds with 404 (but java is checking 501)
54
+ # response_code = context&.response&.response_code
55
+ # return unless response_code
56
+ #
57
+ # method = ia_results.first.value
58
+ # result = if response_code.to_s.start_with?('4', '5')
59
+ # build_attack_without_match(context, nil, nil, method: method, response_code: response_code)
60
+ # else
61
+ # build_attack_with_match(context, nil, nil, nil, method: method, response_code: response_code)
62
+ # end
63
+ #
64
+ # return unless result
65
+ #
66
+ # append_to_activity(context, result)
67
+ # cef_logging result, :ineffective_attack
68
+ # end
69
+ #
70
+ # protected
71
+ #
72
+ # def build_sample context, evaluation, _candidate_string, **kwargs
73
+ # sample = build_base_sample(context, evaluation)
74
+ # sample.user_input.value = kwargs[:method]
75
+ # sample.user_input.input_type = :METHOD
76
+ #
77
+ # sample.method_tampering = Contrast::Api::Dtm::HttpMethodTamperingDetails.new
78
+ # sample.method_tampering.method = Contrast::Utils::StringUtils.protobuf_safe_string(kwargs[:method])
79
+ # code = kwargs[:response_code] || -1
80
+ # sample.method_tampering.response_code = code.to_i
81
+ # sample
82
+ # end
83
+ #
84
+ # private
85
+ #
86
+ # def normal_request? context
87
+ # method = context.request.request_method
88
+ # context.request.static? || method.nil? || STANDARD_METHODS.include?(method.upcase)
89
+ # end
72
90
  end
73
91
  end
74
92
  end
@@ -106,9 +106,9 @@ module Contrast
106
106
  score = evaluate_patterns(value)
107
107
  score = evaluate_rules(value, score)
108
108
 
109
- score_level = if definite_attack? score
109
+ score_level = if definite_attack?(score)
110
110
  DEFINITEATTACK
111
- elsif worth_watching? score
111
+ elsif worth_watching?(score)
112
112
  WORTHWATCHING
113
113
  else
114
114
  IGNORE
@@ -185,7 +185,7 @@ module Contrast
185
185
  def evaluate_or_rule value
186
186
  score = 0
187
187
 
188
- locs = matches_by_position value, NOSQL_OR_REGEXP
188
+ locs = matches_by_position(value, NOSQL_OR_REGEXP)
189
189
  return score if locs.empty?
190
190
 
191
191
  locs.each do |loc|
@@ -18,6 +18,19 @@ module Contrast
18
18
 
19
19
  NAME = 'nosql-injection'
20
20
  BLOCK_MESSAGE = 'NoSQLi rule triggered. Response blocked.'
21
+ class << self
22
+ # @param attack_sample [Contrast::Api::Dtm::RaspRuleSample]
23
+ # @return [Hash] the details for this specific rule
24
+ def extract_details attack_sample
25
+ {
26
+ start: attack_sample.no_sqli.start_idx,
27
+ end: attack_sample.no_sqli.end_idx,
28
+ boundaryOverrunIndex: attack_sample.no_sqli.boundary_overrun_idx,
29
+ inputBoundaryIndex: attack_sample.no_sqli.input_boundary_idx,
30
+ query: attack_sample.no_sqli.query
31
+ }
32
+ end
33
+ end
21
34
 
22
35
  def rule_name
23
36
  NAME
@@ -35,8 +48,8 @@ module Contrast
35
48
 
36
49
  append_to_activity(context, result)
37
50
 
38
- cef_logging result, :successful_attack
39
- raise Contrast::SecurityException.new(self, BLOCK_MESSAGE) if blocked?
51
+ cef_logging(result, :successful_attack)
52
+ raise(Contrast::SecurityException.new(self, BLOCK_MESSAGE)) if blocked?
40
53
  end
41
54
 
42
55
  def build_attack_with_match context, input_analysis_result, result, candidate_string, **kwargs
@@ -66,20 +79,6 @@ module Contrast
66
79
  end
67
80
  super(context, potential_attack_string, **kwargs)
68
81
  end
69
-
70
- def infilter? context
71
- return false unless context&.agent_input_analysis&.results
72
- return false unless enabled?
73
- return false if protect_excluded_by_code?
74
-
75
- true
76
- end
77
-
78
- def gather_ia_results context
79
- context.agent_input_analysis.results.select do |ia_result|
80
- ia_result.rule_id == rule_name
81
- end
82
- end
83
82
  end
84
83
  end
85
84
  end
@@ -25,6 +25,16 @@ module Contrast
25
25
  /windows/repair/
26
26
  ].cs__freeze
27
27
 
28
+ class << self
29
+ # @param attack_sample [Contrast::Api::Dtm::RaspRuleSample]
30
+ # @return [Hash] the details for this specific rule
31
+ def extract_details attack_sample
32
+ {
33
+ path: attack_sample.path_traversal.path
34
+ }
35
+ end
36
+ end
37
+
28
38
  def rule_name
29
39
  NAME
30
40
  end
@@ -38,9 +48,9 @@ module Contrast
38
48
  append_to_activity(context, result)
39
49
  return unless blocked?
40
50
 
41
- cef_logging result, :successful_attack, path
42
- raise Contrast::SecurityException.new(self,
43
- "Path Traversal rule triggered. Call to File.#{ method } blocked.")
51
+ cef_logging(result, :successful_attack, path)
52
+ raise(Contrast::SecurityException.new(self,
53
+ "Path Traversal rule triggered. Call to File.#{ method } blocked."))
44
54
  end
45
55
 
46
56
  protected