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
@@ -54,7 +54,6 @@ module Contrast
54
54
  # will have the same functional characteristics as the original.
55
55
  # Regexp#inspect gives you a "more nicely formatted" version than #to_s.
56
56
  # Regexp#source will give you the original source.
57
- # TODO RUBY-683, would we ever get a hit on one but not the other?
58
57
 
59
58
  # Use #match? because it doesn't fill out global variables
60
59
  # in the way match or =~ do.
@@ -7,28 +7,42 @@ module Contrast
7
7
  # A Tag represents a range in a given piece of data. It is used by the
8
8
  # Agent to determine if a vulnerable dataflow has occurred.
9
9
  class Tag
10
- attr_reader :length, # length of tagged text within string
10
+ attr_reader :label, # the label of this tag
11
+ :length, # length of tagged text within string
11
12
  :start_idx, # start of range
12
- :end_idx # end of range (calculated from start + length)
13
+ :end_idx # end of range (calculated from start + length), exclusive
13
14
 
14
15
  # Initialize a new tag
15
- # length : the length of the string described with this tag
16
- # start_idx : (default: 0) the starting position in the string for this tag
17
- def initialize length, start_idx = 0
16
+ #
17
+ # @param label [String] the lable of the tag
18
+ # @param length [Integer] the length of the string described with this
19
+ # tag
20
+ # @param start_idx [Integer] (0) the starting position in the string for
21
+ # this tag
22
+ def initialize label, length, start_idx = 0
23
+ @label = label
18
24
  update_range(start_idx, start_idx + length)
19
25
  end
20
26
 
21
27
  # Return true if the tag covers the given position in the string
28
+ #
29
+ # @param idx [Integer] the index to check
30
+ # @return [Boolean]
22
31
  def covers? idx
23
32
  idx >= start_idx && idx < end_idx
24
33
  end
25
34
 
26
35
  # Return true if the tag is above the given position in the string
36
+ # @param idx [Integer] the index to check
37
+ # @return [Boolean]
27
38
  def above? idx
28
39
  idx < start_idx
29
40
  end
30
41
 
31
- # Return the range that this tag covers
42
+ # Return the range that this tag covers, from start (inclusive) to
43
+ # end (exclusive).
44
+ #
45
+ # @return [Range]
32
46
  def range
33
47
  start_idx...end_idx
34
48
  end
@@ -38,10 +52,11 @@ module Contrast
38
52
  end
39
53
 
40
54
  # Return if a given tag overlaps this one
41
- def overlaps? other
42
- return true if @start_idx < other.start_idx && @end_idx >= other.start_idx # we start below other & end in it
43
- return true if @start_idx >= other.start_idx && @end_idx <= other.end_idx # we start and end in other
44
- return true if @start_idx <= other.end_idx && @end_idx > other.end_idx # we start in other & end above it
55
+ def overlaps? start_idx, end_idx
56
+ return true if @start_idx < start_idx && @end_idx >= start_idx # we start below range & end in it
57
+ return true if @start_idx >= start_idx && @end_idx <= end_idx # we start and end in range
58
+
59
+ @start_idx <= end_idx && @end_idx > end_idx # we start in range & end above it
45
60
  end
46
61
 
47
62
  def shift idx
@@ -71,7 +86,7 @@ module Contrast
71
86
  # Returns true if the other tag was merged into
72
87
  # this tag
73
88
  def merge other
74
- return unless overlaps?(other)
89
+ return unless overlaps?(other.start_idx, other.end_idx)
75
90
 
76
91
  start = other.start_idx < @start_idx ? other.start_idx : @start_idx
77
92
  finish = other.end_idx > @end_idx ? other.end_idx : @end_idx
@@ -86,7 +101,7 @@ module Contrast
86
101
  new_start_idx = start >= 0 ? start : 0
87
102
  # If a tag were to go negative, cut off the negative portion from length
88
103
  new_length = start >= 0 ? length : (length + start)
89
- Contrast::Agent::Assess::Tag.new(new_length, new_start_idx)
104
+ Contrast::Agent::Assess::Tag.new(label, new_length, new_start_idx)
90
105
  end
91
106
 
92
107
  def str_val
@@ -8,7 +8,7 @@ module Contrast
8
8
  # This module adds an at_exit hook for us to send messages that may be lost at process exit
9
9
  module AtExitHook
10
10
  include Contrast::Components::Interface
11
- access_component :contrast_service, :logging
11
+ access_component :logging
12
12
  def self.exit_hook
13
13
  @_exit_hook ||= begin
14
14
  at_exit do
@@ -29,7 +29,9 @@ module Contrast
29
29
  process_pp_id: Process.ppid)
30
30
 
31
31
  context = Contrast::Agent::REQUEST_TRACKER.current
32
- CONTRAST_SERVICE.send_message(context.activity) if context
32
+ return unless context
33
+
34
+ Contrast::Agent.messaging_queue.send_event_immediately(context.activity)
33
35
  end
34
36
  end
35
37
  end
@@ -2,25 +2,30 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  cs__scoped_require 'ripper'
5
- cs__scoped_require 'contrast/extensions/ruby_core/module'
5
+ cs__scoped_require 'contrast/extension/module'
6
6
  cs__scoped_require 'contrast/components/interface'
7
+ cs__scoped_require 'contrast/logger/log'
7
8
 
8
9
  # This method is left purposefully at the top level namespace. Moving it
9
10
  # elsewhere will break functionality as it executes evaluations against the
10
11
  # namespace from which it is called -- ie putting it in Contrast would make all
11
12
  # changes it intends for Foo happen to Contrast::Foo instead
12
13
  #
13
- # @param class_name [String] the name of the class in which the eval will
14
+ # @param _class_name [String] the name of the class in which the eval will
14
15
  # redefine functionality
15
16
  # @param content [String] the String content that will function as the code in
16
17
  # the given class
17
- def unbound_eval class_name, content
18
+ def unbound_eval _class_name, content
18
19
  # Yuck, this is a top-level method that has to break encapsulation
19
20
  # in order to access scoping!
20
21
  Contrast::Components::Scope::COMPONENT_INTERFACE.scope_for_current_ec.enter_contrast_scope!
21
22
  eval(content) # rubocop:disable Security/Eval
22
23
  rescue Exception # rubocop:disable Lint/RescueException
23
- Contrast::Agent::SettingsState.log_error("Unable to perform unbound eval of new content for #{ class_name }.")
24
+ # We can't use components here, so we have to access the log directly. I hate
25
+ # it, but we'll have to deal with it until we remove 2.5 support.
26
+ Contrast::Logger::Log.instance.logger.error('Unable to perform unbound eval of new content', module: class_name)
27
+ # And we need to return nil here, not the value from the logger.
28
+ nil
24
29
  ensure
25
30
  Contrast::Components::Scope::COMPONENT_INTERFACE.scope_for_current_ec.exit_contrast_scope!
26
31
  end
@@ -2,7 +2,6 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  cs__scoped_require 'contrast/components/interface'
5
- cs__scoped_require 'contrast/api/decorators/exclusion.rb'
6
5
 
7
6
  module Contrast
8
7
  module Agent
@@ -93,7 +92,7 @@ module Contrast
93
92
  end
94
93
 
95
94
  def code?
96
- @exclusion.type == :CODE
95
+ @exclusion.type == Contrast::Api::Settings::Exclusion::ExclusionType::CODE
97
96
  end
98
97
 
99
98
  def name
@@ -101,7 +100,7 @@ module Contrast
101
100
  end
102
101
 
103
102
  def match_all?
104
- @exclusion.match_strategy == :ALL
103
+ @exclusion.urls.nil? || @exclusion.urls.empty?
105
104
  end
106
105
 
107
106
  # Determine if the given rule is excluded by this exclusion.
@@ -0,0 +1,53 @@
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/utils/inventory_util'
6
+
7
+ module Contrast
8
+ module Agent
9
+ module Inventory
10
+ module Policy
11
+ # This Module is how we apply the Data Store detection required for the
12
+ # FlowMap feature. It is called from our patches of the targeted methods
13
+ # in which database operations occur. It is responsible for deciding if
14
+ # the given invocation is worth reporting or not.
15
+ module DataStores
16
+ class << self
17
+ include Contrast::Components::Interface
18
+
19
+ access_component :analysis, :logging
20
+ # The key used in policy.json to indicate the database type to
21
+ # report.
22
+ DATA_STORE_MARKER = 'data_store'
23
+
24
+ def report_data_store _method, _exception, properties, object, _args
25
+ return unless INVENTORY.enabled?
26
+
27
+ marker = properties[DATA_STORE_MARKER]
28
+ return unless marker
29
+
30
+ file_report(marker)
31
+ rescue StandardError => e
32
+ logger.error('Error reporting database call', e, object: object)
33
+ end
34
+
35
+ private
36
+
37
+ def file_report data_store
38
+ return unless data_store
39
+
40
+ context = Contrast::Agent::REQUEST_TRACKER.current
41
+ return unless context&.activity
42
+
43
+ context.activity.query_count += 1
44
+ return unless context.activity.query_count == 1
45
+
46
+ Contrast::Utils::InventoryUtil.append_db_config(context.activity)
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -5,7 +5,7 @@ cs__scoped_require 'contrast/agent/inventory/policy/trigger_node'
5
5
  cs__scoped_require 'contrast/agent/patching/policy/policy'
6
6
 
7
7
  # classes required by patches in the policy
8
- cs__scoped_require 'contrast/extensions/ruby_core/inventory/datastores'
8
+ cs__scoped_require 'contrast/agent/inventory/policy/datastores'
9
9
 
10
10
  module Contrast
11
11
  module Agent
@@ -7,7 +7,6 @@ cs__scoped_require 'rack'
7
7
 
8
8
  cs__scoped_require 'contrast/security_exception'
9
9
  cs__scoped_require 'contrast/utils/object_share'
10
- cs__scoped_require 'contrast/agent/service_heartbeat'
11
10
  cs__scoped_require 'contrast/components/interface'
12
11
  cs__scoped_require 'contrast/utils/heap_dump_util'
13
12
  cs__scoped_require 'contrast/agent/request_handler'
@@ -15,8 +14,6 @@ cs__scoped_require 'contrast/agent/static_analysis'
15
14
 
16
15
  cs__scoped_require 'contrast/utils/timer'
17
16
  cs__scoped_require 'contrast/utils/freeze_util'
18
- cs__scoped_require 'contrast/utils/service_sender_util'
19
- cs__scoped_require 'contrast/utils/service_response_util'
20
17
 
21
18
  module Contrast
22
19
  module Agent
@@ -26,49 +23,45 @@ module Contrast
26
23
  # as it goes through the middleware stack inside of #call.
27
24
  class Middleware
28
25
  include Contrast::Components::Interface
29
- access_component :agent, :logging, :scope
26
+ access_component :agent, :config, :logging, :scope, :settings
30
27
 
31
28
  attr_reader :app
32
29
 
33
30
  # Allows the Agent to function as a middleware. We perform all our one-time whole-app routines in here
34
- # since we're only going to be initialized a single time. Our initializatoin order is:
35
- # - log environment, configuration, and all libraries
36
- # - start the service sending thread, responsible for sending and processing messages
37
- # - start the heartbeat thread, which triggers service startup
38
- # - start instrumenting libraries and do a 'catchup' patch for everything we didn't see get loaded
39
- # - enable TracePoint, which handles all class loads and required instrumentation going forward
31
+ # since we're only going to be initialized a single time. Our initialization order is:
32
+ # - capture the application
33
+ # - setup the Agent
34
+ # - startup the Agent
40
35
  #
41
36
  # @param app [Rack::Application] the application to be instrumented
42
37
  # @param _legacy_param [nil] was a flag we no longer need, but Sinatra may call it
43
-
44
38
  def initialize app, _legacy_param = nil
45
- @app = app # THIS MUST BE FIRST AND ALWAYS SET!
46
- # TODO: RUBY-545 nomenclature here needs to be updated.
47
- # enabled/initialized are really only reflective of whether we were able to parse the config,
48
- # which is the bare minimum for the agent to do anything.
39
+ @app = app # THIS MUST BE FIRST AND ALWAYS SET!
40
+ setup_agent # THIS MUST BE SECOND AND ALWAYS CALLED!
49
41
  unless AGENT.enabled?
50
42
  logger.error(
51
- 'Contrast middleware initializer detected an early-stage setup failure (likely config parse). Disabling.')
43
+ 'The Agent was unable to initialize before the application middleware was initialized. Disabling permanently.')
52
44
  AGENT.disable! # ensure the agent is disabled (probably redundant)
53
45
  return
54
46
  end
55
47
  agent_startup_routine
56
48
  end
57
49
 
50
+ # Startup the Agent as part of the initialization process:
51
+ # - start the service sending thread, responsible for sending and
52
+ # processing messages
53
+ # - start the heartbeat thread, which triggers service startup
54
+ # - start instrumenting libraries and do a 'catchup' patch for everything
55
+ # we didn't see get loaded
56
+ # - enable TracePoint, which handles all class loads and required
57
+ # instrumentation going forward
58
58
  def agent_startup_routine
59
- logger.debug_with_time('middleware: log environment') do
60
- settings.log_environment
61
- settings.log_configuration
62
- settings.log_specific_libraries
63
- settings.log_all_libraries
64
- end
65
-
66
59
  logger.debug_with_time('middleware: starting service') do
67
- run_service_threads
60
+ Contrast::Agent.thread_watcher.ensure_running?
68
61
  end
69
62
 
70
63
  logger.debug_with_time('middleware: instrument shared libraries and patch') do
71
- instrument_and_patch
64
+ Contrast::Agent::Patching::Policy::Patcher.patch
72
65
  end
73
66
 
74
67
  AGENT.enable_tracepoint
@@ -96,10 +89,27 @@ module Contrast
96
89
 
97
90
  private
98
91
 
92
+ def setup_agent
93
+ SETTINGS.reset_state
94
+
95
+ if CONFIG.invalid?
96
+ AGENT.disable!
97
+ logger.error('!!! CONFIG FILE IS INVALID - DISABLING CONTRAST AGENT !!!')
98
+ elsif CONFIG.disabled?
99
+ AGENT.disable!
100
+ logger.warn('Contrast disabled by configuration. Continuing without instrumentation.')
101
+ else
102
+ AGENT.enable!
103
+ end
104
+ end
105
+
99
106
  # This is where we process each request we intercept as a middleware. We make the request context
100
107
  # available globally so that it can be accessed from anywhere. A RequestHandler object is made
101
108
  # for each request, which handles prefilter and postfilter operations.
102
109
  def call_with_agent env
110
+ Contrast::Agent.thread_watcher.ensure_running?
111
+ return unless AGENT.enabled?
112
+
103
113
  framework_request = Contrast::Agent.framework_manager.retrieve_request(env)
104
114
  context = Contrast::Agent::RequestContext.new(framework_request)
105
115
  response = nil
@@ -155,7 +165,7 @@ module Contrast
155
165
  if exception.is_a?(Contrast::SecurityException) ||
156
166
  exception.message&.include?(SECURITY_EXCEPTION_MARKER)
157
167
 
158
- exception_control = settings.exception_control
168
+ exception_control = AGENT.exception_control
159
169
  raise exception unless exception_control[:enable]
160
170
 
161
171
  [exception_control[:status], {}, [exception_control[:message]]]
@@ -164,24 +174,6 @@ module Contrast
164
174
  raise exception
165
175
  end
166
176
  end
167
-
168
- def settings
169
- Contrast::Agent::FeatureState.instance
170
- end
171
-
172
- # Rspec stubs over these methods for simplicity's sake in testing
173
- def run_service_threads
174
- Contrast::Utils::ServiceSenderUtil.start
175
- Contrast::Agent::ServiceHeartbeat.new.start
176
- end
177
-
178
- # All this method chain does anymore is load the Thread patch that
179
- # propagates request contexts. That can go away RUBY-700.
180
- SHARED_LIBRARIES = %w[contrast/extensions/ruby_core/thread].cs__freeze
181
- def instrument_and_patch
182
- SHARED_LIBRARIES.each { |lib| settings.instrument(lib) }
183
- Contrast::Agent::Patching::Policy::Patcher.patch
184
- end
185
177
  end
186
178
  end
187
179
  end
@@ -1,6 +1,8 @@
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/components/interface'
5
+ cs__scoped_require 'contrast/extension/module'
4
6
  cs__scoped_require 'contrast/utils/class_util'
5
7
 
6
8
  module Contrast
@@ -9,13 +11,16 @@ module Contrast
9
11
  module Policy
10
12
  # Used to handle tracking patches that need to apply special instrumentation when a module is loaded
11
13
  class AfterLoadPatch
12
- attr_reader :applied, :module_name, :instrumentation_file_path, :method_to_instrument
14
+ include Contrast::Components::Interface
15
+ access_component :scope
16
+ attr_reader :applied, :module_name, :instrumentation_file_path, :method_to_instrument, :instrumenting_module
13
17
 
14
- def initialize module_name, instrumentation_file_path, method_to_instrument: nil
18
+ def initialize module_name, instrumentation_file_path, method_to_instrument: nil, instrumenting_module:
15
19
  @applied = false
16
20
  @module_name = module_name
17
21
  @method_to_instrument = method_to_instrument
18
22
  @instrumentation_file_path = instrumentation_file_path
23
+ @instrumenting_module = instrumenting_module
19
24
  end
20
25
 
21
26
  def applied?
@@ -55,6 +60,10 @@ module Contrast
55
60
 
56
61
  def instrument!
57
62
  cs__scoped_require instrumentation_file_path
63
+ if instrumenting_module
64
+ mod = Module.cs__const_get(instrumenting_module)
65
+ with_contrast_scope { mod.instrument } if mod
66
+ end
58
67
  @applied = true
59
68
  end
60
69
 
@@ -3,6 +3,7 @@
3
3
 
4
4
  cs__scoped_require 'contrast/components/interface'
5
5
  cs__scoped_require 'contrast/agent/patching/policy/after_load_patch'
6
+ cs__scoped_require 'contrast/framework/manager'
6
7
 
7
8
  module Contrast
8
9
  module Agent
@@ -14,84 +15,78 @@ module Contrast
14
15
  include Contrast::Components::Interface
15
16
  access_component :agent, :logging
16
17
 
17
- AFTER_LOAD_PATCHES = Set.new([
18
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
19
- 'ActiveRecord::AttributeMethods::TimeZoneConversion::ClassMethods',
20
- 'contrast/extensions/framework/rails/active_record_time_zone_inherited',
21
- method_to_instrument: :inherited),
22
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
23
- 'ActionController::Railties::Helper::ClassMethods',
24
- 'contrast/extensions/framework/rails/action_controller_railties_helper_inherited',
25
- method_to_instrument: :inherited),
26
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
27
- 'ActiveRecord::Scoping::Named::ClassMethods',
28
- 'contrast/extensions/framework/rails/active_record_named'),
29
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
30
- 'ActiveRecord::AttributeMethods::Read::ClassMethods',
31
- 'contrast/extensions/framework/rails/active_record'),
32
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
33
- 'Sinatra::Base',
34
- 'contrast/extensions/framework/sinatra/base'),
35
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
36
- 'Rails::Application::Configuration',
37
- 'contrast/extensions/framework/rails/configuration',
38
- method_to_instrument: :session_store),
39
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
40
- 'Rack::Request',
41
- 'contrast/extensions/framework/rack/request'),
42
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
43
- 'Rack::Response',
44
- 'contrast/extensions/framework/rack/response'),
45
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
46
- 'ActionController::Live::Buffer',
47
- 'contrast/extensions/framework/rails/buffer'),
48
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
49
- 'Rack::Session::Cookie',
50
- 'contrast/extensions/framework/rack/cookie')
51
- ])
52
-
53
18
  # After initialization run a catchup check to instrument any already loaded modules we care about
54
19
  def catchup_after_load_patches
20
+ apply_require_patches!
55
21
  apply_direct_patches!
56
- applied = []
57
- AFTER_LOAD_PATCHES.each do |after_load_patch|
58
- next unless after_load_patch.target_defined?
59
- next if AGENT.skip_instrumentation?(after_load_patch.module_name)
60
-
61
- logger.trace(
62
- 'Catching up on already loaded afterload patch - applying instrumentation',
63
- module: after_load_patch.module_name)
64
- after_load_patch.instrument!
65
- applied << after_load_patch
66
- end
67
- AFTER_LOAD_PATCHES.subtract(applied)
22
+ apply_load_patches!
68
23
  end
69
24
 
25
+ private
26
+
70
27
  # These patches need to be applied directly, not from our policy, so
71
28
  # do so and do so only once. This should be the new standard so that
72
29
  # there are no require time side effects of loading our core
73
30
  # extensions.
74
31
  def apply_direct_patches!
75
32
  @_apply_direct_patches ||= begin
76
- Contrast::CoreExtensions::Assess::FiberPropagator.instrument_fiber_track
77
- Contrast::CoreExtensions::Assess::HashPropagator.instrument_hash_track
78
- Contrast::CoreExtensions::Assess::RegexpPropagator.instrument_regexp_track
79
- Contrast::CoreExtensions::Assess::StringPropagator.instrument_string
80
- Contrast::CoreExtensions::Assess::StringPropagator.instrument_string_interpolation
33
+ Contrast::Extension::Assess::ArrayPropagator.instrument_array_track
34
+ Contrast::Extension::Assess::EvalTrigger.instrument_basic_object_track
35
+ Contrast::Extension::Assess::EvalTrigger.instrument_module_track
36
+ Contrast::Extension::Assess::FiberPropagator.instrument_fiber_track
37
+ Contrast::Extension::Assess::HashPropagator.instrument_hash_track
38
+ Contrast::Extension::Assess::KernelPropagator.instrument_kernel_track
39
+ Contrast::Extension::Assess::RegexpPropagator.instrument_regexp_track
40
+ Contrast::Extension::Assess::StringPropagator.instrument_string
41
+ Contrast::Extension::Assess::StringPropagator.instrument_string_interpolation
42
+
43
+ Contrast::Extension::Protect::Kernel.instrument
81
44
  true
82
45
  end
83
46
  end
84
47
 
48
+ def apply_load_patches!
49
+ after_load_patches.each do |after_load_patch|
50
+ next unless after_load_patch.target_defined?
51
+ next if AGENT.skip_instrumentation?(after_load_patch.module_name)
52
+
53
+ logger.trace(
54
+ 'Catching up on already loaded afterload patch - applying instrumentation',
55
+ module: after_load_patch.module_name)
56
+ after_load_patch.instrument!
57
+ end
58
+ after_load_patches.delete_if(&:applied?)
59
+ end
60
+
61
+ # These patches need to be applied directly, not from our policy, and
62
+ # are applied as a result of requiring the file as they alias methods
63
+ # directly, allowing us to control things like scope and exception
64
+ # handling
65
+ def apply_require_patches!
66
+ @_apply_require_patches ||= begin
67
+ cs__scoped_require 'contrast/extension/thread'
68
+ cs__scoped_require 'contrast/extension/kernel'
69
+ true
70
+ rescue LoadError, StandardError => e
71
+ logger.error('failed instrumenting apply_require_patches!', e)
72
+ false
73
+ end
74
+ end
75
+
76
+ def after_load_patches
77
+ @_after_load_patches ||= Contrast::Agent.framework_manager.find_after_load_patches
78
+ end
79
+
85
80
  # Use for any checks after we've initialized
86
81
  def load_patches_for_module module_name
87
- return if AFTER_LOAD_PATCHES.empty?
82
+ return if after_load_patches.empty?
88
83
 
89
- patch = AFTER_LOAD_PATCHES.find { |after_load_patch| after_load_patch.applies?(module_name) }
84
+ patch = after_load_patches.find { |after_load_patch| after_load_patch.applies?(module_name) }
90
85
  return unless patch
91
86
 
92
87
  logger.trace('Detected loading of afterload patch - applying instrumentation', module: module_name)
93
88
  patch.instrument!
94
- AFTER_LOAD_PATCHES.subtract([patch]) if patch.applied?
89
+ after_load_patches.delete_if(&:applied?)
95
90
  end
96
91
  end
97
92
  end