contrast-agent 5.2.0 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (228) hide show
  1. checksums.yaml +4 -4
  2. data/ext/cs__assess_array/cs__assess_array.c +7 -0
  3. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +19 -5
  4. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +1 -1
  5. data/ext/cs__assess_hash/cs__assess_hash.c +3 -4
  6. data/ext/cs__assess_kernel/cs__assess_kernel.c +1 -2
  7. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +26 -12
  8. data/ext/cs__assess_module/cs__assess_module.c +1 -1
  9. data/ext/cs__assess_regexp/cs__assess_regexp.c +15 -2
  10. data/ext/cs__assess_regexp/cs__assess_regexp.h +2 -0
  11. data/ext/cs__assess_string/cs__assess_string.c +21 -1
  12. data/ext/cs__assess_test/cs__assess_test.h +9 -0
  13. data/ext/cs__assess_test/cs__assess_tests.c +22 -0
  14. data/ext/cs__assess_test/extconf.rb +5 -0
  15. data/ext/cs__common/cs__common.c +113 -11
  16. data/ext/cs__common/cs__common.h +29 -5
  17. data/ext/cs__contrast_patch/cs__contrast_patch.c +55 -44
  18. data/ext/cs__os_information/cs__os_information.c +13 -10
  19. data/ext/cs__scope/cs__scope.c +146 -97
  20. data/ext/cs__tests/cs__tests.c +12 -0
  21. data/ext/cs__tests/cs__tests.h +3 -0
  22. data/ext/cs__tests/extconf.rb +5 -0
  23. data/lib/contrast/agent/assess/contrast_object.rb +16 -16
  24. data/lib/contrast/agent/assess/events/source_event.rb +17 -19
  25. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -0
  26. data/lib/contrast/agent/assess/policy/policy_node.rb +50 -27
  27. data/lib/contrast/agent/assess/policy/policy_node_utils.rb +51 -0
  28. data/lib/contrast/agent/assess/policy/policy_scanner.rb +2 -16
  29. data/lib/contrast/agent/assess/policy/preshift.rb +8 -2
  30. data/lib/contrast/agent/assess/policy/propagation_method.rb +47 -13
  31. data/lib/contrast/agent/assess/policy/propagator/buffer.rb +118 -0
  32. data/lib/contrast/agent/assess/policy/propagator/keep.rb +19 -4
  33. data/lib/contrast/agent/assess/policy/propagator/remove.rb +18 -2
  34. data/lib/contrast/agent/assess/policy/propagator/splat.rb +17 -3
  35. data/lib/contrast/agent/assess/policy/propagator/split.rb +15 -19
  36. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +1 -1
  37. data/lib/contrast/agent/assess/policy/propagator/substitution_utils.rb +1 -1
  38. data/lib/contrast/agent/assess/policy/propagator/trim.rb +1 -1
  39. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  40. data/lib/contrast/agent/assess/policy/source_method.rb +7 -7
  41. data/lib/contrast/agent/assess/policy/trigger_method.rb +4 -10
  42. data/lib/contrast/agent/assess/property/tagged.rb +1 -1
  43. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +7 -2
  44. data/lib/contrast/agent/assess/rule/response/auto_complete_rule.rb +1 -1
  45. data/lib/contrast/agent/assess/rule/response/base_rule.rb +11 -3
  46. data/lib/contrast/agent/assess/rule/response/body_rule.rb +3 -3
  47. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +60 -36
  48. data/lib/contrast/agent/assess/rule/response/framework/rails_support.rb +3 -3
  49. data/lib/contrast/agent/assess/rule/response/parameters_pollution_rule.rb +1 -1
  50. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +1 -2
  51. data/lib/contrast/agent/at_exit_hook.rb +1 -1
  52. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +0 -7
  53. data/lib/contrast/agent/deadzone/policy/policy.rb +0 -6
  54. data/lib/contrast/agent/exclusion_matcher.rb +3 -3
  55. data/lib/contrast/agent/inventory/database_config.rb +10 -3
  56. data/lib/contrast/agent/middleware.rb +5 -3
  57. data/lib/contrast/agent/patching/policy/after_load_patch.rb +0 -2
  58. data/lib/contrast/agent/patching/policy/patch.rb +13 -12
  59. data/lib/contrast/agent/patching/policy/patcher.rb +4 -4
  60. data/lib/contrast/agent/patching/policy/policy_node.rb +15 -2
  61. data/lib/contrast/agent/protect/exploitable_collection.rb +38 -0
  62. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +66 -9
  63. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -1
  64. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +2 -2
  65. data/lib/contrast/agent/protect/rule/base.rb +37 -5
  66. data/lib/contrast/agent/protect/rule/base_service.rb +3 -1
  67. data/lib/contrast/agent/protect/rule/cmd_injection.rb +13 -0
  68. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +83 -0
  69. data/lib/contrast/agent/protect/rule/cmdi/cmdi_worth_watching.rb +64 -0
  70. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +96 -0
  71. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +8 -0
  72. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +231 -0
  73. data/lib/contrast/agent/protect/rule/no_sqli.rb +27 -0
  74. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +18 -54
  75. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +1 -4
  76. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +82 -0
  77. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +45 -0
  78. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +42 -0
  79. data/lib/contrast/agent/reporting/attack_result/attack_result.rb +63 -0
  80. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +52 -0
  81. data/lib/contrast/agent/reporting/attack_result/response_type.rb +29 -0
  82. data/lib/contrast/agent/reporting/attack_result/user_input.rb +87 -0
  83. data/lib/contrast/agent/reporting/masker/masker.rb +243 -0
  84. data/lib/contrast/agent/reporting/masker/masker_utils.rb +62 -0
  85. data/lib/contrast/agent/reporting/report.rb +2 -0
  86. data/lib/contrast/agent/reporting/reporter.rb +29 -22
  87. data/lib/contrast/agent/reporting/reporter_heartbeat.rb +49 -0
  88. data/lib/contrast/agent/reporting/reporting_events/agent_startup.rb +34 -0
  89. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +53 -0
  90. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +48 -0
  91. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +64 -0
  92. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +70 -0
  93. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +57 -0
  94. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +56 -0
  95. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +12 -4
  96. data/lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb +58 -0
  97. data/lib/contrast/agent/reporting/reporting_events/application_reporting_event.rb +27 -0
  98. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +50 -0
  99. data/lib/contrast/agent/reporting/reporting_events/application_startup_instrumentation.rb +27 -0
  100. data/lib/contrast/agent/reporting/reporting_events/application_update.rb +7 -12
  101. data/lib/contrast/agent/reporting/reporting_events/finding.rb +10 -4
  102. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +2 -4
  103. data/lib/contrast/agent/reporting/reporting_events/finding_event_object.rb +3 -3
  104. data/lib/contrast/agent/reporting/reporting_events/library_usage_observation.rb +5 -5
  105. data/lib/contrast/agent/reporting/reporting_events/observed_library_usage.rb +6 -2
  106. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +16 -12
  107. data/lib/contrast/agent/reporting/reporting_events/poll.rb +6 -2
  108. data/lib/contrast/agent/reporting/reporting_events/preflight.rb +10 -8
  109. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +8 -11
  110. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +2 -1
  111. data/lib/contrast/agent/reporting/reporting_events/route_coverage.rb +8 -6
  112. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +12 -20
  113. data/lib/contrast/agent/reporting/reporting_events/server_reporting_event.rb +27 -0
  114. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +17 -27
  115. data/lib/contrast/agent/reporting/reporting_utilities/build_preflight.rb +38 -0
  116. data/lib/contrast/agent/reporting/reporting_utilities/dtm_message.rb +8 -0
  117. data/lib/contrast/agent/reporting/reporting_utilities/endpoints.rb +6 -0
  118. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +23 -7
  119. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +64 -76
  120. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +1 -1
  121. data/lib/contrast/agent/reporting/reporting_utilities/response.rb +17 -7
  122. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +100 -0
  123. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +75 -13
  124. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_mode.rb +63 -0
  125. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +154 -113
  126. data/lib/contrast/agent/reporting/settings/application_settings.rb +9 -0
  127. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +5 -33
  128. data/lib/contrast/agent/reporting/settings/protect.rb +1 -1
  129. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +2 -2
  130. data/lib/contrast/agent/reporting/settings/sampling.rb +36 -0
  131. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +110 -0
  132. data/lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb +58 -0
  133. data/lib/contrast/agent/request.rb +3 -3
  134. data/lib/contrast/agent/request_context.rb +1 -1
  135. data/lib/contrast/agent/request_context_extend.rb +2 -2
  136. data/lib/contrast/agent/request_handler.rb +7 -3
  137. data/lib/contrast/agent/response.rb +2 -0
  138. data/lib/contrast/agent/service_heartbeat.rb +6 -48
  139. data/lib/contrast/agent/static_analysis.rb +1 -1
  140. data/lib/contrast/agent/telemetry/base.rb +151 -0
  141. data/lib/contrast/agent/telemetry/events/event.rb +35 -0
  142. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +59 -0
  143. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +44 -0
  144. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message.rb +115 -0
  145. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +83 -0
  146. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +64 -0
  147. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions.rb +20 -0
  148. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions_report.rb +32 -0
  149. data/lib/contrast/agent/telemetry/events/metric_event.rb +28 -0
  150. data/lib/contrast/agent/telemetry/events/startup_metrics_event.rb +123 -0
  151. data/lib/contrast/agent/thread_watcher.rb +52 -68
  152. data/lib/contrast/agent/version.rb +1 -1
  153. data/lib/contrast/agent/worker_thread.rb +8 -0
  154. data/lib/contrast/agent.rb +4 -3
  155. data/lib/contrast/api/communication/messaging_queue.rb +28 -11
  156. data/lib/contrast/api/communication/response_processor.rb +7 -10
  157. data/lib/contrast/api/communication/speedracer.rb +1 -1
  158. data/lib/contrast/api/decorators/activity.rb +33 -0
  159. data/lib/contrast/api/decorators/address.rb +1 -1
  160. data/lib/contrast/api/decorators/http_request.rb +1 -1
  161. data/lib/contrast/api/decorators/response_type.rb +30 -0
  162. data/lib/contrast/api/decorators.rb +1 -0
  163. data/lib/contrast/components/app_context.rb +0 -4
  164. data/lib/contrast/components/assess.rb +14 -0
  165. data/lib/contrast/components/config.rb +13 -22
  166. data/lib/contrast/components/contrast_service.rb +9 -0
  167. data/lib/contrast/components/protect.rb +2 -2
  168. data/lib/contrast/components/sampling.rb +7 -11
  169. data/lib/contrast/components/settings.rb +116 -8
  170. data/lib/contrast/config/agent_configuration.rb +34 -41
  171. data/lib/contrast/config/api_configuration.rb +16 -75
  172. data/lib/contrast/config/api_proxy_configuration.rb +9 -48
  173. data/lib/contrast/config/application_configuration.rb +24 -95
  174. data/lib/contrast/config/assess_configuration.rb +21 -76
  175. data/lib/contrast/config/assess_rules_configuration.rb +13 -38
  176. data/lib/contrast/config/base_configuration.rb +11 -76
  177. data/lib/contrast/config/certification_configuration.rb +15 -68
  178. data/lib/contrast/config/exception_configuration.rb +15 -59
  179. data/lib/contrast/config/heap_dump_configuration.rb +19 -73
  180. data/lib/contrast/config/inventory_configuration.rb +11 -55
  181. data/lib/contrast/config/logger_configuration.rb +8 -41
  182. data/lib/contrast/config/protect_configuration.rb +23 -10
  183. data/lib/contrast/config/protect_rule_configuration.rb +23 -37
  184. data/lib/contrast/config/protect_rules_configuration.rb +39 -43
  185. data/lib/contrast/config/request_audit_configuration.rb +16 -55
  186. data/lib/contrast/config/root_configuration.rb +70 -13
  187. data/lib/contrast/config/ruby_configuration.rb +14 -47
  188. data/lib/contrast/config/sampling_configuration.rb +12 -65
  189. data/lib/contrast/config/server_configuration.rb +13 -45
  190. data/lib/contrast/config/service_configuration.rb +36 -17
  191. data/lib/contrast/configuration.rb +21 -13
  192. data/lib/contrast/extension/assess/string.rb +20 -1
  193. data/lib/contrast/extension/module.rb +0 -1
  194. data/lib/contrast/framework/manager.rb +2 -2
  195. data/lib/contrast/framework/rails/patch/support.rb +13 -45
  196. data/lib/contrast/logger/aliased_logging.rb +87 -0
  197. data/lib/contrast/logger/application.rb +1 -5
  198. data/lib/contrast/logger/cef_log.rb +1 -1
  199. data/lib/contrast/tasks/config.rb +100 -4
  200. data/lib/contrast/utils/assess/object_store.rb +36 -0
  201. data/lib/contrast/utils/assess/propagation_method_utils.rb +6 -0
  202. data/lib/contrast/utils/class_util.rb +5 -18
  203. data/lib/contrast/utils/input_classification.rb +73 -0
  204. data/lib/contrast/utils/invalid_configuration_util.rb +1 -1
  205. data/lib/contrast/utils/log_utils.rb +3 -1
  206. data/lib/contrast/utils/middleware_utils.rb +10 -9
  207. data/lib/contrast/utils/net_http_base.rb +1 -1
  208. data/lib/contrast/utils/object_share.rb +2 -1
  209. data/lib/contrast/utils/os.rb +0 -5
  210. data/lib/contrast/utils/patching/policy/patch_utils.rb +4 -5
  211. data/lib/contrast/utils/response_utils.rb +18 -33
  212. data/lib/contrast/utils/telemetry.rb +20 -2
  213. data/lib/contrast/utils/telemetry_client.rb +23 -11
  214. data/lib/contrast/utils/telemetry_hash.rb +41 -0
  215. data/lib/contrast/utils/telemetry_identifier.rb +16 -1
  216. data/lib/contrast.rb +9 -0
  217. data/resources/assess/policy.json +98 -0
  218. data/resources/deadzone/policy.json +0 -86
  219. data/ruby-agent.gemspec +10 -9
  220. data/service_executables/VERSION +1 -1
  221. data/service_executables/linux/contrast-service +0 -0
  222. data/service_executables/mac/contrast-service +0 -0
  223. metadata +98 -30
  224. data/lib/contrast/agent/metric_telemetry_event.rb +0 -26
  225. data/lib/contrast/agent/startup_metrics_telemetry_event.rb +0 -121
  226. data/lib/contrast/agent/telemetry.rb +0 -137
  227. data/lib/contrast/agent/telemetry_event.rb +0 -33
  228. data/lib/contrast/utils/exclude_key.rb +0 -20
@@ -0,0 +1,33 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/utils/string_utils'
5
+
6
+ module Contrast
7
+ module Api
8
+ module Decorators
9
+ # Used to decorate the {Contrast::Api::Dtm::Activity} protobuf model
10
+ # to handle conversion between framework route classes and the dtm.
11
+ module Activity
12
+ def self.included klass
13
+ klass.extend(ClassMethods)
14
+ end
15
+
16
+ # Class methods for Activity
17
+ module ClassMethods
18
+ def source_or_string obj
19
+ if obj.cs__is_a?(Regexp)
20
+ obj.source
21
+ elsif obj.cs__respond_to?(:safe_string)
22
+ obj.safe_string
23
+ else
24
+ obj.to_s
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ Contrast::Api::Dtm::Activity.include(Contrast::Api::Decorators::Activity)
@@ -28,7 +28,7 @@ module Contrast
28
28
  @_build_receiver ||= begin
29
29
  address = new
30
30
  address.host = 'localhost'
31
- address.ip = '127.0.0.1'
31
+ address.ip = '127.0.0.1' # rubocop:disable Style/IpAddresses
32
32
  begin
33
33
  Timeout.timeout(1) do
34
34
  address.host = Contrast::Utils::StringUtils.force_utf8(Socket.gethostname)
@@ -94,7 +94,7 @@ module Contrast
94
94
  return true if ::Contrast::AGENT.omit_body?
95
95
  return false if request.document_type != :NORMAL
96
96
 
97
- request.content_type&.include?('multipart/form-data')
97
+ request.media_type&.include?('multipart/form-data')
98
98
  end
99
99
 
100
100
  def append_pair map, key, value
@@ -0,0 +1,30 @@
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/api/dtm.pb'
5
+ require 'protobuf/message'
6
+
7
+ module Contrast
8
+ module Api
9
+ module Decorators
10
+ # Used to decorate the {Contrast::Api::Dtm::AttackResult::ResponseType} protobuf
11
+ # model so it can add SUSPICIOUS.
12
+ module ResponseType
13
+ ::Protobuf::Enum.define(:SUSPICIOUS, 6)
14
+
15
+ def self.included klass
16
+ klass.extend(ClassMethods)
17
+ end
18
+
19
+ # Used to add class methods to the class on inclusion of the decorator
20
+ module ClassMethods
21
+ def build
22
+ new
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
29
+
30
+ Contrast::Api::Dtm::AttackResult::ResponseType.include(Contrast::Api::Decorators::ResponseType)
@@ -28,3 +28,4 @@ require 'contrast/api/decorators/rasp_rule_sample'
28
28
  require 'contrast/api/decorators/user_input'
29
29
  require 'contrast/api/decorators/address'
30
30
  require 'contrast/api/decorators/http_request'
31
+ require 'contrast/api/decorators/response_type'
@@ -48,10 +48,6 @@ module Contrast
48
48
  end
49
49
  end
50
50
 
51
- def session_id
52
- @_session_id ||= build_app_startup_message.session_id
53
- end
54
-
55
51
  def app_version
56
52
  @_app_version ||= Contrast::CONFIG.root.application.version
57
53
  end
@@ -110,6 +110,20 @@ module Contrast
110
110
  []
111
111
  end
112
112
 
113
+ def track_original_object?
114
+ if @_track_original_object.nil?
115
+ @_track_original_object = !false?(::Contrast::CONFIG.root.assess.enable_original_object)
116
+ end
117
+
118
+ @_track_original_object
119
+ end
120
+
121
+ # The id for this process, based on the session metadata or id provided by the user, as indicated in
122
+ # application startup.
123
+ def session_id
124
+ ::Contrast::SETTINGS.assess_state.session_id
125
+ end
126
+
113
127
  private
114
128
 
115
129
  def forcibly_enabled?
@@ -72,6 +72,19 @@ module Contrast
72
72
  @config.loggable
73
73
  end
74
74
 
75
+ # Typically, this would be accessed through Contrast::SETTINGS, but we're specifically checking for the user
76
+ # provided value here rather than that echoed back by TeamServer.
77
+ #
78
+ # @return [String,nil] the value of the session id set in the configuration, or nil if unset
79
+ def session_id
80
+ root.application.session_id
81
+ end
82
+
83
+ # @return [String,nil] the value of the session metadata set in the configuration, or nil if unset
84
+ def session_metadata
85
+ root.application.session_metadata
86
+ end
87
+
75
88
  private
76
89
 
77
90
  SESSION_VARIABLES = 'Invalid configuration. '\
@@ -127,28 +140,6 @@ module Contrast
127
140
  end
128
141
  end
129
142
 
130
- # Typically, this would be accessed through
131
- # Contrast::Components::AppContext, but we're too early in the
132
- # initialization of the Agent to use that mechanism, so we look it up
133
- # directly for ourselves
134
- #
135
- # @return [String,nil] the value of the session id set in the
136
- # configuration, or nil if unset
137
- def session_id
138
- root.application.session_id
139
- end
140
-
141
- # Typically, this would be accessed through
142
- # Contrast::Components::AppContext, but we're too early in the
143
- # initialization of the Agent to use that mechanism, so we look it up
144
- # directly for ourselves
145
- #
146
- # @return [String,nil] the value of the session metadata set in the
147
- # configuration, or nil if unset
148
- def session_metadata
149
- root.application.session_metadata
150
- end
151
-
152
143
  # Typically, the following values would be accessed through Contrast::Components::AppContext
153
144
  # and Contrast::Components::API, but we're too early in the initialization of the Agent to use
154
145
  # that mechanism, so we look it up directly for ourselves.
@@ -38,6 +38,15 @@ module Contrast
38
38
  @_use_agent_communication = true?(::Contrast::CONFIG.root.agent.service.bypass)
39
39
  end
40
40
 
41
+ # If we're using the agent directly and not using protect, then there is no need to start the service. Because
42
+ # we only know this at startup when hardcoded as such (b/c TS could turn protect on otherwise), we can only do
43
+ # so when bypass is on and protect is off in local config
44
+ #
45
+ # @return [Boolean]
46
+ def unnecessary?
47
+ ::Contrast::CONTRAST_SERVICE.use_agent_communication? && ::Contrast::PROTECT.forcibly_disabled?
48
+ end
49
+
41
50
  def host
42
51
  @_host ||=
43
52
  (::Contrast::CONFIG.root.agent.service.host || Contrast::Config::ServiceConfiguration::DEFAULT_HOST).to_s
@@ -41,7 +41,7 @@ module Contrast
41
41
  def report_any_command_execution?
42
42
  if @_report_any_command_execution.nil?
43
43
  ctrl = rule_config[Contrast::Agent::Protect::Rule::CmdInjection::NAME]
44
- @_report_any_command_execution = true?(ctrl.disable_system_commands)
44
+ @_report_any_command_execution = ctrl && true?(ctrl.disable_system_commands)
45
45
  end
46
46
  @_report_any_command_execution
47
47
  end
@@ -50,7 +50,7 @@ module Contrast
50
50
  if @_report_custom_code_sysfile_access.nil?
51
51
  name_changed = Contrast::Agent::Protect::Rule::PathTraversal::NAME.tr('-', '_')
52
52
  ctrl = rule_config[name_changed]
53
- @_report_custom_code_sysfile_access = true?(ctrl.detect_custom_code_accessing_system_files)
53
+ @_report_custom_code_sysfile_access = ctrl && true?(ctrl.detect_custom_code_accessing_system_files)
54
54
  end
55
55
  @_report_custom_code_sysfile_access
56
56
  end
@@ -48,7 +48,7 @@ module Contrast
48
48
  # @param settings [Contrast::Api::Settings::Sampling] the Sampling settings as provided by TeamServer
49
49
  # @return [Boolean] the resolution of the config_settings, settings, and default value
50
50
  def enabled? config_settings, settings
51
- true?([config_settings&.enable, settings&.enabled, DEFAULT_SAMPLING_ENABLED].reject(&:nil?)[0])
51
+ true?([config_settings&.enable, settings&.enabled, DEFAULT_SAMPLING_ENABLED].compact[0])
52
52
  end
53
53
 
54
54
  # @param config_settings [Contrast::Config::SamplingConfiguration] the Sampling configuration as provided by
@@ -56,7 +56,7 @@ module Contrast
56
56
  # @param settings [Contrast::Api::Settings::Sampling] the Sampling settings as provided by TeamServer
57
57
  # @return [Integer] the resolution of the config_settings, settings, and default value
58
58
  def baseline config_settings, settings
59
- [config_settings&.baseline, settings&.baseline, DEFAULT_SAMPLING_BASELINE].map(&:to_i).find(&:positive?)
59
+ [config_settings&.baseline, settings&.baseline].map(&:to_i).find(&:positive?) || DEFAULT_SAMPLING_BASELINE
60
60
  end
61
61
 
62
62
  # @param config_settings [Contrast::Config::SamplingConfiguration] the Sampling configuration as provided by
@@ -64,10 +64,8 @@ module Contrast
64
64
  # @param settings [Contrast::Api::Settings::Sampling] the Sampling settings as provided by TeamServer
65
65
  # @return [Integer] the resolution of the config_settings, settings, and default value
66
66
  def request_frequency config_settings, settings
67
- [
68
- config_settings&.request_frequency, settings&.request_frequency,
69
- DEFAULT_SAMPLING_REQUEST_FREQUENCY
70
- ].map(&:to_i).find(&:positive?)
67
+ [config_settings&.request_frequency, settings&.request_frequency].map(&:to_i).find(&:positive?) ||
68
+ DEFAULT_SAMPLING_REQUEST_FREQUENCY
71
69
  end
72
70
 
73
71
  # @param config_settings [Contrast::Config::SamplingConfiguration] the Sampling configuration as provided by
@@ -75,10 +73,8 @@ module Contrast
75
73
  # @param settings [Contrast::Api::Settings::Sampling] the Sampling settings as provided by TeamServer
76
74
  # @return [Integer] the resolution of the config_settings, settings, and default value
77
75
  def response_frequency config_settings, settings
78
- [
79
- config_settings&.response_frequency, settings&.response_frequency,
80
- DEFAULT_SAMPLING_RESPONSE_FREQUENCY
81
- ].map(&:to_i).find(&:positive?)
76
+ [config_settings&.response_frequency, settings&.response_frequency].map(&:to_i).find(&:positive?) ||
77
+ DEFAULT_SAMPLING_RESPONSE_FREQUENCY
82
78
  end
83
79
 
84
80
  # @param config_settings [Contrast::Config::SamplingConfiguration] the Sampling configuration as provided by
@@ -86,7 +82,7 @@ module Contrast
86
82
  # @param settings [Contrast::Api::Settings::Sampling] the Sampling settings as provided by TeamServer
87
83
  # @return [Integer] the resolution of the config_settings, settings, and default value
88
84
  def window config_settings, settings
89
- [config_settings&.window_ms, settings&.window_ms, DEFAULT_SAMPLING_WINDOW_MS].map(&:to_i).find(&:positive?)
85
+ [config_settings&.window_ms, settings&.window_ms].map(&:to_i).find(&:positive?) || DEFAULT_SAMPLING_WINDOW_MS
90
86
  end
91
87
  end
92
88
 
@@ -2,6 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'contrast/api/settings.pb'
5
+ require 'contrast/agent/reporting/settings/sensitive_data_masking'
5
6
 
6
7
  module Contrast
7
8
  module Components
@@ -13,12 +14,14 @@ module Contrast
13
14
  APPLICATION_STATE_BASE = Struct.new(:modes_by_id, :exclusion_matchers).
14
15
  new(Hash.new(Contrast::Api::Settings::ProtectionRule::Mode::NO_ACTION), [])
15
16
  PROTECT_STATE_BASE = Struct.new(:enabled, :rules).new(false, {})
16
- ASSESS_STATE_BASE = Struct.new(:enabled, :sampling_settings, :disabled_assess_rules).new(false, nil, []) do
17
+ ASSESS_STATE_BASE = Struct.new(:enabled, :sampling_settings, :disabled_assess_rules, :session_id).new(false, nil,
18
+ [], nil) do
17
19
  def sampling_settings= new_val
18
20
  @sampling_settings = new_val
19
21
  Contrast::Utils::Assess::SamplingUtil.instance.update
20
22
  end
21
23
  end
24
+ SENSITIVE_DATA_MASKING_BASE = Contrast::Agent::Reporting::Settings::SensitiveDataMasking.new
22
25
 
23
26
  # This is a class.
24
27
  class Interface
@@ -26,7 +29,67 @@ module Contrast
26
29
 
27
30
  # tainted_columns are database columns that receive unsanitized input.
28
31
  attr_reader :tainted_columns # This can probably go into assess_state?
29
- attr_reader :assess_state, :protect_state, :application_state
32
+ # Current state for Assess.
33
+ # enabled [Boolean] Indicate if the assess feature set is enabled for this server or not.
34
+ #
35
+ # sampling [Hash<AssessSampling>] Hash of AssessSampling Used to control the sampling feature in the agent: {
36
+ # baseline [Integer] The number of baseline requests to take before switching to sampling
37
+ # for the window.
38
+ # enabled [Boolean] If the sampling feature should be used or not.
39
+ # frequency [Integer] The number of requests to skip before observing during the sampling
40
+ # window after the baseline.
41
+ # responseFrequency [Integer]
42
+ # window [Integer]
43
+ # }
44
+ #
45
+ # disabled_assess_rules [array<AssessRuleID(String)>] Assess rules to disable for this application.
46
+ attr_reader :assess_state
47
+ # Current State for Protect.
48
+ # enabled [Boolean] Indicate if the protect feature set is enabled for this server or not.
49
+ #
50
+ # Protection rules are returned as:
51
+ # rules [Hash<RULE_ID => MODE>, nil] Hash with rule_id as key and mode as value
52
+ attr_reader :protect_state
53
+ # Current Application State.
54
+ #
55
+ # modes_by_id [Hash<Rule_id => Mode] Returns Hash with rules and their current mode.
56
+ # exclusion_matchers [Array] Array of all the exclusions.
57
+ # code_exclusions [Array<CodeExclusion>] Array of CodeExclusion: {
58
+ # name [String] The name of the exclusion as defined by the user in TS.
59
+ # modes [String] If this exclusion applies to assess or protect. [assess, defend]
60
+ # assess_rules [Array] Array of assess rules to which this exclusion applies. AssessRuleID [String]
61
+ # denylist [String] The call, if in the stack, should result in the agent not taking action.
62
+ # input_exclusions [Array<InputExclusions>] Array of InputExclusions: {
63
+ # name [String] The name of the input.
64
+ # modes [String] If this exclusion applies to assess or protect. [assess, defend]
65
+ # assess_rules [Array] Array of assess rules to which this exclusion applies. AssessRuleID [String]
66
+ # protect_rules [Array] Array of ProtectRuleID [String] The protect rules to which this exclusion applies.
67
+ # urls [Array] Array of URLs to which the exclusions apply. URL [String]
68
+ # match_strategy [String] If this exclusion applies to all URLs or only those specified. [ALL, ONLY]
69
+ # type [String] The type of the input [COOKIE, PARAMETER, HEADER, BODY, QUERYSTRING]
70
+ # }
71
+ # url_exclusions [Array<UrlExclusions>] Array of UrlExclusions: {
72
+ # name [String] The name of the input.
73
+ # modes [String] If this exclusion applies to assess or protect. [assess, defend]
74
+ # assess_rules [Array] Array of assess rules to which this exclusion applies. AssessRuleID [String]
75
+ # protect_rules [Array] Array of ProtectRuleID [String] The protect rules to which this exclusion applies.
76
+ # urls [Array] Array of URLs to which the exclusions apply. URL [String]
77
+ # match_strategy [String] If this exclusion applies to all URLs or only those specified. [ALL, ONLY]
78
+ # type [String] The type of the input [COOKIE, PARAMETER, HEADER, BODY, QUERYSTRING]
79
+ # }
80
+ attr_reader :application_state
81
+ # This the structure that will hold the masking rules send from TS.
82
+ #
83
+ # @return [Contrast::Agent::Reporting::Settings::SensitiveDataMasking]:
84
+ # mask_http_body [Boolean] Policy flag to enable the use of masking on request body.
85
+ # rules [Array<Contrast::Agent::Reporting::Settings::SensitiveDataMaskingRule>]
86
+ # Rules to follow when using the masking. Each rules contains Id [String]
87
+ # and Keywords [Array<String>].
88
+ attr_reader :sensitive_data_masking
89
+ # @return [Integer] the time, in ms, that application settings last changed
90
+ attr_reader :last_app_update_ms
91
+ # @return [Integer] the time, in ms, that server settings last changed
92
+ attr_reader :last_server_update_ms
30
93
 
31
94
  def initialize
32
95
  reset_state
@@ -38,30 +101,43 @@ module Contrast
38
101
 
39
102
  # @param features [Contrast::Api::Settings::ServerFeatures, Contrast::Agent::Reporting::Response]
40
103
  def update_from_server_features features
41
- if features&.class == Contrast::Agent::Reporting::Response
42
- @protect_state.enabled = features.server_features.protect.enabled?
43
- @assess_state.enabled = features.server_features.assess.enabled?
44
- @assess_state.sampling_settings = features.server_features.assess.sampling
104
+ if features.cs__is_a?(Contrast::Agent::Reporting::Response)
105
+ server_features = features.server_features
106
+ return unless server_features
107
+
108
+ log_file = server_features.log_file
109
+ log_level = server_features.log_level
110
+ Contrast::Logger::Log.instance.update(log_file, log_level) if log_file || log_level
111
+ @protect_state.enabled = server_features.protect.enabled?
112
+ @assess_state.enabled = server_features.assess.enabled?
113
+ @assess_state.sampling_settings = server_features.assess.sampling
45
114
  else
46
115
  @protect_state.enabled = features.protect_enabled?
47
116
  @assess_state.enabled = features.assess_enabled?
48
117
  @assess_state.sampling_settings = features.assess.sampling
49
118
  end
119
+ @last_server_update_ms = Contrast::Utils::Timer.now_ms
50
120
  end
51
121
 
52
122
  # @param features [Contrast::Api::Settings::ApplicationSettings, Contrast::Agent::Reporting::Response]
53
123
  def update_from_application_settings features
54
124
  if features&.class == Contrast::Agent::Reporting::Response
55
- @application_state.modes_by_id = features.application_settings.protect.protection_rules_to_settings_hash
125
+ settings = features.application_settings
126
+ return unless settings
127
+
128
+ @application_state.modes_by_id = settings.protect.protection_rules_to_settings_hash
56
129
  # TODO: RUBY-1438 this needs to be translated
57
130
  # @application_state.exclusion_matchers = new_vals[:exclusion_matchers]
58
- @assess_state.disabled_assess_rules = features.application_settings.assess.disabled_rules
131
+ update_sensitive_data_policy(settings.sensitive_data_masking)
132
+ @assess_state.disabled_assess_rules = settings.assess.disabled_rules
133
+ @assess_state.session_id = settings.assess.session_id if settings.assess.session_id
59
134
  else
60
135
  new_vals = features.application_state_translation
61
136
  @application_state.modes_by_id = new_vals[:modes_by_id]
62
137
  @application_state.exclusion_matchers = new_vals[:exclusion_matchers]
63
138
  @assess_state.disabled_assess_rules = new_vals[:disabled_assess_rules]
64
139
  end
140
+ @last_app_update_ms = Contrast::Utils::Timer.now_ms
65
141
  end
66
142
 
67
143
  # Wipe state to zero.
@@ -70,6 +146,7 @@ module Contrast
70
146
  @assess_state = ASSESS_STATE_BASE.dup
71
147
  @application_state = APPLICATION_STATE_BASE.dup
72
148
  @tainted_columns = {}
149
+ @sensitive_data_masking = SENSITIVE_DATA_MASKING_BASE.dup
73
150
  end
74
151
 
75
152
  def build_protect_rules
@@ -86,6 +163,37 @@ module Contrast
86
163
  Contrast::Agent::Protect::Rule::Xss.new
87
164
  Contrast::Agent::Protect::Rule::Xxe.new
88
165
  end
166
+
167
+ # Update the sensitive data masking policy from settings,
168
+ # received from TS. In case the settings are empty,
169
+ # keep current ones.
170
+ #
171
+ # @param sensitive_data_masking [Contrast::Agent::Reporting::Settings::SensitiveDataMasking]
172
+ # Ts Response settings for sensitive data masking policy
173
+ def update_sensitive_data_policy sensitive_data_masking
174
+ @sensitive_data_masking.mask_http_body = sensitive_data_masking.mask_http_body? unless
175
+ settings_empty?(sensitive_data_masking.mask_http_body?)
176
+ @sensitive_data_masking.mask_attack_vector = sensitive_data_masking.mask_attack_vector? unless
177
+ settings_empty?(sensitive_data_masking.mask_attack_vector?)
178
+ return if settings_empty?(sensitive_data_masking.rules)
179
+
180
+ @sensitive_data_masking.rules = sensitive_data_masking.rules
181
+ # update with the newly received rules.
182
+ Contrast::Agent::Reporting::Masker.send(:update_dictionary)
183
+ end
184
+
185
+ private
186
+
187
+ # check if settings are empty and return true if so.
188
+ #
189
+ # @param settings [String, Boolean, Array, Hash]
190
+ # @return true | false
191
+ def settings_empty? settings
192
+ return false if !!settings == settings
193
+ return true if settings.nil? || settings.empty?
194
+
195
+ false
196
+ end
89
197
  end
90
198
  end
91
199
  end
@@ -9,61 +9,54 @@ require 'contrast/config/api_configuration'
9
9
 
10
10
  module Contrast
11
11
  module Config
12
- # Common Configuration settings. Those in this section pertain to the
13
- # core functionality of the Agent.
14
- class AgentConfiguration < BaseConfiguration
15
- attr_accessor :enable, :omit_body, :start_bundled_service, :api
16
- attr_writer :ruby, :service, :logger, :heap_dump
12
+ # Common Configuration settings. Those in this section pertain to the core functionality of the Agent.
13
+ class AgentConfiguration
14
+ include Contrast::Config::BaseConfiguration
15
+
16
+ # @return [Boolean, nil]
17
+ attr_accessor :enable
18
+ # @return [Boolean, nil]
19
+ attr_accessor :omit_body
20
+ # @return [Contrast::Config::RubyConfiguration]
21
+ attr_writer :ruby
22
+ # @return [Contrast::Config::ServiceConfiguration]
23
+ attr_writer :service
24
+ # @return [ Contrast::Config::LoggerConfiguration]
25
+ attr_writer :logger
26
+ # @return [Contrast::Config::HeapDumpConfiguration]
27
+ attr_writer :heap_dump
17
28
 
18
29
  def initialize hsh = {}
19
- @enable = traverse_config(hsh, :enable)
20
- @start_bundled_service = traverse_config(hsh, :start_bundled_service)
21
- @omit_body = traverse_config(hsh, :omit_body)
22
- @service = Contrast::Config::ServiceConfiguration.new(traverse_config(hsh, :service))
23
- @logger = Contrast::Config::LoggerConfiguration.new(traverse_config(hsh, :logger))
24
- @ruby = Contrast::Config::RubyConfiguration.new(traverse_config(hsh, :ruby))
25
- @heap_dump = Contrast::Config::HeapDumpConfiguration.new(traverse_config(hsh, :heap_dump))
26
- @configuration_map = {}
27
- build_configuration_map
30
+ return unless hsh
31
+
32
+ @enable = hsh[:enable]
33
+ @start_bundled_service = hsh[:start_bundled_service]
34
+ @omit_body = hsh[:omit_body]
35
+ @service = Contrast::Config::ServiceConfiguration.new(hsh[:service])
36
+ @logger = Contrast::Config::LoggerConfiguration.new(hsh[:logger])
37
+ @ruby = Contrast::Config::RubyConfiguration.new(hsh[:ruby])
38
+ @heap_dump = Contrast::Config::HeapDumpConfiguration.new(hsh[:heap_dump])
39
+ end
40
+
41
+ # @return [Boolean, true]
42
+ def start_bundled_service
43
+ @start_bundled_service.nil? ? true : @start_bundled_service
28
44
  end
29
45
 
30
46
  def service
31
- @service ||= Contrast::Config::ServiceConfiguration.new({})
47
+ @service ||= Contrast::Config::ServiceConfiguration.new
32
48
  end
33
49
 
34
50
  def logger
35
- @logger ||= Contrast::Config::LoggerConfiguration.new({})
51
+ @logger ||= Contrast::Config::LoggerConfiguration.new
36
52
  end
37
53
 
38
54
  def ruby
39
- @ruby ||= Contrast::Config::RubyConfiguration.new({})
55
+ @ruby ||= Contrast::Config::RubyConfiguration.new
40
56
  end
41
57
 
42
58
  def heap_dump
43
- @heap_dump ||= Contrast::Config::HeapDumpConfiguration.new({})
44
- end
45
-
46
- # Traverse the given entity to build out the configuration graph.
47
- #
48
- # The values will be either a hash, indicating internal nodes to
49
- # traverse, or a value to set or the EMPTY_VALUE symbol, indicating a
50
- # leaf node.
51
- #
52
- # The spec_key are the Contrast defined keys based on the instance variables of
53
- # a given configuration.
54
- def traverse_config values, spec_key
55
- internal_nodes = values.cs__respond_to?(:has_key?)
56
- val = internal_nodes ? value_from_key_config(spec_key, values) : nil
57
- val == EMPTY_VALUE ? nil : val
58
- end
59
-
60
- def build_configuration_map
61
- instance_variables.each do |key|
62
- str_key = key.to_s.tr('@', '')
63
- next if str_key == 'configuration_map'
64
-
65
- @configuration_map[str_key] = send(str_key.to_sym)
66
- end
59
+ @heap_dump ||= Contrast::Config::HeapDumpConfiguration.new
67
60
  end
68
61
  end
69
62
  end
@@ -8,26 +8,29 @@ require 'contrast/config/request_audit_configuration'
8
8
  module Contrast
9
9
  module Config
10
10
  # Api keys configuration
11
- class ApiConfiguration < BaseConfiguration
11
+ class ApiConfiguration
12
+ include Contrast::Config::BaseConfiguration
13
+
12
14
  # @return [String]
13
- attr_reader :api_key
15
+ attr_accessor :api_key
14
16
  # @return [String]
15
- attr_reader :user_name
17
+ attr_accessor :user_name
16
18
  # @return [String]
17
- attr_reader :service_key
19
+ attr_accessor :service_key
20
+ attr_writer :url, :proxy, :request_audit, :certificate
18
21
 
19
22
  DEFAULT_URL = 'https://app.contrastsecurity.com/Contrast'
20
23
 
21
24
  def initialize hsh = {}
22
- @api_key = traverse_config(hsh, :api_key)
23
- @url = traverse_config(hsh, :url)
24
- @user_name = traverse_config(hsh, :user_name)
25
- @service_key = traverse_config(hsh, :service_key)
26
- @proxy = Contrast::Config::ApiProxyConfiguration.new(traverse_config(hsh, :proxy))
27
- @request_audit = Contrast::Config::RequestAuditConfiguration.new(traverse_config(hsh, :request_audit))
28
- @certificate = Contrast::Config::CertificationConfiguration.new(traverse_config(hsh, :certificate))
29
- @configuration_map = {}
30
- build_configuration_map
25
+ return unless hsh
26
+
27
+ @api_key = hsh[:api_key]
28
+ @url = hsh[:url]
29
+ @user_name = hsh[:user_name]
30
+ @service_key = hsh[:service_key]
31
+ @proxy = Contrast::Config::ApiProxyConfiguration.new(hsh[:proxy])
32
+ @request_audit = Contrast::Config::RequestAuditConfiguration.new(hsh[:request_audit])
33
+ @certificate = Contrast::Config::CertificationConfiguration.new(hsh[:certificate])
31
34
  end
32
35
 
33
36
  def url
@@ -48,68 +51,6 @@ module Contrast
48
51
  def certificate
49
52
  @certificate ||= Contrast::Config::CertificationConfiguration.new
50
53
  end
51
-
52
- def api_key= value
53
- self['api_key'] = value
54
- end
55
-
56
- def url= value
57
- self['url'] = value
58
- end
59
-
60
- def user_name= value
61
- self['user_name'] = value
62
- end
63
-
64
- def service_key= value
65
- self['service_key'] = value
66
- end
67
-
68
- def proxy= value
69
- self['proxy'] = value
70
- end
71
-
72
- def request_audit= value
73
- self['request_audit'] = value
74
- end
75
-
76
- def certificate= value
77
- self['certificate'] = value
78
- end
79
-
80
- # TODO: RUBY-1493 MOVE TO BASE CONFIG
81
-
82
- def []= key, value
83
- instance_variable_set("@#{ key }".to_sym, value)
84
- @configuration_map[key] = value
85
- end
86
-
87
- def [] key
88
- send(key.to_sym)
89
- end
90
-
91
- # Traverse the given entity to build out the configuration graph.
92
- #
93
- # The values will be either a hash, indicating internal nodes to
94
- # traverse, or a value to set or the EMPTY_VALUE symbol, indicating a
95
- # leaf node.
96
- #
97
- # The spec_key are the Contrast defined keys based on the instance variables of
98
- # a given configuration.
99
- def traverse_config values, spec_key
100
- internal_nodes = values.cs__respond_to?(:has_key?)
101
- val = internal_nodes ? value_from_key_config(spec_key, values) : nil
102
- val == EMPTY_VALUE ? nil : val
103
- end
104
-
105
- def build_configuration_map
106
- instance_variables.each do |key|
107
- str_key = key.to_s.tr('@', '')
108
- next if str_key == 'configuration_map'
109
-
110
- @configuration_map[str_key] = send(str_key.to_sym)
111
- end
112
- end
113
54
  end
114
55
  end
115
56
  end