contrast-agent 5.1.0 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (218) 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 +7 -5
  7. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +26 -12
  8. data/ext/cs__assess_module/cs__assess_module.c +7 -7
  9. data/ext/cs__assess_string/cs__assess_string.c +13 -1
  10. data/ext/cs__common/cs__common.c +16 -11
  11. data/ext/cs__common/cs__common.h +1 -0
  12. data/ext/cs__contrast_patch/cs__contrast_patch.c +100 -64
  13. data/ext/cs__contrast_patch/cs__contrast_patch.h +2 -0
  14. data/ext/cs__os_information/cs__os_information.c +13 -10
  15. data/ext/cs__scope/cs__scope.c +796 -0
  16. data/ext/cs__scope/cs__scope.h +88 -0
  17. data/ext/cs__scope/extconf.rb +5 -0
  18. data/lib/contrast/agent/assess/contrast_event.rb +20 -13
  19. data/lib/contrast/agent/assess/contrast_object.rb +4 -1
  20. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -0
  21. data/lib/contrast/agent/assess/policy/policy_node.rb +50 -27
  22. data/lib/contrast/agent/assess/policy/policy_node_utils.rb +51 -0
  23. data/lib/contrast/agent/assess/policy/preshift.rb +8 -2
  24. data/lib/contrast/agent/assess/policy/propagation_method.rb +47 -13
  25. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -5
  26. data/lib/contrast/agent/assess/policy/propagator/buffer.rb +118 -0
  27. data/lib/contrast/agent/assess/policy/propagator/keep.rb +19 -4
  28. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +2 -0
  29. data/lib/contrast/agent/assess/policy/propagator/remove.rb +18 -2
  30. data/lib/contrast/agent/assess/policy/propagator/splat.rb +17 -3
  31. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +1 -1
  32. data/lib/contrast/agent/assess/policy/propagator/substitution_utils.rb +1 -1
  33. data/lib/contrast/agent/assess/policy/propagator/trim.rb +1 -1
  34. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  35. data/lib/contrast/agent/assess/policy/source_method.rb +7 -7
  36. data/lib/contrast/agent/assess/policy/trigger_method.rb +6 -1
  37. data/lib/contrast/agent/assess/property/tagged.rb +1 -1
  38. data/lib/contrast/agent/assess/rule/response/{autocomplete_rule.rb → auto_complete_rule.rb} +4 -3
  39. data/lib/contrast/agent/assess/rule/response/base_rule.rb +12 -79
  40. data/lib/contrast/agent/assess/rule/response/body_rule.rb +109 -0
  41. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +157 -0
  42. data/lib/contrast/agent/assess/rule/response/click_jacking_header_rule.rb +26 -0
  43. data/lib/contrast/agent/assess/rule/response/csp_header_insecure_rule.rb +14 -15
  44. data/lib/contrast/agent/assess/rule/response/csp_header_missing_rule.rb +5 -25
  45. data/lib/contrast/agent/assess/rule/response/framework/rails_support.rb +29 -0
  46. data/lib/contrast/agent/assess/rule/response/header_rule.rb +70 -0
  47. data/lib/contrast/agent/assess/rule/response/hsts_header_rule.rb +12 -36
  48. data/lib/contrast/agent/assess/rule/response/parameters_pollution_rule.rb +2 -1
  49. data/lib/contrast/agent/assess/rule/response/x_content_type_header_rule.rb +26 -0
  50. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +35 -0
  51. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +0 -7
  52. data/lib/contrast/agent/deadzone/policy/policy.rb +0 -6
  53. data/lib/contrast/agent/exclusion_matcher.rb +3 -3
  54. data/lib/contrast/agent/middleware.rb +4 -1
  55. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +1 -3
  56. data/lib/contrast/agent/patching/policy/patch.rb +2 -6
  57. data/lib/contrast/agent/patching/policy/patcher.rb +4 -4
  58. data/lib/contrast/agent/patching/policy/policy_node.rb +15 -2
  59. data/lib/contrast/agent/protect/exploitable_collection.rb +38 -0
  60. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +147 -0
  61. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -1
  62. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +2 -2
  63. data/lib/contrast/agent/protect/rule/base.rb +61 -2
  64. data/lib/contrast/agent/protect/rule/base_service.rb +12 -1
  65. data/lib/contrast/agent/protect/rule/cmd_injection.rb +15 -0
  66. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +83 -0
  67. data/lib/contrast/agent/protect/rule/cmdi/cmdi_worth_watching.rb +64 -0
  68. data/lib/contrast/agent/protect/rule/deserialization.rb +6 -0
  69. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +96 -0
  70. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +13 -1
  71. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +231 -0
  72. data/lib/contrast/agent/protect/rule/no_sqli.rb +28 -0
  73. data/lib/contrast/agent/protect/rule/path_traversal.rb +1 -0
  74. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +88 -0
  75. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +118 -0
  76. data/lib/contrast/agent/protect/rule/sqli.rb +33 -0
  77. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +82 -0
  78. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +45 -0
  79. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +42 -0
  80. data/lib/contrast/agent/protect/rule/xxe.rb +4 -0
  81. data/lib/contrast/agent/reporting/attack_result/attack_result.rb +63 -0
  82. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +52 -0
  83. data/lib/contrast/agent/reporting/attack_result/response_type.rb +29 -0
  84. data/lib/contrast/agent/reporting/attack_result/user_input.rb +87 -0
  85. data/lib/contrast/agent/reporting/input_analysis/input_analysis.rb +44 -0
  86. data/lib/contrast/agent/reporting/input_analysis/input_analysis_result.rb +115 -0
  87. data/lib/contrast/agent/reporting/input_analysis/input_type.rb +44 -0
  88. data/lib/contrast/agent/reporting/input_analysis/score_level.rb +21 -0
  89. data/lib/contrast/agent/reporting/masker/masker.rb +246 -0
  90. data/lib/contrast/agent/reporting/masker/masker_utils.rb +58 -0
  91. data/lib/contrast/agent/reporting/report.rb +3 -0
  92. data/lib/contrast/agent/reporting/reporter.rb +31 -12
  93. data/lib/contrast/agent/reporting/reporting_events/agent_startup.rb +30 -0
  94. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +7 -3
  95. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +40 -0
  96. data/lib/contrast/agent/reporting/reporting_events/application_startup_instrumentation.rb +27 -0
  97. data/lib/contrast/agent/reporting/reporting_events/finding.rb +69 -36
  98. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +88 -59
  99. data/lib/contrast/agent/reporting/reporting_events/{finding_object.rb → finding_event_object.rb} +24 -20
  100. data/lib/contrast/agent/reporting/reporting_events/finding_event_parent_object.rb +39 -0
  101. data/lib/contrast/agent/reporting/reporting_events/finding_event_property.rb +40 -0
  102. data/lib/contrast/agent/reporting/reporting_events/{finding_signature.rb → finding_event_signature.rb} +29 -24
  103. data/lib/contrast/agent/reporting/reporting_events/finding_event_source.rb +12 -8
  104. data/lib/contrast/agent/reporting/reporting_events/{finding_stack.rb → finding_event_stack.rb} +23 -19
  105. data/lib/contrast/agent/reporting/reporting_events/{finding_taint_range.rb → finding_event_taint_range.rb} +17 -15
  106. data/lib/contrast/agent/reporting/reporting_events/finding_request.rb +26 -53
  107. data/lib/contrast/agent/reporting/reporting_events/library_usage_observation.rb +5 -5
  108. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +9 -9
  109. data/lib/contrast/agent/reporting/reporting_events/poll.rb +29 -0
  110. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +2 -1
  111. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +6 -4
  112. data/lib/contrast/agent/reporting/reporting_events/route_coverage.rb +8 -6
  113. data/lib/contrast/agent/reporting/reporting_events/route_discovery.rb +1 -0
  114. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +1 -1
  115. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +10 -3
  116. data/lib/contrast/agent/reporting/reporting_utilities/endpoints.rb +0 -1
  117. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +17 -5
  118. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +54 -45
  119. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +1 -1
  120. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +97 -0
  121. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +69 -7
  122. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_mode.rb +63 -0
  123. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +123 -85
  124. data/lib/contrast/agent/reporting/settings/application_settings.rb +9 -0
  125. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +5 -33
  126. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +1 -1
  127. data/lib/contrast/agent/reporting/settings/sampling.rb +36 -0
  128. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +110 -0
  129. data/lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb +58 -0
  130. data/lib/contrast/agent/request_context.rb +7 -2
  131. data/lib/contrast/agent/request_context_extend.rb +85 -21
  132. data/lib/contrast/agent/request_handler.rb +4 -0
  133. data/lib/contrast/agent/scope.rb +102 -107
  134. data/lib/contrast/agent/service_heartbeat.rb +45 -2
  135. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +51 -0
  136. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +36 -0
  137. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message.rb +97 -0
  138. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +65 -0
  139. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +47 -0
  140. data/lib/contrast/agent/{metric_telemetry_event.rb → telemetry/events/metric_telemetry_event.rb} +1 -1
  141. data/lib/contrast/agent/{startup_metrics_telemetry_event.rb → telemetry/events/startup_metrics_telemetry_event.rb} +3 -3
  142. data/lib/contrast/agent/{telemetry_event.rb → telemetry/events/telemetry_event.rb} +1 -1
  143. data/lib/contrast/agent/{telemetry.rb → telemetry/telemetry.rb} +32 -19
  144. data/lib/contrast/agent/thread_watcher.rb +1 -1
  145. data/lib/contrast/agent/version.rb +1 -1
  146. data/lib/contrast/agent.rb +3 -0
  147. data/lib/contrast/api/communication/speedracer.rb +1 -1
  148. data/lib/contrast/api/decorators/address.rb +1 -1
  149. data/lib/contrast/api/decorators/bot_blocker.rb +37 -0
  150. data/lib/contrast/api/decorators/ip_denylist.rb +37 -0
  151. data/lib/contrast/api/decorators/rasp_rule_sample.rb +29 -0
  152. data/lib/contrast/api/decorators/response_type.rb +30 -0
  153. data/lib/contrast/api/decorators/user_input.rb +11 -1
  154. data/lib/contrast/api/decorators/virtual_patch.rb +34 -0
  155. data/lib/contrast/api/decorators.rb +1 -0
  156. data/lib/contrast/components/app_context.rb +0 -4
  157. data/lib/contrast/components/assess.rb +14 -0
  158. data/lib/contrast/components/logger.rb +5 -0
  159. data/lib/contrast/components/protect.rb +6 -4
  160. data/lib/contrast/components/sampling.rb +7 -11
  161. data/lib/contrast/components/scope.rb +98 -91
  162. data/lib/contrast/components/settings.rb +106 -8
  163. data/lib/contrast/config/agent_configuration.rb +41 -12
  164. data/lib/contrast/config/api_configuration.rb +37 -12
  165. data/lib/contrast/config/api_proxy_configuration.rb +12 -3
  166. data/lib/contrast/config/application_configuration.rb +38 -14
  167. data/lib/contrast/config/assess_configuration.rb +47 -12
  168. data/lib/contrast/config/assess_rules_configuration.rb +15 -3
  169. data/lib/contrast/config/base_configuration.rb +18 -50
  170. data/lib/contrast/config/certification_configuration.rb +17 -3
  171. data/lib/contrast/config/exception_configuration.rb +14 -3
  172. data/lib/contrast/config/heap_dump_configuration.rb +43 -17
  173. data/lib/contrast/config/inventory_configuration.rb +17 -3
  174. data/lib/contrast/config/logger_configuration.rb +10 -3
  175. data/lib/contrast/config/protect_configuration.rb +17 -7
  176. data/lib/contrast/config/protect_rule_configuration.rb +17 -8
  177. data/lib/contrast/config/protect_rules_configuration.rb +115 -17
  178. data/lib/contrast/config/request_audit_configuration.rb +26 -3
  179. data/lib/contrast/config/root_configuration.rb +52 -12
  180. data/lib/contrast/config/ruby_configuration.rb +60 -22
  181. data/lib/contrast/config/sampling_configuration.rb +19 -9
  182. data/lib/contrast/config/server_configuration.rb +19 -10
  183. data/lib/contrast/config/service_configuration.rb +27 -11
  184. data/lib/contrast/configuration.rb +5 -3
  185. data/lib/contrast/extension/assess/string.rb +20 -1
  186. data/lib/contrast/extension/module.rb +0 -1
  187. data/lib/contrast/framework/manager.rb +2 -2
  188. data/lib/contrast/logger/application.rb +1 -1
  189. data/lib/contrast/logger/cef_log.rb +151 -0
  190. data/lib/contrast/tasks/config.rb +90 -3
  191. data/lib/contrast/utils/assess/object_store.rb +36 -0
  192. data/lib/contrast/utils/assess/propagation_method_utils.rb +6 -0
  193. data/lib/contrast/utils/class_util.rb +3 -12
  194. data/lib/contrast/utils/hash_digest.rb +14 -6
  195. data/lib/contrast/utils/input_classification.rb +73 -0
  196. data/lib/contrast/utils/log_utils.rb +114 -0
  197. data/lib/contrast/utils/middleware_utils.rb +9 -9
  198. data/lib/contrast/utils/net_http_base.rb +13 -10
  199. data/lib/contrast/utils/object_share.rb +2 -1
  200. data/lib/contrast/utils/os.rb +0 -5
  201. data/lib/contrast/utils/patching/policy/patch_utils.rb +4 -9
  202. data/lib/contrast/utils/response_utils.rb +18 -33
  203. data/lib/contrast/utils/telemetry.rb +1 -1
  204. data/lib/contrast/utils/telemetry_client.rb +1 -1
  205. data/lib/contrast/utils/telemetry_identifier.rb +1 -1
  206. data/lib/contrast.rb +4 -3
  207. data/resources/assess/policy.json +98 -0
  208. data/resources/deadzone/policy.json +0 -86
  209. data/ruby-agent.gemspec +9 -8
  210. data/service_executables/VERSION +1 -1
  211. data/service_executables/linux/contrast-service +0 -0
  212. data/service_executables/mac/contrast-service +0 -0
  213. metadata +103 -38
  214. data/lib/contrast/agent/assess/rule/response/cachecontrol_rule.rb +0 -184
  215. data/lib/contrast/agent/assess/rule/response/clickjacking_rule.rb +0 -66
  216. data/lib/contrast/agent/assess/rule/response/x_content_type_rule.rb +0 -52
  217. data/lib/contrast/agent/assess/rule/response/x_xss_protection_rule.rb +0 -53
  218. data/lib/contrast/extension/kernel.rb +0 -54
@@ -2,8 +2,8 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  require 'fiber'
5
- require 'monitor'
6
5
  require 'contrast/agent/scope'
6
+ require 'cs__scope/cs__scope'
7
7
 
8
8
  # This is the Scope component.
9
9
  #
@@ -12,85 +12,99 @@ require 'contrast/agent/scope'
12
12
  # execution context for which we need to special case?".
13
13
  module Contrast
14
14
  module Components
15
- module Scope # :nodoc:
16
- MONITOR = Monitor.new
17
- EXECUTION_CONTEXT = {} # rubocop:disable Style/MutableConstant
18
-
19
- class Interface # :nodoc:
20
- def initialize
21
- # This is probably redundant with #scope_for_current_ec's nil check.
22
- EXECUTION_CONTEXT[Fiber.current] = Contrast::Agent::Scope.new
23
- end
24
-
25
- # This returns the scope governing the current execution context. Use this sparingly, preferring the instance
26
- # & class methods to access and query scope, rather than interacting with the scope object directly.
27
- def scope_for_current_ec
28
- MONITOR.synchronize do
29
- return EXECUTION_CONTEXT[Fiber.current] ||= Contrast::Agent::Scope.new
30
- end
31
- end
32
- end
33
-
34
- module InstanceMethods # :nodoc:
35
- # For each instance method on a scope, define a forwarder to the scope on the current execution context's scope.
36
- def scope_for_current_ec
37
- MONITOR.synchronize do
38
- return EXECUTION_CONTEXT[Fiber.current] ||= Contrast::Agent::Scope.new
39
- end
40
- end
41
-
42
- def enter_contrast_scope!
43
- scope_for_current_ec.enter_contrast_scope!
44
- end
45
-
46
- def enter_deserialization_scope!
47
- scope_for_current_ec.enter_deserialization_scope!
48
- end
49
-
50
- def enter_split_scope!
51
- scope_for_current_ec.enter_split_scope!
52
- end
53
-
54
- def enter_scope! name
55
- scope_for_current_ec.enter_scope! name
56
- end
57
-
58
- def exit_contrast_scope!
59
- scope_for_current_ec.exit_contrast_scope!
60
- end
61
-
62
- def exit_deserialization_scope!
63
- scope_for_current_ec.exit_deserialization_scope!
64
- end
65
-
66
- def exit_split_scope!
67
- scope_for_current_ec.exit_split_scope!
68
- end
69
-
70
- def exit_scope! name
71
- scope_for_current_ec.exit_scope! name
72
- end
73
-
74
- def in_contrast_scope?
75
- scope_for_current_ec.in_contrast_scope?
76
- end
77
-
78
- def in_deserialization_scope?
79
- scope_for_current_ec.in_deserialization_scope?
80
- end
81
-
82
- def in_split_scope?
83
- scope_for_current_ec.in_split_scope?
84
- end
85
-
86
- def split_scope_depth
87
- scope_for_current_ec.split_scope_depth
88
- end
89
-
90
- def in_scope? name
91
- scope_for_current_ec.in_scope? name
92
- end
93
-
15
+ # Constants defined in C:
16
+ #
17
+ # MONITOR = Mutex.new This was replaced from Monitor to Mutex.
18
+ # EXECUTION_CONTEXT = {} Hash containing all of the current ecs as keys pointing to their scope instances.
19
+ # EC_KEYS = [] set while we get the current ec, and used for sweeping the dead fibers in C.
20
+ #
21
+ # Methods defined in C:
22
+ #
23
+ # class Interface
24
+ # initializes the scope for the current fiber context.
25
+ # def initialize end;
26
+ #
27
+ # This returns the scope governing the current execution context. Use this sparingly, preferring the instance
28
+ # & class methods to access and query scope, rather than interacting with the scope object directly.
29
+ # def scope_for_current_ec end;
30
+ # end
31
+ #
32
+ # module Scope
33
+ # Singleton method for cleaning the dead fibers and unused scope instances, scope GC.
34
+ # def self.sweep_dead_ecs end;
35
+ # end
36
+ module Scope
37
+ # Methods defined in C:
38
+ #
39
+ # For each instance method on a scope, define a forwarder to the scope on the
40
+ # current execution context's scope.
41
+ # def scope_for_current_ec end;
42
+ #
43
+ # all the methods bellow are used as forwarders to be executed in the current ec.
44
+ # exp: in_contrast_scope? => scope_for_current_ec.enter_contrast_scope!
45
+ #
46
+ # Check if we are in contrast scope.
47
+ # def in_contrast_scope? end;
48
+ # @return [Boolean] check if we are in contrast scope
49
+ # if the scope is above 0 return true.
50
+ #
51
+ # check if we are in deserialization scope.
52
+ # def in_deserialization_scope? end;
53
+ # @return [Boolean] check if we are in contrast scope
54
+ # if the scope is above 0 return true.
55
+ #
56
+ # check if we are in split scope.
57
+ # def in_split_scope? end;
58
+ # @return [Boolean] check if we are in contrast scope
59
+ # if the scope is above 0 return true.
60
+ #
61
+ # enter contrast scope.
62
+ # def enter_contrast_scope! end;
63
+ # @return @contrast_scope [Integer] contrast scope increased.
64
+ #
65
+ # enter deserialization scope.
66
+ # def enter_deserialization_scope! end;
67
+ # @return @deserialization_scope [Integer] deserialization scope increased.
68
+ #
69
+ # enter split scope.
70
+ # def enter_split_scope! end;
71
+ # @return @split_scope [Integer] split scope increased.
72
+ #
73
+ # check split scope depth.
74
+ # def split_scope_depth end;
75
+ # @return @split_scope [Integer] split scope depth.
76
+ #
77
+ # exit contrast scope.
78
+ # def exit_contrast_scope! end;
79
+ # @return @contrast_scope [Integer] contrast scope decreased.
80
+ #
81
+ # exit deserialization scope.
82
+ # def exit_deserialization_scope! end;
83
+ # @return @deserialization_scope [Integer] deserialization scope decreased.
84
+ #
85
+ # exit split scope.
86
+ # def exit_split_scope! end;
87
+ # @return @split_scope [Integer] split scope decreased.
88
+ #
89
+ # Static methods to be used, the cases are defined by the usage from the above methods
90
+ #
91
+ # check if are in specific scope.
92
+ # def in_scope? name end;
93
+ # @param name [Symbol] scope symbol representing scope to check.
94
+ # @return [Boolean] check if we are in passed scope.
95
+ #
96
+ # enter specific scope.
97
+ # def enter_scope! name end;
98
+ # @param name [Symbol] scope symbol representing scope to enter.
99
+ # @return scope [Integer] entered scope value increased.
100
+ #
101
+ # exit specific scope.
102
+ # def exit_cope! name end;
103
+ # @param name [Symbol] scope symbol representing scope to exit.
104
+ # @return scope [Integer] entered scope value decreased.
105
+ module InstanceMethods
106
+ # Forwarder to execute block in contrast scope under current
107
+ # method execution context. On completion exits scope.
94
108
  def with_contrast_scope
95
109
  scope_for_current_ec.enter_contrast_scope!
96
110
  yield
@@ -98,6 +112,8 @@ module Contrast
98
112
  scope_for_current_ec.exit_contrast_scope!
99
113
  end
100
114
 
115
+ # Forwarder to execute block in deserialization scope under current
116
+ # method execution context. On completion exits scope.
101
117
  def with_deserialization_scope
102
118
  scope_for_current_ec.enter_deserialization_scope!
103
119
  yield
@@ -105,6 +121,8 @@ module Contrast
105
121
  scope_for_current_ec.exit_deserialization_scope!
106
122
  end
107
123
 
124
+ # Forwarder to execute block in split scope under current
125
+ # method execution context. On completion exits scope.
108
126
  def with_split_scope
109
127
  scope_for_current_ec.enter_split_scope!
110
128
  yield
@@ -113,17 +131,6 @@ module Contrast
113
131
  end
114
132
  end
115
133
 
116
- def self.sweep_dead_ecs
117
- # TODO: RUBY-534, #sweep_dead_ecs compensates for a lack of weak tables. when we can use WeakRef, we should
118
- # investigate removing this call and instead use the WeakRef for the Execution Context's Keys or using our
119
- # Finalizers Hash for Fibers
120
- MONITOR.synchronize do
121
- EXECUTION_CONTEXT.delete_if do |ec, _scope|
122
- !ec.alive?
123
- end
124
- end
125
- end
126
-
127
134
  ClassMethods = InstanceMethods
128
135
  end
129
136
  end
@@ -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,63 @@ 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
30
89
 
31
90
  def initialize
32
91
  reset_state
@@ -38,10 +97,14 @@ module Contrast
38
97
 
39
98
  # @param features [Contrast::Api::Settings::ServerFeatures, Contrast::Agent::Reporting::Response]
40
99
  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
100
+ if features.cs__is_a?(Contrast::Agent::Reporting::Response)
101
+ server_features = features.server_features
102
+ log_file = server_features.log_file
103
+ log_level = server_features.log_level
104
+ Contrast::Logger::Log.instance.update(log_file, log_level) if log_file || log_level
105
+ @protect_state.enabled = server_features.protect.enabled?
106
+ @assess_state.enabled = server_features.assess.enabled?
107
+ @assess_state.sampling_settings = server_features.assess.sampling
45
108
  else
46
109
  @protect_state.enabled = features.protect_enabled?
47
110
  @assess_state.enabled = features.assess_enabled?
@@ -52,10 +115,13 @@ module Contrast
52
115
  # @param features [Contrast::Api::Settings::ApplicationSettings, Contrast::Agent::Reporting::Response]
53
116
  def update_from_application_settings features
54
117
  if features&.class == Contrast::Agent::Reporting::Response
55
- @application_state.modes_by_id = features.application_settings.protect.protection_rules_to_settings_hash
118
+ settings = features.application_settings
119
+ @application_state.modes_by_id = settings.protect.protection_rules_to_settings_hash
56
120
  # TODO: RUBY-1438 this needs to be translated
57
121
  # @application_state.exclusion_matchers = new_vals[:exclusion_matchers]
58
- @assess_state.disabled_assess_rules = features.application_settings.assess.disabled_rules
122
+ update_sensitive_data_policy(settings.sensitive_data_masking)
123
+ @assess_state.disabled_assess_rules = settings.assess.disabled_rules
124
+ @assess_state.session_id = settings.assess.session_id if settings.assess.session_id
59
125
  else
60
126
  new_vals = features.application_state_translation
61
127
  @application_state.modes_by_id = new_vals[:modes_by_id]
@@ -70,6 +136,7 @@ module Contrast
70
136
  @assess_state = ASSESS_STATE_BASE.dup
71
137
  @application_state = APPLICATION_STATE_BASE.dup
72
138
  @tainted_columns = {}
139
+ @sensitive_data_masking = SENSITIVE_DATA_MASKING_BASE.dup
73
140
  end
74
141
 
75
142
  def build_protect_rules
@@ -86,6 +153,37 @@ module Contrast
86
153
  Contrast::Agent::Protect::Rule::Xss.new
87
154
  Contrast::Agent::Protect::Rule::Xxe.new
88
155
  end
156
+
157
+ # Update the sensitive data masking policy from settings,
158
+ # received from TS. In case the settings are empty,
159
+ # keep current ones.
160
+ #
161
+ # @param sensitive_data_masking [Contrast::Agent::Reporting::Settings::SensitiveDataMasking]
162
+ # Ts Response settings for sensitive data masking policy
163
+ def update_sensitive_data_policy sensitive_data_masking
164
+ @sensitive_data_masking.mask_http_body = sensitive_data_masking.mask_http_body? unless
165
+ settings_empty?(sensitive_data_masking.mask_http_body?)
166
+ @sensitive_data_masking.mask_attack_vector = sensitive_data_masking.mask_attack_vector? unless
167
+ settings_empty?(sensitive_data_masking.mask_attack_vector?)
168
+ return if settings_empty?(sensitive_data_masking.rules)
169
+
170
+ @sensitive_data_masking.rules = sensitive_data_masking.rules
171
+ # update with the newly received rules.
172
+ Contrast::Agent::Reporting::Masker.send(:update_dictionary)
173
+ end
174
+
175
+ private
176
+
177
+ # check if settings are empty and return true if so.
178
+ #
179
+ # @param settings [String, Boolean, Array, Hash]
180
+ # @return true | false
181
+ def settings_empty? settings
182
+ return false if !!settings == settings
183
+ return true if settings.nil? || settings.empty?
184
+
185
+ false
186
+ end
89
187
  end
90
188
  end
91
189
  end
@@ -1,23 +1,52 @@
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/config/service_configuration'
5
+ require 'contrast/config/logger_configuration'
6
+ require 'contrast/config/ruby_configuration'
7
+ require 'contrast/config/heap_dump_configuration'
8
+ require 'contrast/config/api_configuration'
9
+
4
10
  module Contrast
5
11
  module Config
6
12
  # Common Configuration settings. Those in this section pertain to the
7
13
  # core functionality of the Agent.
8
14
  class AgentConfiguration < BaseConfiguration
9
- KEYS = {
10
- enable: EMPTY_VALUE,
11
- start_bundled_service: true,
12
- omit_body: EMPTY_VALUE,
13
- service: Contrast::Config::ServiceConfiguration,
14
- logger: Contrast::Config::LoggerConfiguration,
15
- ruby: Contrast::Config::RubyConfiguration,
16
- heap_dump: Contrast::Config::HeapDumpConfiguration
17
- }.cs__freeze
18
-
19
- def initialize hsh
20
- super(hsh, KEYS)
15
+ # @return [Boolean, nil]
16
+ attr_accessor :enable
17
+ # @return [Boolean, nil]
18
+ attr_accessor :omit_body
19
+ attr_writer :ruby, :service, :logger, :heap_dump
20
+
21
+ def initialize hsh = {}
22
+ @enable = traverse_config(hsh, :enable)
23
+ @start_bundled_service = traverse_config(hsh, :start_bundled_service)
24
+ @omit_body = traverse_config(hsh, :omit_body)
25
+ @service = Contrast::Config::ServiceConfiguration.new(traverse_config(hsh, :service))
26
+ @logger = Contrast::Config::LoggerConfiguration.new(traverse_config(hsh, :logger))
27
+ @ruby = Contrast::Config::RubyConfiguration.new(traverse_config(hsh, :ruby))
28
+ @heap_dump = Contrast::Config::HeapDumpConfiguration.new(traverse_config(hsh, :heap_dump))
29
+ end
30
+
31
+ # @return [Boolean, true]
32
+ def start_bundled_service
33
+ @start_bundled_service.nil? ? true : @start_bundled_service
34
+ end
35
+
36
+ def service
37
+ @service ||= Contrast::Config::ServiceConfiguration.new
38
+ end
39
+
40
+ def logger
41
+ @logger ||= Contrast::Config::LoggerConfiguration.new
42
+ end
43
+
44
+ def ruby
45
+ @ruby ||= Contrast::Config::RubyConfiguration.new
46
+ end
47
+
48
+ def heap_dump
49
+ @heap_dump ||= Contrast::Config::HeapDumpConfiguration.new
21
50
  end
22
51
  end
23
52
  end
@@ -9,18 +9,43 @@ module Contrast
9
9
  module Config
10
10
  # Api keys configuration
11
11
  class ApiConfiguration < BaseConfiguration
12
- URL = 'https://app.contrastsecurity.com/Contrast'
13
- KEYS = {
14
- api_key: EMPTY_VALUE,
15
- url: URL,
16
- user_name: EMPTY_VALUE,
17
- service_key: EMPTY_VALUE,
18
- proxy: Contrast::Config::ApiProxyConfiguration,
19
- request_audit: Contrast::Config::RequestAuditConfiguration,
20
- certificate: Contrast::Config::CertificationConfiguration
21
- }.cs__freeze
22
- def initialize hsh
23
- super(hsh, KEYS)
12
+ # @return [String]
13
+ attr_accessor :api_key
14
+ # @return [String]
15
+ attr_accessor :user_name
16
+ # @return [String]
17
+ attr_accessor :service_key
18
+ attr_writer :url, :proxy, :request_audit, :certificate
19
+
20
+ DEFAULT_URL = 'https://app.contrastsecurity.com/Contrast'
21
+
22
+ def initialize hsh = {}
23
+ @api_key = traverse_config(hsh, :api_key)
24
+ @url = traverse_config(hsh, :url)
25
+ @user_name = traverse_config(hsh, :user_name)
26
+ @service_key = traverse_config(hsh, :service_key)
27
+ @proxy = Contrast::Config::ApiProxyConfiguration.new(traverse_config(hsh, :proxy))
28
+ @request_audit = Contrast::Config::RequestAuditConfiguration.new(traverse_config(hsh, :request_audit))
29
+ @certificate = Contrast::Config::CertificationConfiguration.new(traverse_config(hsh, :certificate))
30
+ end
31
+
32
+ def url
33
+ @url.nil? ? DEFAULT_URL : @url
34
+ end
35
+
36
+ # @return [Contrast::Config::ApiProxyConfiguration]
37
+ def proxy
38
+ @proxy ||= Contrast::Config::ApiProxyConfiguration.new
39
+ end
40
+
41
+ # @return [Contrast::Config::RequestAuditConfiguration]
42
+ def request_audit
43
+ @request_audit ||= Contrast::Config::RequestAuditConfiguration.new
44
+ end
45
+
46
+ # @return [Contrast::Config::CertificationConfiguration]
47
+ def certificate
48
+ @certificate ||= Contrast::Config::CertificationConfiguration.new
24
49
  end
25
50
  end
26
51
  end
@@ -5,9 +5,18 @@ module Contrast
5
5
  module Config
6
6
  # Api Proxy keys configuration
7
7
  class ApiProxyConfiguration < BaseConfiguration
8
- KEYS = { enable: false, url: EMPTY_VALUE }.cs__freeze
9
- def initialize hsh
10
- super(hsh, KEYS)
8
+ # @return [String] proxy url
9
+ attr_accessor :url
10
+ attr_writer :enable
11
+
12
+ def initialize hsh = {}
13
+ @enable = traverse_config(hsh, :enable)
14
+ @url = traverse_config(hsh, :url)
15
+ end
16
+
17
+ # @return [Boolean, false]
18
+ def enable
19
+ @enable.nil? ? false : @enable
11
20
  end
12
21
  end
13
22
  end
@@ -8,21 +8,45 @@ module Contrast
8
8
  # Common Configuration settings. Those in this section pertain to the
9
9
  # application identification functionality of the Agent.
10
10
  class ApplicationConfiguration < BaseConfiguration
11
- KEYS = {
12
- name: EMPTY_VALUE,
13
- version: EMPTY_VALUE,
14
- language: EMPTY_VALUE,
15
- path: EMPTY_VALUE,
16
- group: EMPTY_VALUE,
17
- tags: EMPTY_VALUE,
18
- code: EMPTY_VALUE,
19
- metadata: EMPTY_VALUE,
20
- session_id: Contrast::Utils::ObjectShare::EMPTY_STRING,
21
- session_metadata: Contrast::Utils::ObjectShare::EMPTY_STRING
22
- }.cs__freeze
11
+ # @return [String]
12
+ attr_accessor :name
13
+ # @return [String]
14
+ attr_accessor :version
15
+ # @return [String]
16
+ attr_accessor :language
17
+ # @return [String]
18
+ attr_accessor :path
19
+ # @return [String]
20
+ attr_accessor :group
21
+ # @return [String]
22
+ attr_accessor :tags
23
+ # @return [String]
24
+ attr_accessor :code
25
+ # @return [String]
26
+ attr_accessor :metadata
27
+ attr_writer :session_id, :session_metadata
23
28
 
24
- def initialize hsh
25
- super(hsh, KEYS)
29
+ def initialize hsh = {}
30
+ @name = traverse_config(hsh, :name)
31
+ @version = traverse_config(hsh, :version)
32
+ @language = traverse_config(hsh, :language)
33
+ @path = traverse_config(hsh, :path)
34
+ @group = traverse_config(hsh, :group)
35
+ @tags = traverse_config(hsh, :tags)
36
+ @code = traverse_config(hsh, :code)
37
+ @metadata = traverse_config(hsh, :metadata)
38
+ @session_id = traverse_config(hsh, :session_id)
39
+ @session_metadata = traverse_config(hsh, :session_metadata)
40
+ end
41
+
42
+ # @return [String, Contrast::Utils::ObjectShare::EMPTY_STRING]
43
+ def session_id
44
+ @session_id ||= Contrast::Utils::ObjectShare::EMPTY_STRING
45
+ end
46
+
47
+ # @return [String, Contrast::Utils::ObjectShare::EMPTY_STRING]
48
+ def session_metadata
49
+ @session_metadata ||= Contrast::Utils::ObjectShare::EMPTY_STRING
26
50
  end
27
51
  end
28
52
  end
@@ -6,18 +6,53 @@ module Contrast
6
6
  # Common Configuration settings. Those in this section pertain to the
7
7
  # assess functionality of the Agent.
8
8
  class AssessConfiguration < BaseConfiguration
9
- KEYS = {
10
- tags: EMPTY_VALUE,
11
- enable: EMPTY_VALUE,
12
- enable_scan_response: true,
13
- enable_dynamic_sources: true,
14
- sampling: Contrast::Config::SamplingConfiguration,
15
- rules: Contrast::Config::AssessRulesConfiguration,
16
- stacktraces: 'ALL'
17
- }.cs__freeze
18
-
19
- def initialize hsh
20
- super(hsh, KEYS)
9
+ # @return [String, nil]
10
+ attr_accessor :tags
11
+ # @return [Boolean, nil]
12
+ attr_accessor :enable
13
+ attr_writer :enable_scan_response, :enable_dynamic_sources, :sampling, :rules, :stacktraces
14
+
15
+ DEFAULT_STACKTRACES = 'ALL'
16
+
17
+ def initialize hsh = {}
18
+ @enable = traverse_config(hsh, :enable)
19
+ @tags = traverse_config(hsh, :tags)
20
+ @enable_scan_response = traverse_config(hsh, :enable_scan_response)
21
+ @enable_dynamic_sources = traverse_config(hsh, :enable_dynamic_sources)
22
+ @enable_original_object = traverse_config(hsh, :enable_original_object)
23
+ @sampling = Contrast::Config::SamplingConfiguration.new(traverse_config(hsh, :sampling))
24
+ @rules = Contrast::Config::AssessRulesConfiguration.new(traverse_config(hsh, :rules))
25
+ @stacktraces = traverse_config(hsh, :stacktraces)
26
+ end
27
+
28
+ # @return [Boolean, true]
29
+ def enable_scan_response
30
+ @enable_scan_response.nil? ? true : @enable_scan_response
31
+ end
32
+
33
+ # @return [Boolean, true]
34
+ def enable_dynamic_sources
35
+ @enable_dynamic_sources.nil? ? true : @enable_dynamic_sources
36
+ end
37
+
38
+ # @return [Boolean, true]
39
+ def enable_original_object
40
+ @enable_original_object.nil? ? true : @enable_original_object
41
+ end
42
+
43
+ # @return [Contrast::Config::SamplingConfiguration]
44
+ def sampling
45
+ @sampling ||= Contrast::Config::SamplingConfiguration.new
46
+ end
47
+
48
+ # @return [Contrast::Config::AssessRulesConfiguration]
49
+ def rules
50
+ @rules ||= Contrast::Config::AssessRulesConfiguration.new
51
+ end
52
+
53
+ # @return [String] stacktrace level
54
+ def stacktraces
55
+ @stacktraces ||= DEFAULT_STACKTRACES
21
56
  end
22
57
  end
23
58
  end