contrast-agent 3.9.0 → 3.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (271) hide show
  1. checksums.yaml +4 -4
  2. data/.simplecov +5 -2
  3. data/ext/build_funchook.rb +14 -9
  4. data/ext/cs__assess_active_record_named/cs__active_record_named.c +5 -12
  5. data/ext/cs__assess_active_record_named/extconf.rb +3 -0
  6. data/ext/cs__assess_array/cs__assess_array.c +3 -5
  7. data/ext/cs__assess_array/extconf.rb +3 -0
  8. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +10 -4
  9. data/ext/cs__assess_basic_object/extconf.rb +3 -0
  10. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +4 -3
  11. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.h +3 -3
  12. data/ext/cs__assess_fiber_track/extconf.rb +3 -0
  13. data/ext/cs__assess_hash/cs__assess_hash.c +40 -17
  14. data/ext/cs__assess_hash/cs__assess_hash.h +4 -6
  15. data/ext/cs__assess_hash/extconf.rb +3 -0
  16. data/ext/cs__assess_kernel/cs__assess_kernel.c +10 -8
  17. data/ext/cs__assess_kernel/cs__assess_kernel.h +1 -0
  18. data/ext/cs__assess_kernel/extconf.rb +3 -0
  19. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +3 -6
  20. data/ext/cs__assess_marshal_module/extconf.rb +3 -0
  21. data/ext/cs__assess_module/cs__assess_module.c +13 -9
  22. data/ext/cs__assess_module/extconf.rb +3 -0
  23. data/ext/cs__assess_regexp/cs__assess_regexp.c +13 -9
  24. data/ext/cs__assess_regexp/cs__assess_regexp.h +1 -0
  25. data/ext/cs__assess_regexp/extconf.rb +3 -0
  26. data/ext/cs__assess_string/cs__assess_string.c +5 -8
  27. data/ext/cs__assess_string/cs__assess_string.h +2 -1
  28. data/ext/cs__assess_string/extconf.rb +3 -0
  29. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +2 -2
  30. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.h +3 -3
  31. data/ext/cs__assess_string_interpolation26/extconf.rb +3 -0
  32. data/ext/cs__assess_yield_track/cs__assess_yield_track.h +1 -1
  33. data/ext/cs__assess_yield_track/extconf.rb +3 -0
  34. data/ext/cs__common/cs__common.c +79 -0
  35. data/ext/cs__common/cs__common.h +34 -0
  36. data/ext/cs__common/extconf.rb +9 -8
  37. data/ext/cs__contrast_patch/cs__contrast_patch.h +1 -6
  38. data/ext/cs__contrast_patch/extconf.rb +3 -0
  39. data/ext/cs__protect_kernel/cs__protect_kernel.c +20 -11
  40. data/ext/cs__protect_kernel/extconf.rb +3 -0
  41. data/ext/extconf_common.rb +10 -8
  42. data/funchook/autom4te.cache/output.0 +1 -13
  43. data/funchook/autom4te.cache/requests +44 -45
  44. data/funchook/autom4te.cache/traces.0 +0 -3
  45. data/funchook/config.log +378 -217
  46. data/funchook/config.status +23 -24
  47. data/funchook/configure +1 -13
  48. data/funchook/src/Makefile +7 -7
  49. data/funchook/src/config.h +2 -2
  50. data/funchook/src/decoder.o +0 -0
  51. data/funchook/src/distorm.o +0 -0
  52. data/funchook/src/funchook.o +0 -0
  53. data/funchook/src/funchook_io.o +0 -0
  54. data/funchook/src/funchook_syscall.o +0 -0
  55. data/funchook/src/funchook_unix.o +0 -0
  56. data/funchook/src/funchook_x86.o +0 -0
  57. data/funchook/src/instructions.o +0 -0
  58. data/funchook/src/insts.o +0 -0
  59. data/funchook/src/libfunchook.dylib +0 -0
  60. data/funchook/src/mnemonics.o +0 -0
  61. data/funchook/src/operands.o +0 -0
  62. data/funchook/src/os_func.o +0 -0
  63. data/funchook/src/os_func_unix.o +0 -0
  64. data/funchook/src/prefix.o +0 -0
  65. data/funchook/src/printf_base.o +0 -0
  66. data/funchook/src/textdefs.o +0 -0
  67. data/funchook/src/wstring.o +0 -0
  68. data/funchook/test/Makefile +2 -2
  69. data/funchook/test/funchook_test +0 -0
  70. data/funchook/test/libfunchook_test.so +0 -0
  71. data/funchook/test/libfunchook_test.so.dSYM/Contents/Info.plist +20 -0
  72. data/funchook/test/libfunchook_test.so.dSYM/Contents/Resources/DWARF/libfunchook_test.so +0 -0
  73. data/funchook/test/test_main.o +0 -0
  74. data/funchook/test/x86_64_test.o +0 -0
  75. data/lib/contrast.rb +1 -0
  76. data/lib/contrast/agent.rb +21 -15
  77. data/lib/contrast/agent/assess.rb +1 -2
  78. data/lib/contrast/agent/assess/adjusted_span.rb +3 -1
  79. data/lib/contrast/agent/assess/contrast_event.rb +20 -68
  80. data/lib/contrast/agent/assess/events/event_factory.rb +25 -0
  81. data/lib/contrast/agent/assess/events/source_event.rb +83 -0
  82. data/lib/contrast/agent/assess/insulator.rb +0 -4
  83. data/lib/contrast/agent/assess/policy/patcher.rb +5 -2
  84. data/lib/contrast/agent/assess/policy/policy_node.rb +0 -7
  85. data/lib/contrast/agent/assess/policy/policy_scanner.rb +1 -1
  86. data/lib/contrast/agent/assess/policy/preshift.rb +1 -1
  87. data/lib/contrast/agent/assess/policy/propagation_method.rb +65 -33
  88. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -1
  89. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  90. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +80 -0
  91. data/lib/contrast/agent/assess/policy/propagator/select.rb +35 -22
  92. data/lib/contrast/agent/assess/policy/propagator/split.rb +26 -6
  93. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +2 -0
  94. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +40 -27
  95. data/lib/contrast/agent/assess/policy/source_method.rb +20 -20
  96. data/lib/contrast/agent/assess/policy/source_node.rb +0 -15
  97. data/lib/contrast/agent/assess/policy/trigger_method.rb +29 -40
  98. data/lib/contrast/agent/assess/policy/trigger_node.rb +3 -6
  99. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -31
  100. data/lib/contrast/agent/assess/properties.rb +5 -3
  101. data/lib/contrast/agent/assess/rule/base.rb +1 -5
  102. data/lib/contrast/agent/assess/rule/csrf/csrf_applicator.rb +2 -22
  103. data/lib/contrast/agent/assess/rule/csrf/csrf_watcher.rb +5 -1
  104. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +2 -2
  105. data/lib/contrast/agent/assess/rule/redos.rb +4 -4
  106. data/lib/contrast/agent/assess/tag.rb +24 -14
  107. data/lib/contrast/agent/at_exit_hook.rb +16 -13
  108. data/lib/contrast/agent/class_reopener.rb +15 -6
  109. data/lib/contrast/agent/deadzone/policy/policy.rb +2 -2
  110. data/lib/contrast/agent/disable_reaction.rb +3 -4
  111. data/lib/contrast/agent/exclusion_matcher.rb +8 -48
  112. data/lib/contrast/agent/feature_state.rb +45 -78
  113. data/lib/contrast/agent/logger.rb +173 -0
  114. data/lib/contrast/agent/middleware.rb +87 -250
  115. data/lib/contrast/agent/module_data.rb +2 -1
  116. data/lib/contrast/agent/patching/policy/after_load_patch.rb +2 -1
  117. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +21 -4
  118. data/lib/contrast/agent/patching/policy/method_policy.rb +3 -3
  119. data/lib/contrast/agent/patching/policy/module_policy.rb +0 -25
  120. data/lib/contrast/agent/patching/policy/patch.rb +96 -23
  121. data/lib/contrast/agent/patching/policy/patcher.rb +19 -19
  122. data/lib/contrast/agent/patching/policy/policy.rb +7 -7
  123. data/lib/contrast/agent/patching/policy/policy_node.rb +2 -10
  124. data/lib/contrast/agent/patching/policy/trigger_node.rb +1 -4
  125. data/lib/contrast/agent/protect/rule/base.rb +14 -33
  126. data/lib/contrast/agent/protect/rule/base_service.rb +3 -1
  127. data/lib/contrast/agent/protect/rule/cmd_injection.rb +10 -13
  128. data/lib/contrast/agent/protect/rule/csrf.rb +2 -1
  129. data/lib/contrast/agent/protect/rule/csrf/csrf_evaluator.rb +11 -14
  130. data/lib/contrast/agent/protect/rule/default_scanner.rb +0 -13
  131. data/lib/contrast/agent/protect/rule/deserialization.rb +2 -0
  132. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +2 -2
  133. data/lib/contrast/agent/protect/rule/no_sqli.rb +4 -4
  134. data/lib/contrast/agent/protect/rule/path_traversal.rb +5 -4
  135. data/lib/contrast/agent/protect/rule/sqli.rb +1 -0
  136. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +2 -0
  137. data/lib/contrast/agent/protect/rule/xss.rb +2 -0
  138. data/lib/contrast/agent/protect/rule/xxe.rb +10 -4
  139. data/lib/contrast/agent/railtie.rb +2 -8
  140. data/lib/contrast/agent/reaction_processor.rb +5 -5
  141. data/lib/contrast/agent/request.rb +9 -12
  142. data/lib/contrast/agent/request_context.rb +12 -14
  143. data/lib/contrast/agent/request_handler.rb +35 -0
  144. data/lib/contrast/agent/response.rb +52 -30
  145. data/lib/contrast/agent/rewriter.rb +25 -11
  146. data/lib/contrast/agent/rule_set.rb +49 -0
  147. data/lib/contrast/agent/scope.rb +4 -12
  148. data/lib/contrast/agent/service_heartbeat.rb +1 -2
  149. data/lib/contrast/agent/settings_state.rb +10 -74
  150. data/lib/contrast/agent/socket_client.rb +17 -11
  151. data/lib/contrast/agent/static_analysis.rb +42 -0
  152. data/lib/contrast/agent/thread.rb +1 -1
  153. data/lib/contrast/agent/tracepoint_hook.rb +1 -5
  154. data/lib/contrast/agent/version.rb +1 -1
  155. data/lib/contrast/api.rb +1 -1
  156. data/lib/contrast/api/decorators.rb +14 -0
  157. data/lib/contrast/api/decorators/application_settings.rb +37 -0
  158. data/lib/contrast/api/decorators/application_update.rb +66 -0
  159. data/lib/contrast/api/decorators/exclusion.rb +20 -0
  160. data/lib/contrast/api/decorators/input_analysis.rb +17 -0
  161. data/lib/contrast/api/decorators/server_features.rb +24 -0
  162. data/lib/contrast/api/speedracer.rb +31 -29
  163. data/lib/contrast/api/tcp_socket.rb +0 -2
  164. data/lib/contrast/components/agent.rb +27 -22
  165. data/lib/contrast/components/app_context.rb +18 -43
  166. data/lib/contrast/components/config.rb +7 -5
  167. data/lib/contrast/components/contrast_service.rb +0 -4
  168. data/lib/contrast/components/heap_dump.rb +12 -8
  169. data/lib/contrast/components/interface.rb +11 -10
  170. data/lib/contrast/components/logger.rb +3 -68
  171. data/lib/contrast/components/sampling.rb +22 -11
  172. data/lib/contrast/components/settings.rb +18 -5
  173. data/lib/contrast/config/base_configuration.rb +1 -0
  174. data/lib/contrast/config/default_value.rb +1 -0
  175. data/lib/contrast/config/protect_rule_configuration.rb +0 -14
  176. data/lib/contrast/configuration.rb +3 -5
  177. data/lib/contrast/extensions/framework/rails/action_controller_inheritance.rb +39 -0
  178. data/lib/contrast/extensions/framework/rails/active_record_named.rb +13 -8
  179. data/lib/contrast/extensions/ruby_core/assess.rb +1 -2
  180. data/lib/contrast/extensions/ruby_core/assess/assess_extension.rb +27 -22
  181. data/lib/contrast/extensions/ruby_core/assess/erb.rb +0 -8
  182. data/lib/contrast/extensions/ruby_core/assess/exec_trigger.rb +6 -8
  183. data/lib/contrast/extensions/ruby_core/assess/fiber.rb +88 -100
  184. data/lib/contrast/extensions/ruby_core/assess/hash.rb +32 -15
  185. data/lib/contrast/extensions/ruby_core/assess/kernel.rb +28 -27
  186. data/lib/contrast/extensions/ruby_core/assess/regexp.rb +74 -196
  187. data/lib/contrast/extensions/ruby_core/assess/string.rb +15 -7
  188. data/lib/contrast/extensions/ruby_core/assess/tilt_template_trigger.rb +29 -24
  189. data/lib/contrast/extensions/ruby_core/assess/xpath_library_trigger.rb +2 -2
  190. data/lib/contrast/extensions/ruby_core/eval_trigger.rb +0 -1
  191. data/lib/contrast/extensions/ruby_core/inventory/datastores.rb +2 -2
  192. data/lib/contrast/extensions/ruby_core/protect/applies_command_injection_rule.rb +9 -20
  193. data/lib/contrast/extensions/ruby_core/protect/applies_deserialization_rule.rb +9 -19
  194. data/lib/contrast/extensions/ruby_core/protect/applies_no_sqli_rule.rb +10 -27
  195. data/lib/contrast/extensions/ruby_core/protect/applies_path_traversal_rule.rb +13 -21
  196. data/lib/contrast/extensions/ruby_core/protect/applies_sqli_rule.rb +11 -23
  197. data/lib/contrast/extensions/ruby_core/protect/applies_xxe_rule.rb +62 -78
  198. data/lib/contrast/extensions/ruby_core/protect/rule_applicator.rb +50 -0
  199. data/lib/contrast/framework/base_support.rb +10 -0
  200. data/lib/contrast/framework/manager.rb +28 -2
  201. data/lib/contrast/framework/platform_version.rb +1 -0
  202. data/lib/contrast/framework/rails_support.rb +16 -0
  203. data/lib/contrast/framework/sinatra_support.rb +12 -2
  204. data/lib/contrast/framework/view_technologies_descriptor.rb +1 -0
  205. data/lib/contrast/tasks/service.rb +2 -8
  206. data/lib/contrast/utils/assess/sampling_util.rb +4 -9
  207. data/lib/contrast/utils/assess/tracking_util.rb +7 -1
  208. data/lib/contrast/utils/boolean_util.rb +2 -5
  209. data/lib/contrast/utils/cache.rb +0 -11
  210. data/lib/contrast/utils/class_util.rb +20 -1
  211. data/lib/contrast/utils/gemfile_reader.rb +5 -3
  212. data/lib/contrast/utils/hash_digest.rb +0 -4
  213. data/lib/contrast/utils/heap_dump_util.rb +12 -11
  214. data/lib/contrast/utils/invalid_configuration_util.rb +1 -1
  215. data/lib/contrast/utils/inventory_util.rb +2 -2
  216. data/lib/contrast/utils/io_util.rb +1 -11
  217. data/lib/contrast/utils/job_servers_running.rb +2 -2
  218. data/lib/contrast/utils/object_share.rb +1 -37
  219. data/lib/contrast/utils/os.rb +1 -25
  220. data/lib/contrast/utils/rack_assess_session_cookie.rb +3 -3
  221. data/lib/contrast/utils/rails_assess_configuration.rb +3 -3
  222. data/lib/contrast/utils/ruby_ast_rewriter.rb +5 -1
  223. data/lib/contrast/utils/service_response_util.rb +27 -53
  224. data/lib/contrast/utils/service_sender_util.rb +9 -5
  225. data/lib/contrast/utils/sinatra_helper.rb +0 -6
  226. data/lib/contrast/utils/stack_trace_utils.rb +86 -182
  227. data/lib/contrast/utils/string_utils.rb +18 -2
  228. data/lib/contrast/utils/tag_util.rb +11 -1
  229. data/lib/contrast/utils/thread_tracker.rb +2 -2
  230. data/lib/contrast/utils/timer.rb +0 -40
  231. data/resources/assess/policy.json +33 -21
  232. data/resources/protect/policy.json +9 -9
  233. data/ruby-agent.gemspec +7 -4
  234. data/service_executables/VERSION +1 -1
  235. data/service_executables/linux/contrast-service +0 -0
  236. data/service_executables/mac/contrast-service +0 -0
  237. metadata +79 -54
  238. data/ext/cs__assess_regexp_track/cs__assess_regexp_track.c +0 -63
  239. data/ext/cs__assess_regexp_track/cs__assess_regexp_track.h +0 -29
  240. data/ext/cs__assess_regexp_track/extconf.rb +0 -2
  241. data/funchook/src/libfunchook.so +0 -0
  242. data/lib/contrast/agent/assess/frozen_properties.rb +0 -41
  243. data/lib/contrast/agent/logger_manager.rb +0 -116
  244. data/lib/contrast/delegators.rb +0 -9
  245. data/lib/contrast/delegators/application_update.rb +0 -32
  246. data/lib/contrast/utils/comment_range.rb +0 -19
  247. data/lib/contrast/utils/environment_util.rb +0 -82
  248. data/lib/contrast/utils/performs_logging.rb +0 -152
  249. data/resources/factory-bot-spec/spec_helper.rb +0 -30
  250. data/resources/rubocops/kernel/catch_cop.rb +0 -37
  251. data/resources/rubocops/kernel/require_cop.rb +0 -37
  252. data/resources/rubocops/kernel/require_relative_cop.rb +0 -33
  253. data/resources/rubocops/module/autoload_cop.rb +0 -37
  254. data/resources/rubocops/module/const_defined_cop.rb +0 -37
  255. data/resources/rubocops/module/const_get_cop.rb +0 -37
  256. data/resources/rubocops/module/const_set_cop.rb +0 -37
  257. data/resources/rubocops/module/constants_cop.rb +0 -37
  258. data/resources/rubocops/module/name_cop.rb +0 -37
  259. data/resources/rubocops/object/class_cop.rb +0 -37
  260. data/resources/rubocops/object/freeze_cop.rb +0 -37
  261. data/resources/rubocops/object/frozen_cop.rb +0 -37
  262. data/resources/rubocops/object/is_a_cop.rb +0 -37
  263. data/resources/rubocops/object/method_cop.rb +0 -37
  264. data/resources/rubocops/object/respond_to_cop.rb +0 -37
  265. data/resources/rubocops/object/singleton_class_cop.rb +0 -37
  266. data/resources/rubocops/regexp/spelling_cop.rb +0 -44
  267. data/resources/rubocops/thread/new_cop.rb +0 -39
  268. data/resources/ruby-spec/ancestors_spec.rb +0 -70
  269. data/resources/ruby-spec/modulo_spec.rb +0 -831
  270. data/resources/ruby-spec/parameters_spec.rb +0 -261
  271. data/resources/ruby-spec/ruby_spec_spec_helper.rb +0 -35
@@ -23,8 +23,7 @@ module Contrast
23
23
  # it should break LOUDLY. Better to waste half an hour of the sysadmin's
24
24
  # time than to silently fail to deliver functionality.
25
25
  module Config
26
- LOG_CONFIGURATION_DISABLED = 'Contrast disabled by configuration. Continuing without instrumentation.'
27
- CONTRAST_ENV_MARKER = 'CONTRAST__'
26
+ CONTRAST_ENV_MARKER = 'CONTRAST__'
28
27
 
29
28
  class Interface # :nodoc:
30
29
  def initialize
@@ -51,7 +50,8 @@ module Contrast
51
50
  end
52
51
 
53
52
  def enabled?
54
- Contrast::Utils::BooleanUtil.true?(raw.enable)
53
+ @_enabled = !Contrast::Utils::BooleanUtil.false?(raw.enable) if @_enabled.nil?
54
+ @_enabled
55
55
  end
56
56
 
57
57
  def disabled?
@@ -59,11 +59,13 @@ module Contrast
59
59
  end
60
60
 
61
61
  def protect?
62
- Contrast::Utils::BooleanUtil.true?(raw.protect.enable)
62
+ @_protect = Contrast::Utils::BooleanUtil.true?(raw.protect.enable) if @_protect.nil?
63
+ @_protect
63
64
  end
64
65
 
65
66
  def assess?
66
- Contrast::Utils::BooleanUtil.true?(raw.assess.enable)
67
+ @_assess = Contrast::Utils::BooleanUtil.true?(raw.assess.enable) if @_assess.nil?
68
+ @_assess
67
69
  end
68
70
 
69
71
  def session_id
@@ -25,10 +25,6 @@ module Contrast
25
25
  state.service_enabled?
26
26
  end
27
27
 
28
- def unavailable?
29
- !enabled?
30
- end
31
-
32
28
  def disabled?
33
29
  state.service_forcibly_disabled?
34
30
  end
@@ -16,17 +16,21 @@ module Contrast
16
16
  access_component :config
17
17
 
18
18
  def heap_dump_enabled?
19
- state.heap_dump_enabled?
19
+ heap_dump_control[:enabled]
20
20
  end
21
21
 
22
22
  def heap_dump_control
23
- {
24
- path: File.absolute_path(CONFIG.root.agent.heap_dump.path),
25
- count: CONFIG.root.agent.heap_dump.count.to_i,
26
- window: CONFIG.root.agent.heap_dump.window_ms.to_f / 1000,
27
- delay: CONFIG.root.agent.heap_dump.delay_ms.to_f / 1000,
28
- clean: true?(CONFIG.root.agent.heap_dump.clean)
29
- }
23
+ @_heap_dump_control ||= begin
24
+ config = CONFIG.root&.agent&.heap_dump
25
+ {
26
+ enabled: true?(config&.enable),
27
+ path: File.absolute_path(config&.path),
28
+ count: config&.count.to_i || 0,
29
+ window: (config&.window_ms.to_f || 0) / 1000,
30
+ delay: (config&.delay_ms.to_f || 0) / 1000,
31
+ clean: true?(config&.clean)
32
+ }
33
+ end
30
34
  end
31
35
  end
32
36
  end
@@ -97,7 +97,7 @@ module Contrast
97
97
  #
98
98
  # class Abc
99
99
  # include Contrast::Components::Interface
100
- # access_component :logger, :agent
100
+ # access_component :logging, :agent
101
101
  #
102
102
  # def function
103
103
  # if AGENT.disabled?
@@ -151,6 +151,16 @@ Contrast::Components::ComponentReceiverClassInterface::COMPONENT_MAP[:config] =
151
151
  cs__scoped_require 'contrast/components/settings'
152
152
  Contrast::Components::ComponentReceiverClassInterface::COMPONENT_MAP[:settings] = [Contrast::Components::Settings]
153
153
 
154
+ cs__scoped_require 'contrast/components/assess'
155
+ cs__scoped_require 'contrast/components/protect'
156
+ cs__scoped_require 'contrast/components/inventory'
157
+ Contrast::Components::ComponentReceiverClassInterface::COMPONENT_MAP[:analysis] = [Contrast::Components::Protect,
158
+ Contrast::Components::Assess,
159
+ Contrast::Components::Inventory]
160
+
161
+ cs__scoped_require 'contrast/components/logger'
162
+ Contrast::Components::ComponentReceiverClassInterface::COMPONENT_MAP[:logging] = [Contrast::Components::Logger]
163
+
154
164
  cs__scoped_require 'contrast/components/agent'
155
165
  Contrast::Components::ComponentReceiverClassInterface::COMPONENT_MAP[:agent] = [Contrast::Components::Agent]
156
166
 
@@ -163,15 +173,6 @@ Contrast::Components::ComponentReceiverClassInterface::COMPONENT_MAP[:contrast_s
163
173
  cs__scoped_require 'contrast/components/heap_dump'
164
174
  Contrast::Components::ComponentReceiverClassInterface::COMPONENT_MAP[:heap_dump] = [Contrast::Components::HeapDump]
165
175
 
166
- cs__scoped_require 'contrast/components/logger'
167
- Contrast::Components::ComponentReceiverClassInterface::COMPONENT_MAP[:logging] = [Contrast::Components::Logger]
168
-
169
- cs__scoped_require 'contrast/components/assess'
170
- cs__scoped_require 'contrast/components/protect'
171
- cs__scoped_require 'contrast/components/inventory'
172
- Contrast::Components::ComponentReceiverClassInterface::COMPONENT_MAP[:analysis] = [Contrast::Components::Protect,
173
- Contrast::Components::Assess,
174
- Contrast::Components::Inventory]
175
176
  cs__scoped_require 'contrast/components/sampling'
176
177
  Contrast::Components::ComponentReceiverClassInterface::COMPONENT_MAP[:sampling] = [Contrast::Components::Sampling]
177
178
 
@@ -1,12 +1,14 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
+ cs__scoped_require 'contrast/agent/logger'
5
+
4
6
  module Contrast
5
7
  module Components
6
8
  module Logger
7
9
  module InstanceMethods #:nodoc:
8
10
  def logger
9
- Contrast::Components::Logger::COMPONENT_INTERFACE
11
+ Contrast::Agent::Logger.instance.logger
10
12
  end
11
13
  end
12
14
  ClassMethods = InstanceMethods
@@ -17,73 +19,6 @@ module Contrast
17
19
  # Specifically, this allows for querying the state of the Agent Logger.
18
20
  class Interface
19
21
  include Contrast::Components::ComponentBase
20
-
21
- %w[error? warn? info? debug?].each do |level|
22
- define_method(level) { state.logger.send(level) }
23
- end
24
-
25
- # def trace? # TODO: RUBY-547
26
- # config&.agent&.logger&.level && config.agent.logger.level.to_s.casecmp('TRACE').to_i.zero?
27
- # end
28
-
29
- def error *args
30
- log_with_level(*args, :error)
31
- end
32
-
33
- def warn *args
34
- log_with_level(*args, :warn)
35
- end
36
-
37
- def info *args
38
- log_with_level(*args, :info)
39
- end
40
-
41
- def debug *args
42
- log_with_level(*args, :debug)
43
- end
44
-
45
- def with_level *args
46
- log_with_level(*args)
47
- end
48
-
49
- def debug_with_time msg
50
- ret = nil
51
- a = Contrast::Utils::Timer.now_ms
52
- ret = yield if block_given?
53
- z = Contrast::Utils::Timer.now_ms
54
- log_with_level(nil, "#{ msg }: pid=#{ Process.pid }, elapsed=#{ z - a }ms", :debug)
55
- ret
56
- end
57
-
58
- # def trace_with_time msg # TODO: RUBY-547
59
- # ret = nil
60
- # unless trace?
61
- # ret = yield if block_given?
62
- # return ret
63
- # end
64
- #
65
- # a = Contrast::Utils::Timer.now_ms
66
- # ret = yield if block_given?
67
- # z = Contrast::Utils::Timer.now_ms
68
- # log_with_level(nil, "#{ msg }: pid=#{ Process.pid }, elapsed=#{ z - a }ms", :debug)
69
- # ret
70
- # end
71
-
72
- private
73
-
74
- def log_with_level *args
75
- if args.length == 2
76
- msg, level = *args
77
- state.cs__class.log_with_level(nil, msg, level)
78
- elsif args.length == 3
79
- exception, msg, level = *args
80
- state.cs__class.log_with_level(exception, msg, level)
81
- end
82
-
83
- nil
84
- rescue StandardError
85
- nil
86
- end
87
22
  end
88
23
 
89
24
  COMPONENT_INTERFACE = Interface.new
@@ -9,7 +9,7 @@ module Contrast
9
9
  DEFAULT_SAMPLING_BASELINE = 5
10
10
  DEFAULT_SAMPLING_REQUEST_FREQUENCY = 5
11
11
  DEFAULT_SAMPLING_RESPONSE_FREQUENCY = 25
12
- DEFAULT_SAMPLING_WINDOW_MS = 180_000 # 3 minutes, arbitrary value from Java agent
12
+ DEFAULT_SAMPLING_WINDOW_MS = 180_000
13
13
  end
14
14
 
15
15
  module ClassMethods #:nodoc:
@@ -17,18 +17,29 @@ module Contrast
17
17
  include Constants
18
18
  include Contrast::Components::Interface
19
19
 
20
- access_component :config
20
+ access_component :config, :settings
21
21
 
22
- def sampling_control settings = @sampling_features
23
- cas = CONFIG.root.assess&.sampling
22
+ def sampling_enabled?
23
+ sampling_control[:enabled]
24
+ end
25
+
26
+ def sampling_control
27
+ @_sampling_control ||= begin
28
+ cas = CONFIG.root.assess&.sampling
29
+ settings = SETTINGS&.assess_state&.[](:sampling_settings)
30
+ {
31
+ enabled: true?([cas&.enable, settings&.enabled, DEFAULT_SAMPLING_ENABLED].reject(&:nil?)[0]),
32
+ baseline: [cas&.baseline, settings&.baseline, DEFAULT_SAMPLING_BASELINE].map(&:to_i).find(&:positive?),
33
+ request_frequency: [cas&.request_frequency, settings&.request_frequency, DEFAULT_SAMPLING_REQUEST_FREQUENCY].map(&:to_i).find(&:positive?),
34
+ response_frequency: [cas&.response_frequency, settings&.response_frequency, DEFAULT_SAMPLING_RESPONSE_FREQUENCY].map(&:to_i).find(&:positive?),
35
+ window: [cas&.window_ms, settings&.window_ms, DEFAULT_SAMPLING_WINDOW_MS].map(&:to_i).find(&:positive?)
36
+ }
37
+ end
38
+ end
24
39
 
25
- {
26
- enabled: [cas&.enable, settings&.enabled, DEFAULT_SAMPLING_ENABLED] .reject(&:nil?).first,
27
- baseline: [cas&.baseline, settings&.baseline, DEFAULT_SAMPLING_BASELINE] .map(&:to_i).find(&:positive?),
28
- request_frequency: [cas&.request_frequency, settings&.request_frequency, DEFAULT_SAMPLING_REQUEST_FREQUENCY] .map(&:to_i).find(&:positive?),
29
- response_frequency: [cas&.response_frequency, settings&.response_frequency, DEFAULT_SAMPLING_RESPONSE_FREQUENCY].map(&:to_i).find(&:positive?),
30
- window: [cas&.window_ms, settings&.window_ms, DEFAULT_SAMPLING_WINDOW_MS] .map(&:to_i).find(&:positive?)
31
- }
40
+ # Used to reset sampling when settings from TeamServer change
41
+ def reset_sampling_control
42
+ @_sampling_control = nil
32
43
  end
33
44
  end
34
45
 
@@ -59,7 +59,6 @@ module Contrast
59
59
  PROTECT_STATE_ATTRS = %i[].cs__freeze
60
60
  ASSESS_STATE_ATTRS = %i[sampling_features].cs__freeze
61
61
  APPLICATION_STATE_ATTRS = %i[modes_by_id exclusion_matchers disabled_assess_rules session_id].cs__freeze
62
- STATE_ATTRS = PROTECT_STATE_ATTRS | ASSESS_STATE_ATTRS | APPLICATION_STATE_ATTRS
63
62
 
64
63
  # Meta-define an accessor for each state attribute.
65
64
  begin
@@ -86,6 +85,24 @@ module Contrast
86
85
  reset_state
87
86
  end
88
87
 
88
+ def update_from_server_features server_features
89
+ # protect
90
+ begin
91
+ protect_state[:enabled] = server_features.protect_enabled?
92
+ end
93
+
94
+ # assess
95
+ begin
96
+ assess_state[:enabled] = server_features.assess_enabled?
97
+ assess_state[:sampling_settings] = server_features.assess.sampling
98
+ Contrast::Utils::Assess::SamplingUtil.instance.update
99
+ end
100
+ end
101
+
102
+ def update_from_application_settings application_settings
103
+ application_state.merge!(application_settings.application_state_translation)
104
+ end
105
+
89
106
  # Wipe state to zero.
90
107
  def reset_state
91
108
  @assess_rules = {}
@@ -123,10 +140,6 @@ module Contrast
123
140
  Contrast::Agent::Protect::Rule::Csrf.new
124
141
  end
125
142
 
126
- def protect_rule name
127
- @protect_rules[name]
128
- end
129
-
130
143
  # these are less 'settings' and more 'how do I behave.'
131
144
  # relocate to Agent or Assess/Protect.
132
145
  def protect_rule_mode rule_id
@@ -15,6 +15,7 @@ module Contrast
15
15
  BOOLEANS = [true, false].cs__freeze
16
16
 
17
17
  attr_reader :map
18
+
18
19
  alias_method :to_hash, :map
19
20
  def_delegators :@map, :empty?, :key?,
20
21
  :delete,
@@ -8,6 +8,7 @@ module Contrast
8
8
  # explicitly to false.
9
9
  class DefaultValue
10
10
  attr_reader :value
11
+
11
12
  def initialize value
12
13
  @value = value
13
14
  end
@@ -8,12 +8,6 @@ module Contrast
8
8
  # Common Configuration settings. Those in this section pertain to the
9
9
  # rule mode of a single protect rule in the Agent.
10
10
  class ProtectRuleConfiguration < BaseConfiguration
11
- # rubocop:disable Style/SymbolArray, Layout/SpaceBeforeComma, Layout/SpaceInsideArrayLiteralBrackets:
12
- BLOCKING_RULES = ::Set.new([:BLOCK , :BLOCK_AT_PERIMETER ]).cs__freeze
13
- PREFILTER_RULES = ::Set.new([ :BLOCK_AT_PERIMETER ]).cs__freeze
14
- POSTFILTER_RULES = ::Set.new([:BLOCK , :PERMIT, :MONITOR]).cs__freeze
15
- # rubocop:enable Style/SymbolArray, Layout/SpaceBeforeComma, Layout/SpaceInsideArrayLiteralBrackets:
16
-
17
11
  KEYS = {
18
12
  enable: EMPTY_VALUE,
19
13
  mode: EMPTY_VALUE,
@@ -24,14 +18,6 @@ module Contrast
24
18
  def initialize hsh
25
19
  super(hsh, KEYS)
26
20
  end
27
-
28
- def off?
29
- mode == 'off'
30
- end
31
-
32
- def on?
33
- !off?
34
- end
35
21
  end
36
22
  end
37
23
  end
@@ -63,11 +63,9 @@ module Contrast
63
63
  # in an infinite loop on the to_sym method used later.
64
64
  def method_missing symbol, *args
65
65
  with_contrast_scope do
66
- begin
67
- root.public_send(symbol, *args)
68
- rescue NoMethodError => _e
69
- super
70
- end
66
+ root.public_send(symbol, *args)
67
+ rescue NoMethodError => _e
68
+ super
71
69
  end
72
70
  end
73
71
 
@@ -0,0 +1,39 @@
1
+ # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ module ActionController
5
+ module Railties
6
+ # Used to monkey patch all the inherited calls in action_pack
7
+ #
8
+ # This is the usual entry point for Rails inheritance work, so it should
9
+ # catch most of the calls to inherited.
10
+ module Helpers
11
+ alias_method :cs__patched_inherited, :inherited
12
+ def inherited klass
13
+ klass&.instance_variable_set(:@cs__defining_class, true)
14
+ cs__patched_inherited(klass)
15
+ ensure
16
+ klass&.instance_variable_set(:@cs__defining_class, false)
17
+ end
18
+ end
19
+ end
20
+ end
21
+
22
+ module ActiveRecord
23
+ module AttributeMethods
24
+ # Used to monkey patch all the inherited calls in action_pack
25
+ module TimeZoneConversion
26
+ module ClassMethods #:nodoc:
27
+ private
28
+
29
+ alias_method :cs__patched_inherited, :inherited
30
+ def inherited klass
31
+ klass&.instance_variable_set(:@cs__defining_class, true)
32
+ cs__patched_inherited(klass)
33
+ ensure
34
+ klass&.instance_variable_set(:@cs__defining_class, false)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -13,19 +13,14 @@ module ActiveRecord
13
13
  # TODO: RUBY-534
14
14
  module ClassMethods
15
15
  include Contrast::Components::Interface
16
- access_component :logging, :agent
16
+ access_component :agent, :logging
17
17
 
18
18
  def _cs__rewrite method_name, body
19
19
  return body unless AGENT.rewrite_interpolation?
20
20
  return body unless body.is_a?(Proc)
21
21
 
22
22
  location = body.source_location
23
- return body if location.nil?
24
-
25
- # Good news, once we patch the body once, the source location
26
- # becomes eval. We may need to fix this later though (so it may
27
- # be bad news)
28
- return body if location.empty? || location[0].empty? || location[0].include?('eval')
23
+ return body unless _cs__location_available?(location)
29
24
 
30
25
  opener = Contrast::Agent::ClassReopener.new(Contrast::Agent::ModuleData.new(self))
31
26
  original_source_code = opener.source_code(location, method_name)
@@ -43,9 +38,19 @@ module ActiveRecord
43
38
 
44
39
  unbound_eval(cs__name, new_method_source)
45
40
  rescue SyntaxError, StandardError => e
46
- logger.debug(e, "Can't parse method source in scoped method #{ method_name }: #{ e.message }")
41
+ logger.debug('Can\'t parse method source in scoped method', e, method: method_name)
47
42
  body
48
43
  end
44
+
45
+ # Good news, once we patch the body once, the source location
46
+ # becomes eval. We may need to fix this later though (so it may
47
+ # be bad news)
48
+ def _cs__location_available? location
49
+ return false if location.nil?
50
+ return false if location.empty? || location[0].empty? || location[0].include?('eval')
51
+
52
+ true
53
+ end
49
54
  end
50
55
  end
51
56
  end
@@ -3,7 +3,7 @@
3
3
 
4
4
  module Contrast
5
5
  module CoreExtensions
6
- # Our top level Protect namespace in the Core Extensions section of our
6
+ # Our top level Assess namespace in the Core Extensions section of our
7
7
  # code. These patches are those that are invoked directly from a patched
8
8
  # Class.
9
9
  #
@@ -31,7 +31,6 @@ module Contrast
31
31
  cs__scoped_require 'contrast/agent/assess/policy/patcher'
32
32
 
33
33
  # classes that don't play nice w/ our standard propagation
34
- cs__scoped_require 'contrast/agent/assess/frozen_properties'
35
34
  cs__scoped_require 'contrast/extensions/ruby_core/assess/assess_extension'
36
35
  # this needs to come first b/c array and others work on strings and
37
36
  # expect them to be trackable