contrast-agent 3.11.0 → 3.13.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (298) hide show
  1. checksums.yaml +4 -4
  2. data/.dockerignore +0 -1
  3. data/.flayignore +1 -0
  4. data/.gitignore +1 -1
  5. data/.simplecov +1 -1
  6. data/Rakefile +31 -0
  7. data/ext/build_funchook.rb +0 -2
  8. data/ext/cs__assess_active_record_named/cs__active_record_named.c +7 -2
  9. data/ext/cs__assess_active_record_named/cs__active_record_named.h +1 -0
  10. data/ext/cs__assess_array/cs__assess_array.c +2 -1
  11. data/ext/cs__assess_array/cs__assess_array.h +1 -0
  12. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +3 -7
  13. data/ext/cs__assess_basic_object/cs__assess_basic_object.h +2 -1
  14. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +2 -8
  15. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.h +0 -1
  16. data/ext/cs__assess_kernel/cs__assess_kernel.c +1 -1
  17. data/ext/cs__assess_module/cs__assess_module.c +5 -7
  18. data/ext/cs__assess_module/cs__assess_module.h +3 -0
  19. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +1 -6
  20. data/ext/cs__assess_yield_track/cs__assess_yield_track.c +1 -5
  21. data/ext/cs__assess_yield_track/cs__assess_yield_track.h +0 -1
  22. data/ext/cs__common/cs__common.c +25 -1
  23. data/ext/cs__common/cs__common.h +3 -0
  24. data/ext/cs__common/extconf.rb +0 -14
  25. data/ext/cs__protect_kernel/cs__protect_kernel.c +4 -2
  26. data/ext/cs__protect_kernel/cs__protect_kernel.h +1 -0
  27. data/ext/extconf_common.rb +0 -28
  28. data/lib/contrast.rb +3 -2
  29. data/lib/contrast/agent.rb +33 -24
  30. data/lib/contrast/agent/assess.rb +0 -9
  31. data/lib/contrast/agent/assess/contrast_event.rb +28 -167
  32. data/lib/contrast/agent/assess/events/source_event.rb +3 -7
  33. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +1 -1
  34. data/lib/contrast/agent/assess/policy/patcher.rb +1 -0
  35. data/lib/contrast/agent/assess/policy/policy_node.rb +5 -99
  36. data/lib/contrast/agent/assess/policy/policy_scanner.rb +1 -1
  37. data/lib/contrast/agent/assess/policy/propagation_method.rb +4 -2
  38. data/lib/contrast/agent/assess/policy/propagation_node.rb +5 -1
  39. data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
  40. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  41. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +1 -3
  42. data/lib/contrast/agent/assess/policy/propagator/insert.rb +1 -4
  43. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +9 -1
  44. data/lib/contrast/agent/assess/policy/propagator/remove.rb +6 -11
  45. data/lib/contrast/agent/assess/policy/propagator/select.rb +4 -4
  46. data/lib/contrast/agent/assess/policy/propagator/split.rb +2 -2
  47. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +4 -4
  48. data/lib/contrast/agent/assess/policy/propagator/trim.rb +6 -10
  49. data/lib/contrast/agent/assess/policy/source_method.rb +1 -2
  50. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +90 -0
  51. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +57 -0
  52. data/lib/contrast/agent/assess/policy/trigger_method.rb +2 -14
  53. data/lib/contrast/agent/assess/policy/trigger_node.rb +20 -5
  54. data/lib/contrast/agent/assess/properties.rb +4 -382
  55. data/lib/contrast/agent/assess/property/evented.rb +78 -0
  56. data/lib/contrast/agent/assess/property/tagged.rb +339 -0
  57. data/lib/contrast/agent/assess/rule/base.rb +0 -15
  58. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +5 -6
  59. data/lib/contrast/agent/assess/rule/redos.rb +0 -1
  60. data/lib/contrast/agent/assess/tag.rb +27 -12
  61. data/lib/contrast/agent/at_exit_hook.rb +4 -2
  62. data/lib/contrast/agent/class_reopener.rb +9 -4
  63. data/lib/contrast/agent/exclusion_matcher.rb +2 -3
  64. data/lib/contrast/agent/inventory/policy/datastores.rb +53 -0
  65. data/lib/contrast/agent/inventory/policy/policy.rb +1 -1
  66. data/lib/contrast/agent/middleware.rb +36 -44
  67. data/lib/contrast/agent/patching/policy/after_load_patch.rb +11 -2
  68. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +51 -56
  69. data/lib/contrast/agent/patching/policy/patch.rb +3 -2
  70. data/lib/contrast/agent/patching/policy/patcher.rb +10 -12
  71. data/lib/contrast/agent/patching/policy/policy.rb +3 -3
  72. data/lib/contrast/agent/patching/policy/policy_node.rb +3 -3
  73. data/lib/contrast/agent/patching/policy/trigger_node.rb +1 -1
  74. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +63 -0
  75. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +52 -0
  76. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +68 -0
  77. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +117 -0
  78. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +54 -0
  79. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +129 -0
  80. data/lib/contrast/agent/protect/policy/policy.rb +6 -6
  81. data/lib/contrast/agent/protect/policy/rule_applicator.rb +51 -0
  82. data/lib/contrast/agent/protect/rule.rb +0 -5
  83. data/lib/contrast/agent/protect/rule/base.rb +25 -36
  84. data/lib/contrast/agent/protect/rule/base_service.rb +1 -1
  85. data/lib/contrast/agent/protect/rule/cmd_injection.rb +3 -3
  86. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +2 -7
  87. data/lib/contrast/agent/protect/rule/path_traversal.rb +2 -7
  88. data/lib/contrast/agent/protect/rule/sqli.rb +4 -4
  89. data/lib/contrast/agent/protect/rule/xxe.rb +1 -0
  90. data/lib/contrast/agent/railtie.rb +1 -0
  91. data/lib/contrast/agent/reaction_processor.rb +3 -3
  92. data/lib/contrast/agent/request.rb +91 -334
  93. data/lib/contrast/agent/request_context.rb +17 -18
  94. data/lib/contrast/agent/request_handler.rb +2 -2
  95. data/lib/contrast/agent/response.rb +2 -83
  96. data/lib/contrast/agent/scope.rb +1 -1
  97. data/lib/contrast/agent/service_heartbeat.rb +8 -10
  98. data/lib/contrast/agent/static_analysis.rb +2 -3
  99. data/lib/contrast/agent/thread_watcher.rb +49 -0
  100. data/lib/contrast/agent/version.rb +1 -1
  101. data/lib/contrast/agent/worker_thread.rb +24 -0
  102. data/lib/contrast/api.rb +3 -5
  103. data/lib/contrast/api/communication.rb +20 -0
  104. data/lib/contrast/api/communication/connection_status.rb +41 -0
  105. data/lib/contrast/api/communication/messaging_queue.rb +79 -0
  106. data/lib/contrast/{utils/service_response_util.rb → api/communication/response_processor.rb} +15 -22
  107. data/lib/contrast/api/communication/service_lifecycle.rb +61 -0
  108. data/lib/contrast/api/communication/socket.rb +45 -0
  109. data/lib/contrast/api/communication/socket_client.rb +76 -0
  110. data/lib/contrast/api/communication/speedracer.rb +111 -0
  111. data/lib/contrast/api/communication/tcp_socket.rb +31 -0
  112. data/lib/contrast/api/communication/unix_socket.rb +27 -0
  113. data/lib/contrast/api/decorators.rb +10 -0
  114. data/lib/contrast/api/decorators/address.rb +60 -0
  115. data/lib/contrast/api/decorators/application_settings.rb +7 -3
  116. data/lib/contrast/api/decorators/application_update.rb +0 -9
  117. data/lib/contrast/api/decorators/http_request.rb +139 -0
  118. data/lib/contrast/api/decorators/message.rb +75 -0
  119. data/lib/contrast/api/decorators/rasp_rule_sample.rb +28 -0
  120. data/lib/contrast/api/decorators/route_coverage.rb +57 -0
  121. data/lib/contrast/api/decorators/trace_event.rb +99 -0
  122. data/lib/contrast/api/decorators/trace_event_object.rb +57 -0
  123. data/lib/contrast/api/decorators/trace_event_signature.rb +46 -0
  124. data/lib/contrast/api/decorators/trace_taint_range.rb +51 -0
  125. data/lib/contrast/api/decorators/trace_taint_range_tags.rb +109 -0
  126. data/lib/contrast/api/decorators/user_input.rb +40 -0
  127. data/lib/contrast/components/agent.rb +17 -12
  128. data/lib/contrast/components/app_context.rb +27 -2
  129. data/lib/contrast/components/assess.rb +25 -15
  130. data/lib/contrast/components/config.rb +4 -9
  131. data/lib/contrast/components/contrast_service.rb +23 -67
  132. data/lib/contrast/components/interface.rb +5 -13
  133. data/lib/contrast/components/inventory.rb +5 -1
  134. data/lib/contrast/components/logger.rb +2 -2
  135. data/lib/contrast/components/protect.rb +40 -4
  136. data/lib/contrast/components/scope.rb +2 -52
  137. data/lib/contrast/components/settings.rb +18 -18
  138. data/lib/contrast/config/protect_rules_configuration.rb +0 -1
  139. data/lib/contrast/configuration.rb +2 -2
  140. data/lib/contrast/{extensions/ruby_core → extension}/assess.rb +12 -15
  141. data/lib/contrast/extension/assess/array.rb +77 -0
  142. data/lib/contrast/{extensions/ruby_core → extension}/assess/assess_extension.rb +3 -4
  143. data/lib/contrast/{extensions/ruby_core → extension}/assess/erb.rb +0 -0
  144. data/lib/contrast/extension/assess/eval_trigger.rb +78 -0
  145. data/lib/contrast/{extensions/ruby_core → extension}/assess/exec_trigger.rb +1 -1
  146. data/lib/contrast/{extensions/ruby_core → extension}/assess/fiber.rb +7 -6
  147. data/lib/contrast/{extensions/ruby_core → extension}/assess/hash.rb +2 -2
  148. data/lib/contrast/extension/assess/kernel.rb +110 -0
  149. data/lib/contrast/{extensions/ruby_core → extension}/assess/regexp.rb +4 -4
  150. data/lib/contrast/{extensions/ruby_core → extension}/assess/string.rb +6 -6
  151. data/lib/contrast/{extensions/ruby_core → extension}/delegator.rb +0 -0
  152. data/lib/contrast/{extensions/ruby_core → extension}/inventory.rb +2 -3
  153. data/lib/contrast/extension/kernel.rb +54 -0
  154. data/lib/contrast/{extensions/ruby_core → extension}/module.rb +0 -0
  155. data/lib/contrast/{extensions/ruby_core → extension}/protect.rb +2 -2
  156. data/lib/contrast/extension/protect/kernel.rb +44 -0
  157. data/lib/contrast/{extensions/ruby_core → extension}/protect/psych.rb +1 -1
  158. data/lib/contrast/{extensions/ruby_core → extension}/thread.rb +0 -0
  159. data/lib/contrast/framework/base_support.rb +22 -23
  160. data/lib/contrast/framework/manager.rb +31 -15
  161. data/lib/contrast/framework/rack/patch/session_cookie.rb +126 -0
  162. data/lib/contrast/framework/rack/patch/support.rb +24 -0
  163. data/lib/contrast/framework/rack/support.rb +22 -0
  164. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +41 -0
  165. data/lib/contrast/framework/rails/patch/assess_configuration.rb +102 -0
  166. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +31 -0
  167. data/lib/contrast/framework/rails/patch/support.rb +67 -0
  168. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +34 -0
  169. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +39 -0
  170. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +73 -0
  171. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +33 -0
  172. data/lib/contrast/framework/rails/support.rb +86 -0
  173. data/lib/contrast/framework/sinatra/patch/base.rb +83 -0
  174. data/lib/contrast/framework/sinatra/patch/support.rb +27 -0
  175. data/lib/contrast/framework/sinatra/support.rb +98 -0
  176. data/lib/contrast/funchook/funchook.rb +45 -0
  177. data/lib/contrast/logger/application.rb +80 -0
  178. data/lib/contrast/logger/format.rb +51 -0
  179. data/lib/contrast/{agent/logger.rb → logger/log.rb} +39 -63
  180. data/lib/contrast/logger/time.rb +50 -0
  181. data/lib/contrast/tasks/config.rb +54 -0
  182. data/lib/contrast/tasks/service.rb +1 -5
  183. data/lib/contrast/utils/assess/tracking_util.rb +45 -20
  184. data/lib/contrast/utils/class_util.rb +4 -2
  185. data/lib/contrast/utils/gemfile_reader.rb +2 -2
  186. data/lib/contrast/utils/hash_digest.rb +13 -9
  187. data/lib/contrast/utils/invalid_configuration_util.rb +2 -18
  188. data/lib/contrast/utils/inventory_util.rb +2 -7
  189. data/lib/contrast/utils/job_servers_running.rb +4 -2
  190. data/lib/contrast/utils/object_share.rb +0 -2
  191. data/lib/contrast/utils/os.rb +16 -4
  192. data/lib/contrast/utils/stack_trace_utils.rb +0 -1
  193. data/lib/contrast/utils/tag_util.rb +1 -1
  194. data/lib/contrast/utils/thread_tracker.rb +1 -14
  195. data/lib/contrast/utils/timer.rb +1 -17
  196. data/resources/assess/policy.json +9 -50
  197. data/resources/inventory/policy.json +2 -2
  198. data/resources/protect/policy.json +6 -6
  199. data/ruby-agent.gemspec +9 -5
  200. data/service_executables/VERSION +1 -1
  201. data/service_executables/linux/contrast-service +0 -0
  202. data/service_executables/mac/contrast-service +0 -0
  203. metadata +103 -139
  204. data/funchook/Makefile +0 -29
  205. data/funchook/autom4te.cache/output.0 +0 -4964
  206. data/funchook/autom4te.cache/requests +0 -77
  207. data/funchook/autom4te.cache/traces.0 +0 -361
  208. data/funchook/config.log +0 -651
  209. data/funchook/config.status +0 -1015
  210. data/funchook/configure +0 -4964
  211. data/funchook/src/Makefile +0 -70
  212. data/funchook/src/config.h +0 -101
  213. data/funchook/src/config.h.in +0 -100
  214. data/funchook/src/decoder.o +0 -0
  215. data/funchook/src/distorm.o +0 -0
  216. data/funchook/src/funchook.o +0 -0
  217. data/funchook/src/funchook_io.o +0 -0
  218. data/funchook/src/funchook_syscall.o +0 -0
  219. data/funchook/src/funchook_unix.o +0 -0
  220. data/funchook/src/funchook_x86.o +0 -0
  221. data/funchook/src/instructions.o +0 -0
  222. data/funchook/src/insts.o +0 -0
  223. data/funchook/src/libfunchook.dylib +0 -0
  224. data/funchook/src/mnemonics.o +0 -0
  225. data/funchook/src/operands.o +0 -0
  226. data/funchook/src/os_func.o +0 -0
  227. data/funchook/src/os_func_unix.o +0 -0
  228. data/funchook/src/prefix.o +0 -0
  229. data/funchook/src/printf_base.o +0 -0
  230. data/funchook/src/textdefs.o +0 -0
  231. data/funchook/src/wstring.o +0 -0
  232. data/funchook/test/Makefile +0 -43
  233. data/funchook/test/funchook_test +0 -0
  234. data/funchook/test/libfunchook_test.so +0 -0
  235. data/funchook/test/libfunchook_test.so.dSYM/Contents/Info.plist +0 -20
  236. data/funchook/test/libfunchook_test.so.dSYM/Contents/Resources/DWARF/libfunchook_test.so +0 -0
  237. data/funchook/test/test_main.o +0 -0
  238. data/funchook/test/x86_64_test.o +0 -0
  239. data/lib/contrast/agent/assess/adjusted_span.rb +0 -27
  240. data/lib/contrast/agent/assess/rule/csrf.rb +0 -66
  241. data/lib/contrast/agent/assess/rule/csrf/csrf_action.rb +0 -28
  242. data/lib/contrast/agent/assess/rule/csrf/csrf_applicator.rb +0 -53
  243. data/lib/contrast/agent/assess/rule/csrf/csrf_watcher.rb +0 -136
  244. data/lib/contrast/agent/assess/rule/response_scanning_rule.rb +0 -47
  245. data/lib/contrast/agent/assess/rule/response_watcher.rb +0 -36
  246. data/lib/contrast/agent/assess/rule/watcher.rb +0 -36
  247. data/lib/contrast/agent/feature_state.rb +0 -346
  248. data/lib/contrast/agent/protect/rule/csrf.rb +0 -119
  249. data/lib/contrast/agent/protect/rule/csrf/csrf_evaluator.rb +0 -100
  250. data/lib/contrast/agent/protect/rule/csrf/csrf_token_injector.rb +0 -85
  251. data/lib/contrast/agent/settings_state.rb +0 -88
  252. data/lib/contrast/agent/socket_client.rb +0 -134
  253. data/lib/contrast/api/connection_status.rb +0 -49
  254. data/lib/contrast/api/decorators/exclusion.rb +0 -20
  255. data/lib/contrast/api/socket.rb +0 -43
  256. data/lib/contrast/api/speedracer.rb +0 -188
  257. data/lib/contrast/api/tcp_socket.rb +0 -29
  258. data/lib/contrast/api/unix_socket.rb +0 -25
  259. data/lib/contrast/extensions/framework/rack/cookie.rb +0 -24
  260. data/lib/contrast/extensions/framework/rack/request.rb +0 -24
  261. data/lib/contrast/extensions/framework/rack/response.rb +0 -23
  262. data/lib/contrast/extensions/framework/rails/action_controller_inheritance.rb +0 -39
  263. data/lib/contrast/extensions/framework/rails/action_controller_railties_helper_inherited.rb +0 -20
  264. data/lib/contrast/extensions/framework/rails/active_record.rb +0 -26
  265. data/lib/contrast/extensions/framework/rails/active_record_named.rb +0 -58
  266. data/lib/contrast/extensions/framework/rails/active_record_time_zone_inherited.rb +0 -21
  267. data/lib/contrast/extensions/framework/rails/buffer.rb +0 -28
  268. data/lib/contrast/extensions/framework/rails/configuration.rb +0 -27
  269. data/lib/contrast/extensions/framework/sinatra/base.rb +0 -59
  270. data/lib/contrast/extensions/ruby_core/assess/array.rb +0 -59
  271. data/lib/contrast/extensions/ruby_core/assess/basic_object.rb +0 -15
  272. data/lib/contrast/extensions/ruby_core/assess/kernel.rb +0 -96
  273. data/lib/contrast/extensions/ruby_core/assess/module.rb +0 -14
  274. data/lib/contrast/extensions/ruby_core/assess/tilt_template_trigger.rb +0 -78
  275. data/lib/contrast/extensions/ruby_core/assess/xpath_library_trigger.rb +0 -40
  276. data/lib/contrast/extensions/ruby_core/eval_trigger.rb +0 -51
  277. data/lib/contrast/extensions/ruby_core/inventory/datastores.rb +0 -37
  278. data/lib/contrast/extensions/ruby_core/protect/applies_command_injection_rule.rb +0 -61
  279. data/lib/contrast/extensions/ruby_core/protect/applies_deserialization_rule.rb +0 -50
  280. data/lib/contrast/extensions/ruby_core/protect/applies_no_sqli_rule.rb +0 -66
  281. data/lib/contrast/extensions/ruby_core/protect/applies_path_traversal_rule.rb +0 -115
  282. data/lib/contrast/extensions/ruby_core/protect/applies_sqli_rule.rb +0 -53
  283. data/lib/contrast/extensions/ruby_core/protect/applies_xxe_rule.rb +0 -127
  284. data/lib/contrast/extensions/ruby_core/protect/kernel.rb +0 -30
  285. data/lib/contrast/extensions/ruby_core/protect/rule_applicator.rb +0 -50
  286. data/lib/contrast/framework/rails_support.rb +0 -104
  287. data/lib/contrast/framework/sinatra_application_helper.rb +0 -49
  288. data/lib/contrast/framework/sinatra_support.rb +0 -104
  289. data/lib/contrast/framework/view_technologies_descriptor.rb +0 -21
  290. data/lib/contrast/internal_exception.rb +0 -8
  291. data/lib/contrast/utils/cache.rb +0 -58
  292. data/lib/contrast/utils/data_store_util.rb +0 -23
  293. data/lib/contrast/utils/rack_assess_session_cookie.rb +0 -104
  294. data/lib/contrast/utils/rails_assess_configuration.rb +0 -95
  295. data/lib/contrast/utils/random_util.rb +0 -22
  296. data/lib/contrast/utils/service_sender_util.rb +0 -110
  297. data/lib/contrast/utils/sinatra_helper.rb +0 -49
  298. data/resources/csrf/inject.js +0 -44
@@ -23,11 +23,6 @@ module Contrast
23
23
  # this statefulness
24
24
  attr_reader :tainted_columns # This can probably go into assess_state?
25
25
 
26
- # a vulnerability like padding oracle is exploited across
27
- # multiple requests, as a timing attack. these attempts must be
28
- # accumulated, in order to recognize the pattern and block the attack.
29
- attr_reader :accumulator_settings
30
-
31
26
  # These three 'state' variables represent atomic config/setting state,
32
27
  # outside of things like rule defs.
33
28
 
@@ -40,8 +35,7 @@ module Contrast
40
35
 
41
36
  def protect_state
42
37
  @protect_state ||= { # rubocop:disable Naming/MemoizedInstanceVariableName
43
- enabled: false,
44
- accumulator_settings: Contrast::Api::Settings::AccumulatorSettings.new
38
+ enabled: false
45
39
  }
46
40
  end
47
41
 
@@ -58,7 +52,7 @@ module Contrast
58
52
  # So, between this block and rules, that's setting state.
59
53
  PROTECT_STATE_ATTRS = %i[].cs__freeze
60
54
  ASSESS_STATE_ATTRS = %i[sampling_features].cs__freeze
61
- APPLICATION_STATE_ATTRS = %i[modes_by_id exclusion_matchers disabled_assess_rules session_id].cs__freeze
55
+ APPLICATION_STATE_ATTRS = %i[modes_by_id exclusion_matchers disabled_assess_rules].cs__freeze
62
56
 
63
57
  # Meta-define an accessor for each state attribute.
64
58
  begin
@@ -85,14 +79,30 @@ module Contrast
85
79
  reset_state
86
80
  end
87
81
 
82
+ def protect_enabled?
83
+ @_protect_enabled = !!protect_state[:enabled] if @_protect_enabled.nil?
84
+ @_protect_enabled
85
+ end
86
+
87
+ def assess_enabled?
88
+ @_assess_enabled = !!assess_state[:enabled] if @_assess_enabled.nil?
89
+ @_assess_enabled
90
+ end
91
+
92
+ def code_exclusions
93
+ exclusion_matchers.select(&:code?)
94
+ end
95
+
88
96
  def update_from_server_features server_features
89
97
  # protect
90
98
  begin
99
+ @_protect_enabled = nil
91
100
  protect_state[:enabled] = server_features.protect_enabled?
92
101
  end
93
102
 
94
103
  # assess
95
104
  begin
105
+ @_assess_enabled = nil
96
106
  assess_state[:enabled] = server_features.assess_enabled?
97
107
  assess_state[:sampling_settings] = server_features.assess.sampling
98
108
  Contrast::Utils::Assess::SamplingUtil.instance.update
@@ -118,7 +128,6 @@ module Contrast
118
128
  def build_assess_rules
119
129
  @assess_rules = {}
120
130
 
121
- Contrast::Agent::Assess::Rule::Csrf.new
122
131
  Contrast::Agent::Assess::Rule::Redos.new
123
132
  end
124
133
 
@@ -135,15 +144,6 @@ module Contrast
135
144
  Contrast::Agent::Protect::Rule::UnsafeFileUpload.new
136
145
  Contrast::Agent::Protect::Rule::Xss.new
137
146
  Contrast::Agent::Protect::Rule::Xxe.new
138
-
139
- # Beta Rules
140
- Contrast::Agent::Protect::Rule::Csrf.new
141
- end
142
-
143
- # these are less 'settings' and more 'how do I behave.'
144
- # relocate to Agent or Assess/Protect.
145
- def protect_rule_mode rule_id
146
- CONFIG.root.protect.rules[rule_id]&.mode || modes_by_id[rule_id] || :NO_ACTION
147
147
  end
148
148
  end
149
149
 
@@ -14,7 +14,6 @@ module Contrast
14
14
  'nosql-injection' => Contrast::Config::ProtectRuleConfiguration,
15
15
  'untrusted-deserialization' => Contrast::Config::ProtectRuleConfiguration,
16
16
  'method-tampering' => Contrast::Config::ProtectRuleConfiguration,
17
- csrf: Contrast::Config::ProtectRuleConfiguration,
18
17
  xxe: Contrast::Config::ProtectRuleConfiguration,
19
18
  'path-traversal' => Contrast::Config::ProtectRuleConfiguration,
20
19
  'reflected-xss' => Contrast::Config::ProtectRuleConfiguration,
@@ -88,13 +88,13 @@ module Contrast
88
88
  puts "!!! Contrast - Configuration file at #{ path } is not readable by current user"
89
89
  next
90
90
  end
91
- config = yaml_to_hash(path)
91
+ config = yaml_to_hash(path) || {}
92
92
  break
93
93
  end
94
94
 
95
95
  if config.empty?
96
96
  puts "!!! Contrast - working directory: #{ Dir.pwd }"
97
- puts '!!! Contrast - configuration file could not be found at any of the search paths'
97
+ puts '!!! Contrast - valid configuration file could not be found at any of the search paths'
98
98
  puts 'Valid configuration paths are: '
99
99
  configuration_paths.each do |path|
100
100
  puts(path)
@@ -2,14 +2,15 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Contrast
5
- module CoreExtensions
6
- # Our top level Assess namespace in the Core Extensions section of our
5
+ module Extension
6
+ # Our top level Assess namespace in the Core Extension section of our
7
7
  # code. These patches are those that are invoked directly from a patched
8
8
  # Class.
9
9
  #
10
10
  # @deprecated This is one of our earliest designs and is not nearly as
11
11
  # relevant given the move to C based patching and the lessons learned
12
12
  # therein.
13
+ # Removing it requires a C refactor to handle the namespace.
13
14
  module Assess
14
15
  cs__scoped_require 'contrast/agent/patching/policy/patcher'
15
16
 
@@ -19,7 +20,6 @@ module Contrast
19
20
  cs__scoped_require 'contrast/agent/assess/rule/provider'
20
21
 
21
22
  # tagging / dataflow
22
- cs__scoped_require 'contrast/agent/assess/adjusted_span'
23
23
  cs__scoped_require 'contrast/agent/assess/policy/policy_node'
24
24
  cs__scoped_require 'contrast/agent/assess/policy/source_node'
25
25
  cs__scoped_require 'contrast/agent/assess/policy/source_method'
@@ -31,21 +31,18 @@ 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/extensions/ruby_core/assess/assess_extension'
34
+ cs__scoped_require 'contrast/extension/assess/assess_extension'
35
35
  # this needs to come first b/c array and others work on strings and
36
36
  # expect them to be trackable
37
- cs__scoped_require 'contrast/extensions/ruby_core/assess/string'
37
+ cs__scoped_require 'contrast/extension/assess/string'
38
38
 
39
- cs__scoped_require 'contrast/extensions/ruby_core/assess/array'
40
- cs__scoped_require 'contrast/extensions/ruby_core/assess/erb'
41
- cs__scoped_require 'contrast/extensions/ruby_core/assess/fiber'
42
- cs__scoped_require 'contrast/extensions/ruby_core/assess/hash'
43
- cs__scoped_require 'contrast/extensions/ruby_core/assess/kernel'
44
- cs__scoped_require 'contrast/extensions/ruby_core/assess/regexp'
45
- cs__scoped_require 'contrast/extensions/ruby_core/assess/module'
46
- cs__scoped_require 'contrast/extensions/ruby_core/assess/basic_object'
47
- cs__scoped_require 'contrast/extensions/ruby_core/assess/tilt_template_trigger'
48
- cs__scoped_require 'contrast/extensions/ruby_core/assess/xpath_library_trigger'
39
+ cs__scoped_require 'contrast/extension/assess/array'
40
+ cs__scoped_require 'contrast/extension/assess/erb'
41
+ cs__scoped_require 'contrast/extension/assess/eval_trigger'
42
+ cs__scoped_require 'contrast/extension/assess/fiber'
43
+ cs__scoped_require 'contrast/extension/assess/hash'
44
+ cs__scoped_require 'contrast/extension/assess/kernel'
45
+ cs__scoped_require 'contrast/extension/assess/regexp'
49
46
  end
50
47
  end
51
48
  end
@@ -0,0 +1,77 @@
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
+ cs__scoped_require 'contrast/agent/patching/policy/patch'
5
+ cs__scoped_require 'contrast/agent/patching/policy/patcher'
6
+ cs__scoped_require 'contrast/components/interface'
7
+
8
+ module Contrast
9
+ module Extension
10
+ module Assess
11
+ # This is our patch of the Array class required to handle propagation
12
+ # Disclaimer: there may be a better way, but we're in a 'get it work' state.
13
+ # Hopefully, we'll be in a 'get it right' state soon.
14
+ class ArrayPropagator
15
+ include Contrast::Components::Interface
16
+
17
+ access_component :scope
18
+
19
+ ARRAY_JOIN_HASH = {
20
+ 'class_name' => 'Array',
21
+ 'instance_method' => true,
22
+ 'method_visibility' => 'public',
23
+ 'method_name' => 'join',
24
+ 'action' => 'CUSTOM',
25
+ 'source' => 'O',
26
+ 'target' => 'R',
27
+ 'patch_class' => 'NOOP',
28
+ 'patch_method' => 'cs__track_join'
29
+ }.cs__freeze
30
+ ARRAY_JOIN_NODE = Contrast::Agent::Assess::Policy::PropagationNode.new(ARRAY_JOIN_HASH)
31
+
32
+ class << self
33
+ # When you call join, they use an internal thing, so there's no good way to get at the thing being returned.
34
+ # Multiple Strings are appended with the #join method. Because that
35
+ # operation happens in C, we have to do it here rather than rely on the
36
+ # patch of our String append or concat methods.
37
+ def cs__track_join ary, separator, ret
38
+ return unless ary
39
+ return ret if Contrast::Agent::Patching::Policy::Patch.skip_assess_analysis?
40
+
41
+ with_contrast_scope do
42
+ shift = 0
43
+ separator_length = separator.nil? ? 0 : separator.to_s.length
44
+ ary.each do |obj|
45
+ if obj # skip nil here
46
+ ret.cs__copy_from(obj, shift)
47
+ shift += obj.to_s.length
48
+ end
49
+ shift += separator_length
50
+ end
51
+ return ret unless ret.cs__tracked?
52
+
53
+ ret.cs__properties.cleanup_tags
54
+ ret.cs__properties.build_event(
55
+ ARRAY_JOIN_NODE,
56
+ ret,
57
+ ary,
58
+ ret,
59
+ [separator])
60
+ ret
61
+ end
62
+ end
63
+
64
+ def instrument_array_track
65
+ @_instrument_array_track ||= begin
66
+ cs__scoped_require 'cs__assess_array/cs__assess_array'
67
+ true
68
+ end
69
+ rescue StandardError, LoadError => e
70
+ logger.error('Error loading assess track patch', e)
71
+ false
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -5,13 +5,13 @@ cs__scoped_require 'contrast/agent/assess/properties'
5
5
  cs__scoped_require 'contrast/agent/assess/insulator'
6
6
 
7
7
  module Contrast
8
- module CoreExtensions
8
+ module Extension
9
9
  module Assess
10
10
  # This module is responsible for maintaining the data we need to
11
11
  # construct a trace event for the object in which it is included. Rather
12
12
  # than have this code all over the place, any class that wants to use
13
13
  # dataflow features should be sent
14
- # 'include Contrast::CoreExtensions::Assess::AssessExtension'
14
+ # 'include Contrast::Extension::Assess::AssessExtension'
15
15
  module AssessExtension
16
16
  # Lazily build properties object. Only objects that have been tracked
17
17
  # will have the @_cs__properties, but all will respond to the
@@ -122,8 +122,7 @@ module Contrast
122
122
  range = existing[0]
123
123
  range.repurpose(0, ret_length)
124
124
  else
125
- span = Contrast::Agent::Assess::AdjustedSpan.new(0, ret_length)
126
- ret.cs__properties.add_tag(key, span)
125
+ ret.cs__properties.add_tag(key, 0...ret_length)
127
126
  end
128
127
  end
129
128
  end
@@ -0,0 +1,78 @@
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
+ cs__scoped_require 'contrast/components/interface'
5
+
6
+ module Contrast
7
+ module Extension
8
+ module Assess
9
+ # This Module allows us to track calls to the BasicObject#eval method,
10
+ # which violates the design of most methods we track in that we have to
11
+ # apply the trigger in a custom patch over one of the generic triggers in
12
+ # TriggerMethod.
13
+ class EvalTrigger
14
+ include Contrast::Components::Interface
15
+ access_component :logging
16
+
17
+ class << self
18
+ def instance_eval_trigger_check obj, source, ret
19
+ apply_trigger(obj, source, ret, 'BasicObject', :instance_eval)
20
+ end
21
+
22
+ def eval_trigger_check obj, source, ret, method
23
+ apply_trigger(obj, source, ret, 'Module', method)
24
+ end
25
+
26
+ def apply_trigger obj, source, ret, clazz, method
27
+ current_context = Contrast::Agent::REQUEST_TRACKER.current
28
+ return unless current_context
29
+
30
+ # Since we know this is the source of the trigger, we can do some
31
+ # optimization here and return when it is not tracked
32
+ return unless Contrast::Utils::Assess::TrackingUtil.tracked?(source)
33
+
34
+ # source might not be all the args passed in, but it is the one we care
35
+ # about. we could pass in all the args in the last param here if it
36
+ # becomes an issue in rendering on TS
37
+ Contrast::Agent::Assess::Policy::TriggerMethod.apply_eval_trigger(
38
+ current_context,
39
+ trigger_node(clazz, method),
40
+ source,
41
+ obj,
42
+ ret,
43
+ source)
44
+ end
45
+
46
+ def instrument_basic_object_track
47
+ @_instrument_basic_object_track ||= begin
48
+ cs__scoped_require 'cs__assess_basic_object/cs__assess_basic_object'
49
+ true
50
+ end
51
+ rescue StandardError, LoadError => e
52
+ logger.error('Error loading basic object track patch', e)
53
+ false
54
+ end
55
+
56
+ def instrument_module_track
57
+ @_instrument_module_track ||= begin
58
+ cs__scoped_require 'cs__assess_module/cs__assess_module'
59
+ true
60
+ end
61
+ rescue StandardError, LoadError => e
62
+ logger.error('Error loading module track patch', e)
63
+ false
64
+ end
65
+
66
+ private
67
+
68
+ def trigger_node clazz, method
69
+ triggers = Contrast::Agent::Assess::Policy::Policy.instance.triggers
70
+ return unless triggers
71
+
72
+ triggers.find { |node| node.class_name == clazz && node.method_name == method }
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -4,7 +4,7 @@
4
4
  cs__scoped_require 'contrast/agent/at_exit_hook'
5
5
 
6
6
  module Contrast
7
- module CoreExtensions
7
+ module Extension
8
8
  module Assess
9
9
  # This Module allows us to track calls to the Kernel#exec method, which
10
10
  # violates the design of most methods we track in that we have to apply
@@ -9,7 +9,7 @@ cs__scoped_require 'contrast/components/interface'
9
9
  # Specifically, we instrument 'rb_fiber_yield' and 'rb_fiber_new' in
10
10
  # order to track what happens within Enumerator#next.
11
11
  module Contrast
12
- module CoreExtensions
12
+ module Extension
13
13
  module Assess
14
14
  # This Class provides us with a way to invoke Regexp propagation for those
15
15
  # methods which are too complex to fit into one of the standard
@@ -20,15 +20,16 @@ module Contrast
20
20
 
21
21
  access_component :analysis, :logging, :scope
22
22
 
23
+ # we use funchook to patch rb_fiber_new the initialize method is not exposed by Ruby core
23
24
  FIBER_NEW_NODE_HASH = {
24
25
  'class_name' => 'Fiber',
25
26
  'instance_method' => true,
26
27
  'method_visibility' => 'public',
27
- 'method_name' => 'c_new', # TODO: Why do we patch new here and not initialize? Historically, new has been problematic.
28
+ 'method_name' => 'c_new',
28
29
  'action' => 'CUSTOM',
29
30
  'source' => 'O',
30
31
  'target' => 'R',
31
- 'patch_class' => 'Contrast::CoreExtensions::Assess::FiberPropagator',
32
+ 'patch_class' => 'Contrast::Extension::Assess::FiberPropagator',
32
33
  'patch_method' => 'track_rb_fiber_new'
33
34
  }.cs__freeze
34
35
  FIBER_NEW_NODE = Contrast::Agent::Assess::Policy::PropagationNode.new(FIBER_NEW_NODE_HASH)
@@ -43,7 +44,7 @@ module Contrast
43
44
  'action' => 'CUSTOM',
44
45
  'source' => 'O',
45
46
  'target' => 'R',
46
- 'patch_class' => 'Contrast::CoreExtensions::Assess::FiberPropagator',
47
+ 'patch_class' => 'Contrast::Extension::Assess::FiberPropagator',
47
48
  'patch_method' => 'track_rb_fiber_yield'
48
49
  }.cs__freeze
49
50
  FIBER_YIELD_NODE = Contrast::Agent::Assess::Policy::PropagationNode.new(FIBER_YIELD_NODE_HASH)
@@ -98,10 +99,10 @@ module Contrast
98
99
 
99
100
  def instrument_fiber_track
100
101
  @_instrument_fiber_variables ||= begin
101
- cs__scoped_require 'cs__assess_fiber_track/cs__assess_fiber_track'
102
+ cs__scoped_require 'cs__assess_fiber_track/cs__assess_fiber_track' if Funchook.available?
102
103
  true
103
104
  end
104
- rescue StandardError => e
105
+ rescue StandardError, LoadError => e
105
106
  logger.error('Error loading fiber track patch', e)
106
107
  false
107
108
  end
@@ -4,7 +4,7 @@
4
4
  cs__scoped_require 'contrast/components/interface'
5
5
 
6
6
  module Contrast
7
- module CoreExtensions
7
+ module Extension
8
8
  module Assess
9
9
  # This Class provides us with a way to invoke Hash propagation for those
10
10
  # methods which are too complex to fit into one of the standard
@@ -28,7 +28,7 @@ module Contrast
28
28
  cs__scoped_require 'cs__assess_hash/cs__assess_hash'
29
29
  true
30
30
  end
31
- rescue StandardError => e
31
+ rescue StandardError, LoadError => e
32
32
  logger.error('Error loading hash track patch', e)
33
33
  false
34
34
  end
@@ -0,0 +1,110 @@
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
+ cs__scoped_require 'contrast/components/interface'
5
+ cs__scoped_require 'contrast/extension/assess/exec_trigger'
6
+
7
+ module Contrast
8
+ module Extension
9
+ module Assess
10
+ # This module provides us with a way to invoke Kernel propagation for those
11
+ # methods which are too complex to fit into one of the standard
12
+ # Contrast::Agent::Assess::Policy::Propagator molds without cluttering up the
13
+ # Kernel Module or exposing our methods there.
14
+ module KernelPropagator
15
+ class << self
16
+ include Contrast::Components::Interface
17
+ include Contrast::Extension::Assess::ExecTrigger
18
+
19
+ access_component :logging
20
+
21
+ # We're 'tracking' sprintf now, meaning if anything is tracked on the way
22
+ # in, the entire result will be tracked out. We're going to take this
23
+ # approach for now b/c it's fast and easy. I don't super love it, and by
24
+ # that I mean I hate it.
25
+ #
26
+ # To actually track this, we'd have to find the index of the new things
27
+ # being added, then remove the tags at the range of the format marker,
28
+ # which is some arbitrary length thing, and add the new tags from the
29
+ # inserted string, shifted down by the length of the aforementioned
30
+ # marker.
31
+ #
32
+ # marker is in the format %[flags][width][.precision]type, type being a
33
+ # single character. We could regexp this with %.+[bBdiouxXeEfgGaAcps%]
34
+ #
35
+ # also, b/c Ruby hates us, there are things called absolute markers,
36
+ # (digit)$, that go in the flags section. These cannot be mixed w/ the
37
+ # order assumed type
38
+ #
39
+ # oh, and there's also %<name>type and %{name}... b/c of course there is
40
+ # -HM
41
+ def sprintf_tagger patcher, preshift, ret, _block
42
+ format_string = preshift.args[0]
43
+ args = preshift.args[1]
44
+
45
+ track_sprintf(ret, format_string, args)
46
+
47
+ ret.cs__properties.build_event(
48
+ patcher,
49
+ ret,
50
+ preshift.object,
51
+ ret,
52
+ preshift.args,
53
+ 1)
54
+ ret
55
+ end
56
+
57
+ def track_sprintf result, format_string, args
58
+ handle_sprintf_value(format_string, result)
59
+ if args.is_a?(String)
60
+ handle_sprintf_value(args, result)
61
+ elsif args.is_a?(Hash)
62
+ handle_sprintf_hash(args, result)
63
+ elsif args.is_a?(Array)
64
+ handle_sprintf_array args, result
65
+ end
66
+
67
+ result
68
+ rescue StandardError => e
69
+ logger.error(
70
+ 'Unable to track dataflow through sprintf', e)
71
+ result
72
+ end
73
+
74
+ def instrument_kernel_track
75
+ @_instrument_fiber_variables ||= begin
76
+ cs__scoped_require 'cs__assess_kernel/cs__assess_kernel'
77
+ true
78
+ end
79
+ rescue StandardError, LoadError => e
80
+ logger.error('Error loading kernel track patch', e)
81
+ false
82
+ end
83
+
84
+ private
85
+
86
+ def handle_sprintf_value value, result
87
+ return unless Contrast::Utils::DuckUtils.trackable?(value) && value.cs__tracked?
88
+
89
+ value.cs__properties.events.each do |event|
90
+ result.cs__properties.events << event
91
+ end
92
+ value.cs__splat_tags(result)
93
+ end
94
+
95
+ def handle_sprintf_array args, result
96
+ args.each do |value|
97
+ handle_sprintf_value(value, result)
98
+ end
99
+ end
100
+
101
+ def handle_sprintf_hash args, result
102
+ args.each_value do |value|
103
+ handle_sprintf_value(value, result)
104
+ end
105
+ end
106
+ end
107
+ end
108
+ end
109
+ end
110
+ end