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
@@ -0,0 +1,56 @@
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
+ module Settings
8
+ # Helper methods used across the settings.
9
+ module Helpers
10
+ class << self
11
+ # Fills instance variable [Array] with new settings as new instance type
12
+ # for each entry.
13
+ #
14
+ # @param instance_type [Class] a class to create new instance from to
15
+ # @param instance_variable [Array] instance variable accessor method
16
+ # return an array to fill.
17
+ # @param array [Array] array to be used to fill the iv.
18
+ # fill the iv with the new type containing data from the provided array
19
+ # param.
20
+ # @return instance_variable [Class]
21
+ def array_to_iv instance_type, instance_variable, array
22
+ return unless array.is_a?(Array)
23
+
24
+ array.each_with_index do |entry, index|
25
+ new_instance = instance_type.new
26
+ instance_type::ATTRIBUTES.each do |attr|
27
+ new_instance.send("#{ attr }=".to_sym, entry[no_more_underscore(attr)])
28
+ end
29
+ instance_variable[index] = new_instance
30
+ end
31
+
32
+ instance_variable
33
+ end
34
+
35
+ # :attr_name => :attrName
36
+ # return original if no '_'
37
+ #
38
+ # @param attr_name [Symbol]
39
+ # @return normalized_name [Symbol]
40
+ def no_more_underscore attr_name
41
+ name = attr_name.to_s
42
+ idx = name.index('_')
43
+ return attr_name if idx.nil? || (idx.zero? && idx >= name.length)
44
+
45
+ result = name
46
+ result.slice!(idx)
47
+ result.insert(idx, name[idx]&.upcase)
48
+ result.slice!(idx + 1)
49
+ result.index('_') ? no_more_underscore(result).to_sym : result.to_sym
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,37 @@
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/settings/exclusion_base'
5
+ require 'contrast/utils/object_share'
6
+
7
+ module Contrast
8
+ module Agent
9
+ module Reporting
10
+ module Settings
11
+ # InputExclusions class
12
+ class InputExclusion < ExclusionBase
13
+ ATTRIBUTES = BASE_ATTRIBUTES.dup << :type
14
+ ATTRIBUTES.cs__freeze
15
+ VALID_INPUT_TYPES = %w[COOKIE PARAMETER HEADER BODY QUERYSTRING].cs__freeze
16
+
17
+ # @return type [String] The type of the input
18
+ def type
19
+ @_type ||= Contrast::Utils::ObjectShare::EMPTY_STRING
20
+ end
21
+
22
+ # @param new_type [String] Set new input type.
23
+ # @return type [String] The type of the input.
24
+ def type= new_type
25
+ @_type = new_type if VALID_INPUT_TYPES.include?(new_type)
26
+ end
27
+
28
+ def to_controlled_hash
29
+ hash = super
30
+ hash[:type] = type
31
+ hash
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,35 @@
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
+ module Settings
8
+ # The IP addresses for which to disable protection.
9
+ class IpFilter
10
+ ATTRIBUTES = %i[expires ip name uuid].cs__freeze
11
+
12
+ # The value in milliseconds since epoch for expiration. Value of '0' means no expiration.
13
+ #
14
+ # @return expires [Integer] The time after which the filter is no longer valid.
15
+ attr_accessor :expires
16
+ # @return ip [String] The IP or range of IPs to which this message pertains.
17
+ attr_accessor :ip
18
+ # @return name [String] The user defined name of the filter.
19
+ attr_accessor :name
20
+ # @return uuid [String] The identifier of the filter as defined by TeamServer.
21
+ attr_accessor :uuid
22
+
23
+ def to_controlled_hash
24
+ {
25
+ expires: expires,
26
+ ip: ip,
27
+ name: name, # rubocop:disable Security/Module/Name
28
+ uuid: uuid
29
+ }
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,74 @@
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/settings/helpers'
5
+
6
+ module Contrast
7
+ module Agent
8
+ module Reporting
9
+ module Settings
10
+ # The words to search for in input that indicate an attack
11
+ class Keyword
12
+ ATTRIBUTES = %i[id value case_sensitive score prohibited_features mandatory_features].cs__freeze
13
+
14
+ # @return id [String]
15
+ attr_accessor :id
16
+ # @return value [string]
17
+ attr_accessor :value
18
+ # @return case_sensitive [Boolean]
19
+ attr_accessor :case_sensitive
20
+ # @return score [Integer] The impact of matching this entry;
21
+ # higher meaning more likely to be an attack
22
+ attr_accessor :score
23
+
24
+ # Disable this pattern or keyword if agent implements one of
25
+ # the specified features
26
+ #
27
+ # @return [Array<String>]
28
+ def prohibited_features
29
+ @_prohibited_features ||= []
30
+ end
31
+
32
+ # Disable this pattern or keyword if agent implements one of
33
+ # the specified features
34
+ #
35
+ # @return [Array<String>]
36
+ def mandatory_features
37
+ @_mandatory_features ||= []
38
+ end
39
+
40
+ # Set the features.
41
+ #
42
+ # @param features [Array]
43
+ # @return [Array<String>]
44
+ def prohibited_features= features
45
+ @_prohibited_features = features if features.is_a?(Array)
46
+ end
47
+
48
+ # Set the features.
49
+ #
50
+ # @param features [Array]
51
+ # @return [Array<String>]
52
+ def mandatory_features= features
53
+ @_mandatory_features = features if features.is_a?(Array)
54
+ end
55
+
56
+ def to_controlled_hash
57
+ {
58
+ id: id,
59
+ value: value,
60
+ caseSensitive: case_sensitive,
61
+ score: score,
62
+ prohibitedFeatures: prohibited_features,
63
+ mandatoryFeatures: mandatory_features
64
+ }
65
+ end
66
+ end
67
+
68
+ # A word or pattern whose presence in an input represents an attack
69
+ class Pattern < Keyword
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,65 @@
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/object_share'
5
+
6
+ module Contrast
7
+ module Agent
8
+ module Reporting
9
+ module Settings
10
+ # All of the apis to add new logging calls to the application at runtime
11
+ class LogEnhancer
12
+ ATTRIBUTES = %i[id api format level name type].cs__freeze
13
+ LOG_LEVELS = %w[TRACE DEBUG INFO WARN ERROR].cs__freeze
14
+ TYPES = %w[AUDIT ERROR SECURITY].cs__freeze
15
+
16
+ # @return api [String] The method signature to instrument, as understood by the agent.
17
+ attr_accessor :api
18
+ # @return format [String] The format of the message to log.
19
+ attr_accessor :format
20
+ # @return id [Integer] The identifier of the enhancer as defined by TeamServer.
21
+ attr_accessor :id
22
+ # @return name [String] The user defined name of the enhancer.
23
+ attr_accessor :name
24
+
25
+ # @return level [String] The level at which to log this message. Trace as 0 and Error as 4.
26
+ # [ TRACE, DEBUG, INFO, WARN, ERROR ]
27
+ def level
28
+ @_level ||= Contrast::Utils::ObjectShare::EMPTY_STRING
29
+ end
30
+
31
+ # @param new_level [String] new level to set.
32
+ # @return level [String] The level at which to log this message. Trace as 0 and Error as 4.
33
+ # [ TRACE, DEBUG, INFO, WARN, ERROR ]
34
+ def level= new_level
35
+ @_level = new_level if LOG_LEVELS.include?(new_level)
36
+ end
37
+
38
+ # @return type [String] The type of log message to generate. Audit as 0, Security as 2.
39
+ # [ AUDIT, ERROR, SECURITY ]
40
+ def type
41
+ @_type ||= Contrast::Utils::ObjectShare::EMPTY_STRING
42
+ end
43
+
44
+ # @param new_type [String] new type to set.
45
+ # @return type [String] The type of log message to generate. Audit as 0, Security as 2.
46
+ # [ AUDIT, ERROR, SECURITY ]
47
+ def type= new_type
48
+ @_type = new_type if TYPES.include?(new_type)
49
+ end
50
+
51
+ def to_controlled_hash
52
+ {
53
+ id: id,
54
+ api: api,
55
+ format: format,
56
+ name: name, # rubocop:disable Security/Module/Name
57
+ level: level,
58
+ type: type
59
+ }
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
@@ -2,6 +2,8 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/utils/object_share'
5
+ require 'contrast/api/dtm.pb'
6
+ require 'contrast/api/settings.pb'
5
7
 
6
8
  module Contrast
7
9
  module Agent
@@ -11,7 +13,7 @@ module Contrast
11
13
  # Application level settings for the Protect featureset
12
14
  class Protect
13
15
  # block at perimeter needs to be check against the blockAtEntry boolean value
14
- PROTECT_RULES_MODE = %w[OFF BLOCKING MONITORING].cs__freeze
16
+ PROTECT_RULES_MODE = %w[OFF MONITORING BLOCKING].cs__freeze
15
17
  # The settings for each protect rule for this application
16
18
  #
17
19
  # @return protection_rules [Array<protectRule>] protectRule: {
@@ -33,7 +35,7 @@ module Contrast
33
35
  # id [String] The id of a rule in Contrast.
34
36
  # mode [String] The mode that this rule should run in. [OFF, MONITORING, BLOCKING] }
35
37
  def protection_rules= protection_rules
36
- @_protection_rules = protection_rules if protection_rules.is_a? Array
38
+ @_protection_rules = protection_rules if protection_rules.is_a?(Array)
37
39
  end
38
40
 
39
41
  # The virtual patches to apply for this application
@@ -66,7 +68,7 @@ module Contrast
66
68
  # urls [Array] The urls that must be present in the request to result in the request being blocked.
67
69
  # uuids [String] The UUID of the Virtual Patch }
68
70
  def virtual_patches= virtual_patches
69
- @_virtual_patches = virtual_patches if virtual_patches.is_a? Array
71
+ @_virtual_patches = virtual_patches if virtual_patches.is_a?(Array)
70
72
  end
71
73
 
72
74
  # Converts settings into Agent Settings understandable hash {RULE_ID => MODE}
@@ -77,8 +79,23 @@ module Contrast
77
79
 
78
80
  modes_by_id = {}
79
81
  protection_rules.each do |rule|
80
- PROTECT_RULES_MODE.include? rule[:mode]
81
- modes_by_id[rule[:id]] = rule[:mode]
82
+ setting_mode = rule[:mode]
83
+ next unless PROTECT_RULES_MODE.include?(setting_mode)
84
+
85
+ api_mode = case setting_mode
86
+ when PROTECT_RULES_MODE[1]
87
+ ::Contrast::Api::Settings::ProtectionRule::Mode::MONITOR
88
+ when PROTECT_RULES_MODE[2]
89
+ if rule[:blockAtEntry]
90
+ ::Contrast::Api::Settings::ProtectionRule::Mode::BLOCK_AT_PERIMETER
91
+ else
92
+ ::Contrast::Api::Settings::ProtectionRule::Mode::BLOCK
93
+ end
94
+ else
95
+ ::Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION
96
+ end
97
+
98
+ modes_by_id[rule[:id]] = api_mode
82
99
  end
83
100
  modes_by_id
84
101
  end
@@ -2,6 +2,12 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/utils/object_share'
5
+ require 'contrast/agent/reporting/settings/bot_blocker'
6
+ require 'contrast/agent/reporting/settings/ip_filter'
7
+ require 'contrast/agent/reporting/settings/log_enhancer'
8
+ require 'contrast/agent/reporting/settings/rule_definition'
9
+ require 'contrast/agent/reporting/settings/syslog'
10
+ require 'contrast/agent/reporting/settings/helpers'
5
11
 
6
12
  module Contrast
7
13
  module Agent
@@ -28,22 +34,14 @@ module Contrast
28
34
 
29
35
  # Indicate if the bot protection feature set is enabled for this server or not.
30
36
  #
31
- # @return bot_blocker [Boolean]
37
+ # @return bot_blocker [Contrast::Agent::Reporting::Settings::BotBlocker]
32
38
  def bot_blocker
33
- @_bot_blocker
34
- end
35
-
36
- # set bot_blocker
37
- #
38
- # @param bot_blocker [Boolean]
39
- # @return bot_blocker [Boolean]
40
- def bot_blocker= bot_blocker
41
- @_bot_blocker = bot_blocker if !!bot_blocker == bot_blocker
39
+ @_bot_blocker ||= Contrast::Agent::Reporting::Settings::BotBlocker.new
42
40
  end
43
41
 
44
42
  # The IP addresses for which to disable protection.
45
43
  #
46
- # @return ip_allowlist [Array<IpFilter>]
44
+ # @return ip_allowlist [Array<Contrast::Agent::Reporting::Settings::IpFilter>, []]
47
45
  # expires [Integer] The time after which the filter is no longer valid.
48
46
  # ip [String] The IP or range of IPs to which this message pertains.
49
47
  # name [String] The user defined name of the filter.
@@ -60,18 +58,16 @@ module Contrast
60
58
  # name [String] The user defined name of the filter.
61
59
  # uuid [String] The identifier of the filter as defined by TeamServer.
62
60
  # }
63
- # @return ip_allowlist [Array<IpFilter>]
64
- # expires [Integer] The time after which the filter is no longer valid.
65
- # ip [String] The IP or range of IPs to which this message pertains.
66
- # name [String] The user defined name of the filter.
67
- # uuid [String] The identifier of the filter as defined by TeamServer.
61
+ # @return ip_allowlist [Array<Contrast::Agent::Reporting::Settings::IpFilter>]
68
62
  def ip_allowlist= allowlist
69
- @_ip_allowlist = allowlist if allowlist.is_a? Array
63
+ Contrast::Agent::Reporting::Settings::Helpers.array_to_iv(Contrast::Agent::Reporting::Settings::IpFilter,
64
+ ip_allowlist,
65
+ allowlist)
70
66
  end
71
67
 
72
68
  # The IP addresses for which to disable protection.
73
69
  #
74
- # @return ip_denylist [Array<IpFilter>]
70
+ # @return ip_denylist [Array<IpFilter>, []]
75
71
  # expires [Integer] The time after which the filter is no longer valid.
76
72
  # ip [String] The IP or range of IPs to which this message pertains.
77
73
  # name [String] The user defined name of the filter.
@@ -88,72 +84,42 @@ module Contrast
88
84
  # name [String] The user defined name of the filter.
89
85
  # uuid [String] The identifier of the filter as defined by TeamServer.
90
86
  # }
91
- # @return ip_denylist [Array<IpFilter>]
92
- # expires [Integer] The time after which the filter is no longer valid.
93
- # ip [String] The IP or range of IPs to which this message pertains.
94
- # name [String] The user defined name of the filter.
95
- # uuid [String] The identifier of the filter as defined by TeamServer.
87
+ # @return ip_denylist [Array<Contrast::Agent::Reporting::Settings::IpFilter>]
96
88
  def ip_denylist= denylist
97
- @_ip_denylist = denylist if denylist.is_a? Array
89
+ Contrast::Agent::Reporting::Settings::Helpers.array_to_iv(Contrast::Agent::Reporting::Settings::IpFilter,
90
+ ip_denylist,
91
+ denylist)
98
92
  end
99
93
 
100
94
  # All of the apis to add new logging calls to the application at runtime.
101
95
  #
102
- # @return log_enchancers [Array<LogEnchancers>]
103
- # api [String] The method signature to instrument, as understood by the agent.
104
- # format [String] The format of the message to log.
105
- # id [Integer] The identifier of the enhancer as defined by TeamServer.
106
- # level [String] The level at which to log this message. Trace as 0 and Error as 4.
107
- # [ TRACE, DEBUG, INFO, WARN, ERROR ]
108
- # name [String] The user defined name of the enhancer.
109
- # type [String] The type of log message to tenerate. Audit as 0, Security as 2.
110
- # [ AUDIT, ERROR, SECURITY ]
111
- def log_enchancers
112
- @_log_enchancers ||= []
96
+ # @return log_enhancers [Array<Contrast::Agent::Reporting::Settings::LogEnhancer>, []]
97
+ def log_enhancers
98
+ @_log_enhancers ||= []
113
99
  end
114
100
 
115
101
  # All of the apis to add new logging calls to the application at runtime.
116
102
  #
117
- # @param log_enchancers [Array<LogEnchancers>] of LogEnchancers: {
103
+ # @param log_enhancers_array [Array<LogEnhancers>] of LogEnhancers: {
118
104
  # api [String] The method signature to instrument, as understood by the agent.
119
105
  # format [String] The format of the message to log.
120
106
  # id [Integer] The identifier of the enhancer as defined by TeamServer.
121
107
  # level [String] The level at which to log this message. Trace as 0 and Error as 4.
122
108
  # [ TRACE, DEBUG, INFO, WARN, ERROR ]
123
109
  # name [String] The user defined name of the enhancer.
124
- # type [String] The type of log message to tenerate. Audit as 0, Security as 2.
110
+ # type [String] The type of log message to generate. Audit as 0, Security as 2.
125
111
  # [ AUDIT, ERROR, SECURITY ]
126
112
  # }
127
- # @return log_enchancers [Array<LogEnchancers>]
128
- # api [String] The method signature to instrument, as understood by the agent.
129
- # format [String] The format of the message to log.
130
- # id [Integer] The identifier of the enhancer as defined by TeamServer.
131
- # level [String] The level at which to log this message. Trace as 0 and Error as 4.
132
- # [ TRACE, DEBUG, INFO, WARN, ERROR ]
133
- # name [String] The user defined name of the enhancer.
134
- # type [String] The type of log message to tenerate. Audit as 0, Security as 2.
135
- # [ AUDIT, ERROR, SECURITY ]
136
- def log_enchancers= log_enchancers
137
- @_log_enchancers = log_enchancers if log_enchancers.is_a? Array
113
+ # @return log_enhancers [Array<Contrast::Agent::Reporting::Settings::LogEnhancer>]
114
+ def log_enhancers= log_enhancers_array
115
+ Contrast::Agent::Reporting::Settings::Helpers.array_to_iv(Contrast::Agent::Reporting::Settings::LogEnhancer,
116
+ log_enhancers,
117
+ log_enhancers_array)
138
118
  end
139
119
 
140
120
  # The keywords and patterns required for the input analysis of each rule with that capability.
141
121
  #
142
- # @return rule_defenition_list [Array<RuleDefinition>]
143
- # keywords [Array] The words to search for in input that indicate an attack.{
144
- # caseSensitive [Boolean]
145
- # id [String]
146
- # score [Integer] The impact of matching this entry; higher meaning more
147
- # likely to be an attack
148
- # value [String] }
149
- # name [String] AssessRuleID
150
- # patterns [Array] A word or pattern whose presence in an input represents an attack {
151
- # caseSensitive [Boolean]
152
- # id [String]
153
- # score [Integer] The impact of matching this entry; higher meaning more
154
- # likely to be an attack
155
- # value [String] }
156
- # }
122
+ # @return rule_definition_list [Array<Contrast::Agent::Reporting::Settings::RuleDefinition>]
157
123
  def rule_definition_list
158
124
  @_rule_definition_list ||= []
159
125
  end
@@ -175,66 +141,47 @@ module Contrast
175
141
  # likely to be an attack
176
142
  # value [String] }
177
143
  # }
178
- # @return rule_defenition_list [Array<RuleDefinition>] Array of RuleDefinition: {
179
- # keywords [Array] The words to search for in input that indicate an attack.{
180
- # caseSensitive [Boolean]
181
- # id [String]
182
- # score [Integer] The impact of matching this entry; higher meaning more
183
- # likely to be an attack
184
- # value [String] }
185
- # name [String] AssessRuleID
186
- # patterns [Array] A word or pattern whose presence in an input represents an attack {
187
- # caseSensitive [Boolean]
188
- # id [String]
189
- # score [Integer] The impact of matching this entry; higher meaning more
190
- # likely to be an attack
191
- # value [String] }
192
- # }
144
+ # @return rule_definition_list [Array<Contrast::Agent::Reporting::Settings::RuleDefinition>]
193
145
  def rule_definition_list= list
194
- @_rule_definition_list = list if list.is_a? Array
146
+ Contrast::Agent::Reporting::Settings::Helpers.array_to_iv(
147
+ Contrast::Agent::Reporting::Settings::RuleDefinition,
148
+ rule_definition_list,
149
+ list)
195
150
  end
196
151
 
197
152
  # Controls for the syslogging feature in the agent.
198
153
  #
199
- # @return syslog [Hash<logsSettings>]
200
- # syslogConnectionType [String]
201
- # syslogEnabled [Integer]
202
- # syslogFacilityCode [Integer]
203
- # syslogIpAddress [String]
204
- # syslogPortNumber [Integer]
205
- # syslogProtocol [String]
206
- # syslogSeverityExploited [String]
207
- # syslogSeverityProbed [String]
208
- # syslogSeveritySuspicous [String]
154
+ # @return syslog [Contrast::Agent::Reporting::Settings::Syslog]
209
155
  def syslog
210
- @_syslog ||= {}
156
+ @_syslog ||= Contrast::Agent::Reporting::Settings::Syslog.new
211
157
  end
212
158
 
213
- # Controls for the syslogging feature in the agent.
214
- #
215
- # @param log [Hash<logsSettings>] {
216
- # syslogConnectionType [String]
217
- # syslogEnabled [Integer]
218
- # syslogFacilityCode [Integer]
219
- # syslogIpAddress [String]
220
- # syslogPortNumber [Integer]
221
- # syslogProtocol [String]
222
- # syslogSeverityExploited [String]
223
- # syslogSeverityProbed [String]
224
- # syslogSeveritySuspicous [String]
225
- # }
226
- # @return syslog [Hash<logsSettings>]
227
- # syslogConnectionType [String]
228
- # syslogEnabled [Integer]
229
- # syslogFacilityCode [Integer]
230
- # syslogIpAddress [String]
231
- # syslogPortNumber [Integer]
232
- # syslogProtocol [String]
233
- # syslogSeverityExploited [String]
234
- # syslogSeverityProbed [String]
235
- # syslogSeveritySuspicous [String]
236
- def syslog= log
237
- @_syslog = log if log.is_a? Hash
159
+ # The protect response should be structured like this:
160
+ # protect{ enable, observability, rules, log_enhancers }
161
+ # instead we receive all the data under the protect:
162
+ # the rules array is merged under protect and the ruleDefinition
163
+ # list is separate:
164
+ # "defend" : {
165
+ # "botBlockers" : [],
166
+ # "enabled" : true,
167
+ # "logEnhancers" : [],
168
+ # "ipDenylist" : [],
169
+ # "ipAllowlist" : [],
170
+ # "syslog" : {},
171
+ # "ruleDefinitionList" : [{...}],
172
+ # "bot-blocker" : false
173
+ # },
174
+ def to_controlled_hash
175
+ {
176
+ botBlockers: bot_blocker.bots.map(&:to_controlled_hash),
177
+ enabled: enabled?,
178
+ logEnhancers: log_enhancers.map(&:to_controlled_hash),
179
+ ipDenylist: ip_denylist.map(&:to_controlled_hash),
180
+ ipAllowlist: ip_allowlist.map(&:to_controlled_hash),
181
+ syslog: syslog.to_controlled_hash,
182
+ ruleDefinitionList: rule_definition_list.map(&:to_controlled_hash),
183
+ 'bot-blocker': bot_blocker.to_controlled_hash
184
+ }
238
185
  end
239
186
  end
240
187
  end
@@ -1,6 +1,8 @@
1
1
  # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
+ require 'contrast/agent/reporting/settings/log_enhancer'
5
+
4
6
  module Contrast
5
7
  module Agent
6
8
  module Reporting
@@ -10,7 +12,6 @@ module Contrast
10
12
  attr_accessor :level, :operation, :message
11
13
 
12
14
  # used to check the parameters and also before reactions settings update
13
- LEVELS = %w[ERROR WARN INFO DEBUG TRACE].cs__freeze
14
15
  OPERATIONS = %w[NOOP DISABLE].cs__freeze
15
16
 
16
17
  # Reaction the agent should take based on a state in TS.
@@ -19,9 +20,17 @@ module Contrast
19
20
  # @param message [String] A message to log when receiving this reaction.
20
21
  # @param operation [String] What to do in response to this reaction.[NOOP, DISABLE]
21
22
  def initialize level, operation, message
22
- @level = level if LEVELS.include? level
23
- @operation = operation if OPERATIONS.include? operation
24
- @message = message if message.is_a? String
23
+ @level = level if Contrast::Agent::Reporting::Settings::LogEnhancer::LOG_LEVELS.include?(level)
24
+ @operation = operation if OPERATIONS.include?(operation)
25
+ @message = message if message.is_a?(String)
26
+ end
27
+
28
+ def to_controlled_hash
29
+ {
30
+ message: message,
31
+ level: level,
32
+ operation: operation
33
+ }
25
34
  end
26
35
  end
27
36
  end