contrast-agent 6.7.0 → 6.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (280) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -2
  3. data/.simplecov +0 -1
  4. data/Rakefile +0 -1
  5. data/ext/cs__assess_array/cs__assess_array.c +41 -10
  6. data/ext/cs__assess_array/cs__assess_array.h +4 -1
  7. data/lib/contrast/agent/assess/policy/trigger_method.rb +3 -3
  8. data/lib/contrast/agent/assess/policy/trigger_validation/redos_validator.rb +1 -1
  9. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +1 -1
  10. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +1 -1
  11. data/lib/contrast/agent/assess/property/evented.rb +11 -11
  12. data/lib/contrast/agent/assess.rb +0 -1
  13. data/lib/contrast/agent/excluder.rb +53 -35
  14. data/lib/contrast/agent/exclusion_matcher.rb +21 -9
  15. data/lib/contrast/agent/middleware.rb +12 -6
  16. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +6 -0
  17. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +146 -127
  18. data/lib/contrast/agent/protect/input_analyzer/worth_watching_analyzer.rb +116 -0
  19. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +20 -0
  20. data/lib/contrast/agent/protect/policy/rule_applicator.rb +1 -1
  21. data/lib/contrast/agent/protect/rule/base.rb +47 -55
  22. data/lib/contrast/agent/protect/rule/base_service.rb +48 -24
  23. data/lib/contrast/agent/protect/rule/bot_blocker/bot_blocker_input_classification.rb +98 -0
  24. data/lib/contrast/agent/protect/rule/bot_blocker.rb +81 -0
  25. data/lib/contrast/agent/protect/rule/cmd_injection.rb +20 -2
  26. data/lib/contrast/agent/protect/rule/cmdi/cmdi_backdoors.rb +8 -5
  27. data/lib/contrast/agent/protect/rule/cmdi/cmdi_base_rule.rb +22 -22
  28. data/lib/contrast/agent/protect/rule/cmdi/cmdi_chained_command.rb +64 -0
  29. data/lib/contrast/agent/protect/rule/cmdi/cmdi_dangerous_path.rb +63 -0
  30. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +2 -58
  31. data/lib/contrast/agent/protect/rule/default_scanner.rb +1 -1
  32. data/lib/contrast/agent/protect/rule/deserialization.rb +3 -14
  33. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +2 -2
  34. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +0 -11
  35. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +29 -34
  36. data/lib/contrast/agent/protect/rule/no_sqli.rb +25 -18
  37. data/lib/contrast/agent/protect/rule/path_traversal/path_traversal_input_classification.rb +61 -0
  38. data/lib/contrast/agent/protect/rule/path_traversal/path_traversal_semantic_security_bypass.rb +114 -0
  39. data/lib/contrast/agent/protect/rule/path_traversal.rb +40 -13
  40. data/lib/contrast/agent/protect/rule/sql_sample_builder.rb +33 -15
  41. data/lib/contrast/agent/protect/rule/sqli/sqli_base_rule.rb +0 -14
  42. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +2 -62
  43. data/lib/contrast/agent/protect/rule/sqli.rb +74 -3
  44. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +39 -63
  45. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +6 -33
  46. data/lib/contrast/agent/protect/rule/xss/reflected_xss_input_classification.rb +58 -0
  47. data/lib/contrast/agent/protect/rule/xss.rb +15 -20
  48. data/lib/contrast/agent/protect/rule/xxe.rb +4 -24
  49. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +19 -40
  50. data/lib/contrast/agent/reporting/attack_result/response_type.rb +9 -9
  51. data/lib/contrast/agent/reporting/details/ip_denylist_details.rb +10 -2
  52. data/lib/contrast/agent/reporting/details/virtual_patch_details.rb +8 -2
  53. data/lib/contrast/agent/reporting/input_analysis/details/bot_blocker_details.rb +27 -0
  54. data/lib/contrast/agent/reporting/input_analysis/details/protect_rule_details.rb +15 -0
  55. data/lib/contrast/agent/reporting/input_analysis/input_analysis.rb +1 -2
  56. data/lib/contrast/agent/reporting/input_analysis/input_analysis_result.rb +16 -2
  57. data/lib/contrast/agent/reporting/masker/masker.rb +2 -0
  58. data/lib/contrast/agent/reporting/report.rb +1 -0
  59. data/lib/contrast/agent/reporting/reporter.rb +35 -14
  60. data/lib/contrast/agent/reporting/reporter_heartbeat.rb +3 -9
  61. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +16 -13
  62. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +12 -7
  63. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +3 -3
  64. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +1 -2
  65. data/lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb +6 -1
  66. data/lib/contrast/agent/reporting/reporting_events/application_update.rb +0 -2
  67. data/lib/contrast/agent/reporting/reporting_events/architecture_component.rb +0 -1
  68. data/lib/contrast/agent/reporting/reporting_events/finding.rb +6 -6
  69. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +239 -93
  70. data/lib/contrast/agent/reporting/reporting_events/finding_event_signature.rb +10 -23
  71. data/lib/contrast/agent/reporting/reporting_events/finding_event_source.rb +10 -9
  72. data/lib/contrast/agent/reporting/reporting_events/finding_request.rb +0 -5
  73. data/lib/contrast/agent/reporting/reporting_events/library_discovery.rb +0 -1
  74. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +12 -0
  75. data/lib/contrast/agent/reporting/reporting_events/poll.rb +1 -11
  76. data/lib/contrast/agent/reporting/reporting_events/route_discovery.rb +0 -1
  77. data/lib/contrast/agent/reporting/reporting_events/route_discovery_observation.rb +0 -1
  78. data/lib/contrast/agent/reporting/reporting_events/server_reporting_event.rb +8 -0
  79. data/lib/contrast/agent/reporting/reporting_events/server_settings.rb +40 -0
  80. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +2 -2
  81. data/lib/contrast/agent/reporting/reporting_utilities/endpoints.rb +6 -0
  82. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +43 -1
  83. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +8 -4
  84. data/lib/contrast/agent/reporting/reporting_utilities/response.rb +1 -1
  85. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +58 -4
  86. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +4 -6
  87. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +77 -16
  88. data/lib/contrast/agent/reporting/server_settings_worker.rb +44 -0
  89. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +14 -2
  90. data/lib/contrast/agent/reporting/settings/code_exclusion.rb +6 -1
  91. data/lib/contrast/agent/reporting/settings/exclusion_base.rb +18 -0
  92. data/lib/contrast/agent/reporting/settings/exclusions.rb +2 -1
  93. data/lib/contrast/agent/reporting/settings/helpers.rb +7 -0
  94. data/lib/contrast/agent/reporting/settings/input_exclusion.rb +9 -3
  95. data/lib/contrast/agent/reporting/settings/protect.rb +15 -15
  96. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +39 -2
  97. data/lib/contrast/agent/reporting/settings/rule_definition.rb +3 -0
  98. data/lib/contrast/agent/reporting/settings/security_logger.rb +77 -0
  99. data/lib/contrast/agent/reporting/settings/server_features.rb +9 -0
  100. data/lib/contrast/agent/reporting/settings/syslog.rb +34 -5
  101. data/lib/contrast/agent/request.rb +3 -14
  102. data/lib/contrast/agent/request_context.rb +6 -9
  103. data/lib/contrast/agent/request_context_extend.rb +9 -148
  104. data/lib/contrast/agent/request_handler.rb +5 -10
  105. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +1 -1
  106. data/lib/contrast/agent/thread_watcher.rb +37 -18
  107. data/lib/contrast/agent/version.rb +1 -1
  108. data/lib/contrast/agent.rb +6 -11
  109. data/lib/contrast/agent_lib/api/command_injection.rb +46 -0
  110. data/lib/contrast/agent_lib/api/init.rb +101 -0
  111. data/lib/contrast/agent_lib/api/input_tracing.rb +267 -0
  112. data/lib/contrast/agent_lib/api/method_tempering.rb +29 -0
  113. data/lib/contrast/agent_lib/api/panic.rb +87 -0
  114. data/lib/contrast/agent_lib/api/path_semantic_file_security_bypass.rb +40 -0
  115. data/lib/contrast/agent_lib/interface.rb +260 -0
  116. data/lib/contrast/agent_lib/interface_base.rb +118 -0
  117. data/lib/contrast/agent_lib/return_types/eval_result.rb +44 -0
  118. data/lib/contrast/agent_lib/test.rb +29 -0
  119. data/lib/contrast/api/communication/connection_status.rb +20 -5
  120. data/lib/contrast/components/agent.rb +34 -14
  121. data/lib/contrast/components/api.rb +23 -0
  122. data/lib/contrast/components/app_context.rb +23 -5
  123. data/lib/contrast/components/app_context_extend.rb +0 -25
  124. data/lib/contrast/components/assess.rb +34 -4
  125. data/lib/contrast/components/assess_rules.rb +18 -0
  126. data/lib/contrast/components/base.rb +40 -0
  127. data/lib/contrast/components/config/sources.rb +95 -0
  128. data/lib/contrast/components/config.rb +19 -19
  129. data/lib/contrast/components/heap_dump.rb +10 -0
  130. data/lib/contrast/components/inventory.rb +15 -2
  131. data/lib/contrast/components/logger.rb +18 -0
  132. data/lib/contrast/components/polling.rb +36 -0
  133. data/lib/contrast/components/protect.rb +52 -2
  134. data/lib/contrast/components/ruby_component.rb +16 -1
  135. data/lib/contrast/components/sampling.rb +70 -13
  136. data/lib/contrast/components/security_logger.rb +13 -0
  137. data/lib/contrast/components/settings.rb +105 -90
  138. data/lib/contrast/config/certification_configuration.rb +14 -0
  139. data/lib/contrast/config/config.rb +46 -0
  140. data/lib/contrast/config/diagnostics.rb +114 -0
  141. data/lib/contrast/config/diagnostics_tools.rb +98 -0
  142. data/lib/contrast/config/effective_config.rb +65 -0
  143. data/lib/contrast/config/effective_config_value.rb +32 -0
  144. data/lib/contrast/config/exception_configuration.rb +12 -0
  145. data/lib/contrast/config/protect_rule_configuration.rb +8 -8
  146. data/lib/contrast/config/protect_rules_configuration.rb +23 -60
  147. data/lib/contrast/config/request_audit_configuration.rb +13 -0
  148. data/lib/contrast/config/server_configuration.rb +41 -2
  149. data/lib/contrast/configuration.rb +29 -12
  150. data/lib/contrast/extension/assess/array.rb +9 -0
  151. data/lib/contrast/extension/assess/erb.rb +1 -1
  152. data/lib/contrast/extension/delegator.rb +2 -0
  153. data/lib/contrast/framework/manager.rb +3 -1
  154. data/lib/contrast/framework/rails/railtie.rb +0 -1
  155. data/lib/contrast/framework/rails/support.rb +0 -1
  156. data/lib/contrast/tasks/config.rb +1 -8
  157. data/lib/contrast/utils/assess/event_limit_utils.rb +31 -9
  158. data/lib/contrast/utils/assess/trigger_method_utils.rb +5 -4
  159. data/lib/contrast/utils/duck_utils.rb +1 -0
  160. data/lib/contrast/utils/hash_digest.rb +2 -2
  161. data/lib/contrast/utils/input_classification_base.rb +155 -0
  162. data/lib/contrast/utils/os.rb +0 -20
  163. data/lib/contrast/utils/reporting/application_activity_batch_utils.rb +81 -0
  164. data/lib/contrast/utils/response_utils.rb +0 -16
  165. data/lib/contrast/utils/routes_sent.rb +60 -0
  166. data/lib/contrast/utils/stack_trace_utils.rb +3 -15
  167. data/lib/contrast/utils/string_utils.rb +10 -7
  168. data/lib/contrast/utils/telemetry_client.rb +1 -2
  169. data/lib/contrast/utils/timer.rb +16 -0
  170. data/lib/contrast.rb +5 -4
  171. data/resources/protect/policy.json +1 -2
  172. data/ruby-agent.gemspec +7 -6
  173. metadata +69 -130
  174. data/exe/contrast_service +0 -23
  175. data/lib/contrast/agent/assess/contrast_event.rb +0 -157
  176. data/lib/contrast/agent/assess/events/event_factory.rb +0 -34
  177. data/lib/contrast/agent/assess/events/source_event.rb +0 -46
  178. data/lib/contrast/agent/protect/rule/cmdi/cmdi_worth_watching.rb +0 -64
  179. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +0 -118
  180. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +0 -45
  181. data/lib/contrast/agent/reaction_processor.rb +0 -47
  182. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +0 -36
  183. data/lib/contrast/agent/service_heartbeat.rb +0 -35
  184. data/lib/contrast/api/communication/messaging_queue.rb +0 -128
  185. data/lib/contrast/api/communication/response_processor.rb +0 -90
  186. data/lib/contrast/api/communication/service_lifecycle.rb +0 -77
  187. data/lib/contrast/api/communication/socket.rb +0 -44
  188. data/lib/contrast/api/communication/socket_client.rb +0 -130
  189. data/lib/contrast/api/communication/speedracer.rb +0 -138
  190. data/lib/contrast/api/communication/tcp_socket.rb +0 -32
  191. data/lib/contrast/api/communication/unix_socket.rb +0 -28
  192. data/lib/contrast/api/communication.rb +0 -20
  193. data/lib/contrast/api/decorators/address.rb +0 -59
  194. data/lib/contrast/api/decorators/agent_startup.rb +0 -56
  195. data/lib/contrast/api/decorators/application_settings.rb +0 -43
  196. data/lib/contrast/api/decorators/application_startup.rb +0 -56
  197. data/lib/contrast/api/decorators/bot_blocker.rb +0 -37
  198. data/lib/contrast/api/decorators/http_request.rb +0 -137
  199. data/lib/contrast/api/decorators/input_analysis.rb +0 -18
  200. data/lib/contrast/api/decorators/instrumentation_mode.rb +0 -35
  201. data/lib/contrast/api/decorators/ip_denylist.rb +0 -37
  202. data/lib/contrast/api/decorators/message.rb +0 -67
  203. data/lib/contrast/api/decorators/rasp_rule_sample.rb +0 -52
  204. data/lib/contrast/api/decorators/response_type.rb +0 -17
  205. data/lib/contrast/api/decorators/server_features.rb +0 -25
  206. data/lib/contrast/api/decorators/user_input.rb +0 -51
  207. data/lib/contrast/api/decorators/virtual_patch.rb +0 -34
  208. data/lib/contrast/api/decorators.rb +0 -22
  209. data/lib/contrast/api/dtm.pb.rb +0 -363
  210. data/lib/contrast/api/settings.pb.rb +0 -500
  211. data/lib/contrast/api.rb +0 -16
  212. data/lib/contrast/components/contrast_service.rb +0 -88
  213. data/lib/contrast/components/service.rb +0 -55
  214. data/lib/contrast/tasks/service.rb +0 -84
  215. data/lib/contrast/utils/input_classification.rb +0 -73
  216. data/lib/protobuf/code_generator.rb +0 -129
  217. data/lib/protobuf/decoder.rb +0 -28
  218. data/lib/protobuf/deprecation.rb +0 -117
  219. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +0 -79
  220. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +0 -360
  221. data/lib/protobuf/descriptors.rb +0 -3
  222. data/lib/protobuf/encoder.rb +0 -11
  223. data/lib/protobuf/enum.rb +0 -365
  224. data/lib/protobuf/exceptions.rb +0 -9
  225. data/lib/protobuf/field/base_field.rb +0 -380
  226. data/lib/protobuf/field/base_field_object_definitions.rb +0 -504
  227. data/lib/protobuf/field/bool_field.rb +0 -64
  228. data/lib/protobuf/field/bytes_field.rb +0 -67
  229. data/lib/protobuf/field/double_field.rb +0 -25
  230. data/lib/protobuf/field/enum_field.rb +0 -56
  231. data/lib/protobuf/field/field_array.rb +0 -102
  232. data/lib/protobuf/field/field_hash.rb +0 -122
  233. data/lib/protobuf/field/fixed32_field.rb +0 -25
  234. data/lib/protobuf/field/fixed64_field.rb +0 -28
  235. data/lib/protobuf/field/float_field.rb +0 -43
  236. data/lib/protobuf/field/int32_field.rb +0 -21
  237. data/lib/protobuf/field/int64_field.rb +0 -34
  238. data/lib/protobuf/field/integer_field.rb +0 -23
  239. data/lib/protobuf/field/message_field.rb +0 -51
  240. data/lib/protobuf/field/sfixed32_field.rb +0 -27
  241. data/lib/protobuf/field/sfixed64_field.rb +0 -28
  242. data/lib/protobuf/field/signed_integer_field.rb +0 -29
  243. data/lib/protobuf/field/sint32_field.rb +0 -21
  244. data/lib/protobuf/field/sint64_field.rb +0 -21
  245. data/lib/protobuf/field/string_field.rb +0 -51
  246. data/lib/protobuf/field/uint32_field.rb +0 -21
  247. data/lib/protobuf/field/uint64_field.rb +0 -21
  248. data/lib/protobuf/field/varint_field.rb +0 -77
  249. data/lib/protobuf/field.rb +0 -74
  250. data/lib/protobuf/generators/base.rb +0 -85
  251. data/lib/protobuf/generators/enum_generator.rb +0 -39
  252. data/lib/protobuf/generators/extension_generator.rb +0 -27
  253. data/lib/protobuf/generators/field_generator.rb +0 -193
  254. data/lib/protobuf/generators/file_generator.rb +0 -262
  255. data/lib/protobuf/generators/group_generator.rb +0 -122
  256. data/lib/protobuf/generators/message_generator.rb +0 -104
  257. data/lib/protobuf/generators/option_generator.rb +0 -17
  258. data/lib/protobuf/generators/printable.rb +0 -160
  259. data/lib/protobuf/generators/service_generator.rb +0 -50
  260. data/lib/protobuf/lifecycle.rb +0 -33
  261. data/lib/protobuf/logging.rb +0 -39
  262. data/lib/protobuf/message/fields.rb +0 -233
  263. data/lib/protobuf/message/serialization.rb +0 -85
  264. data/lib/protobuf/message.rb +0 -241
  265. data/lib/protobuf/optionable.rb +0 -72
  266. data/lib/protobuf/tasks/compile.rake +0 -80
  267. data/lib/protobuf/tasks.rb +0 -1
  268. data/lib/protobuf/varint.rb +0 -20
  269. data/lib/protobuf/varint_pure.rb +0 -31
  270. data/lib/protobuf/version.rb +0 -3
  271. data/lib/protobuf/wire_type.rb +0 -10
  272. data/lib/protobuf.rb +0 -91
  273. data/proto/dynamic_discovery.proto +0 -46
  274. data/proto/google/protobuf/compiler/plugin.proto +0 -183
  275. data/proto/google/protobuf/descriptor.proto +0 -911
  276. data/proto/rpc.proto +0 -71
  277. data/service_executables/.gitkeep +0 -0
  278. data/service_executables/VERSION +0 -1
  279. data/service_executables/linux/contrast-service +0 -0
  280. data/service_executables/mac/contrast-service +0 -0
@@ -11,7 +11,7 @@ module Contrast
11
11
  module Rule
12
12
  # The Ruby implementation of the Protect Unsafe File Upload rule.
13
13
  # The unsafe-file-upload rule can trigger the following results:
14
- # BLOCKED in Blocking mode na SUSPICIOUS in Monitor mode.
14
+ # BLOCKED in Blocking mode and SUSPICIOUS in Monitor mode.
15
15
  class UnsafeFileUpload < Contrast::Agent::Protect::Rule::BaseService
16
16
  include Contrast::Agent::Reporting::InputType
17
17
 
@@ -23,40 +23,13 @@ module Contrast
23
23
  NAME
24
24
  end
25
25
 
26
- # This rule is solely based on input analysis, which the Service handles. When we move from the Service to the
27
- # agent with protect library, we should re-enable these tests and that rule.
28
- # TODO: RUBY-1574
29
- def enabled?
30
- super && false
26
+ def applicable_user_inputs
27
+ APPLICABLE_USER_INPUTS
31
28
  end
32
29
 
33
- # def block_message
34
- # BLOCK_MESSAGE
35
- # end
36
- #
37
- # def prefilter context
38
- # return unless prefilter?(context)
39
- #
40
- # ia_results = gather_ia_results context
41
- #
42
- # ia_results.each do |ia_result|
43
- # result = build_attack_result(context)
44
- # build_attack_without_match context, ia_result, result
45
- # append_to_activity context, result
46
- #
47
- # cef_logging result, :successful_attack
48
- # raise Contrast::SecurityException.new(self, BLOCK_MESSAGE) if blocked?
49
- # end
50
- # end
51
- #
52
- # private
53
- #
54
- # def prefilter? _context
55
- # return false unless enabled?
56
- # return false if protect_excluded_by_code?
57
- #
58
- # true
59
- # end
30
+ def block_message
31
+ BLOCK_MESSAGE
32
+ end
60
33
  end
61
34
  end
62
35
  end
@@ -0,0 +1,58 @@
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/input_classification_base'
5
+
6
+ module Contrast
7
+ module Agent
8
+ module Protect
9
+ module Rule
10
+ # The Ruby implementation of the Protect Reflected XSS rule
11
+ # Input classification
12
+ module ReflectedXssInputClassification
13
+ REFLECTED_XSS_MATCH = 'reflected-xss-input-tracing-v1'.cs__freeze
14
+ WORTHWATCHING_MATCH = 'xss-worth-watching-v2'.cs__freeze
15
+ class << self
16
+ include InputClassificationBase
17
+
18
+ private
19
+
20
+ # This methods checks if input is tagged WORTHWATCHING or IGNORE matches value with it's
21
+ # key if needed and Creates new isntance of InputAnalysisResult.
22
+ #
23
+ # @param request [Contrast::Agent::Request] the current request context.
24
+ # @param rule_id [String] The name of the Protect Rule.
25
+ # @param input_type [Contrast::Agent::Reporting::InputType] The type of the user input.
26
+ # @param value [String, Array<String>] the value of the input.
27
+ #
28
+ # @return res [Contrast::Agent::Reporting::InputAnalysisResult]
29
+ def create_new_input_result request, rule_id, input_type, value
30
+ return unless Contrast::AGENT_LIB
31
+
32
+ input_eval = Contrast::AGENT_LIB.eval_input(value,
33
+ convert_input_type(input_type),
34
+ Contrast::AGENT_LIB.rule_set[rule_id],
35
+ Contrast::AGENT_LIB.
36
+ eval_option[:WORTHWATCHING])
37
+
38
+ score = input_eval&.score || 0
39
+ ia_result = new_ia_result(rule_id, input_type, request.path, value)
40
+ if score >= THRESHOLD
41
+ ia_result.score_level = DEFINITEATTACK
42
+ ia_result.ids << REFLECTED_XSS_MATCH
43
+ elsif score >= WORTHWATCHING_THRESHOLD
44
+ ia_result.score_level = WORTHWATCHING
45
+ ia_result.ids << WORTHWATCHING_MATCH
46
+ else
47
+ ia_result.score_level = IGNORE
48
+ end
49
+
50
+ add_needed_key(request, ia_result, input_type, value)
51
+ ia_result
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/agent/protect/rule/base_service'
5
+ require 'contrast/agent/reporting/input_analysis/input_type'
5
6
 
6
7
  module Contrast
7
8
  module Agent
@@ -9,37 +10,31 @@ module Contrast
9
10
  module Rule
10
11
  # The Ruby implementation of the Protect Cross-Site Scripting rule.
11
12
  class Xss < Contrast::Agent::Protect::Rule::BaseService
13
+ include Contrast::Agent::Reporting::InputType
12
14
  NAME = 'reflected-xss'
13
15
  BLOCK_MESSAGE = 'XSS rule triggered. Response blocked.'
14
16
 
15
- class << self
16
- # @param attack_sample [Contrast::Api::Dtm::RaspRuleSample]
17
- # @return [Hash] the details for this specific rule
18
- def extract_details attack_sample
19
- # TODO: RUBY-1702 - figure out why xss isn't populated when reported; probably something to do w/
20
- # suspicious
21
- return Contrast::Utils::ObjectShare::EMPTY_HASH unless attack_sample&.xss
22
-
23
- {
24
- input: attack_sample.xss.input,
25
- matches: attack_sample.xss.matches.map do |match|
26
- {
27
- evidenceStart: match.evidence_start_ms,
28
- evidence: match.evidence,
29
- offset: match.offset
30
- }
31
- end
32
- }
33
- end
34
- end
17
+ APPLICABLE_USER_INPUTS = [
18
+ BODY, PARAMETER_NAME, PARAMETER_VALUE, JSON_VALUE,
19
+ MULTIPART_VALUE, MULTIPART_FIELD_NAME, XML_VALUE,
20
+ DWR_VALUE, URI, QUERYSTRING
21
+ ].cs__freeze
35
22
 
36
23
  def rule_name
37
24
  NAME
38
25
  end
39
26
 
27
+ def block_message
28
+ BLOCK_MESSAGE
29
+ end
30
+
40
31
  def stream_safe?
41
32
  false
42
33
  end
34
+
35
+ def applicable_user_inputs
36
+ APPLICABLE_USER_INPUTS
37
+ end
43
38
  end
44
39
  end
45
40
  end
@@ -22,28 +22,6 @@ module Contrast
22
22
  BLOCK_MESSAGE = 'XXE rule triggered. Response blocked.'
23
23
  EXTERNAL_ENTITY_PATTERN = /<!ENTITY\s+[a-zA-Z0-f]+\s+(?:SYSTEM|PUBLIC)\s+(.*?)>/.cs__freeze
24
24
 
25
- class << self
26
- # @param attack_sample [Contrast::Api::Dtm::RaspRuleSample]
27
- # @return [Hash] the details for this specific rule
28
- def extract_details attack_sample
29
- {
30
- xml: attack_sample.xxe.xml,
31
- declaredEntities: attack_sample.xxe.declared_entities.map do |entity|
32
- {
33
- start: entity.start_idx,
34
- end: entity.end_idx
35
- }
36
- end,
37
- entitiesResolved: attack_sample.xxe.entities_resolved.map do |entity|
38
- {
39
- systemId: entity.system_id,
40
- publicId: entity.public_id
41
- }
42
- end
43
- }
44
- end
45
- end
46
-
47
25
  def rule_name
48
26
  NAME
49
27
  end
@@ -81,7 +59,7 @@ module Contrast
81
59
  # @param xml [String] the literal value of the XML being checked for
82
60
  # external entity resolution
83
61
  # @param _kwargs [Hash]
84
- # @return [Contrast::Api::Dtm::AttackResult, nil] the determination
62
+ # @return [Contrast::Agent::Reporting, nil] the determination
85
63
  # as to whether or not this XML has an XXE attack in it.
86
64
  def find_attacker context, xml, **_kwargs
87
65
  return unless xml
@@ -142,8 +120,10 @@ module Contrast
142
120
  # We know that this attack happened, so the result is always matched
143
121
  # and the level is always critical. Only variable is the XML value
144
122
  # supplied by the attacker.
123
+ #
124
+ # @return [Contrast::Agent::Reporting::InputAnalysisResult]
145
125
  def build_evaluation xml
146
- ia_result = Contrast::Api::Settings::InputAnalysisResult.new
126
+ ia_result = Contrast::Agent::Reporting::InputAnalysisResult.new
147
127
  ia_result.rule_id = rule_name
148
128
  ia_result.input_type = :UNKNOWN
149
129
  ia_result.value = Contrast::Utils::StringUtils.protobuf_safe_string(xml)
@@ -6,6 +6,7 @@ require 'contrast/utils/timer'
6
6
  require 'contrast/agent/reporting/attack_result/user_input'
7
7
  require 'contrast/agent/reporting/input_analysis/input_type'
8
8
  require 'contrast/agent/reporting/details/protect_rule_details'
9
+ require 'contrast/agent/reporting/reporting_events/application_defend_attack_sample_stack'
9
10
 
10
11
  module Contrast
11
12
  module Agent
@@ -19,19 +20,15 @@ module Contrast
19
20
  #
20
21
  # @return [Contrast::Agent::Reporting::Details::ProtectRuleDetails, nil]
21
22
  attr_accessor :details
22
- # @return [Contrast::Agent::Reporting::Details::IpDenylistDetails, nil]
23
- attr_accessor :ip_denylist
24
- # @return [Contrast::Agent::Reporting::Details::VirtualPatchDetails, nil]
25
- attr_accessor :virtual_patch
26
23
 
27
24
  class << self
28
25
  # @param context [Contrast::Agent::RequestContext]
29
- # @param ia_result [Contrast::Api::Settings::InputAnalysisResult] the analysis of the input that was
30
- # determined to be an attack
26
+ # @param ia_result [Contrast::Agent::Reporting::Settings::InputAnalysisResult] the analysis of the input that
27
+ # was determined to be an attack
31
28
  # @return [Contrast::Agent::Reporting::RaspRuleSample]
32
29
  def build context, ia_result
33
30
  sample = new
34
- sample.time_stamp = context&.timer&.start_ms
31
+ sample.time_stamp = context&.timer&.start_ms || Contrast::Utils::Timer.now_ms
35
32
  sample.user_input = build_user_input_from_ia(ia_result)
36
33
  sample.user_input.document_type = if context&.request
37
34
  Contrast::Utils::StringUtils.force_utf8(context.request.document_type)
@@ -39,43 +36,19 @@ module Contrast
39
36
  sample
40
37
  end
41
38
 
42
- # @param ia_result [Contrast::Api::Settings::InputAnalysisResult] the analysis of the input that was
43
- # determined to be an attack
39
+ # @param ia_result [Contrast::Agent::Reporting::Settings::InputAnalysisResult] the analysis of the input that
40
+ # was determined to be an attack
44
41
  def build_user_input_from_ia ia_result
45
- # TODO: RUBY-99999 remove once only using Agent IA
46
- result = if ia_result.cs__is_a?(Contrast::Api::Settings::InputAnalysisResult)
47
- transform_ia_result(ia_result)
48
- else
49
- # Use Agent ia_result
50
- ia_result
51
- end
52
42
  user_input = Contrast::Agent::Reporting::UserInput.new
53
- return user_input unless result
43
+ return user_input unless ia_result
54
44
 
55
- user_input.input_type = result.input_type
56
- user_input.matcher_ids = result.ids
57
- user_input.path = result.path
58
- user_input.key = result.key if result.key
59
- user_input.value = result.value if result.value
45
+ user_input.input_type = ia_result.input_type
46
+ user_input.matcher_ids = ia_result.ids
47
+ user_input.path = ia_result.path
48
+ user_input.key = ia_result.key if ia_result.key
49
+ user_input.value = ia_result.value if ia_result.value
60
50
  user_input
61
51
  end
62
-
63
- # @param [Contrast::Api::Settings::InputAnalysisResult]
64
- # @return [Contrast::Agent::Reporting::InputAnalysisResult]
65
- def transform_ia_result dtm_ia_result
66
- ia_result = Contrast::Agent::Reporting::InputAnalysisResult.new
67
- ia_result.input_type = Contrast::Agent::Reporting::InputType.to_a.find do |value|
68
- value == dtm_ia_result.input_type.name # rubocop:disable Security/Module/Name
69
- end
70
- ia_result.score_level = dtm_ia_result.score_level.name # rubocop:disable Security/Module/Name
71
- ia_result.value = dtm_ia_result.value
72
- ia_result.key = dtm_ia_result.key
73
- ia_result.path = dtm_ia_result.path
74
- ia_result.rule_id = dtm_ia_result.rule_id
75
- ia_result.attack_count = dtm_ia_result.attack_count
76
- ia_result.ids = dtm_ia_result.ids
77
- ia_result
78
- end
79
52
  end
80
53
 
81
54
  def time_stamp
@@ -94,11 +67,17 @@ module Contrast
94
67
  @_user_input = input if input.is_a?(Contrast::Agent::Reporting::UserInput)
95
68
  end
96
69
 
70
+ # @return [Array<Contrast::Agent::Reporting::ApplicationDefendAttackSampleStack>,Array]
71
+ def stack
72
+ @_stack ||= []
73
+ end
74
+
97
75
  def to_controlled_hash
98
76
  {
99
77
  timeStamp: Time.at(time_stamp).iso8601,
100
78
  userInput: user_input.to_controlled_hash,
101
- details: details&.to_controlled_hash
79
+ details: details&.to_controlled_hash,
80
+ stack: stack.map(&:to_controlled_hash)
102
81
  }
103
82
  end
104
83
  end
@@ -9,18 +9,18 @@ module Contrast
9
9
  # This module will hold the response types used to generate
10
10
  # attack result.
11
11
  module ResponseType
12
- BLOCKED = :BLOCKED.cs__freeze
13
- MONITORED = :MONITORED.cs__freeze
14
- PROBED = :PROBED.cs__freeze
15
- BLOCK_AT_PERIMETER = :BLOCK_AT_PERIMETER.cs__freeze
16
- SUSPICIOUS = :SUSPICIOUS.cs__freeze
17
- AGGREGATED = :AGGREGATED.cs__freeze
18
- EXPLOITED = :EXPLOITED.cs__freeze
19
- NO_ACTION = :NO_ACTION.cs__freeze
12
+ BLOCKED = :BLOCKED.cs__freeze
13
+ MONITORED = :MONITORED.cs__freeze
14
+ PROBED = :PROBED.cs__freeze
15
+ BLOCKED_AT_PERIMETER = :BLOCKED_AT_PERIMETER.cs__freeze
16
+ SUSPICIOUS = :SUSPICIOUS.cs__freeze
17
+ AGGREGATED = :AGGREGATED.cs__freeze
18
+ EXPLOITED = :EXPLOITED.cs__freeze
19
+ NO_ACTION = :NO_ACTION.cs__freeze
20
20
 
21
21
  class << self
22
22
  def to_a
23
- [NO_ACTION, BLOCKED, MONITORED, PROBED, BLOCK_AT_PERIMETER, EXPLOITED, SUSPICIOUS, AGGREGATED]
23
+ [NO_ACTION, BLOCKED, MONITORED, PROBED, BLOCKED_AT_PERIMETER, EXPLOITED, SUSPICIOUS, AGGREGATED]
24
24
  end
25
25
  end
26
26
  end
@@ -10,9 +10,17 @@ module Contrast
10
10
  # Bot blocker IA result details info.
11
11
  class IpDenylistDetails < ProtectRuleDetails
12
12
  # @return [String]
13
- attr_accessor :ip
13
+ attr_reader :ip
14
14
  # @return [String]
15
- attr_accessor :uuid
15
+ attr_reader :uuid
16
+
17
+ # @param ip [String] the IP address that was blocked
18
+ # @param uuid [String] the UUID to identify the block rule in TeamServer
19
+ def initialize ip, uuid
20
+ @ip = ip
21
+ @uuid = uuid
22
+ super()
23
+ end
16
24
 
17
25
  def to_controlled_hash
18
26
  {
@@ -7,10 +7,16 @@ module Contrast
7
7
  module Agent
8
8
  module Reporting
9
9
  module Details
10
- # Bot blocker IA result details info.
10
+ # Virtual Patch IA result details info.
11
11
  class VirtualPatchDetails < ProtectRuleDetails
12
12
  # @return [String]
13
- attr_accessor :uuid
13
+ attr_reader :uuid
14
+
15
+ # @param uuid [String] the UUID to identify the block rule in TeamServer
16
+ def initialize uuid
17
+ @uuid = uuid
18
+ super()
19
+ end
14
20
 
15
21
  def to_controlled_hash
16
22
  {
@@ -0,0 +1,27 @@
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/agent/reporting/input_analysis/details/protect_rule_details'
5
+
6
+ module Contrast
7
+ module Agent
8
+ module Reporting
9
+ # Bot blocker IA result details info.
10
+ class BotBlockerDetails < ProtectRuleDetails
11
+ # @return [String]
12
+ attr_accessor :bot
13
+ # User agent header value
14
+ #
15
+ # @return [String]
16
+ attr_accessor :user_agent
17
+
18
+ def to_controlled_hash
19
+ {
20
+ bot: bot,
21
+ userAgent: user_agent
22
+ }
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,15 @@
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
+ module Contrast
5
+ module Agent
6
+ module Reporting
7
+ # This class is holding additional info which is rule specific and this is
8
+ # the base class for type check made easy.
9
+ class ProtectRuleDetails
10
+ # Extend per each rule.
11
+ def to_controlled_hash; end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -7,8 +7,7 @@ require 'contrast/agent/reporting/input_analysis/input_analysis_result'
7
7
  module Contrast
8
8
  module Agent
9
9
  module Reporting
10
- # This class will do ia analysis for our protect rules instead of
11
- # using the service.
10
+ # This class will do ia analysis for our protect rules
12
11
  class InputAnalysis
13
12
  # result from input analysis
14
13
  #
@@ -4,12 +4,12 @@
4
4
  require 'contrast/utils/object_share'
5
5
  require 'contrast/agent/reporting/input_analysis/input_type'
6
6
  require 'contrast/agent/reporting/input_analysis/score_level'
7
+ require 'contrast/agent/reporting/input_analysis/details/protect_rule_details'
7
8
 
8
9
  module Contrast
9
10
  module Agent
10
11
  module Reporting
11
- # This class will do ia analysis for our protect rules instead of
12
- # using the service.
12
+ # This class will do ia analysis for our protect rules
13
13
  class InputAnalysisResult
14
14
  INPUT_TYPE = Contrast::Agent::Reporting::InputType
15
15
  SCORE_LEVEL = Contrast::Agent::Reporting::ScoreLevel
@@ -109,6 +109,20 @@ module Contrast
109
109
  def score_level= score_level
110
110
  @_score_level = score_level if SCORE_LEVEL.to_a.include?(score_level)
111
111
  end
112
+
113
+ # Additional per rule details containing more specific info.
114
+ #
115
+ # @param protect_rule_details [Contrast::Agent::Reporting::ProtectRuleDetails]
116
+ def details= protect_rule_details
117
+ @_details = protect_rule_details if protect_rule_details.is_a?(Contrast::Agent::Reporting::ProtectRuleDetails)
118
+ end
119
+
120
+ # Additional per rule details containing more specific info.
121
+ #
122
+ # @return [Contrast::Agent::Reporting::ProtectRuleDetails, nil]
123
+ def details
124
+ @_details
125
+ end
112
126
  end
113
127
  end
114
128
  end
@@ -35,6 +35,8 @@ module Contrast
35
35
  return unless activity
36
36
 
37
37
  logger.debug('Masker: masking sensitive data', activity: activity.__id__, request: activity.request&.__id__)
38
+ return if activity.request.nil?
39
+
38
40
  mask_body(activity)
39
41
  mask_query_string(activity)
40
42
  mask_request_params(activity)
@@ -26,5 +26,6 @@ require 'contrast/agent/reporting/reporting_events/observed_route'
26
26
  require 'contrast/agent/reporting/reporting_events/route_coverage'
27
27
  require 'contrast/agent/reporting/reporting_events/observed_library_usage'
28
28
  require 'contrast/agent/reporting/reporting_events/poll'
29
+ require 'contrast/agent/reporting/reporting_events/server_settings'
29
30
  # Sensitive data masking
30
31
  require 'contrast/agent/reporting/masker/masker'
@@ -5,6 +5,8 @@ require 'contrast/agent/worker_thread'
5
5
  require 'contrast/agent/reporting/report'
6
6
  require 'contrast/components/logger'
7
7
  require 'contrast/agent/reporting/reporting_events/agent_startup'
8
+ require 'contrast/agent/telemetry/events/exceptions/telemetry_exceptions'
9
+ require 'contrast/agent/telemetry/events/exceptions/obfuscate'
8
10
 
9
11
  module Contrast
10
12
  module Agent
@@ -13,12 +15,14 @@ module Contrast
13
15
  include Contrast::Components::Logger::InstanceMethods
14
16
  include Contrast::Utils::ObjectShare
15
17
 
18
+ MAX_QUEUE_SIZE = 1000
19
+
16
20
  class << self
17
21
  # check if we can report to TS
18
22
  #
19
23
  # @return[Boolean] true if bypass is enabled, or false if bypass disabled
20
24
  def enabled?
21
- @_enabled = Contrast::CONTRAST_SERVICE.use_agent_communication? if @_enabled.nil?
25
+ @_enabled = ::Contrast::AGENT.enabled? if @_enabled.nil?
22
26
  @_enabled
23
27
  end
24
28
  end
@@ -39,7 +43,6 @@ module Contrast
39
43
  logger.debug('Starting background Reporter thread.')
40
44
  loop do
41
45
  next unless connected?
42
- next unless app_create_complete?
43
46
 
44
47
  process_event(queue.pop)
45
48
  rescue StandardError => e
@@ -69,6 +72,14 @@ module Contrast
69
72
  end
70
73
  return unless event
71
74
 
75
+ if queue.size >= MAX_QUEUE_SIZE
76
+ if Contrast::Agent::Telemetry::Base.enabled?
77
+ Contrast::Agent.thread_watcher.telemetry_queue.send_event(queue_limit_telemetry_event)
78
+ end
79
+
80
+ return
81
+ end
82
+
72
83
  queue << event
73
84
  end
74
85
 
@@ -120,18 +131,6 @@ module Contrast
120
131
  false
121
132
  end
122
133
 
123
- # Unless we're in bypass mode, we need to make sure the service has started and built the application on
124
- # TeamServer since we're doing a split style here.
125
- #
126
- # @return [Boolean]
127
- def app_create_complete?
128
- return true if Contrast::APP_CONTEXT.session_id
129
-
130
- logger.debug('Service startup incomplete; Application may not be created; sleeping')
131
- sleep(5)
132
- false
133
- end
134
-
135
134
  # @param event [Contrast::Agent::Reporting::ReportingEvent]
136
135
  def process_event event
137
136
  client.send_event(event, connection)
@@ -139,6 +138,28 @@ module Contrast
139
138
  rescue StandardError => e
140
139
  logger.error('Could not send message to TeamServer from Reporter queue.', e)
141
140
  end
141
+
142
+ # @return [Contrast::Agent::Telemetry::TelemetryException::Event]
143
+ def queue_limit_telemetry_event
144
+ message_exception = Contrast::Agent::Telemetry::TelemetryException::MessageException.build(
145
+ 'String',
146
+ "Maximum queue size (#{ MAX_QUEUE_SIZE }) reached for reporting events", nil, stack_frame)
147
+ message = Contrast::Agent::Telemetry::TelemetryException::Message.build({}, [message_exception])
148
+ Contrast::Agent::Telemetry::TelemetryException::Event.new(message)
149
+ end
150
+
151
+ def stack_frame
152
+ stack_trace = caller_locations(20, 20)
153
+ stack_frame_type = if stack_trace.nil? || stack_trace[1].nil?
154
+ 'none'
155
+ else
156
+ Contrast::Agent::Telemetry::TelemetryException::Obfuscate.obfuscate_type(
157
+ stack_trace[1].path.delete_prefix(Dir.pwd))
158
+ end
159
+
160
+ stack_frame_function = stack_trace.nil? || stack_trace[1].nil? ? 'none' : stack_trace[1].label
161
+ Contrast::Agent::Telemetry::TelemetryException::StackFrame.build(stack_frame_function, stack_frame_type, nil)
162
+ end
142
163
  end
143
164
  end
144
165
  end
@@ -1,18 +1,16 @@
1
1
  # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'contrast/agent/worker_thread'
5
- require 'contrast/agent/reporting/report'
4
+ require 'contrast/agent/reporting/reporter'
6
5
  require 'contrast/agent/inventory/dependency_usage_analysis'
7
6
  require 'contrast/agent/reporting/reporting_events/poll'
8
- require 'contrast/agent/reporting/reporting_events/server_activity'
9
7
 
10
8
  module Contrast
11
9
  module Agent
12
10
  # The ReporterHeartbeat will make sure that the process remains marked alive by TeamServer and that we periodically
13
11
  # reach out to get the latest settings for this application. It also sends out those messages which do not need to
14
12
  # be associated directly with a request, such as Server Activity and Library Observation.
15
- class ReporterHeartbeat < WorkerThread
13
+ class ReporterHeartbeat < Reporter
16
14
  # TeamServer will mark an application offline after 5 minutes. Sending this every one should be more than enough
17
15
  # to satisfy our goals.
18
16
  REFRESH_INTERVAL_SEC = 60
@@ -42,11 +40,7 @@ module Contrast
42
40
  #
43
41
  # @return [Array<Contrast::Agent::Reporting::ReportingEvent>]
44
42
  def polling_events
45
- [
46
- Contrast::Agent::Inventory::DependencyUsageAnalysis.instance.generate_library_usage,
47
- Contrast::Agent::Reporting::ServerActivity.new,
48
- poll_message
49
- ].compact
43
+ [Contrast::Agent::Inventory::DependencyUsageAnalysis.instance.generate_library_usage, poll_message].compact
50
44
  end
51
45
  end
52
46
  end