contrast-agent 3.11.0 → 3.12.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (217) hide show
  1. checksums.yaml +4 -4
  2. data/.flayignore +1 -0
  3. data/ext/cs__assess_active_record_named/cs__active_record_named.c +7 -2
  4. data/ext/cs__assess_active_record_named/cs__active_record_named.h +1 -0
  5. data/ext/cs__assess_array/cs__assess_array.c +2 -1
  6. data/ext/cs__assess_array/cs__assess_array.h +1 -0
  7. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +3 -7
  8. data/ext/cs__assess_basic_object/cs__assess_basic_object.h +2 -1
  9. data/ext/cs__assess_kernel/cs__assess_kernel.c +1 -1
  10. data/ext/cs__assess_module/cs__assess_module.c +5 -7
  11. data/ext/cs__assess_module/cs__assess_module.h +3 -0
  12. data/ext/cs__common/cs__common.c +1 -1
  13. data/ext/cs__protect_kernel/cs__protect_kernel.c +4 -2
  14. data/ext/cs__protect_kernel/cs__protect_kernel.h +1 -0
  15. data/funchook/autom4te.cache/output.0 +13 -1
  16. data/funchook/autom4te.cache/requests +49 -48
  17. data/funchook/autom4te.cache/traces.0 +3 -0
  18. data/funchook/config.log +217 -378
  19. data/funchook/config.status +24 -23
  20. data/funchook/configure +13 -1
  21. data/funchook/src/Makefile +7 -7
  22. data/funchook/src/config.h +2 -2
  23. data/funchook/src/decoder.o +0 -0
  24. data/funchook/src/distorm.o +0 -0
  25. data/funchook/src/funchook.o +0 -0
  26. data/funchook/src/funchook_io.o +0 -0
  27. data/funchook/src/funchook_syscall.o +0 -0
  28. data/funchook/src/funchook_unix.o +0 -0
  29. data/funchook/src/funchook_x86.o +0 -0
  30. data/funchook/src/instructions.o +0 -0
  31. data/funchook/src/insts.o +0 -0
  32. data/funchook/src/libfunchook.so +0 -0
  33. data/funchook/src/mnemonics.o +0 -0
  34. data/funchook/src/operands.o +0 -0
  35. data/funchook/src/os_func.o +0 -0
  36. data/funchook/src/os_func_unix.o +0 -0
  37. data/funchook/src/prefix.o +0 -0
  38. data/funchook/src/printf_base.o +0 -0
  39. data/funchook/src/textdefs.o +0 -0
  40. data/funchook/src/wstring.o +0 -0
  41. data/funchook/test/Makefile +2 -2
  42. data/funchook/test/funchook_test +0 -0
  43. data/funchook/test/libfunchook_test.so +0 -0
  44. data/funchook/test/test_main.o +0 -0
  45. data/funchook/test/x86_64_test.o +0 -0
  46. data/lib/contrast.rb +0 -1
  47. data/lib/contrast/agent.rb +19 -22
  48. data/lib/contrast/agent/assess.rb +0 -9
  49. data/lib/contrast/agent/assess/policy/patcher.rb +1 -0
  50. data/lib/contrast/agent/assess/policy/policy_node.rb +1 -1
  51. data/lib/contrast/agent/assess/policy/policy_scanner.rb +1 -1
  52. data/lib/contrast/agent/assess/policy/propagation_method.rb +3 -0
  53. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  54. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +1 -3
  55. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +90 -0
  56. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +57 -0
  57. data/lib/contrast/agent/assess/policy/trigger_method.rb +3 -7
  58. data/lib/contrast/agent/assess/policy/trigger_node.rb +4 -1
  59. data/lib/contrast/agent/assess/rule/base.rb +0 -15
  60. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +22 -5
  61. data/lib/contrast/agent/assess/rule/redos.rb +0 -1
  62. data/lib/contrast/agent/at_exit_hook.rb +2 -2
  63. data/lib/contrast/agent/class_reopener.rb +9 -4
  64. data/lib/contrast/agent/exclusion_matcher.rb +0 -1
  65. data/lib/contrast/agent/inventory/policy/datastores.rb +54 -0
  66. data/lib/contrast/agent/inventory/policy/policy.rb +1 -1
  67. data/lib/contrast/agent/middleware.rb +38 -34
  68. data/lib/contrast/agent/patching/policy/after_load_patch.rb +11 -2
  69. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +51 -56
  70. data/lib/contrast/agent/patching/policy/patch.rb +2 -1
  71. data/lib/contrast/agent/patching/policy/patcher.rb +10 -12
  72. data/lib/contrast/agent/patching/policy/policy_node.rb +1 -1
  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 +6 -5
  84. data/lib/contrast/agent/protect/rule/cmd_injection.rb +3 -3
  85. data/lib/contrast/agent/protect/rule/path_traversal.rb +2 -7
  86. data/lib/contrast/agent/protect/rule/sqli.rb +4 -4
  87. data/lib/contrast/agent/railtie.rb +1 -0
  88. data/lib/contrast/agent/request.rb +2 -6
  89. data/lib/contrast/agent/request_context.rb +5 -6
  90. data/lib/contrast/agent/request_handler.rb +2 -2
  91. data/lib/contrast/agent/response.rb +0 -69
  92. data/lib/contrast/agent/service_heartbeat.rb +2 -2
  93. data/lib/contrast/agent/socket_client.rb +8 -8
  94. data/lib/contrast/agent/static_analysis.rb +2 -3
  95. data/lib/contrast/agent/version.rb +1 -1
  96. data/lib/contrast/api/decorators/application_settings.rb +1 -1
  97. data/lib/contrast/api/speedracer.rb +1 -1
  98. data/lib/contrast/components/agent.rb +17 -12
  99. data/lib/contrast/components/app_context.rb +33 -1
  100. data/lib/contrast/components/assess.rb +25 -15
  101. data/lib/contrast/components/contrast_service.rb +23 -67
  102. data/lib/contrast/components/interface.rb +4 -12
  103. data/lib/contrast/components/inventory.rb +5 -1
  104. data/lib/contrast/components/logger.rb +2 -2
  105. data/lib/contrast/components/protect.rb +40 -4
  106. data/lib/contrast/components/scope.rb +2 -52
  107. data/lib/contrast/components/settings.rb +24 -18
  108. data/lib/contrast/config/protect_rules_configuration.rb +0 -1
  109. data/lib/contrast/{extensions/ruby_core → extension}/assess.rb +12 -14
  110. data/lib/contrast/extension/assess/array.rb +77 -0
  111. data/lib/contrast/{extensions/ruby_core → extension}/assess/assess_extension.rb +2 -2
  112. data/lib/contrast/{extensions/ruby_core → extension}/assess/erb.rb +0 -0
  113. data/lib/contrast/extension/assess/eval_trigger.rb +78 -0
  114. data/lib/contrast/{extensions/ruby_core → extension}/assess/exec_trigger.rb +1 -1
  115. data/lib/contrast/{extensions/ruby_core → extension}/assess/fiber.rb +6 -5
  116. data/lib/contrast/{extensions/ruby_core → extension}/assess/hash.rb +2 -2
  117. data/lib/contrast/extension/assess/kernel.rb +110 -0
  118. data/lib/contrast/{extensions/ruby_core → extension}/assess/regexp.rb +4 -4
  119. data/lib/contrast/{extensions/ruby_core → extension}/assess/string.rb +5 -5
  120. data/lib/contrast/{extensions/ruby_core → extension}/delegator.rb +0 -0
  121. data/lib/contrast/{extensions/ruby_core → extension}/inventory.rb +2 -2
  122. data/lib/contrast/extension/kernel.rb +54 -0
  123. data/lib/contrast/{extensions/ruby_core → extension}/module.rb +0 -0
  124. data/lib/contrast/{extensions/ruby_core → extension}/protect.rb +2 -2
  125. data/lib/contrast/extension/protect/kernel.rb +44 -0
  126. data/lib/contrast/{extensions/ruby_core → extension}/protect/psych.rb +1 -1
  127. data/lib/contrast/{extensions/ruby_core → extension}/thread.rb +0 -0
  128. data/lib/contrast/framework/base_support.rb +22 -0
  129. data/lib/contrast/framework/manager.rb +33 -8
  130. data/lib/contrast/framework/rack/patch/session_cookie.rb +126 -0
  131. data/lib/contrast/framework/rack/patch/support.rb +24 -0
  132. data/lib/contrast/framework/rack/support.rb +22 -0
  133. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +43 -0
  134. data/lib/contrast/framework/rails/patch/assess_configuration.rb +103 -0
  135. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +31 -0
  136. data/lib/contrast/framework/rails/patch/support.rb +67 -0
  137. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +34 -0
  138. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +39 -0
  139. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +73 -0
  140. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +33 -0
  141. data/lib/contrast/framework/rails/support.rb +115 -0
  142. data/lib/contrast/framework/sinatra/application_helper.rb +51 -0
  143. data/lib/contrast/framework/sinatra/patch/base.rb +83 -0
  144. data/lib/contrast/framework/sinatra/patch/support.rb +27 -0
  145. data/lib/contrast/framework/sinatra/support.rb +109 -0
  146. data/lib/contrast/logger/application.rb +80 -0
  147. data/lib/contrast/{agent/logger.rb → logger/log.rb} +23 -54
  148. data/lib/contrast/logger/time.rb +50 -0
  149. data/lib/contrast/tasks/config.rb +54 -0
  150. data/lib/contrast/tasks/service.rb +1 -5
  151. data/lib/contrast/utils/class_util.rb +1 -1
  152. data/lib/contrast/utils/gemfile_reader.rb +2 -2
  153. data/lib/contrast/utils/hash_digest.rb +2 -7
  154. data/lib/contrast/utils/invalid_configuration_util.rb +3 -3
  155. data/lib/contrast/utils/job_servers_running.rb +4 -2
  156. data/lib/contrast/utils/object_share.rb +0 -1
  157. data/lib/contrast/utils/service_response_util.rb +14 -12
  158. data/lib/contrast/utils/service_sender_util.rb +78 -21
  159. data/resources/assess/policy.json +9 -50
  160. data/resources/inventory/policy.json +2 -2
  161. data/resources/protect/policy.json +6 -6
  162. data/ruby-agent.gemspec +5 -1
  163. data/service_executables/VERSION +1 -1
  164. data/service_executables/linux/contrast-service +0 -0
  165. data/service_executables/mac/contrast-service +0 -0
  166. metadata +69 -83
  167. data/funchook/src/libfunchook.dylib +0 -0
  168. data/funchook/test/libfunchook_test.so.dSYM/Contents/Info.plist +0 -20
  169. data/funchook/test/libfunchook_test.so.dSYM/Contents/Resources/DWARF/libfunchook_test.so +0 -0
  170. data/lib/contrast/agent/assess/rule/csrf.rb +0 -66
  171. data/lib/contrast/agent/assess/rule/csrf/csrf_action.rb +0 -28
  172. data/lib/contrast/agent/assess/rule/csrf/csrf_applicator.rb +0 -53
  173. data/lib/contrast/agent/assess/rule/csrf/csrf_watcher.rb +0 -136
  174. data/lib/contrast/agent/assess/rule/response_scanning_rule.rb +0 -47
  175. data/lib/contrast/agent/assess/rule/response_watcher.rb +0 -36
  176. data/lib/contrast/agent/assess/rule/watcher.rb +0 -36
  177. data/lib/contrast/agent/feature_state.rb +0 -346
  178. data/lib/contrast/agent/protect/rule/csrf.rb +0 -119
  179. data/lib/contrast/agent/protect/rule/csrf/csrf_evaluator.rb +0 -100
  180. data/lib/contrast/agent/protect/rule/csrf/csrf_token_injector.rb +0 -85
  181. data/lib/contrast/agent/settings_state.rb +0 -88
  182. data/lib/contrast/api/decorators/exclusion.rb +0 -20
  183. data/lib/contrast/extensions/framework/rack/cookie.rb +0 -24
  184. data/lib/contrast/extensions/framework/rack/request.rb +0 -24
  185. data/lib/contrast/extensions/framework/rack/response.rb +0 -23
  186. data/lib/contrast/extensions/framework/rails/action_controller_inheritance.rb +0 -39
  187. data/lib/contrast/extensions/framework/rails/action_controller_railties_helper_inherited.rb +0 -20
  188. data/lib/contrast/extensions/framework/rails/active_record.rb +0 -26
  189. data/lib/contrast/extensions/framework/rails/active_record_named.rb +0 -58
  190. data/lib/contrast/extensions/framework/rails/active_record_time_zone_inherited.rb +0 -21
  191. data/lib/contrast/extensions/framework/rails/buffer.rb +0 -28
  192. data/lib/contrast/extensions/framework/rails/configuration.rb +0 -27
  193. data/lib/contrast/extensions/framework/sinatra/base.rb +0 -59
  194. data/lib/contrast/extensions/ruby_core/assess/array.rb +0 -59
  195. data/lib/contrast/extensions/ruby_core/assess/basic_object.rb +0 -15
  196. data/lib/contrast/extensions/ruby_core/assess/kernel.rb +0 -96
  197. data/lib/contrast/extensions/ruby_core/assess/module.rb +0 -14
  198. data/lib/contrast/extensions/ruby_core/assess/tilt_template_trigger.rb +0 -78
  199. data/lib/contrast/extensions/ruby_core/assess/xpath_library_trigger.rb +0 -40
  200. data/lib/contrast/extensions/ruby_core/eval_trigger.rb +0 -51
  201. data/lib/contrast/extensions/ruby_core/inventory/datastores.rb +0 -37
  202. data/lib/contrast/extensions/ruby_core/protect/applies_command_injection_rule.rb +0 -61
  203. data/lib/contrast/extensions/ruby_core/protect/applies_deserialization_rule.rb +0 -50
  204. data/lib/contrast/extensions/ruby_core/protect/applies_no_sqli_rule.rb +0 -66
  205. data/lib/contrast/extensions/ruby_core/protect/applies_path_traversal_rule.rb +0 -115
  206. data/lib/contrast/extensions/ruby_core/protect/applies_sqli_rule.rb +0 -53
  207. data/lib/contrast/extensions/ruby_core/protect/applies_xxe_rule.rb +0 -127
  208. data/lib/contrast/extensions/ruby_core/protect/kernel.rb +0 -30
  209. data/lib/contrast/extensions/ruby_core/protect/rule_applicator.rb +0 -50
  210. data/lib/contrast/framework/rails_support.rb +0 -104
  211. data/lib/contrast/framework/sinatra_application_helper.rb +0 -49
  212. data/lib/contrast/framework/sinatra_support.rb +0 -104
  213. data/lib/contrast/utils/data_store_util.rb +0 -23
  214. data/lib/contrast/utils/rack_assess_session_cookie.rb +0 -104
  215. data/lib/contrast/utils/rails_assess_configuration.rb +0 -95
  216. data/lib/contrast/utils/random_util.rb +0 -22
  217. data/resources/csrf/inject.js +0 -44
@@ -0,0 +1,90 @@
1
+ # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ module Contrast
5
+ module Agent
6
+ module Assess
7
+ module Policy
8
+ module Trigger
9
+ # This acts a trigger to handle the special cases of the Tilt
10
+ # library gem. Reflected XSS data may come into the trigger methods
11
+ # from these classes.
12
+ class ReflectedXss
13
+ class << self
14
+ NODE_HASH = {
15
+ 'class_name' => 'Tilt::Template',
16
+ 'instance_method' => true,
17
+ 'method_name' => 'render',
18
+ 'method_visibility' => 'public',
19
+ 'action' => 'CUSTOM',
20
+ 'source' => 'O,P0',
21
+ 'target' => 'R',
22
+ 'patch_class' => 'Contrast::Agent::Assess::Policy::Trigger::ReflectedXss',
23
+ 'patch_method' => 'xss_tilt_trigger'
24
+ }.cs__freeze
25
+ TEMPLATE_PROPAGATION_NODE = Contrast::Agent::Assess::Policy::PropagationNode.new(NODE_HASH)
26
+
27
+ def xss_tilt_trigger context, trigger_node, _source, object, ret, *args
28
+ scope = args[0]
29
+
30
+ erb_template_prerender = object.instance_variable_get(:@data)
31
+ interpolated_inputs = []
32
+ handle_binding_variables(scope, erb_template_prerender, ret, interpolated_inputs)
33
+
34
+ handle_local_variables(args, erb_template_prerender, ret, interpolated_inputs)
35
+
36
+ unless interpolated_inputs.empty?
37
+ interpolated_inputs.each do |input|
38
+ input.cs__properties.events.each do |event|
39
+ ret.cs__properties.events << event
40
+ end
41
+ end
42
+ ret.cs__properties.build_event(TEMPLATE_PROPAGATION_NODE, ret, erb_template_prerender, ret, interpolated_inputs)
43
+ end
44
+
45
+ if ret.cs__tracked?
46
+ Contrast::Agent::Assess::Policy::TriggerMethod.build_finding(context, trigger_node, ret, erb_template_prerender, ret, interpolated_inputs)
47
+ end
48
+
49
+ ret
50
+ end
51
+
52
+ private
53
+
54
+ def handle_binding_variables scope, erb_template_prerender, ret, interpolated_inputs
55
+ binding_variables = scope.instance_variables
56
+
57
+ binding_variables.each do |bound_variable_sym|
58
+ bound_variable_value = scope.instance_variable_get(bound_variable_sym)
59
+
60
+ next unless bound_variable_value.cs__respond_to?(:cs__tracked?) && bound_variable_value.cs__tracked?
61
+ next unless erb_template_prerender.include?(bound_variable_sym.to_s)
62
+
63
+ start_index = ret.index(bound_variable_value)
64
+ next if start_index.nil?
65
+
66
+ ret.cs__copy_from(bound_variable_value, start_index)
67
+ interpolated_inputs << bound_variable_sym
68
+ end
69
+ end
70
+
71
+ def handle_local_variables args, erb_template_prerender, ret, interpolated_inputs
72
+ locals = args[1]
73
+ locals.each do |local_name, local_value|
74
+ next unless local_value.cs__respond_to?(:cs__tracked?) && local_value.cs__tracked?
75
+ next unless erb_template_prerender.include?(local_name.to_s)
76
+
77
+ start_index = ret.index(local_value)
78
+ next if start_index.nil?
79
+
80
+ ret.cs__copy_from(local_value, start_index)
81
+ interpolated_inputs << local_name
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,57 @@
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 Agent
8
+ module Assess
9
+ module Policy
10
+ module Trigger
11
+ # This acts a trigger to handle the special cases of the XPath
12
+ # library gem and the Oga gem. Untrusted data may come into the
13
+ # trigger methods from these classes as an array or hash,
14
+ # respectively. Since untrusted user input comes into these triggers
15
+ # as a splat argument or an options hash, we need to iterate through
16
+ # these objects to see if we were tracking on any of them and report
17
+ # a finding if so.
18
+ class Xpath
19
+ include Contrast::Components::Interface
20
+
21
+ class << self
22
+ def xpath_expression_trigger context, trigger_node, _source, object, ret, *args
23
+ return ret unless args
24
+
25
+ process(context, trigger_node, object, ret, *args)
26
+ end
27
+
28
+ def xpath_oga_trigger context, trigger_node, _source, object, ret, *args
29
+ return ret unless args
30
+
31
+ # convert the options arg in Oga::XML::CharacterNode#initialize into an
32
+ # array of its values so we can check if any are unsafe
33
+ args = args.first.values if args.first.cs__is_a?(Hash)
34
+ process(context, trigger_node, object, ret, *args)
35
+ end
36
+
37
+ private
38
+
39
+ def process context, trigger_node, object, ret, *args
40
+ args.each do |arg|
41
+ next unless arg.cs__is_a?(String) || arg.cs__is_a?(Symbol)
42
+ next unless arg.cs__tracked?
43
+ next unless trigger_node.violated?(arg)
44
+
45
+ Contrast::Agent::Assess::Policy::TriggerMethod.build_finding(
46
+ context, trigger_node, arg, object, ret, args)
47
+ end
48
+
49
+ ret
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
@@ -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 :analysis, :logging
21
+ access_component :analysis, :logging, :settings
22
22
 
23
23
  # The level of TeamServer compliance our traces meet
24
24
  CURRENT_FINDING_VERSION = 2
@@ -89,7 +89,7 @@ module Contrast
89
89
 
90
90
  finding = Contrast::Api::Dtm::Finding.new
91
91
  finding.rule_id = Contrast::Utils::StringUtils.protobuf_safe_string(trigger_node.rule_id)
92
- finding.session_id = Contrast::Agent::FeatureState.instance.current_session_id
92
+ finding.session_id = SETTINGS.session_id
93
93
  finding.version = CURRENT_FINDING_VERSION
94
94
 
95
95
  build_from_source(finding, source)
@@ -109,10 +109,6 @@ module Contrast
109
109
 
110
110
  private
111
111
 
112
- def settings
113
- Contrast::Agent::FeatureState.instance
114
- end
115
-
116
112
  # This is our method that actually checks the taint on the object
117
113
  # our trigger_node targets.
118
114
  #
@@ -140,7 +136,7 @@ module Contrast
140
136
  build_finding(context, trigger_node, source, object, ret, *args)
141
137
  end
142
138
  rescue StandardError => e
143
- logger.warn('Unable to apply trigger', e, node_id: trigger_ndoe.id)
139
+ logger.warn('Unable to apply trigger', e, node_id: trigger_node.id)
144
140
  end
145
141
 
146
142
  # Given the marker from the trigger_node (the pointer indicating
@@ -1,6 +1,9 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
+ cs__scoped_require 'contrast/agent/assess/policy/trigger/reflected_xss'
5
+ cs__scoped_require 'contrast/agent/assess/policy/trigger/xpath'
6
+
4
7
  module Contrast
5
8
  module Agent
6
9
  module Assess
@@ -64,7 +67,7 @@ module Contrast
64
67
  end
65
68
 
66
69
  def rule_disabled?
67
- Contrast::Agent::FeatureState.instance.assess_disabled_rules.include?(rule_id)
70
+ ASSESS.rule_disabled?(rule_id)
68
71
  end
69
72
 
70
73
  # Indicate if this is a dataflow based trigger, meaning it has a proper
@@ -46,21 +46,6 @@ module Contrast
46
46
  ex.assess_rule?(name)
47
47
  end
48
48
  end
49
-
50
- def send_report finding
51
- finding.rule_id = name
52
- finding.hash_code = generate_hash(finding)
53
- finding.preflight = Contrast::Utils::PreflightUtil.create_preflight(finding)
54
- finding.version = Contrast::Agent::Assess::Policy::TriggerMethod::CURRENT_FINDING_VERSION
55
- finding.tags = ASSESS.tags.to_s
56
-
57
- current_context = Contrast::Agent::REQUEST_TRACKER.current
58
- current_context.activity.findings << finding
59
- end
60
-
61
- def generate_hash finding
62
- Contrast::Utils::HashDigest.generate_trigger_hash(finding)
63
- end
64
49
  end
65
50
  end
66
51
  end
@@ -2,7 +2,7 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  cs__scoped_require 'contrast/components/interface'
5
- cs__scoped_require 'contrast/extensions/ruby_core/module'
5
+ cs__scoped_require 'contrast/extension/module'
6
6
 
7
7
  module Contrast
8
8
  module Agent
@@ -19,7 +19,7 @@ module Contrast
19
19
  # 4) redacted_marker : the value to plug in for the obfuscated value
20
20
  module HardcodedValueRule
21
21
  include Contrast::Components::Interface
22
- access_component :analysis, :contrast_service, :logging
22
+ access_component :analysis, :app_context, :logging, :settings
23
23
 
24
24
  def disabled?
25
25
  !ASSESS.enabled? || ASSESS.rule_disabled?(rule_id)
@@ -88,12 +88,14 @@ module Contrast
88
88
  # The constant name
89
89
  SOURCE_KEY = 'source'
90
90
 
91
+ private
92
+
91
93
  def report_finding clazz, constant_string
92
94
  class_name = clazz.cs__name
93
95
 
94
96
  finding = Contrast::Api::Dtm::Finding.new
95
97
  finding.rule_id = Contrast::Utils::StringUtils.protobuf_safe_string(rule_id)
96
- finding.session_id = Contrast::Agent::FeatureState.instance.current_session_id
98
+ finding.session_id = SETTINGS.session_id
97
99
 
98
100
  finding.version = Contrast::Agent::Assess::Policy::TriggerMethod::CURRENT_FINDING_VERSION
99
101
 
@@ -106,13 +108,28 @@ module Contrast
106
108
  finding.preflight = Contrast::Utils::PreflightUtil.create_preflight(finding)
107
109
 
108
110
  activity = Contrast::Api::Dtm::Activity.new
109
- activity.finding_tags = Contrast::Utils::StringUtils.protobuf_safe_string(ASSESS.tags)
110
111
  activity.findings << finding
111
112
 
112
- CONTRAST_SERVICE.queue_message activity
113
+ # If assess is enabled, we can just send the activity
114
+ if APP_CONTEXT.ready?
115
+ build_tags(activity)
116
+ Contrast::Utils::ServiceSenderUtil.push_to_ready_queue activity
117
+ # Otherwise, if the Agent isn't ready, we have to queue the messages
118
+ # until we know the starting state.
119
+ else
120
+ Contrast::Utils::ServiceSenderUtil.add_to_assess_messages activity
121
+ end
113
122
  rescue StandardError => e
114
123
  logger.error('Unable to build a finding for Hardcoded Rule', e)
115
124
  end
125
+
126
+ # This seems silly to pull out, but we can ONLY call this in the case
127
+ # where we have a configuration. Doing otherwise results in a bad error
128
+ # case where we try to do other things, like logging, which behave
129
+ # strangely without a config
130
+ def build_tags activity
131
+ activity.finding_tags = Contrast::Utils::StringUtils.force_utf8(ASSESS.tags)
132
+ end
116
133
  end
117
134
  end
118
135
  end
@@ -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.
@@ -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,7 @@ 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
+ Contrast::Utils::ServiceSenderUtil.send_event_immediately(context.activity) if context
33
33
  end
34
34
  end
35
35
  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
@@ -0,0 +1,54 @@
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.technologies[data_store] = true
44
+ context.activity.query_count += 1
45
+ return unless context.activity.query_count == 1
46
+
47
+ Contrast::Utils::InventoryUtil.append_db_config(context.activity)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+ 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
@@ -26,49 +26,45 @@ module Contrast
26
26
  # as it goes through the middleware stack inside of #call.
27
27
  class Middleware
28
28
  include Contrast::Components::Interface
29
- access_component :agent, :logging, :scope
29
+ access_component :agent, :config, :logging, :scope, :settings
30
30
 
31
31
  attr_reader :app
32
32
 
33
33
  # 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
34
+ # since we're only going to be initialized a single time. Our initialization order is:
35
+ # - capture the application
36
+ # - setup the Agent
37
+ # - startup the Agent
40
38
  #
41
39
  # @param app [Rack::Application] the application to be instrumented
42
40
  # @param _legacy_param [nil] was a flag we no longer need, but Sinatra may call it
43
-
44
41
  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.
42
+ @app = app # THIS MUST BE FIRST AND ALWAYS SET!
43
+ setup_agent # THIS MUST BE SECOND AND ALWAYS CALLED!
49
44
  unless AGENT.enabled?
50
45
  logger.error(
51
- 'Contrast middleware initializer detected an early-stage setup failure (likely config parse). Disabling.')
46
+ 'The Agent was unable to initialize before the application middleware was initialized. Disabling permanently.')
52
47
  AGENT.disable! # ensure the agent is disabled (probably redundant)
53
48
  return
54
49
  end
55
50
  agent_startup_routine
56
51
  end
57
52
 
53
+ # Startup the Agent as part of the initialization process:
54
+ # - start the service sending thread, responsible for sending and
55
+ # processing messages
56
+ # - start the heartbeat thread, which triggers service startup
57
+ # - start instrumenting libraries and do a 'catchup' patch for everything
58
+ # we didn't see get loaded
59
+ # - enable TracePoint, which handles all class loads and required
60
+ # instrumentation going forward
58
61
  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
62
  logger.debug_with_time('middleware: starting service') do
67
63
  run_service_threads
68
64
  end
69
65
 
70
66
  logger.debug_with_time('middleware: instrument shared libraries and patch') do
71
- instrument_and_patch
67
+ Contrast::Agent::Patching::Policy::Patcher.patch
72
68
  end
73
69
 
74
70
  AGENT.enable_tracepoint
@@ -96,10 +92,26 @@ module Contrast
96
92
 
97
93
  private
98
94
 
95
+ def setup_agent
96
+ SETTINGS.reset_state
97
+
98
+ if CONFIG.invalid?
99
+ AGENT.disable!
100
+ logger.error('!!! CONFIG FILE IS INVALID - DISABLING CONTRAST AGENT !!!')
101
+ elsif CONFIG.disabled?
102
+ AGENT.disable!
103
+ logger.warn('Contrast disabled by configuration. Continuing without instrumentation.')
104
+ else
105
+ AGENT.enable!
106
+ end
107
+ end
108
+
99
109
  # This is where we process each request we intercept as a middleware. We make the request context
100
110
  # available globally so that it can be accessed from anywhere. A RequestHandler object is made
101
111
  # for each request, which handles prefilter and postfilter operations.
102
112
  def call_with_agent env
113
+ return unless AGENT.enabled?
114
+
103
115
  framework_request = Contrast::Agent.framework_manager.retrieve_request(env)
104
116
  context = Contrast::Agent::RequestContext.new(framework_request)
105
117
  response = nil
@@ -155,7 +167,7 @@ module Contrast
155
167
  if exception.is_a?(Contrast::SecurityException) ||
156
168
  exception.message&.include?(SECURITY_EXCEPTION_MARKER)
157
169
 
158
- exception_control = settings.exception_control
170
+ exception_control = AGENT.exception_control
159
171
  raise exception unless exception_control[:enable]
160
172
 
161
173
  [exception_control[:status], {}, [exception_control[:message]]]
@@ -165,23 +177,15 @@ module Contrast
165
177
  end
166
178
  end
167
179
 
168
- def settings
169
- Contrast::Agent::FeatureState.instance
170
- end
171
-
180
+ # TODO: RUBY-920
181
+ # Move this somewhere that controls our threads, ensuring they're
182
+ # recreated on Fork
183
+ #
172
184
  # Rspec stubs over these methods for simplicity's sake in testing
173
185
  def run_service_threads
174
186
  Contrast::Utils::ServiceSenderUtil.start
175
187
  Contrast::Agent::ServiceHeartbeat.new.start
176
188
  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
189
  end
186
190
  end
187
191
  end