contrast-agent 3.13.0 → 3.16.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (241) hide show
  1. checksums.yaml +4 -4
  2. data/exe/contrast_service +1 -7
  3. data/ext/cs__assess_active_record_named/cs__active_record_named.c +8 -7
  4. data/ext/cs__assess_array/cs__assess_array.c +6 -5
  5. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +5 -5
  6. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +2 -1
  7. data/ext/cs__assess_hash/cs__assess_hash.c +18 -17
  8. data/ext/cs__assess_hash/cs__assess_hash.h +2 -1
  9. data/ext/cs__assess_kernel/cs__assess_kernel.c +7 -8
  10. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +18 -16
  11. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.h +1 -0
  12. data/ext/cs__assess_module/cs__assess_module.c +6 -6
  13. data/ext/cs__assess_regexp/cs__assess_regexp.c +4 -4
  14. data/ext/cs__assess_string/cs__assess_string.c +31 -16
  15. data/ext/cs__assess_string/cs__assess_string.h +6 -1
  16. data/ext/cs__assess_string_interpolation26/cs__assess_string_interpolation26.c +4 -2
  17. data/ext/cs__assess_yield_track/cs__assess_yield_track.c +2 -2
  18. data/ext/cs__common/cs__common.c +48 -39
  19. data/ext/cs__common/cs__common.h +16 -21
  20. data/ext/cs__contrast_patch/cs__contrast_patch.c +27 -25
  21. data/ext/cs__contrast_patch/cs__contrast_patch.h +5 -7
  22. data/ext/cs__protect_kernel/cs__protect_kernel.c +11 -12
  23. data/ext/cs__protect_kernel/cs__protect_kernel.h +2 -2
  24. data/lib/contrast-agent.rb +1 -1
  25. data/lib/contrast.rb +13 -23
  26. data/lib/contrast/agent.rb +39 -46
  27. data/lib/contrast/agent/assess.rb +12 -12
  28. data/lib/contrast/agent/assess/contrast_event.rb +151 -85
  29. data/lib/contrast/agent/assess/events/event_factory.rb +2 -2
  30. data/lib/contrast/agent/assess/events/source_event.rb +3 -3
  31. data/lib/contrast/agent/assess/finalizers/freeze.rb +15 -0
  32. data/lib/contrast/agent/assess/finalizers/hash.rb +97 -0
  33. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +11 -4
  34. data/lib/contrast/agent/assess/policy/patcher.rb +6 -6
  35. data/lib/contrast/agent/assess/policy/policy.rb +9 -11
  36. data/lib/contrast/agent/assess/policy/policy_node.rb +17 -12
  37. data/lib/contrast/agent/assess/policy/policy_scanner.rb +9 -4
  38. data/lib/contrast/agent/assess/policy/preshift.rb +13 -7
  39. data/lib/contrast/agent/assess/policy/propagation_method.rb +64 -44
  40. data/lib/contrast/agent/assess/policy/propagation_node.rb +2 -2
  41. data/lib/contrast/agent/assess/policy/propagator.rb +18 -18
  42. data/lib/contrast/agent/assess/policy/propagator/append.rb +8 -5
  43. data/lib/contrast/agent/assess/policy/propagator/base.rb +1 -1
  44. data/lib/contrast/agent/assess/policy/propagator/center.rb +9 -5
  45. data/lib/contrast/agent/assess/policy/propagator/custom.rb +1 -1
  46. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +6 -4
  47. data/lib/contrast/agent/assess/policy/propagator/insert.rb +7 -4
  48. data/lib/contrast/agent/assess/policy/propagator/keep.rb +4 -1
  49. data/lib/contrast/agent/assess/policy/propagator/match_data.rb +7 -9
  50. data/lib/contrast/agent/assess/policy/propagator/next.rb +7 -5
  51. data/lib/contrast/agent/assess/policy/propagator/prepend.rb +13 -5
  52. data/lib/contrast/agent/assess/policy/propagator/remove.rb +8 -4
  53. data/lib/contrast/agent/assess/policy/propagator/replace.rb +5 -2
  54. data/lib/contrast/agent/assess/policy/propagator/reverse.rb +7 -5
  55. data/lib/contrast/agent/assess/policy/propagator/select.rb +13 -7
  56. data/lib/contrast/agent/assess/policy/propagator/splat.rb +10 -9
  57. data/lib/contrast/agent/assess/policy/propagator/split.rb +27 -22
  58. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +52 -35
  59. data/lib/contrast/agent/assess/policy/propagator/trim.rb +11 -5
  60. data/lib/contrast/agent/assess/policy/rewriter_patch.rb +5 -5
  61. data/lib/contrast/agent/assess/policy/source_method.rb +90 -72
  62. data/lib/contrast/agent/assess/policy/source_validation/cross_site_validator.rb +1 -1
  63. data/lib/contrast/agent/assess/policy/source_validation/source_validation.rb +1 -1
  64. data/lib/contrast/agent/assess/policy/trigger/reflected_xss.rb +16 -12
  65. data/lib/contrast/agent/assess/policy/trigger/xpath.rb +2 -2
  66. data/lib/contrast/agent/assess/policy/trigger_method.rb +81 -33
  67. data/lib/contrast/agent/assess/policy/trigger_node.rb +41 -46
  68. data/lib/contrast/agent/assess/policy/trigger_validation/ssrf_validator.rb +2 -1
  69. data/lib/contrast/agent/assess/policy/trigger_validation/trigger_validation.rb +2 -2
  70. data/lib/contrast/agent/assess/properties.rb +15 -5
  71. data/lib/contrast/agent/assess/property/evented.rb +7 -20
  72. data/lib/contrast/agent/assess/property/tagged.rb +13 -7
  73. data/lib/contrast/agent/assess/property/updated.rb +131 -0
  74. data/lib/contrast/agent/assess/rule.rb +2 -2
  75. data/lib/contrast/agent/assess/rule/base.rb +3 -4
  76. data/lib/contrast/agent/assess/rule/provider.rb +3 -3
  77. data/lib/contrast/agent/assess/rule/provider/hardcoded_key.rb +58 -5
  78. data/lib/contrast/agent/assess/rule/provider/hardcoded_password.rb +24 -9
  79. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +84 -16
  80. data/lib/contrast/agent/assess/tag.rb +1 -1
  81. data/lib/contrast/agent/assess/tracker.rb +66 -0
  82. data/lib/contrast/agent/at_exit_hook.rb +6 -6
  83. data/lib/contrast/agent/class_reopener.rb +14 -11
  84. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +1 -1
  85. data/lib/contrast/agent/deadzone/policy/policy.rb +2 -2
  86. data/lib/contrast/agent/disable_reaction.rb +1 -1
  87. data/lib/contrast/agent/exclusion_matcher.rb +1 -1
  88. data/lib/contrast/agent/inventory/policy/datastores.rb +2 -2
  89. data/lib/contrast/agent/inventory/policy/policy.rb +3 -3
  90. data/lib/contrast/agent/inventory/policy/trigger_node.rb +1 -1
  91. data/lib/contrast/agent/middleware.rb +32 -32
  92. data/lib/contrast/agent/patching/policy/after_load_patch.rb +9 -9
  93. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +23 -22
  94. data/lib/contrast/agent/patching/policy/module_policy.rb +11 -11
  95. data/lib/contrast/agent/patching/policy/patch.rb +15 -15
  96. data/lib/contrast/agent/patching/policy/patcher.rb +43 -44
  97. data/lib/contrast/agent/patching/policy/policy.rb +23 -12
  98. data/lib/contrast/agent/patching/policy/policy_node.rb +1 -1
  99. data/lib/contrast/agent/patching/policy/trigger_node.rb +2 -2
  100. data/lib/contrast/agent/protect/policy/applies_command_injection_rule.rb +6 -8
  101. data/lib/contrast/agent/protect/policy/applies_deserialization_rule.rb +2 -2
  102. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -2
  103. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +3 -3
  104. data/lib/contrast/agent/protect/policy/applies_sqli_rule.rb +2 -2
  105. data/lib/contrast/agent/protect/policy/applies_xxe_rule.rb +4 -4
  106. data/lib/contrast/agent/protect/policy/policy.rb +8 -8
  107. data/lib/contrast/agent/protect/policy/rule_applicator.rb +1 -1
  108. data/lib/contrast/agent/protect/policy/trigger_node.rb +1 -1
  109. data/lib/contrast/agent/protect/rule.rb +18 -18
  110. data/lib/contrast/agent/protect/rule/base.rb +4 -3
  111. data/lib/contrast/agent/protect/rule/base_service.rb +1 -1
  112. data/lib/contrast/agent/protect/rule/cmd_injection.rb +5 -5
  113. data/lib/contrast/agent/protect/rule/deserialization.rb +1 -1
  114. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +1 -1
  115. data/lib/contrast/agent/protect/rule/no_sqli.rb +1 -1
  116. data/lib/contrast/agent/protect/rule/no_sqli/mongo_no_sql_scanner.rb +1 -0
  117. data/lib/contrast/agent/protect/rule/path_traversal.rb +4 -5
  118. data/lib/contrast/agent/protect/rule/sqli.rb +2 -2
  119. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +1 -1
  120. data/lib/contrast/agent/protect/rule/xss.rb +1 -1
  121. data/lib/contrast/agent/protect/rule/xxe.rb +3 -5
  122. data/lib/contrast/agent/protect/rule/xxe/entity_wrapper.rb +2 -2
  123. data/lib/contrast/agent/railtie.rb +1 -1
  124. data/lib/contrast/agent/reaction_processor.rb +2 -2
  125. data/lib/contrast/agent/request.rb +45 -43
  126. data/lib/contrast/agent/request_context.rb +10 -6
  127. data/lib/contrast/agent/response.rb +23 -12
  128. data/lib/contrast/agent/rewriter.rb +6 -9
  129. data/lib/contrast/agent/service_heartbeat.rb +2 -2
  130. data/lib/contrast/agent/static_analysis.rb +9 -9
  131. data/lib/contrast/agent/thread.rb +1 -1
  132. data/lib/contrast/agent/thread_watcher.rb +2 -2
  133. data/lib/contrast/agent/tracepoint_hook.rb +1 -1
  134. data/lib/contrast/agent/version.rb +1 -1
  135. data/lib/contrast/api.rb +4 -4
  136. data/lib/contrast/api/communication.rb +9 -9
  137. data/lib/contrast/api/communication/messaging_queue.rb +3 -6
  138. data/lib/contrast/api/communication/response_processor.rb +1 -1
  139. data/lib/contrast/api/communication/socket_client.rb +41 -6
  140. data/lib/contrast/api/communication/speedracer.rb +1 -1
  141. data/lib/contrast/api/communication/tcp_socket.rb +1 -1
  142. data/lib/contrast/api/communication/unix_socket.rb +1 -1
  143. data/lib/contrast/api/decorators.rb +14 -14
  144. data/lib/contrast/api/decorators/address.rb +20 -19
  145. data/lib/contrast/api/decorators/application_settings.rb +3 -2
  146. data/lib/contrast/api/decorators/application_update.rb +7 -8
  147. data/lib/contrast/api/decorators/http_request.rb +13 -12
  148. data/lib/contrast/api/decorators/input_analysis.rb +3 -2
  149. data/lib/contrast/api/decorators/message.rb +4 -2
  150. data/lib/contrast/api/decorators/rasp_rule_sample.rb +2 -1
  151. data/lib/contrast/api/decorators/route_coverage.rb +3 -2
  152. data/lib/contrast/api/decorators/server_features.rb +3 -2
  153. data/lib/contrast/api/decorators/trace_event.rb +28 -25
  154. data/lib/contrast/api/decorators/trace_event_object.rb +6 -5
  155. data/lib/contrast/api/decorators/trace_event_signature.rb +5 -4
  156. data/lib/contrast/api/decorators/trace_taint_range.rb +4 -3
  157. data/lib/contrast/api/decorators/user_input.rb +4 -4
  158. data/lib/contrast/common_agent_configuration.rb +2 -2
  159. data/lib/contrast/components/agent.rb +2 -2
  160. data/lib/contrast/components/app_context.rb +50 -39
  161. data/lib/contrast/components/config.rb +7 -17
  162. data/lib/contrast/components/contrast_service.rb +10 -10
  163. data/lib/contrast/components/interface.rb +15 -15
  164. data/lib/contrast/components/logger.rb +1 -1
  165. data/lib/contrast/components/scope.rb +3 -3
  166. data/lib/contrast/components/settings.rb +20 -23
  167. data/lib/contrast/config.rb +18 -18
  168. data/lib/contrast/config/application_configuration.rb +5 -2
  169. data/lib/contrast/config/base_configuration.rb +2 -2
  170. data/lib/contrast/config/protect_rule_configuration.rb +1 -1
  171. data/lib/contrast/config/service_configuration.rb +8 -0
  172. data/lib/contrast/configuration.rb +93 -52
  173. data/lib/contrast/extension/assess.rb +21 -22
  174. data/lib/contrast/extension/assess/array.rb +18 -11
  175. data/lib/contrast/extension/assess/erb.rb +11 -3
  176. data/lib/contrast/extension/assess/eval_trigger.rb +7 -7
  177. data/lib/contrast/extension/assess/exec_trigger.rb +2 -2
  178. data/lib/contrast/extension/assess/fiber.rb +14 -14
  179. data/lib/contrast/extension/assess/hash.rb +7 -6
  180. data/lib/contrast/extension/assess/kernel.rb +34 -28
  181. data/lib/contrast/extension/assess/marshal.rb +67 -0
  182. data/lib/contrast/extension/assess/regexp.rb +10 -9
  183. data/lib/contrast/extension/assess/string.rb +23 -23
  184. data/lib/contrast/extension/inventory.rb +4 -4
  185. data/lib/contrast/extension/kernel.rb +1 -1
  186. data/lib/contrast/extension/module.rb +1 -1
  187. data/lib/contrast/extension/protect.rb +3 -3
  188. data/lib/contrast/extension/protect/kernel.rb +4 -4
  189. data/lib/contrast/extension/protect/psych.rb +2 -2
  190. data/lib/contrast/framework/base_support.rb +1 -1
  191. data/lib/contrast/framework/manager.rb +10 -11
  192. data/lib/contrast/framework/rack/patch/session_cookie.rb +22 -28
  193. data/lib/contrast/framework/rack/patch/support.rb +1 -1
  194. data/lib/contrast/framework/rack/support.rb +2 -2
  195. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +13 -13
  196. data/lib/contrast/framework/rails/patch/assess_configuration.rb +6 -12
  197. data/lib/contrast/framework/rails/patch/rails_application_configuration.rb +11 -11
  198. data/lib/contrast/framework/rails/patch/support.rb +4 -4
  199. data/lib/contrast/framework/rails/rewrite/action_controller_railties_helper_inherited.rb +11 -11
  200. data/lib/contrast/framework/rails/rewrite/active_record_attribute_methods_read.rb +12 -12
  201. data/lib/contrast/framework/rails/rewrite/active_record_named.rb +4 -4
  202. data/lib/contrast/framework/rails/rewrite/active_record_time_zone_inherited.rb +12 -12
  203. data/lib/contrast/framework/rails/support.rb +64 -14
  204. data/lib/contrast/framework/sinatra/patch/base.rb +12 -12
  205. data/lib/contrast/framework/sinatra/patch/support.rb +1 -1
  206. data/lib/contrast/framework/sinatra/support.rb +6 -6
  207. data/lib/contrast/funchook/funchook.rb +1 -1
  208. data/lib/contrast/logger/application.rb +13 -5
  209. data/lib/contrast/logger/format.rb +22 -9
  210. data/lib/contrast/logger/log.rb +17 -10
  211. data/lib/contrast/logger/request.rb +30 -0
  212. data/lib/contrast/tasks/config.rb +1 -1
  213. data/lib/contrast/tasks/service.rb +2 -2
  214. data/lib/contrast/utils/assess/sampling_util.rb +2 -2
  215. data/lib/contrast/utils/assess/tracking_util.rb +49 -4
  216. data/lib/contrast/utils/boolean_util.rb +1 -1
  217. data/lib/contrast/utils/class_util.rb +2 -2
  218. data/lib/contrast/utils/duck_utils.rb +0 -10
  219. data/lib/contrast/utils/env_configuration_item.rb +2 -1
  220. data/lib/contrast/utils/gemfile_reader.rb +5 -5
  221. data/lib/contrast/utils/hash_digest.rb +2 -1
  222. data/lib/contrast/utils/heap_dump_util.rb +2 -2
  223. data/lib/contrast/utils/invalid_configuration_util.rb +21 -22
  224. data/lib/contrast/utils/inventory_util.rb +4 -4
  225. data/lib/contrast/utils/io_util.rb +1 -1
  226. data/lib/contrast/utils/os.rb +1 -1
  227. data/lib/contrast/utils/ruby_ast_rewriter.rb +1 -1
  228. data/lib/contrast/utils/sha256_builder.rb +2 -2
  229. data/lib/contrast/utils/stack_trace_utils.rb +2 -2
  230. data/lib/contrast/utils/string_utils.rb +11 -6
  231. data/resources/assess/policy.json +31 -22
  232. data/resources/deadzone/policy.json +5 -0
  233. data/ruby-agent.gemspec +21 -19
  234. data/service_executables/VERSION +1 -1
  235. data/service_executables/linux/contrast-service +0 -0
  236. data/service_executables/mac/contrast-service +0 -0
  237. metadata +67 -29
  238. data/lib/contrast/agent/assess/insulator.rb +0 -49
  239. data/lib/contrast/agent/require_state.rb +0 -61
  240. data/lib/contrast/extension/assess/assess_extension.rb +0 -147
  241. data/lib/contrast/utils/freeze_util.rb +0 -32
@@ -1,50 +1,50 @@
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 'English'
4
+ require 'English'
5
5
 
6
6
  # This must precede other Contrast C extensions
7
- cs__scoped_require 'cs__common/cs__common'
7
+ require 'cs__common/cs__common'
8
8
  # This must precede any patching we do as we log patches and we shouldn't cause
9
9
  # requires to happen during that process.
10
- cs__scoped_require 'contrast/components/logger'
10
+ require 'contrast/components/logger'
11
11
 
12
12
  # defining instrumentation, this must precede core extensions
13
13
  # because they need to register their patches
14
- cs__scoped_require 'contrast/agent/patching/policy/patcher'
15
- cs__scoped_require 'contrast/agent/patching/policy/patch'
14
+ require 'contrast/agent/patching/policy/patcher'
15
+ require 'contrast/agent/patching/policy/patch'
16
16
 
17
17
  # core extensions
18
- cs__scoped_require 'contrast/extension/assess'
19
- cs__scoped_require 'contrast/extension/delegator'
20
- cs__scoped_require 'contrast/extension/inventory'
21
- cs__scoped_require 'contrast/extension/module'
22
- cs__scoped_require 'contrast/extension/protect'
23
- cs__scoped_require 'contrast/extension/protect/kernel'
18
+ require 'contrast/extension/assess'
19
+ require 'contrast/extension/delegator'
20
+ require 'contrast/extension/inventory'
21
+ require 'contrast/extension/module'
22
+ require 'contrast/extension/protect'
23
+ require 'contrast/extension/protect/kernel'
24
24
 
25
- cs__scoped_require 'contrast/utils/object_share'
26
- cs__scoped_require 'contrast/utils/boolean_util'
27
- cs__scoped_require 'contrast/utils/string_utils'
28
- cs__scoped_require 'contrast/utils/io_util'
29
- cs__scoped_require 'contrast/utils/os'
25
+ require 'contrast/utils/object_share'
26
+ require 'contrast/utils/boolean_util'
27
+ require 'contrast/utils/string_utils'
28
+ require 'contrast/utils/io_util'
29
+ require 'contrast/utils/os'
30
30
 
31
- cs__scoped_require 'contrast/common_agent_configuration'
31
+ require 'contrast/common_agent_configuration'
32
32
 
33
- cs__scoped_require 'contrast/utils/hash_digest'
34
- cs__scoped_require 'contrast/utils/invalid_configuration_util'
33
+ require 'contrast/utils/hash_digest'
34
+ require 'contrast/utils/invalid_configuration_util'
35
35
 
36
36
  # scoping
37
- cs__scoped_require 'contrast/agent/scope'
37
+ require 'contrast/agent/scope'
38
38
 
39
- cs__scoped_require 'contrast/utils/thread_tracker'
39
+ require 'contrast/utils/thread_tracker'
40
40
 
41
41
  # Framework support
42
- cs__scoped_require 'contrast/framework/manager'
42
+ require 'contrast/framework/manager'
43
43
 
44
44
  # Communication to SR
45
- cs__scoped_require 'contrast/api/communication'
45
+ require 'contrast/api/communication'
46
46
 
47
- cs__scoped_require 'contrast/agent/thread_watcher'
47
+ require 'contrast/agent/thread_watcher'
48
48
 
49
49
  module Contrast
50
50
  # Top namespace of the Agent section. Holds tracking contexts that will be
@@ -67,39 +67,32 @@ module Contrast
67
67
  end
68
68
  end
69
69
 
70
- cs__scoped_require 'contrast/api'
70
+ require 'contrast/api'
71
71
 
72
- cs__scoped_require 'contrast/utils/resource_loader'
73
- cs__scoped_require 'contrast/utils/duck_utils'
74
- cs__scoped_require 'contrast/agent/tracepoint_hook'
75
- cs__scoped_require 'contrast/agent/at_exit_hook'
72
+ require 'contrast/utils/resource_loader'
73
+ require 'contrast/utils/duck_utils'
74
+ require 'contrast/agent/tracepoint_hook'
75
+ require 'contrast/agent/at_exit_hook'
76
76
 
77
77
  # communication with contrast service
78
- cs__scoped_require 'contrast/agent/exclusion_matcher'
78
+ require 'contrast/agent/exclusion_matcher'
79
79
 
80
80
  # threads that handle contrast scope
81
- cs__scoped_require 'contrast/agent/thread'
81
+ require 'contrast/agent/thread'
82
82
 
83
83
  # keep track of attacks
84
- cs__scoped_require 'contrast/agent/request_context'
84
+ require 'contrast/agent/request_context'
85
85
 
86
- cs__scoped_require 'contrast/agent/assess'
86
+ require 'contrast/agent/assess'
87
87
 
88
88
  # protect rules
89
- cs__scoped_require 'contrast/agent/protect/rule'
89
+ require 'contrast/agent/protect/rule'
90
90
 
91
91
  # application libraries
92
- cs__scoped_require 'contrast/utils/gemfile_reader'
92
+ require 'contrast/utils/gemfile_reader'
93
93
 
94
94
  # rack event monitoring
95
- cs__scoped_require 'contrast/agent/middleware'
96
-
97
- # TODO: RUBY-919
98
- # Refactor to use Contrast::Framework::Manager
99
- # Contrast::Framework::Manager.before_load_patches!
100
- if defined?(::Rails)
101
- cs__scoped_require 'contrast/framework/rails/patch/support'
102
- cs__scoped_require 'contrast/framework/rails/patch/rails_application_configuration'
103
- Contrast::Framework::Rails::Patch::RailsApplicationConfiguration.instrument
104
- cs__scoped_require 'contrast/agent/railtie' if ::Rails::VERSION::MAJOR.to_i >= 3
105
- end
95
+ require 'contrast/agent/middleware'
96
+
97
+ # Install the patches we need before the application has a chance to initialize
98
+ Contrast::Agent.framework_manager.before_load_patches!
@@ -8,27 +8,27 @@ module Contrast
8
8
  # class under this namespace should be required here, providing a single
9
9
  # point of require for this functionality.
10
10
  module Assess
11
- cs__scoped_require 'contrast/agent/module_data'
12
- cs__scoped_require 'contrast/agent/rewriter'
13
- cs__scoped_require 'contrast/agent/assess/policy/preshift'
11
+ require 'contrast/agent/assess/tracker'
12
+ require 'contrast/agent/module_data'
13
+ require 'contrast/agent/rewriter'
14
+ require 'contrast/agent/assess/policy/preshift'
14
15
 
15
- cs__scoped_require 'contrast/utils/prevent_serialization'
16
+ require 'contrast/utils/prevent_serialization'
16
17
 
17
18
  # Rules - generic
18
- cs__scoped_require 'contrast/agent/assess/rule'
19
- cs__scoped_require 'contrast/agent/assess/rule/base'
19
+ require 'contrast/agent/assess/rule'
20
+ require 'contrast/agent/assess/rule/base'
20
21
 
21
22
  # Dynamic Sources
22
- cs__scoped_require 'contrast/agent/assess/policy/dynamic_source_factory'
23
+ require 'contrast/agent/assess/policy/dynamic_source_factory'
23
24
 
24
25
  # Rule: REDOS
25
- cs__scoped_require 'contrast/agent/assess/rule/redos'
26
+ require 'contrast/agent/assess/rule/redos'
26
27
 
27
28
  # reporting / tracking
28
- cs__scoped_require 'contrast/agent/assess/insulator'
29
- cs__scoped_require 'contrast/agent/assess/properties'
30
- cs__scoped_require 'contrast/agent/assess/tag'
31
- cs__scoped_require 'contrast/agent/assess/events/event_factory'
29
+ require 'contrast/agent/assess/properties'
30
+ require 'contrast/agent/assess/tag'
31
+ require 'contrast/agent/assess/events/event_factory'
32
32
  end
33
33
  end
34
34
  end
@@ -1,14 +1,14 @@
1
1
  # Copyright (c) 2020 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- cs__scoped_require 'contrast/utils/assess/tracking_util'
5
- cs__scoped_require 'contrast/utils/class_util'
6
- cs__scoped_require 'contrast/utils/duck_utils'
7
- cs__scoped_require 'contrast/utils/object_share'
8
- cs__scoped_require 'contrast/utils/prevent_serialization'
9
- cs__scoped_require 'contrast/utils/stack_trace_utils'
10
- cs__scoped_require 'contrast/utils/string_utils'
11
- cs__scoped_require 'contrast/utils/timer'
4
+ require 'contrast/utils/assess/tracking_util'
5
+ require 'contrast/utils/class_util'
6
+ require 'contrast/utils/duck_utils'
7
+ require 'contrast/utils/object_share'
8
+ require 'contrast/utils/prevent_serialization'
9
+ require 'contrast/utils/stack_trace_utils'
10
+ require 'contrast/utils/string_utils'
11
+ require 'contrast/utils/timer'
12
12
 
13
13
  module Contrast
14
14
  module Agent
@@ -16,10 +16,32 @@ module Contrast
16
16
  # This class holds the data about an event in the application
17
17
  # We'll use it to build an event that TeamServer can consume if
18
18
  # the object to which this event belongs ends in a trigger.
19
+ #
20
+ # @attr_reader event_id [Integer] the atomic id of this event
21
+ # @attr_reader policy_node [Contrast::Agent::Assess::Policy::PolicyNode]
22
+ # the node that governs this event.
23
+ # @attr_reader stack_trace [Array<String>] the execution stack at the
24
+ # time the method for this event was invoked
25
+ # @attr_reader time [Integer] the time, in epoch ms, when this event was
26
+ # created
27
+ # @attr_reader thread [Integer] the object id of the thread on which this
28
+ # event was generated
29
+ # @attr_reader object [String] the safe representation of the Object on
30
+ # which the method was invoked
31
+ # @attr_reader ret [String] the safe representation of the Return of the
32
+ # invoked method
33
+ # @attr_reader args [Array<Object>] the safe representation of the
34
+ # Arguments with which the method was invoked
19
35
  class ContrastEvent
20
36
  include Contrast::Utils::PreventSerialization
21
37
 
22
38
  class << self
39
+ # Given an array of arguments, copy them into a safe, meaning String,
40
+ # format that we can use to send to SR and TS for rendering.
41
+ #
42
+ # @param args [Array<Object>] the arguments to translate
43
+ # @return [Array<String>] the String forms of those Objects, as
44
+ # determined by Contrast::Utils::ClassUtil.to_contrast_string
23
45
  def safe_args_representation args
24
46
  return nil unless args
25
47
  return Contrast::Utils::ObjectShare::EMPTY_ARRAY if args.empty?
@@ -36,30 +58,31 @@ module Contrast
36
58
  rep
37
59
  end
38
60
 
39
- def safe_arg_hash_representation hash
40
- # since this is the named hash for arguments, only the value is
41
- # suspect here
42
- hash.transform_values { |v| Contrast::Utils::ClassUtil.to_contrast_string(v) }
43
- end
44
-
45
61
  # if given an object that can be duped, duplicate it. otherwise just
46
62
  # return the original object. swallow all exceptions from
47
63
  # non-duplicable things.
48
64
  #
49
65
  # we can't just check respond_to? though b/c dup exists on the
50
66
  # base Object class
67
+ #
68
+ # @param original [Object, nil] the thing to duplicate
69
+ # @return [Object, nil] a copy of that thing
51
70
  def safe_dup original
52
71
  return nil unless original
53
72
 
54
- begin
55
- original.dup
56
- rescue StandardError
57
- original
58
- end
73
+ Contrast::Agent::Assess::Tracker.duplicate(original)
74
+ end
75
+
76
+ private
77
+
78
+ def safe_arg_hash_representation hash
79
+ # since this is the named hash for arguments, only the value is
80
+ # suspect here
81
+ hash.transform_values { |v| Contrast::Utils::ClassUtil.to_contrast_string(v) }
59
82
  end
60
83
  end
61
84
 
62
- attr_reader :event_id, :parent_ids, :policy_node, :stack_trace, :time, :thread, :object, :ret, :args
85
+ attr_reader :event_id, :policy_node, :stack_trace, :time, :thread, :object, :ret, :args
63
86
 
64
87
  # We need this to track the parent id's of events to build up a flow
65
88
  # chart of the finding
@@ -74,6 +97,13 @@ module Contrast
74
97
  end
75
98
  end
76
99
 
100
+ # @param policy_node [Contrast::Agent::Assess::Policy::PolicyNode]
101
+ # the node that governs this event.
102
+ # @param tagged [Object] the Target to which this event pertains.
103
+ # @param object [Object] the Object on which the method was invoked
104
+ # @param ret [Object] the Return of the invoked method
105
+ # @param args [Array<Object>] the Arguments with which the method
106
+ # was invoked
77
107
  def initialize policy_node, tagged, object, ret, args
78
108
  @policy_node = policy_node
79
109
  # so long as this event is built in a factory, we know Contrast Code
@@ -84,28 +114,110 @@ module Contrast
84
114
 
85
115
  # These methods rely on the above being set. Don't move them!
86
116
  @event_id = Contrast::Agent::Assess::ContrastEvent.next_atomic_id
87
- @parent_ids = find_parent_ids(policy_node, object, ret, args)
88
- snapshot(tagged, object, ret, args)
117
+ find_parent_events!(policy_node, object, ret, args)
118
+ snapshot!(tagged, object, ret, args)
119
+ end
120
+
121
+ def parent_events
122
+ @_parent_events ||= []
89
123
  end
90
124
 
91
- # Parent IDs are the event ids of all the sources of this event which
92
- # were tracked prior to this event occurring
93
- def find_parent_ids policy_node, object, ret, args
94
- mapped = policy_node.sources.map do |source|
95
- value_of_source(source, object, ret, args)
125
+ # We have to do a little work to figure out what our TS appropriate
126
+ # target is. To break this down, the logic is as follows:
127
+ # 1) If my policy_node has a target, work on targets. Else, work on sources.
128
+ # Per TS law, each policy_node must have at least a source or a target.
129
+ # The only type of policy_node w/o targets is a Trigger, but that may
130
+ # change.
131
+ # 2) If I have a highlight, it means that I have a P target that is
132
+ # not in integer form (it was a named / keyword type for which I had
133
+ # to find the index). I need to address this so that TS can process
134
+ # it.
135
+ # 3) I'll set the event's source and target to TS values.
136
+ # 4) Return the highlight or the first source/target as the taint
137
+ # target.
138
+ def determine_taint_target event_dtm
139
+ if @policy_node&.targets&.any?
140
+ event_dtm.source = @policy_node.source_string if @policy_node.source_string
141
+ event_dtm.target = @highlight ? "P#{ @highlight }" : @policy_node.target_string
142
+ @highlight || @policy_node.targets[0]
143
+ elsif policy_node&.sources&.any?
144
+ event_dtm.source = @highlight ? "P#{ @highlight }" : @policy_node.source_string
145
+ event_dtm.target = @policy_node.target_string if @policy_node.target_string
146
+ @highlight || @policy_node.sources[0]
96
147
  end
97
- selected = mapped.select do |source|
98
- source &&
99
- Contrast::Utils::DuckUtils.quacks_to?(source, :cs__properties) &&
100
- source.cs__properties.events &&
101
- source.cs__properties.events.last
148
+ end
149
+
150
+ # Convert this event into a DTM that TeamServer can consume
151
+ def to_dtm_event
152
+ Contrast::Api::Dtm::TraceEvent.build(self)
153
+ end
154
+
155
+ private
156
+
157
+ # Parent events are the events of all the sources of this event which
158
+ # were tracked prior to this event occurring. Depending on which, if
159
+ # any of the sources were tracked, there may be more than one parent.
160
+ #
161
+ # All events except for [Contrast::Agent::Assess::Events::SourceEvent]
162
+ # will have at least one parent.
163
+ #
164
+ # We set those events to this event's instance variables.
165
+ #
166
+ # @param policy_node [Contrast::Agent::Assess::Policy::PolicyNode]
167
+ # the node that governs this event.
168
+ # @param object [Object] the Object on which the method was invoked
169
+ # @param ret [Object] the Return of the invoked method
170
+ # @param args [Array<Object>] the Arguments with which the method
171
+ # was invoked
172
+ def find_parent_events! policy_node, object, ret, args
173
+ policy_node.sources.each do |source_marker|
174
+ source = value_of_source(source_marker, object, ret, args)
175
+ next unless source
176
+
177
+ event = Contrast::Agent::Assess::Tracker.properties(source)&.event
178
+ parent_events << event if event
102
179
  end
103
- selected.map do |source|
104
- source.cs__properties.events.last.event_id
180
+ end
181
+
182
+ # @param source [String] the marker for the source type
183
+ # @param object [Object] the Object on which the method was invoked
184
+ # @param ret [Object] the Return of the invoked method
185
+ # @param args [Array<Object>] the Arguments with which the method
186
+ # was invoked
187
+ # @return [Object,nil] the literal value of the source indicated by the
188
+ # given marker
189
+ def value_of_source source, object, ret, args
190
+ case source
191
+ when Contrast::Utils::ObjectShare::OBJECT_KEY
192
+ object
193
+ when Contrast::Utils::ObjectShare::RETURN_KEY
194
+ ret
195
+ else
196
+ if source.is_a?(Integer)
197
+ args[source]
198
+ else
199
+ args.each do |search|
200
+ next unless search.is_a?(Hash)
201
+
202
+ s = search[source]
203
+ return s if s
204
+ end
205
+ end
105
206
  end
106
207
  end
107
208
 
108
- def snapshot tagged, object, ret, args
209
+ # Everything* is mutable in Ruby. As such, to ensure we can accurately
210
+ # report the application state at the time of this method's invocation,
211
+ # we have to snapshot the given values, making safe representations of
212
+ # them for our later use. We set those safe values to this event's
213
+ # instance variables.
214
+ #
215
+ # @param tagged [Object] the Target to which this event pertains.
216
+ # @param object [Object] the Object on which the method was invoked
217
+ # @param ret [Object] the Return of the invoked method
218
+ # @param args [Array<Object>] the Arguments with which the method
219
+ # was invoked
220
+ def snapshot! tagged, object, ret, args
109
221
  target = @policy_node.target
110
222
  case target
111
223
  # If the target is nil, this rule was violated simply by a method
@@ -139,6 +251,10 @@ module Contrast
139
251
  # I know we're creating an extra string here since we replace the safe
140
252
  # one w/ a dup, but good enough for now. Trying not to make this too
141
253
  # complicated. - HM 8/8/19
254
+ #
255
+ # @param target [String,Integer] the marker for the target index
256
+ # @param tagged [Object] the actual Object that we're acting on which
257
+ # has tags
142
258
  def save_target_arg target, tagged
143
259
  return if @args.cs__frozen?
144
260
 
@@ -156,56 +272,6 @@ module Contrast
156
272
  break
157
273
  end
158
274
  end
159
-
160
- # We have to do a little work to figure out what our TS appropriate
161
- # target is. To break this down, the logic is as follows:
162
- # 1) If my policy_node has a target, work on targets. Else, work on sources.
163
- # Per TS law, each policy_node must have at least a source or a target.
164
- # The only type of policy_node w/o targets is a Trigger, but that may
165
- # change.
166
- # 2) If I have a highlight, it means that I have a P target that is
167
- # not in integer form (it was a named / keyword type for which I had
168
- # to find the index). I need to address this so that TS can process
169
- # it.
170
- # 3) I'll set the event's source and target to TS values.
171
- # 4) Return the highlight or the first source/target as the taint
172
- # target.
173
- def determine_taint_target event_dtm
174
- if @policy_node&.targets&.any?
175
- event_dtm.source = @policy_node.source_string if @policy_node.source_string
176
- event_dtm.target = @highlight ? "P#{ @highlight }" : @policy_node.target_string
177
- @highlight || @policy_node.targets[0]
178
- elsif policy_node&.sources&.any?
179
- event_dtm.source = @highlight ? "P#{ @highlight }" : @policy_node.source_string
180
- event_dtm.target = @policy_node.target_string if @policy_node.target_string
181
- @highlight || @policy_node.sources[0]
182
- end
183
- end
184
-
185
- def value_of_source source, object, ret, args
186
- case source
187
- when Contrast::Utils::ObjectShare::OBJECT_KEY
188
- object
189
- when Contrast::Utils::ObjectShare::RETURN_KEY
190
- ret
191
- else
192
- if source.is_a?(Integer)
193
- args[source]
194
- else
195
- args.each do |search|
196
- next unless search.is_a?(Hash)
197
-
198
- s = search[source]
199
- return s if s
200
- end
201
- end
202
- end
203
- end
204
-
205
- # Convert this event into a DTM that TeamServer can consume
206
- def to_dtm_event
207
- Contrast::Api::Dtm::TraceEvent.build(self)
208
- end
209
275
  end
210
276
  end
211
277
  end