contrast-agent 3.10.2 → 3.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (266) hide show
  1. checksums.yaml +4 -4
  2. data/.simplecov +5 -2
  3. data/ext/build_funchook.rb +13 -17
  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 +16 -62
  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 +37 -26
  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 +14 -4
  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 -75
  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 +33 -30
  145. data/lib/contrast/agent/rewriter.rb +22 -10
  146. data/lib/contrast/agent/rule_set.rb +49 -0
  147. data/lib/contrast/agent/scope.rb +0 -6
  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 +27 -23
  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/extensions/framework/rails/action_controller_inheritance.rb +39 -0
  177. data/lib/contrast/extensions/framework/rails/active_record_named.rb +13 -8
  178. data/lib/contrast/extensions/ruby_core/assess.rb +1 -2
  179. data/lib/contrast/extensions/ruby_core/assess/assess_extension.rb +27 -22
  180. data/lib/contrast/extensions/ruby_core/assess/erb.rb +0 -8
  181. data/lib/contrast/extensions/ruby_core/assess/exec_trigger.rb +6 -8
  182. data/lib/contrast/extensions/ruby_core/assess/fiber.rb +88 -100
  183. data/lib/contrast/extensions/ruby_core/assess/hash.rb +32 -15
  184. data/lib/contrast/extensions/ruby_core/assess/kernel.rb +28 -27
  185. data/lib/contrast/extensions/ruby_core/assess/regexp.rb +74 -196
  186. data/lib/contrast/extensions/ruby_core/assess/string.rb +15 -7
  187. data/lib/contrast/extensions/ruby_core/assess/tilt_template_trigger.rb +29 -24
  188. data/lib/contrast/extensions/ruby_core/assess/xpath_library_trigger.rb +2 -2
  189. data/lib/contrast/extensions/ruby_core/eval_trigger.rb +0 -1
  190. data/lib/contrast/extensions/ruby_core/inventory/datastores.rb +2 -2
  191. data/lib/contrast/extensions/ruby_core/protect/applies_command_injection_rule.rb +9 -20
  192. data/lib/contrast/extensions/ruby_core/protect/applies_deserialization_rule.rb +9 -19
  193. data/lib/contrast/extensions/ruby_core/protect/applies_no_sqli_rule.rb +10 -27
  194. data/lib/contrast/extensions/ruby_core/protect/applies_path_traversal_rule.rb +13 -21
  195. data/lib/contrast/extensions/ruby_core/protect/applies_sqli_rule.rb +11 -23
  196. data/lib/contrast/extensions/ruby_core/protect/applies_xxe_rule.rb +62 -78
  197. data/lib/contrast/extensions/ruby_core/protect/rule_applicator.rb +50 -0
  198. data/lib/contrast/framework/base_support.rb +10 -0
  199. data/lib/contrast/framework/manager.rb +28 -2
  200. data/lib/contrast/framework/platform_version.rb +1 -0
  201. data/lib/contrast/framework/rails_support.rb +16 -0
  202. data/lib/contrast/framework/sinatra_support.rb +12 -2
  203. data/lib/contrast/framework/view_technologies_descriptor.rb +1 -0
  204. data/lib/contrast/tasks/service.rb +2 -8
  205. data/lib/contrast/utils/assess/sampling_util.rb +4 -9
  206. data/lib/contrast/utils/assess/tracking_util.rb +7 -1
  207. data/lib/contrast/utils/boolean_util.rb +2 -2
  208. data/lib/contrast/utils/cache.rb +0 -11
  209. data/lib/contrast/utils/class_util.rb +20 -1
  210. data/lib/contrast/utils/gemfile_reader.rb +5 -3
  211. data/lib/contrast/utils/hash_digest.rb +0 -4
  212. data/lib/contrast/utils/heap_dump_util.rb +12 -11
  213. data/lib/contrast/utils/invalid_configuration_util.rb +1 -1
  214. data/lib/contrast/utils/inventory_util.rb +2 -2
  215. data/lib/contrast/utils/io_util.rb +1 -11
  216. data/lib/contrast/utils/job_servers_running.rb +2 -2
  217. data/lib/contrast/utils/object_share.rb +1 -27
  218. data/lib/contrast/utils/os.rb +1 -25
  219. data/lib/contrast/utils/rack_assess_session_cookie.rb +3 -3
  220. data/lib/contrast/utils/rails_assess_configuration.rb +3 -3
  221. data/lib/contrast/utils/service_response_util.rb +27 -53
  222. data/lib/contrast/utils/service_sender_util.rb +9 -5
  223. data/lib/contrast/utils/sinatra_helper.rb +0 -6
  224. data/lib/contrast/utils/stack_trace_utils.rb +86 -182
  225. data/lib/contrast/utils/string_utils.rb +18 -2
  226. data/lib/contrast/utils/tag_util.rb +11 -1
  227. data/lib/contrast/utils/thread_tracker.rb +2 -2
  228. data/lib/contrast/utils/timer.rb +0 -40
  229. data/resources/assess/policy.json +33 -21
  230. data/resources/protect/policy.json +9 -9
  231. data/ruby-agent.gemspec +6 -3
  232. metadata +76 -51
  233. data/ext/cs__assess_regexp_track/cs__assess_regexp_track.c +0 -63
  234. data/ext/cs__assess_regexp_track/cs__assess_regexp_track.h +0 -29
  235. data/ext/cs__assess_regexp_track/extconf.rb +0 -2
  236. data/funchook/src/libfunchook.so +0 -0
  237. data/lib/contrast/agent/assess/frozen_properties.rb +0 -41
  238. data/lib/contrast/agent/logger_manager.rb +0 -116
  239. data/lib/contrast/delegators.rb +0 -9
  240. data/lib/contrast/delegators/application_update.rb +0 -32
  241. data/lib/contrast/utils/comment_range.rb +0 -19
  242. data/lib/contrast/utils/environment_util.rb +0 -81
  243. data/lib/contrast/utils/performs_logging.rb +0 -152
  244. data/resources/factory-bot-spec/spec_helper.rb +0 -30
  245. data/resources/rubocops/kernel/catch_cop.rb +0 -37
  246. data/resources/rubocops/kernel/require_cop.rb +0 -37
  247. data/resources/rubocops/kernel/require_relative_cop.rb +0 -33
  248. data/resources/rubocops/module/autoload_cop.rb +0 -37
  249. data/resources/rubocops/module/const_defined_cop.rb +0 -37
  250. data/resources/rubocops/module/const_get_cop.rb +0 -37
  251. data/resources/rubocops/module/const_set_cop.rb +0 -37
  252. data/resources/rubocops/module/constants_cop.rb +0 -37
  253. data/resources/rubocops/module/name_cop.rb +0 -37
  254. data/resources/rubocops/object/class_cop.rb +0 -37
  255. data/resources/rubocops/object/freeze_cop.rb +0 -37
  256. data/resources/rubocops/object/frozen_cop.rb +0 -37
  257. data/resources/rubocops/object/is_a_cop.rb +0 -37
  258. data/resources/rubocops/object/method_cop.rb +0 -37
  259. data/resources/rubocops/object/respond_to_cop.rb +0 -37
  260. data/resources/rubocops/object/singleton_class_cop.rb +0 -37
  261. data/resources/rubocops/regexp/spelling_cop.rb +0 -44
  262. data/resources/rubocops/thread/new_cop.rb +0 -39
  263. data/resources/ruby-spec/ancestors_spec.rb +0 -70
  264. data/resources/ruby-spec/modulo_spec.rb +0 -831
  265. data/resources/ruby-spec/parameters_spec.rb +0 -261
  266. data/resources/ruby-spec/ruby_spec_spec_helper.rb +0 -35
@@ -12,9 +12,6 @@ module Contrast
12
12
  # in a 'get it work' state. hopefully, we'll be in
13
13
  # a 'get it right' state soon.
14
14
  class Select
15
- include Contrast::Components::Interface
16
- access_component :logging
17
-
18
15
  class << self
19
16
  def select_tagger patcher, preshift, ret, _block
20
17
  source = preshift.object
@@ -49,32 +46,48 @@ module Contrast
49
46
 
50
47
  private
51
48
 
49
+ def handle_integer args, arg, source
50
+ length = args[1] || 1
51
+ # (void) negative range
52
+ arg += source.length if arg.negative?
53
+ Contrast::Agent::Assess::AdjustedSpan.new(arg, arg + length)
54
+ end
55
+
56
+ def handle_string arg, source
57
+ idx = source.index(arg)
58
+ Contrast::Agent::Assess::AdjustedSpan.new(idx, idx + arg.length)
59
+ end
60
+
61
+ def handle_regexp args, arg, source
62
+ match_data = arg.match(source)
63
+ # nil has the same meaning as 0. use full match
64
+ group = args[1] || 0
65
+ Contrast::Agent::Assess::AdjustedSpan.new(match_data.begin(group), match_data.end(group))
66
+ end
67
+
68
+ def handle_range arg, source
69
+ start = arg.begin
70
+ finish = arg.end
71
+
72
+ # (void) negative range
73
+ start += source.length if start.negative?
74
+ finish += source.length if finish.negative?
75
+ finish += 1 unless arg.exclude_end?
76
+
77
+ Contrast::Agent::Assess::AdjustedSpan.new(start, finish)
78
+ end
79
+
52
80
  def determine_select_range source, args
53
81
  arg = args[0]
54
82
  case arg
55
83
  when Integer
56
- length = args[1] || 1
57
- # (void) negative range
58
- arg += source.length if arg.negative?
59
- Contrast::Agent::Assess::AdjustedSpan.new(arg, arg + length)
84
+ handle_integer(args, arg, source)
60
85
  when String
61
- idx = source.index(arg)
62
- Contrast::Agent::Assess::AdjustedSpan.new(idx, idx + arg.length)
86
+ handle_string(arg, source)
63
87
  when Regexp
64
- match_data = arg.match(source)
65
- # nil has the same meaning as 0. use full match
66
- group = args[1] || 0
67
- Contrast::Agent::Assess::AdjustedSpan.new(match_data.begin(group), match_data.end(group))
88
+ handle_regexp(args, arg, source)
68
89
  when Range
69
- start = arg.begin
70
- finish = arg.end
71
-
72
- # (void) negative range
73
- start += source.length if start.negative?
74
- finish += source.length if finish.negative?
75
- finish += 1 unless arg.exclude_end?
76
-
77
- Contrast::Agent::Assess::AdjustedSpan.new(start, finish)
90
+ handle_range(arg, source)
78
91
  end
79
92
  end
80
93
  end
@@ -19,8 +19,25 @@ module Contrast
19
19
 
20
20
  SPLIT_TRACKER = Contrast::Utils::ThreadTracker.new
21
21
  class << self
22
+ # Propagate taint from a source as it is split into composite
23
+ # sections. This method MUST return nil, otherwise it risks
24
+ # changing the result of of the propagation.
25
+ #
26
+ # @param propagation_node [Contrast::Agent::Assess::Policy::PropagationNode]
27
+ # the node that governs this propagation event.
28
+ # @param preshift [Contrast::Agent::Assess::PreShift] The capture
29
+ # of the state of the code just prior to the invocation of the
30
+ # patched method.
31
+ # @param target [Array, String] the target to which to propagate.
32
+ # @return [nil]
22
33
  def propagate propagation_node, preshift, target
23
- return Contrast::Agent::Assess::Policy::Propagator::Keep.propagate(propagation_node, preshift, target) unless target.is_a?(Array)
34
+ logger.trace('Propagation detected',
35
+ node_id: propagation_node.id,
36
+ target_id: target.__id__)
37
+ unless target.is_a?(Array)
38
+ Contrast::Agent::Assess::Policy::Propagator::Keep.propagate(propagation_node, preshift, target)
39
+ return
40
+ end
24
41
 
25
42
  source = find_source(propagation_node.sources[0], preshift)
26
43
 
@@ -49,6 +66,7 @@ module Contrast
49
66
  elem.cs__properties.add_properties(propagation_node.properties)
50
67
  current_index = current_index + elem_length + separator_length
51
68
  end
69
+ nil
52
70
  end
53
71
 
54
72
  # Marks the point in which the String#split method is called.
@@ -66,7 +84,7 @@ module Contrast
66
84
  rescue Exception => e # rubocop:disable Lint/RescueException
67
85
  # don't let our errors propagate and disable String#split for
68
86
  # this since we're in an error state
69
- logger.warn(e, 'Unable to record split context')
87
+ logger.warn('Unable to record split context', e)
70
88
  end_split
71
89
  end
72
90
 
@@ -86,7 +104,7 @@ module Contrast
86
104
  SPLIT_TRACKER.set(:split_depth, depth)
87
105
  end
88
106
  rescue StandardError => e
89
- logger.warn(e, 'Unable to remove split context')
107
+ logger.warn('Unable to remove split context', e)
90
108
  end
91
109
 
92
110
  # This method is called whenever an rb_yield is called. We need
@@ -96,6 +114,8 @@ module Contrast
96
114
  # @param target [String] the entity being passed to the yield
97
115
  # block
98
116
  def propagate_yield target
117
+ depth, index = nil
118
+
99
119
  depth = SPLIT_TRACKER.get(:split_depth)
100
120
  return unless depth
101
121
 
@@ -108,9 +128,9 @@ module Contrast
108
128
  true_source = source[index]
109
129
  target.cs__copy_from(true_source)
110
130
  rescue StandardError => e
111
- logger.warn(e, 'Unable to track within split context')
131
+ logger.warn('Unable to track within split context', e)
112
132
  ensure
113
- if defined?(depth) && defined?(index) && depth && index
133
+ if depth && index
114
134
  idx = SPLIT_TRACKER.get(:split_index)
115
135
  idx[depth] = index + 1 if defined?(idx) && idx.is_a?(Array)
116
136
  end
@@ -122,7 +142,7 @@ module Contrast
122
142
  cs__scoped_require 'cs__assess_yield_track/cs__assess_yield_track' if AGENT.patch_yield?
123
143
  true
124
144
  rescue StandardError => e
125
- logger.error(e, 'Error loading split rb_yield patch')
145
+ logger.error('Error loading split rb_yield patch', e)
126
146
  false
127
147
  end
128
148
  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
+
4
6
  module Contrast
5
7
  module Agent
6
8
  module Assess
@@ -23,36 +23,17 @@ module Contrast
23
23
 
24
24
  class << self
25
25
  def rewrite_interpolations
26
- return unless ASSESS.enabled?
27
- return unless AGENT.rewrite_interpolation?
26
+ return unless agent_should_rewrite?
28
27
 
29
- logger.debug_with_time("\t\tRunning Assess interpolation rewrite") do
28
+ logger.debug_with_time('Running Assess interpolation rewrite') do
30
29
  ObjectSpace.each_object(Module) do |mod|
31
30
  rewrite_interpolation(mod)
32
31
  end
33
32
  end
34
33
  end
35
34
 
36
- # Rails is being a jerk, again. It passes in a class before it is
37
- # done being defined. There is a state where the files have been
38
- # loaded, but the class definition is not complete, so the
39
- # methods of the class are not defined despite the class existing
40
- #
41
- # To get around this, we have those methods tell us the class
42
- # isn't ready
43
- def mid_defining? mod
44
- mod.instance_variable_defined?(:@cs__defining_class) &&
45
- mod.instance_variable_get(:@cs__defining_class)
46
- end
47
-
48
35
  def rewrite_interpolation mod, redo_rewrite = false
49
- return unless ASSESS.enabled?
50
- return unless AGENT.rewrite_interpolation?
51
- return unless AGENT.interpolation_enabled?
52
- return if AGENT.skip_instrumentation? mod.cs__name
53
- return if mod.cs__frozen?
54
- return if mod.singleton_class?
55
- return if mid_defining?(mod)
36
+ return unless should_rewrite?(mod)
56
37
 
57
38
  status = Contrast::Agent::Patching::Policy::PatchStatus.get_status(mod)
58
39
  return if (status&.rewritten? || status&.rewriting?) && !redo_rewrite
@@ -66,13 +47,43 @@ module Contrast
66
47
 
67
48
  end
68
49
  module_data = Contrast::Agent::ModuleData.new(mod, module_name)
69
- logger.debug_with_time("\t\t\tRewriting #{ module_name }") do
50
+ logger.trace_with_time('Rewriting module', module: module_name) do
70
51
  Contrast::Agent::Rewriter.rewrite_class(module_data, redo_rewrite)
71
52
  end
72
53
  rescue StandardError => e
73
- logger.error(
74
- e,
75
- "Unable to patch assess into the module #{ mod }")
54
+ logger.error('Unable to patch for assess', e, module: mod)
55
+ end
56
+
57
+ private
58
+
59
+ # Rails is being a jerk, again. It passes in a class before it is
60
+ # done being defined. There is a state where the files have been
61
+ # loaded, but the class definition is not complete, so the
62
+ # methods of the class are not defined despite the class existing
63
+ #
64
+ # To get around this, we have those methods tell us the class
65
+ # isn't ready
66
+ def mid_defining? mod
67
+ mod.instance_variable_defined?(:@cs__defining_class) &&
68
+ mod.instance_variable_get(:@cs__defining_class)
69
+ end
70
+
71
+ def agent_should_rewrite?
72
+ return false unless ASSESS.enabled?
73
+ return false unless AGENT.rewrite_interpolation?
74
+ return false unless AGENT.interpolation_enabled?
75
+
76
+ true
77
+ end
78
+
79
+ def should_rewrite? mod
80
+ return false unless agent_should_rewrite?
81
+ return false if AGENT.skip_instrumentation? mod.cs__name
82
+ return false if mod.cs__frozen?
83
+ return false if mod.singleton_class?
84
+ return false if mid_defining?(mod)
85
+
86
+ true
76
87
  end
77
88
  end
78
89
  end
@@ -82,8 +82,7 @@ module Contrast
82
82
  target = ret
83
83
  end
84
84
 
85
- invoked = 3 # apply_post_patch => apply_assess => source_patchers
86
- apply_source(current_context, source_node, target, object, ret, source_node.type, nil, invoked, *args)
85
+ apply_source(current_context, source_node, target, object, ret, source_node.type, nil, *args)
87
86
 
88
87
  ret.cs__freeze if restore_frozen_state
89
88
  ret
@@ -106,37 +105,31 @@ module Contrast
106
105
  # @param source_name [String, nil] the name of this source, i.e.
107
106
  # the key used to accessed if from a map or nil if a type like
108
107
  # BODY
109
- # @param invoked [Integer] the depth of this invocation from
110
- # application code; often a lie.
111
108
  # @param args [Array<Object>] the Arguments with which the method
112
109
  # was invoked
113
- def apply_source context, source_node, target, object, ret, source_type, source_name = nil, invoked = 0, *args
110
+ def apply_source context, source_node, target, object, ret, source_type, source_name = nil, *args
114
111
  return unless context && source_node && target
115
112
 
116
113
  source_name ||= determine_source_name(source_node, object, ret, *args)
117
114
  # We know we only work on certain things.
118
115
  # Skip if this isn't one of them
119
116
  if Contrast::Utils::DuckUtils.quacks_to?(target, :cs__properties)
120
- apply_tags(source_node, target, object, ret, source_type, source_name, invoked, *args)
117
+ apply_tags(source_node, target, object, ret, source_type, source_name, *args)
121
118
  # While we don't taint hashes themselves, we may taint the things
122
119
  # they hold. Let's pass their keys and values back to ourselves and
123
120
  # try again
124
121
  elsif Contrast::Utils::DuckUtils.iterable_hash?(target)
125
- source_key_type = invoked.zero? ? key_type(source_type) : source_type
126
- invoked += 1
127
122
  to_replace = []
128
123
  target.each_pair do |key, value|
129
124
  # We only do this for Strings b/c of the way Hash lookup works.
130
125
  # To replace another object would break hash lookup and,
131
126
  # therefore, the application
132
- if ASSESS.track_frozen_sources? &&
133
- key.is_a?(String) &&
134
- Contrast::Utils::DuckUtils.quacks_to?(target, :delete)
127
+ if can_track_hash_key?(key, target)
135
128
  key = Contrast::Utils::FreezeUtil.unfreeze_dup(key)
136
129
  to_replace << key
137
130
  end
138
- apply_source(context, source_node, key, object, ret, source_key_type, key, invoked, *args)
139
- apply_source(context, source_node, value, object, ret, source_type, key, invoked, *args)
131
+ apply_source(context, source_node, key, object, ret, key_type(source_type), key, *args)
132
+ apply_source(context, source_node, value, object, ret, source_type, key, *args)
140
133
  end
141
134
 
142
135
  # Hash is designed to keep one instance of the string key in it.
@@ -152,19 +145,23 @@ module Contrast
152
145
  # they hold. Let's pass their keys and values back to ourselves and
153
146
  # try again
154
147
  elsif Contrast::Utils::DuckUtils.iterable_enumerable?(target)
155
- invoked += 1
156
- target.each { |value| apply_source(context, source_node, value, object, ret, source_type, source_name, invoked, *args) }
148
+ target.each { |value| apply_source(context, source_node, value, object, ret, source_type, source_name, *args) }
157
149
  end
158
150
  rescue StandardError => e
159
- logger.warn(e, "Unable to apply source for source_node #{ source_node.id }")
151
+ logger.warn('Unable to apply source', e, node_id: source_node.id)
160
152
  end
161
153
 
162
- def apply_tags source_node, target, object, ret, source_type, source_name, invoked, *args
154
+ def can_track_hash_key? key, target
155
+ ASSESS.track_frozen_sources? &&
156
+ key.is_a?(String) &&
157
+ Contrast::Utils::DuckUtils.quacks_to?(target, :delete)
158
+ end
159
+
160
+ def apply_tags source_node, target, object, ret, source_type, source_name, *args
163
161
  # don't apply second source -- probably needs tuning later if we
164
162
  # use more than 'UNTRUSTED' in our sources
165
163
  return if target.cs__tracked? || target.cs__frozen?
166
164
 
167
- invoked += 1
168
165
  # otherwise for each tag this source_node applies, create a tag range
169
166
  # on the target object
170
167
  # I realize this looping is counter-intuitive from the above
@@ -175,11 +172,14 @@ module Contrast
175
172
  length = Contrast::Utils::StringUtils.ret_length(target)
176
173
  target.cs__properties.add_tag(tag, Contrast::Agent::Assess::AdjustedSpan.new(0, length))
177
174
  target.cs__properties.add_properties(source_node.properties)
178
- logger.debug(nil, "Source #{ source_node.id } detected: #{ target.__id__ } tagged with #{ tag }")
175
+ logger.trace('Source detected',
176
+ node_id: source_node.id,
177
+ target_id: target.__id__,
178
+ tag: tag)
179
179
  end
180
180
 
181
181
  # make a representation of this method that TeamServer can render
182
- target.cs__properties.build_event(source_node, target, object, ret, args, invoked, source_type, source_name)
182
+ target.cs__properties.build_event(source_node, target, object, ret, args, source_type, source_name)
183
183
  end
184
184
 
185
185
  # Find the name of the source
@@ -13,22 +13,7 @@ module Contrast
13
13
  class SourceNode < PolicyNode
14
14
  attr_accessor :type
15
15
 
16
- DB_SOURCE_TYPE = 'TAINTED_DATABASE'
17
- def self.build_dynamic_source _id, dynamic_source
18
- dynamic_source_hash = {
19
- JSON_CLASS_NAME => dynamic_source.class_name,
20
- JSON_METHOD_NAME => dynamic_source.method_name,
21
- JSON_INSTANCE_METHOD => dynamic_source.instance_method,
22
- JSON_TYPE => DB_SOURCE_TYPE,
23
- JSON_METHOD_VISIBILITY => 'public',
24
- JSON_TARGET => dynamic_source.target,
25
- JSON_PROPERTIES => dynamic_source.properties
26
- }
27
- Contrast::Agent::Assess::Policy::SourceNode.new(dynamic_source_hash)
28
- end
29
-
30
16
  JSON_TYPE = 'type'
31
- JSON_SOURCE_NAME = 'source_name'
32
17
  SOURCE_TAG = 'UNTRUSTED'
33
18
  def initialize source_hash = {}
34
19
  super(source_hash)
@@ -3,8 +3,8 @@
3
3
 
4
4
  cs__scoped_require 'contrast/utils/object_share'
5
5
  cs__scoped_require 'contrast/utils/sha256_builder'
6
+ cs__scoped_require 'contrast/agent/assess/events/event_factory'
6
7
  cs__scoped_require 'contrast/agent/assess/policy/trigger_validation/trigger_validation'
7
-
8
8
  cs__scoped_require 'contrast/components/interface'
9
9
 
10
10
  module Contrast
@@ -18,7 +18,7 @@ module Contrast
18
18
  # it was not, a Finding report is issued to the Service
19
19
  module TriggerMethod
20
20
  include Contrast::Components::Interface
21
- access_component :logging, :analysis
21
+ access_component :analysis, :logging
22
22
 
23
23
  # The level of TeamServer compliance our traces meet
24
24
  CURRENT_FINDING_VERSION = 2
@@ -47,7 +47,6 @@ module Contrast
47
47
  source,
48
48
  object,
49
49
  ret,
50
- 1,
51
50
  *args)
52
51
  end
53
52
  else
@@ -56,13 +55,12 @@ module Contrast
56
55
  nil,
57
56
  object,
58
57
  ret,
59
- 1,
60
58
  *args)
61
59
  end
62
60
  end
63
61
 
64
- def apply_eval_trigger context, trigger_node, source, object, ret, invoked, *args
65
- apply_trigger(context, trigger_node, source, object, ret, invoked, *args)
62
+ def apply_eval_trigger context, trigger_node, source, object, ret, *args
63
+ apply_trigger(context, trigger_node, source, object, ret, *args)
66
64
  end
67
65
 
68
66
  # This converts the source of the finding, and the events leading
@@ -75,14 +73,12 @@ module Contrast
75
73
  # @param source [Object] the source of the Trigger Event
76
74
  # @param object [Object] the Object on which the method was invoked
77
75
  # @param ret [Object] the Return of the invoked method
78
- # @param invoked [Integer] the depth of this invocation from
79
- # application code; often a lie.
80
76
  # @param args [Array<Object>] the Arguments with which the method
81
77
  # was invoked
82
78
  # @return [Contrast::Api::Dtm::Finding, nil] the
83
79
  # Contrast::Api::Dtm::Finding to send to TeamServer or nil if
84
80
  # conditions were not met
85
- def build_finding context, trigger_node, source, object, ret, invoked, *args
81
+ def build_finding context, trigger_node, source, object, ret, *args
86
82
  return unless Contrast::Agent::Assess::Policy::TriggerValidation.valid?(trigger_node, object, ret, args)
87
83
 
88
84
  request = context.request
@@ -97,15 +93,18 @@ module Contrast
97
93
  finding.version = CURRENT_FINDING_VERSION
98
94
 
99
95
  build_from_source(finding, source)
100
- trigger_event = Contrast::Agent::Assess::ContrastEvent.new(trigger_node, source, object, ret, args, invoked + 1).to_dtm_event
96
+ trigger_event = Contrast::Agent::Assess::Events::EventFactory.build(trigger_node, source, object, ret, args).to_dtm_event
101
97
  finding.events << trigger_event
102
98
  build_hash(finding, source)
103
99
  build_tags(context)
104
100
  finding.routes << context.route if context.route
105
101
  context.activity.findings << finding
106
- logger.debug(nil, "Trigger #{ trigger_node.id } detected: #{ source.__id__ } triggered #{ trigger_node.rule_id }")
102
+ logger.trace('Finding created',
103
+ node_id: trigger_node.id,
104
+ source_id: source.__id__,
105
+ rule: trigger_node.rule_id)
107
106
  rescue StandardError => e
108
- logger.error(e, "Unable to build a finding for #{ trigger_node.id }")
107
+ logger.error('Unable to build a finding', e, rule: trigger_node.rule_id, node_id: trigger_node.id)
109
108
  end
110
109
 
111
110
  private
@@ -124,27 +123,24 @@ module Contrast
124
123
  # @param source [Object] the source of the Trigger Event
125
124
  # @param object [Object] the Object on which the method was invoked
126
125
  # @param ret [Object] the Return of the invoked method
127
- # @param invoked [Integer] the depth of this invocation from
128
- # application code; often a lie.
129
126
  # @param args [Array<Object>] the Arguments with which the method
130
127
  # was invoked
131
- def apply_trigger context, trigger_node, source, object, ret, invoked, *args
128
+ def apply_trigger context, trigger_node, source, object, ret, *args
132
129
  return unless context && trigger_node
133
130
  return if trigger_node.rule_disabled?
134
131
  return if trigger_node.dataflow? && source.nil?
135
132
 
136
- invoked += 1
137
133
  if trigger_node.regexp_rule?
138
- apply_regexp_rule(context, trigger_node, source, object, ret, invoked, *args)
134
+ apply_regexp_rule(context, trigger_node, source, object, ret, *args)
139
135
  elsif trigger_node.custom_trigger?
140
- trigger_node.apply_custom_trigger(context, trigger_node, source, object, ret, invoked, *args)
136
+ trigger_node.apply_custom_trigger(context, trigger_node, source, object, ret, *args)
141
137
  elsif trigger_node.dataflow?
142
- apply_dataflow_rule(context, trigger_node, source, object, ret, invoked, *args)
138
+ apply_dataflow_rule(context, trigger_node, source, object, ret, *args)
143
139
  else # trigger rule - just calling the method is dangerous
144
- build_finding(context, trigger_node, source, object, ret, invoked, *args)
140
+ build_finding(context, trigger_node, source, object, ret, *args)
145
141
  end
146
142
  rescue StandardError => e
147
- logger.warn(e, 'Unable to apply trigger.')
143
+ logger.warn('Unable to apply trigger', e, node_id: trigger_ndoe.id)
148
144
  end
149
145
 
150
146
  # Given the marker from the trigger_node (the pointer indicating
@@ -196,17 +192,14 @@ module Contrast
196
192
  # @param source [Object] the source of the Trigger Event
197
193
  # @param object [Object] the Object on which the method was invoked
198
194
  # @param ret [Object] the Return of the invoked method
199
- # @param invoked [Integer] the depth of this invocation from
200
- # application code; often a lie.
201
195
  # @param args [Array<Object>] the Arguments with which the method
202
196
  # was invoked
203
- def apply_regexp_rule context, trigger_node, source, object, ret, invoked, *args
197
+ def apply_regexp_rule context, trigger_node, source, object, ret, *args
204
198
  return unless source.is_a?(String)
205
199
  return if trigger_node.good_value && source.match?(trigger_node.good_value)
206
200
  return if trigger_node.bad_value && source !~ trigger_node.bad_value
207
201
 
208
- invoked += 1
209
- build_finding(context, trigger_node, source, object, ret, invoked, *args)
202
+ build_finding(context, trigger_node, source, object, ret, *args)
210
203
  end
211
204
 
212
205
  # This is our method that actually checks the taint on the object
@@ -219,35 +212,31 @@ module Contrast
219
212
  # @param source [Object] the source of the Trigger Event
220
213
  # @param object [Object] the Object on which the method was invoked
221
214
  # @param ret [Object] the Return of the invoked method
222
- # @param invoked [Integer] the depth of this invocation from
223
- # application code; often a lie.
224
215
  # @param args [Array<Object>] the Arguments with which the method
225
216
  # was invoked
226
- def apply_dataflow_rule context, trigger_node, source, object, ret, invoked, *args
217
+ def apply_dataflow_rule context, trigger_node, source, object, ret, *args
227
218
  return unless source
228
219
 
229
- invoked += 1
230
220
  if Contrast::Utils::DuckUtils.quacks_to?(source, :cs__properties)
231
221
  return unless source.cs__tracked?
232
222
  return unless trigger_node.violated?(source)
233
223
 
234
- build_finding(context, trigger_node, source, object, ret, invoked, *args)
224
+ build_finding(context, trigger_node, source, object, ret, *args)
235
225
  elsif Contrast::Utils::DuckUtils.iterable_hash?(source)
236
- invoked += 2 # the each & the block
237
226
  source.each_pair do |key, value|
238
- apply_dataflow_rule(context, trigger_node, key, object, ret, invoked, *args)
239
- apply_dataflow_rule(context, trigger_node, value, object, ret, invoked, *args)
227
+ apply_dataflow_rule(context, trigger_node, key, object, ret, *args)
228
+ apply_dataflow_rule(context, trigger_node, value, object, ret, *args)
240
229
  end
241
230
  elsif Contrast::Utils::DuckUtils.iterable_enumerable?(source)
242
- invoked += 2 # the each & the block
243
231
  source.each do |value|
244
- apply_dataflow_rule(context, trigger_node, value, object, ret, invoked, *args)
232
+ apply_dataflow_rule(context, trigger_node, value, object, ret, *args)
245
233
  end
246
234
  else
247
- logger.warn(
248
- nil,
249
- "Target is a #{ source.cs__class.name } -- not sure how to inspect: #{ trigger_node.inspect }")
250
- logger.debug(nil, source.to_s[0..99])
235
+ logger.warn('Trigger source is of unknown type. Unable to inspect.',
236
+ node_id: trigger_node.id,
237
+ source_id: target.__id__,
238
+ source_type: source.cs__class.to_s)
239
+ logger.trace(source.to_s[0..99])
251
240
  end
252
241
  end
253
242