contrast-agent 5.2.0 → 6.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (228) hide show
  1. checksums.yaml +4 -4
  2. data/ext/cs__assess_array/cs__assess_array.c +7 -0
  3. data/ext/cs__assess_basic_object/cs__assess_basic_object.c +19 -5
  4. data/ext/cs__assess_fiber_track/cs__assess_fiber_track.c +1 -1
  5. data/ext/cs__assess_hash/cs__assess_hash.c +3 -4
  6. data/ext/cs__assess_kernel/cs__assess_kernel.c +1 -2
  7. data/ext/cs__assess_marshal_module/cs__assess_marshal_module.c +26 -12
  8. data/ext/cs__assess_module/cs__assess_module.c +1 -1
  9. data/ext/cs__assess_regexp/cs__assess_regexp.c +15 -2
  10. data/ext/cs__assess_regexp/cs__assess_regexp.h +2 -0
  11. data/ext/cs__assess_string/cs__assess_string.c +21 -1
  12. data/ext/cs__assess_test/cs__assess_test.h +9 -0
  13. data/ext/cs__assess_test/cs__assess_tests.c +22 -0
  14. data/ext/cs__assess_test/extconf.rb +5 -0
  15. data/ext/cs__common/cs__common.c +113 -11
  16. data/ext/cs__common/cs__common.h +29 -5
  17. data/ext/cs__contrast_patch/cs__contrast_patch.c +55 -44
  18. data/ext/cs__os_information/cs__os_information.c +13 -10
  19. data/ext/cs__scope/cs__scope.c +146 -97
  20. data/ext/cs__tests/cs__tests.c +12 -0
  21. data/ext/cs__tests/cs__tests.h +3 -0
  22. data/ext/cs__tests/extconf.rb +5 -0
  23. data/lib/contrast/agent/assess/contrast_object.rb +16 -16
  24. data/lib/contrast/agent/assess/events/source_event.rb +17 -19
  25. data/lib/contrast/agent/assess/finalizers/hash.rb +2 -0
  26. data/lib/contrast/agent/assess/policy/policy_node.rb +50 -27
  27. data/lib/contrast/agent/assess/policy/policy_node_utils.rb +51 -0
  28. data/lib/contrast/agent/assess/policy/policy_scanner.rb +2 -16
  29. data/lib/contrast/agent/assess/policy/preshift.rb +8 -2
  30. data/lib/contrast/agent/assess/policy/propagation_method.rb +47 -13
  31. data/lib/contrast/agent/assess/policy/propagator/buffer.rb +118 -0
  32. data/lib/contrast/agent/assess/policy/propagator/keep.rb +19 -4
  33. data/lib/contrast/agent/assess/policy/propagator/remove.rb +18 -2
  34. data/lib/contrast/agent/assess/policy/propagator/splat.rb +17 -3
  35. data/lib/contrast/agent/assess/policy/propagator/split.rb +15 -19
  36. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +1 -1
  37. data/lib/contrast/agent/assess/policy/propagator/substitution_utils.rb +1 -1
  38. data/lib/contrast/agent/assess/policy/propagator/trim.rb +1 -1
  39. data/lib/contrast/agent/assess/policy/propagator.rb +1 -0
  40. data/lib/contrast/agent/assess/policy/source_method.rb +7 -7
  41. data/lib/contrast/agent/assess/policy/trigger_method.rb +4 -10
  42. data/lib/contrast/agent/assess/property/tagged.rb +1 -1
  43. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +7 -2
  44. data/lib/contrast/agent/assess/rule/response/auto_complete_rule.rb +1 -1
  45. data/lib/contrast/agent/assess/rule/response/base_rule.rb +11 -3
  46. data/lib/contrast/agent/assess/rule/response/body_rule.rb +3 -3
  47. data/lib/contrast/agent/assess/rule/response/cache_control_header_rule.rb +60 -36
  48. data/lib/contrast/agent/assess/rule/response/framework/rails_support.rb +3 -3
  49. data/lib/contrast/agent/assess/rule/response/parameters_pollution_rule.rb +1 -1
  50. data/lib/contrast/agent/assess/rule/response/x_xss_protection_header_rule.rb +1 -2
  51. data/lib/contrast/agent/at_exit_hook.rb +1 -1
  52. data/lib/contrast/agent/deadzone/policy/deadzone_node.rb +0 -7
  53. data/lib/contrast/agent/deadzone/policy/policy.rb +0 -6
  54. data/lib/contrast/agent/exclusion_matcher.rb +3 -3
  55. data/lib/contrast/agent/inventory/database_config.rb +10 -3
  56. data/lib/contrast/agent/middleware.rb +5 -3
  57. data/lib/contrast/agent/patching/policy/after_load_patch.rb +0 -2
  58. data/lib/contrast/agent/patching/policy/patch.rb +13 -12
  59. data/lib/contrast/agent/patching/policy/patcher.rb +4 -4
  60. data/lib/contrast/agent/patching/policy/policy_node.rb +15 -2
  61. data/lib/contrast/agent/protect/exploitable_collection.rb +38 -0
  62. data/lib/contrast/agent/protect/input_analyzer/input_analyzer.rb +66 -9
  63. data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +2 -1
  64. data/lib/contrast/agent/protect/policy/applies_path_traversal_rule.rb +2 -2
  65. data/lib/contrast/agent/protect/rule/base.rb +37 -5
  66. data/lib/contrast/agent/protect/rule/base_service.rb +3 -1
  67. data/lib/contrast/agent/protect/rule/cmd_injection.rb +13 -0
  68. data/lib/contrast/agent/protect/rule/cmdi/cmdi_input_classification.rb +83 -0
  69. data/lib/contrast/agent/protect/rule/cmdi/cmdi_worth_watching.rb +64 -0
  70. data/lib/contrast/agent/protect/rule/http_method_tampering/http_method_tampering_input_classification.rb +96 -0
  71. data/lib/contrast/agent/protect/rule/http_method_tampering.rb +8 -0
  72. data/lib/contrast/agent/protect/rule/no_sqli/no_sqli_input_classification.rb +231 -0
  73. data/lib/contrast/agent/protect/rule/no_sqli.rb +27 -0
  74. data/lib/contrast/agent/protect/rule/sqli/sqli_input_classification.rb +18 -54
  75. data/lib/contrast/agent/protect/rule/sqli/sqli_worth_watching.rb +1 -4
  76. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_input_classification.rb +82 -0
  77. data/lib/contrast/agent/protect/rule/unsafe_file_upload/unsafe_file_upload_matcher.rb +45 -0
  78. data/lib/contrast/agent/protect/rule/unsafe_file_upload.rb +42 -0
  79. data/lib/contrast/agent/reporting/attack_result/attack_result.rb +63 -0
  80. data/lib/contrast/agent/reporting/attack_result/rasp_rule_sample.rb +52 -0
  81. data/lib/contrast/agent/reporting/attack_result/response_type.rb +29 -0
  82. data/lib/contrast/agent/reporting/attack_result/user_input.rb +87 -0
  83. data/lib/contrast/agent/reporting/masker/masker.rb +243 -0
  84. data/lib/contrast/agent/reporting/masker/masker_utils.rb +62 -0
  85. data/lib/contrast/agent/reporting/report.rb +2 -0
  86. data/lib/contrast/agent/reporting/reporter.rb +29 -22
  87. data/lib/contrast/agent/reporting/reporter_heartbeat.rb +49 -0
  88. data/lib/contrast/agent/reporting/reporting_events/agent_startup.rb +34 -0
  89. data/lib/contrast/agent/reporting/reporting_events/application_activity.rb +53 -0
  90. data/lib/contrast/agent/reporting/reporting_events/application_defend_activity.rb +48 -0
  91. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_activity.rb +64 -0
  92. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample.rb +70 -0
  93. data/lib/contrast/agent/reporting/reporting_events/application_defend_attack_sample_activity.rb +57 -0
  94. data/lib/contrast/agent/reporting/reporting_events/application_defend_attacker_activity.rb +56 -0
  95. data/lib/contrast/agent/reporting/reporting_events/application_inventory.rb +12 -4
  96. data/lib/contrast/agent/reporting/reporting_events/application_inventory_activity.rb +58 -0
  97. data/lib/contrast/agent/reporting/reporting_events/application_reporting_event.rb +27 -0
  98. data/lib/contrast/agent/reporting/reporting_events/application_startup.rb +50 -0
  99. data/lib/contrast/agent/reporting/reporting_events/application_startup_instrumentation.rb +27 -0
  100. data/lib/contrast/agent/reporting/reporting_events/application_update.rb +7 -12
  101. data/lib/contrast/agent/reporting/reporting_events/finding.rb +10 -4
  102. data/lib/contrast/agent/reporting/reporting_events/finding_event.rb +2 -4
  103. data/lib/contrast/agent/reporting/reporting_events/finding_event_object.rb +3 -3
  104. data/lib/contrast/agent/reporting/reporting_events/library_usage_observation.rb +5 -5
  105. data/lib/contrast/agent/reporting/reporting_events/observed_library_usage.rb +6 -2
  106. data/lib/contrast/agent/reporting/reporting_events/observed_route.rb +16 -12
  107. data/lib/contrast/agent/reporting/reporting_events/poll.rb +6 -2
  108. data/lib/contrast/agent/reporting/reporting_events/preflight.rb +10 -8
  109. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +8 -11
  110. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +2 -1
  111. data/lib/contrast/agent/reporting/reporting_events/route_coverage.rb +8 -6
  112. data/lib/contrast/agent/reporting/reporting_events/server_activity.rb +12 -20
  113. data/lib/contrast/agent/reporting/reporting_events/server_reporting_event.rb +27 -0
  114. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +17 -27
  115. data/lib/contrast/agent/reporting/reporting_utilities/build_preflight.rb +38 -0
  116. data/lib/contrast/agent/reporting/reporting_utilities/dtm_message.rb +8 -0
  117. data/lib/contrast/agent/reporting/reporting_utilities/endpoints.rb +6 -0
  118. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +23 -7
  119. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client_utils.rb +64 -76
  120. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +1 -1
  121. data/lib/contrast/agent/reporting/reporting_utilities/response.rb +17 -7
  122. data/lib/contrast/agent/reporting/reporting_utilities/response_extractor.rb +100 -0
  123. data/lib/contrast/agent/reporting/reporting_utilities/response_handler.rb +75 -13
  124. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_mode.rb +63 -0
  125. data/lib/contrast/agent/reporting/reporting_utilities/response_handler_utils.rb +154 -113
  126. data/lib/contrast/agent/reporting/settings/application_settings.rb +9 -0
  127. data/lib/contrast/agent/reporting/settings/assess_server_feature.rb +5 -33
  128. data/lib/contrast/agent/reporting/settings/protect.rb +1 -1
  129. data/lib/contrast/agent/reporting/settings/protect_server_feature.rb +2 -2
  130. data/lib/contrast/agent/reporting/settings/sampling.rb +36 -0
  131. data/lib/contrast/agent/reporting/settings/sensitive_data_masking.rb +110 -0
  132. data/lib/contrast/agent/reporting/settings/sensitive_data_masking_rule.rb +58 -0
  133. data/lib/contrast/agent/request.rb +3 -3
  134. data/lib/contrast/agent/request_context.rb +1 -1
  135. data/lib/contrast/agent/request_context_extend.rb +2 -2
  136. data/lib/contrast/agent/request_handler.rb +7 -3
  137. data/lib/contrast/agent/response.rb +2 -0
  138. data/lib/contrast/agent/service_heartbeat.rb +6 -48
  139. data/lib/contrast/agent/static_analysis.rb +1 -1
  140. data/lib/contrast/agent/telemetry/base.rb +151 -0
  141. data/lib/contrast/agent/telemetry/events/event.rb +35 -0
  142. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_base.rb +59 -0
  143. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_event.rb +44 -0
  144. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message.rb +115 -0
  145. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_message_exception.rb +83 -0
  146. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exception_stack_frame.rb +64 -0
  147. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions.rb +20 -0
  148. data/lib/contrast/agent/telemetry/events/exceptions/telemetry_exceptions_report.rb +32 -0
  149. data/lib/contrast/agent/telemetry/events/metric_event.rb +28 -0
  150. data/lib/contrast/agent/telemetry/events/startup_metrics_event.rb +123 -0
  151. data/lib/contrast/agent/thread_watcher.rb +52 -68
  152. data/lib/contrast/agent/version.rb +1 -1
  153. data/lib/contrast/agent/worker_thread.rb +8 -0
  154. data/lib/contrast/agent.rb +4 -3
  155. data/lib/contrast/api/communication/messaging_queue.rb +28 -11
  156. data/lib/contrast/api/communication/response_processor.rb +7 -10
  157. data/lib/contrast/api/communication/speedracer.rb +1 -1
  158. data/lib/contrast/api/decorators/activity.rb +33 -0
  159. data/lib/contrast/api/decorators/address.rb +1 -1
  160. data/lib/contrast/api/decorators/http_request.rb +1 -1
  161. data/lib/contrast/api/decorators/response_type.rb +30 -0
  162. data/lib/contrast/api/decorators.rb +1 -0
  163. data/lib/contrast/components/app_context.rb +0 -4
  164. data/lib/contrast/components/assess.rb +14 -0
  165. data/lib/contrast/components/config.rb +13 -22
  166. data/lib/contrast/components/contrast_service.rb +9 -0
  167. data/lib/contrast/components/protect.rb +2 -2
  168. data/lib/contrast/components/sampling.rb +7 -11
  169. data/lib/contrast/components/settings.rb +116 -8
  170. data/lib/contrast/config/agent_configuration.rb +34 -41
  171. data/lib/contrast/config/api_configuration.rb +16 -75
  172. data/lib/contrast/config/api_proxy_configuration.rb +9 -48
  173. data/lib/contrast/config/application_configuration.rb +24 -95
  174. data/lib/contrast/config/assess_configuration.rb +21 -76
  175. data/lib/contrast/config/assess_rules_configuration.rb +13 -38
  176. data/lib/contrast/config/base_configuration.rb +11 -76
  177. data/lib/contrast/config/certification_configuration.rb +15 -68
  178. data/lib/contrast/config/exception_configuration.rb +15 -59
  179. data/lib/contrast/config/heap_dump_configuration.rb +19 -73
  180. data/lib/contrast/config/inventory_configuration.rb +11 -55
  181. data/lib/contrast/config/logger_configuration.rb +8 -41
  182. data/lib/contrast/config/protect_configuration.rb +23 -10
  183. data/lib/contrast/config/protect_rule_configuration.rb +23 -37
  184. data/lib/contrast/config/protect_rules_configuration.rb +39 -43
  185. data/lib/contrast/config/request_audit_configuration.rb +16 -55
  186. data/lib/contrast/config/root_configuration.rb +70 -13
  187. data/lib/contrast/config/ruby_configuration.rb +14 -47
  188. data/lib/contrast/config/sampling_configuration.rb +12 -65
  189. data/lib/contrast/config/server_configuration.rb +13 -45
  190. data/lib/contrast/config/service_configuration.rb +36 -17
  191. data/lib/contrast/configuration.rb +21 -13
  192. data/lib/contrast/extension/assess/string.rb +20 -1
  193. data/lib/contrast/extension/module.rb +0 -1
  194. data/lib/contrast/framework/manager.rb +2 -2
  195. data/lib/contrast/framework/rails/patch/support.rb +13 -45
  196. data/lib/contrast/logger/aliased_logging.rb +87 -0
  197. data/lib/contrast/logger/application.rb +1 -5
  198. data/lib/contrast/logger/cef_log.rb +1 -1
  199. data/lib/contrast/tasks/config.rb +100 -4
  200. data/lib/contrast/utils/assess/object_store.rb +36 -0
  201. data/lib/contrast/utils/assess/propagation_method_utils.rb +6 -0
  202. data/lib/contrast/utils/class_util.rb +5 -18
  203. data/lib/contrast/utils/input_classification.rb +73 -0
  204. data/lib/contrast/utils/invalid_configuration_util.rb +1 -1
  205. data/lib/contrast/utils/log_utils.rb +3 -1
  206. data/lib/contrast/utils/middleware_utils.rb +10 -9
  207. data/lib/contrast/utils/net_http_base.rb +1 -1
  208. data/lib/contrast/utils/object_share.rb +2 -1
  209. data/lib/contrast/utils/os.rb +0 -5
  210. data/lib/contrast/utils/patching/policy/patch_utils.rb +4 -5
  211. data/lib/contrast/utils/response_utils.rb +18 -33
  212. data/lib/contrast/utils/telemetry.rb +20 -2
  213. data/lib/contrast/utils/telemetry_client.rb +23 -11
  214. data/lib/contrast/utils/telemetry_hash.rb +41 -0
  215. data/lib/contrast/utils/telemetry_identifier.rb +16 -1
  216. data/lib/contrast.rb +9 -0
  217. data/resources/assess/policy.json +98 -0
  218. data/resources/deadzone/policy.json +0 -86
  219. data/ruby-agent.gemspec +10 -9
  220. data/service_executables/VERSION +1 -1
  221. data/service_executables/linux/contrast-service +0 -0
  222. data/service_executables/mac/contrast-service +0 -0
  223. metadata +98 -30
  224. data/lib/contrast/agent/metric_telemetry_event.rb +0 -26
  225. data/lib/contrast/agent/startup_metrics_telemetry_event.rb +0 -121
  226. data/lib/contrast/agent/telemetry.rb +0 -137
  227. data/lib/contrast/agent/telemetry_event.rb +0 -33
  228. data/lib/contrast/utils/exclude_key.rb +0 -20
@@ -3,9 +3,10 @@
3
3
 
4
4
  module Contrast
5
5
  module Config
6
- # Common Configuration settings. Those in this section pertain to the
7
- # specific settings that apply to Ruby
8
- class RubyConfiguration < BaseConfiguration
6
+ # Common Configuration settings. Those in this section pertain to the specific settings that apply to Ruby
7
+ class RubyConfiguration
8
+ include Contrast::Config::BaseConfiguration
9
+
9
10
  DISABLED_RAKE_TASK_LIST = %w[
10
11
  about assets:clean assets:clobber assets:environment
11
12
  assets:precompile assets:precompile:all db:create db:drop db:fixtures:load db:migrate
@@ -23,16 +24,16 @@ module Contrast
23
24
  :track_frozen_sources, :non_request_tracking, :uninstrument_namespace
24
25
 
25
26
  def initialize hsh = {}
26
- @disabled_agent_rake_tasks = traverse_config(hsh, :disabled_agent_rake_tasks)
27
- @exceptions = Contrast::Config::ExceptionConfiguration.new(traverse_config(hsh, :exceptions))
28
- @interpolate = traverse_config(hsh, :interpolate)
29
- @propagate_yield = traverse_config(hsh, :propagate_yield)
30
- @require_scan = traverse_config(hsh, :require_scan)
31
- @track_frozen_sources = traverse_config(hsh, :track_frozen_sources)
32
- @non_request_tracking = traverse_config(hsh, :non_request_tracking)
33
- @uninstrument_namespace = traverse_config(hsh, :uninstrument_namespace)
34
- @configuration_map = {}
35
- build_configuration_map
27
+ return unless hsh
28
+
29
+ @disabled_agent_rake_tasks = hsh[:disabled_agent_rake_tasks]
30
+ @exceptions = Contrast::Config::ExceptionConfiguration.new(hsh[:exceptions])
31
+ @interpolate = hsh[:interpolate]
32
+ @propagate_yield = hsh[:propagate_yield]
33
+ @require_scan = hsh[:require_scan]
34
+ @track_frozen_sources = hsh[:track_frozen_sources]
35
+ @non_request_tracking = hsh[:non_request_tracking]
36
+ @uninstrument_namespace = hsh[:uninstrument_namespace]
36
37
  end
37
38
 
38
39
  # These commands being detected will result the agent disabling instrumentation, generally any command
@@ -82,40 +83,6 @@ module Contrast
82
83
  def uninstrument_namespace
83
84
  @uninstrument_namespace.nil? ? DEFAULT_UNINSTRUMENTED_NAMESPACES : @uninstrument_namespace
84
85
  end
85
-
86
- # TODO: RUBY-1493 MOVE TO BASE CONFIG
87
-
88
- def []= key, value
89
- instance_variable_set("@#{ key }".to_sym, value)
90
- @configuration_map[key] = value
91
- end
92
-
93
- def [] key
94
- send(key.to_sym)
95
- end
96
-
97
- # Traverse the given entity to build out the configuration graph.
98
- #
99
- # The values will be either a hash, indicating internal nodes to
100
- # traverse, or a value to set or the EMPTY_VALUE symbol, indicating a
101
- # leaf node.
102
- #
103
- # The spec_key are the Contrast defined keys based on the instance variables of
104
- # a given configuration.
105
- def traverse_config values, spec_key
106
- internal_nodes = values.cs__respond_to?(:has_key?)
107
- val = internal_nodes ? value_from_key_config(spec_key, values) : nil
108
- val == EMPTY_VALUE ? nil : val
109
- end
110
-
111
- def build_configuration_map
112
- instance_variables.each do |key|
113
- str_key = key.to_s.tr('@', '')
114
- next if str_key == 'configuration_map'
115
-
116
- @configuration_map[str_key] = send(str_key.to_sym)
117
- end
118
- end
119
86
  end
120
87
  end
121
88
  end
@@ -3,9 +3,10 @@
3
3
 
4
4
  module Contrast
5
5
  module Config
6
- # Common Configuration settings. Those in this section pertain to the
7
- # sampling functionality of the Agent.
8
- class SamplingConfiguration < BaseConfiguration
6
+ # Common Configuration settings. Those in this section pertain to the sampling functionality of the Agent.
7
+ class SamplingConfiguration
8
+ include Contrast::Config::BaseConfiguration
9
+
9
10
  # @return [Integer, nil]
10
11
  attr_reader :baseline
11
12
  # @return [Integer, nil]
@@ -16,72 +17,18 @@ module Contrast
16
17
  attr_reader :window_ms
17
18
 
18
19
  def initialize hsh = {}
19
- @enable = traverse_config(hsh, :enable)
20
- @baseline = traverse_config(hsh, :baseline)
21
- @request_frequency = traverse_config(hsh, :request_frequency)
22
- @response_frequency = traverse_config(hsh, :response_frequency)
23
- @window_ms = traverse_config(hsh, :window_ms)
24
- @configuration_map = {}
25
- build_configuration_map
20
+ return unless hsh
21
+
22
+ @enable = hsh[:enable]
23
+ @baseline = hsh[:baseline]
24
+ @request_frequency = hsh[:request_frequency]
25
+ @response_frequency = hsh[:response_frequency]
26
+ @window_ms = hsh[:window_ms]
26
27
  end
27
28
 
28
29
  # @return [Boolean, false]
29
30
  def enable
30
- @enable.nil? ? false : @enable
31
- end
32
-
33
- def enable= value
34
- self['enable'] = value
35
- end
36
-
37
- def baseline= value
38
- self['baseline'] = value
39
- end
40
-
41
- def request_frequency= value
42
- self['request_frequency'] = value
43
- end
44
-
45
- def response_frequency= value
46
- self['response_frequency'] = value
47
- end
48
-
49
- def window_ms= value
50
- self['window_ms'] = value
51
- end
52
-
53
- # TODO: RUBY-1493 MOVE TO BASE CONFIG
54
-
55
- def []= key, value
56
- instance_variable_set("@#{ key }".to_sym, value)
57
- @configuration_map[key] = value
58
- end
59
-
60
- def [] key
61
- send(key.to_sym)
62
- end
63
-
64
- # Traverse the given entity to build out the configuration graph.
65
- #
66
- # The values will be either a hash, indicating internal nodes to
67
- # traverse, or a value to set or the EMPTY_VALUE symbol, indicating a
68
- # leaf node.
69
- #
70
- # The spec_key are the Contrast defined keys based on the instance variables of
71
- # a given configuration.
72
- def traverse_config values, spec_key
73
- internal_nodes = values.cs__respond_to?(:has_key?)
74
- val = internal_nodes ? value_from_key_config(spec_key, values) : nil
75
- val == EMPTY_VALUE ? nil : val
76
- end
77
-
78
- def build_configuration_map
79
- instance_variables.each do |key|
80
- str_key = key.to_s.tr('@', '')
81
- next if str_key == 'configuration_map'
82
-
83
- @configuration_map[str_key] = send(str_key.to_sym)
84
- end
31
+ !!@enable
85
32
  end
86
33
  end
87
34
  end
@@ -3,9 +3,11 @@
3
3
 
4
4
  module Contrast
5
5
  module Config
6
- # Common Configuration settings. Those in this section pertain to the
7
- # server identification functionality of the Agent.
8
- class ServerConfiguration < BaseConfiguration
6
+ # Common Configuration settings. Those in this section pertain to the server identification functionality of the
7
+ # Agent.
8
+ class ServerConfiguration
9
+ include Contrast::Config::BaseConfiguration
10
+
9
11
  # @return [String, nil]
10
12
  attr_accessor :name
11
13
  # @return [String, nil]
@@ -20,48 +22,14 @@ module Contrast
20
22
  attr_accessor :version
21
23
 
22
24
  def initialize hsh = {}
23
- @path = traverse_config(hsh, :path)
24
- @name = traverse_config(hsh, :name)
25
- @type = traverse_config(hsh, :type)
26
- @tags = traverse_config(hsh, :tags)
27
- @environment = traverse_config(hsh, :environment)
28
- @version = traverse_config(hsh, :version)
29
- @configuration_map = {}
30
- build_configuration_map
31
- end
32
-
33
- # TODO: RUBY-1493 MOVE TO BASE CONFIG
34
-
35
- def []= key, value
36
- instance_variable_set("@#{ key }".to_sym, value)
37
- @configuration_map[key] = value
38
- end
39
-
40
- def [] key
41
- send(key.to_sym)
42
- end
43
-
44
- # Traverse the given entity to build out the configuration graph.
45
- #
46
- # The values will be either a hash, indicating internal nodes to
47
- # traverse, or a value to set or the EMPTY_VALUE symbol, indicating a
48
- # leaf node.
49
- #
50
- # The spec_key are the Contrast defined keys based on the instance variables of
51
- # a given configuration.
52
- def traverse_config values, spec_key
53
- internal_nodes = values.cs__respond_to?(:has_key?)
54
- val = internal_nodes ? value_from_key_config(spec_key, values) : nil
55
- val == EMPTY_VALUE ? nil : val
56
- end
57
-
58
- def build_configuration_map
59
- instance_variables.each do |key|
60
- str_key = key.to_s.tr('@', '')
61
- next if str_key == 'configuration_map'
62
-
63
- @configuration_map[str_key] = send(str_key.to_sym)
64
- end
25
+ return unless hsh
26
+
27
+ @path = hsh[:path]
28
+ @name = hsh[:name]
29
+ @type = hsh[:type]
30
+ @tags = hsh[:tags]
31
+ @environment = hsh[:environment]
32
+ @version = hsh[:version]
65
33
  end
66
34
  end
67
35
  end
@@ -5,25 +5,44 @@ require 'contrast/config/logger_configuration'
5
5
 
6
6
  module Contrast
7
7
  module Config
8
- # Common Configuration settings. Those in this section pertain to the
9
- # communication between the Agent & the Service.
10
- class ServiceConfiguration < BaseConfiguration
11
- # We don't set these b/c we've been asked to handle the default values of
12
- # these settings differently, logging when we have to use them.
13
- DEFAULT_HOST = '127.0.0.1'
8
+ # Common Configuration settings. Those in this section pertain to the communication between the Agent & the Service
9
+ class ServiceConfiguration
10
+ include Contrast::Config::BaseConfiguration
11
+
12
+ # We don't set these b/c we've been asked to handle the default values of these settings differently, logging
13
+ # when we have to use them.
14
+ DEFAULT_HOST = '127.0.0.1' # rubocop:disable Style/IpAddresses
14
15
  DEFAULT_PORT = '30555'
15
16
 
16
- KEYS = {
17
- enable: EMPTY_VALUE,
18
- host: EMPTY_VALUE,
19
- port: EMPTY_VALUE,
20
- socket: EMPTY_VALUE,
21
- logger: Contrast::Config::LoggerConfiguration,
22
- bypass: false
23
- }.cs__freeze
24
-
25
- def initialize hsh
26
- super(hsh, KEYS)
17
+ attr_writer :logger, :bypass
18
+ # @return [String, nil]
19
+ attr_accessor :socket
20
+ # @return [String, nil]
21
+ attr_accessor :port
22
+ # @return [String, nil]
23
+ attr_accessor :host
24
+ # @return [Boolean, nil]
25
+ attr_accessor :enable
26
+
27
+ def initialize hsh = {}
28
+ return unless hsh
29
+
30
+ @enable = hsh[:enable]
31
+ @host = hsh[:host]
32
+ @port = hsh[:port]
33
+ @socket = hsh[:socket]
34
+ @logger = Contrast::Config::LoggerConfiguration.new(hsh[:logger])
35
+ @bypass = hsh[:bypass]
36
+ end
37
+
38
+ # @return [Contrast::Config::LoggerConfiguration]
39
+ def logger
40
+ @logger ||= Contrast::Config::LoggerConfiguration.new
41
+ end
42
+
43
+ # @return [Boolean, false]
44
+ def bypass
45
+ @bypass.nil? ? false : @bypass
27
46
  end
28
47
  end
29
48
  end
@@ -7,7 +7,6 @@ require 'fileutils'
7
7
  require 'contrast/config'
8
8
  require 'contrast/utils/object_share'
9
9
  require 'contrast/components/scope'
10
- require 'contrast/utils/exclude_key'
11
10
 
12
11
  module Contrast
13
12
  # This is how we read in the local settings for the Agent, both ENV/ CMD line
@@ -27,15 +26,17 @@ module Contrast
27
26
  MILLISECOND_MARKER = '_ms'
28
27
  CONVERSION = { 'agent.service.enable' => 'agent.start_bundled_service' }.cs__freeze
29
28
  CONFIG_BASE_PATHS = ['', 'config/', '/etc/contrast/ruby/', '/etc/contrast/', '/etc/'].cs__freeze
29
+ KEYS_TO_REDACT = %i[api_key url service_key user_name].cs__freeze
30
+ REDACTED = '**REDACTED**'
30
31
 
31
32
  def initialize cli_options = nil, default_name = DEFAULT_YAML_PATH
32
33
  @default_name = default_name
33
34
 
34
35
  # Load config_kv from file
35
- config_kv = deep_stringify_all_keys(load_config)
36
+ config_kv = deep_symbolize_all_keys(load_config)
36
37
 
37
38
  # Overlay CLI options - they take precedence over config file
38
- cli_options = deep_stringify_all_keys(cli_options)
39
+ cli_options = deep_symbolize_all_keys(cli_options)
39
40
  config_kv = deep_merge(cli_options, config_kv) if cli_options
40
41
 
41
42
  # Some in-flight rewrites to maintain backwards compatibility
@@ -105,7 +106,7 @@ module Contrast
105
106
  def update_prop_keys config
106
107
  CONVERSION.each_pair do |old_method, new_method|
107
108
  # See if the old value was set and needs to be translated
108
- deprecated_keys = old_method.split('.')
109
+ deprecated_keys = old_method.split('.').map(&:to_sym)
109
110
  old_value = config
110
111
  deprecated_keys.each do |key|
111
112
  old_value = old_value[key]
@@ -114,7 +115,7 @@ module Contrast
114
115
  next if old_value.nil? # have to account for literal false
115
116
 
116
117
  log_deprecated_property(old_method, new_method)
117
- new_keys = new_method.split('.')
118
+ new_keys = new_method.split('.').map(&:to_sym)
118
119
  # We changed the seconds values into ms values. Multiply them accordingly
119
120
  old_value = old_value.to_i * 1000 if new_method.end_with?(MILLISECOND_MARKER)
120
121
  new_value = config
@@ -147,12 +148,12 @@ module Contrast
147
148
  end
148
149
  end
149
150
 
150
- def deep_stringify_all_keys hash
151
+ def deep_symbolize_all_keys hash
151
152
  return if hash.nil?
152
153
 
153
154
  new_hash = {}
154
155
  hash.each do |key, value|
155
- new_hash[key.to_s] = value.is_a?(Hash) ? deep_stringify_all_keys(value) : value
156
+ new_hash[key.to_sym] = value.is_a?(Hash) ? deep_symbolize_all_keys(value) : value
156
157
  end
157
158
  new_hash
158
159
  end
@@ -219,11 +220,9 @@ module Contrast
219
220
  when Contrast::Config::BaseConfiguration
220
221
  # to_hash returns @configuration_map
221
222
  convert.to_hash.each_key do |key|
222
- next if Contrast::Utils::ExcludeKey.excludable?(key.to_s)
223
-
224
- # keys can contain '-' but methods getters cannot
225
- # clean_key = key.to_s.tr('-', '_')
226
- hash[key] = convert_to_hash(convert.send(key.to_sym), {})
223
+ # change '-' to '_' for ProtectRulesConfiguration
224
+ hash[key] = convert_to_hash(convert.send(key.tr('-', '_').to_sym), {})
225
+ hash[key] = REDACTED if redactable?(key)
227
226
  end
228
227
  hash
229
228
  else
@@ -235,7 +234,7 @@ module Contrast
235
234
  idx = 0
236
235
  end_idx = new_keys.length - 1
237
236
  while idx < new_keys.length
238
- new_key = new_keys[idx]
237
+ new_key = new_keys[idx].to_sym
239
238
  if idx == end_idx
240
239
  new_value[new_key] = old_value if new_value[new_key].nil?
241
240
  else
@@ -246,5 +245,14 @@ module Contrast
246
245
  idx += 1
247
246
  end
248
247
  end
248
+
249
+ # Check if keys with sensitive data needs to be
250
+ # redacted.
251
+ #
252
+ # @param key [Symbol] key to check
253
+ # @return[Boolean] true | false
254
+ def redactable? key
255
+ KEYS_TO_REDACT.include?(key.to_sym)
256
+ end
249
257
  end
250
258
  end
@@ -13,7 +13,7 @@ module Contrast
13
13
  # methods which are too complex to fit into one of the standard
14
14
  # Contrast::Agent::Assess::Policy::Propagator molds without cluttering up the
15
15
  # String Class or exposing our methods there.
16
- class StringPropagator # rubocop:disable Style/StaticClass
16
+ class StringPropagator
17
17
  extend Contrast::Components::Logger::InstanceMethods
18
18
  extend Contrast::Components::Scope::InstanceMethods
19
19
 
@@ -43,6 +43,7 @@ module Contrast
43
43
  offset = 0
44
44
  inputs.each do |input|
45
45
  properties.copy_from(input, result, offset)
46
+ add_dynamic_sources_info input, result
46
47
  offset += input.length
47
48
  parent_event = Contrast::Agent::Assess::Tracker.properties(input)&.event
48
49
  parent_events << parent_event if parent_event
@@ -58,6 +59,24 @@ module Contrast
58
59
  rescue StandardError => e
59
60
  logger.error('Unable to track interpolation', e)
60
61
  end
62
+
63
+ private
64
+
65
+ # When there is a string interpolation on input coming from tainted database,
66
+ # the Contrast::Agent::Assess::Properties::Updated.copy_from method won't copy
67
+ # the dynamic source properties needed in the build findings from TS to display
68
+ # the column and Table information as database source information.
69
+ #
70
+ # @param source [Object] the source object with the required properties.
71
+ # @param target [Object] the result form the interpolation and the object
72
+ # that needs to keep the source properties, in order to be reporter on
73
+ # trigger event.
74
+ # @return updated_properties [Hash<DynamicSourceInfo>, nil]
75
+ def add_dynamic_sources_info source, target
76
+ return unless (dynamic_props = Contrast::Agent::Assess::Tracker.properties(source)&.properties)
77
+
78
+ Contrast::Agent::Assess::Tracker.properties(target)&.add_properties(dynamic_props)
79
+ end
61
80
  end
62
81
  end
63
82
  end
@@ -13,5 +13,4 @@ class Module
13
13
  alias_method :cs__const_defined?, :const_defined?
14
14
  alias_method :cs__const_get, :const_get
15
15
  alias_method :cs__const_set, :const_set
16
- alias_method :cs__autoload?, :autoload?
17
16
  end
@@ -125,7 +125,7 @@ module Contrast
125
125
  # @return [Contrast::Api::Dtm::RouteCoverage] the current route as a Dtm.
126
126
  def get_route_dtm request
127
127
  @_frameworks.lazy.map { |framework_support| framework_support.current_route(request) }.
128
- reject(&:nil?).first
128
+ reject(&:nil?).first # rubocop:disable Style/CollectionCompact
129
129
  end
130
130
 
131
131
  # Iterate through current frameworks and return the current request's route. This will be the first non-nil
@@ -135,7 +135,7 @@ module Contrast
135
135
  # @return [Contrast::Agent::Reporting::RouteCoverage] the current route as a Dtm.
136
136
  def get_route_information request
137
137
  @_frameworks.lazy.map { |framework_support| framework_support.current_route_coverage(request) }.
138
- reject(&:nil?).first
138
+ reject(&:nil?).first # rubocop:disable Style/CollectionCompact
139
139
  end
140
140
 
141
141
  # Sometimes the framework we want to instrument is loaded after our agent code. To catch that case, we'll detect
@@ -25,51 +25,19 @@ module Contrast
25
25
 
26
26
  # (See BaseSupport#after_load_patches)
27
27
  def after_load_patches
28
- patches = Set.new([
29
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
30
- 'ActionController::Live::Buffer',
31
- 'contrast/framework/rails/patch/action_controller_live_buffer',
32
- instrumenting_module:
33
- 'Contrast::Framework::Rails::Patch::ActionControllerLiveBuffer'),
34
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
35
- 'Rails::Application::Configuration',
36
- 'contrast/framework/rails/patch/rails_application_configuration',
37
- method_to_instrument: :session_store,
38
- instrumenting_module:
39
- 'Contrast::Framework::Rails::Patch::RailsApplicationConfiguration')
40
- ])
41
- patches.merge(special_after_load_patches) if RUBY_VERSION < '2.6.0'
42
- patches
43
- end
44
-
45
- def special_after_load_patches
46
- [
47
- # TODO: RUBY-714 remove w/ EOL of 2.5
48
- #
49
- # @deprecated Everything past here is used for Rewriting and can
50
- # be removed once we no longer support 2.5.
51
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
52
- 'ActionController::Railties::Helper::ClassMethods',
53
- 'contrast/framework/rails/rewrite/action_controller_railties_helper_inherited',
54
- method_to_instrument: :inherited,
55
- instrumenting_module:
56
- 'Contrast::Framework::Rails::Rewrite::ActionControllerRailtiesHelperInherited'),
57
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
58
- 'ActiveRecord::AttributeMethods::Read::ClassMethods',
59
- 'contrast/framework/rails/rewrite/active_record_attribute_methods_read',
60
- instrumenting_module:
61
- 'Contrast::Framework::Rails::Rewrite::ActiveRecordAttributeMethodsRead'),
62
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
63
- 'ActiveRecord::Scoping::Named::ClassMethods',
64
- 'contrast/framework/rails/rewrite/active_record_named',
65
- instrumenting_module: 'Contrast::Framework::Rails::Rewrite::ActiveRecordNamed'),
66
- Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
67
- 'ActiveRecord::AttributeMethods::TimeZoneConversion::ClassMethods',
68
- 'contrast/framework/rails/rewrite/active_record_time_zone_inherited',
69
- method_to_instrument: :inherited,
70
- instrumenting_module:
71
- 'Contrast::Framework::Rails::Rewrite::ActiveRecordTimeZoneInherited')
72
- ]
28
+ Set.new([
29
+ Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
30
+ 'ActionController::Live::Buffer',
31
+ 'contrast/framework/rails/patch/action_controller_live_buffer',
32
+ instrumenting_module:
33
+ 'Contrast::Framework::Rails::Patch::ActionControllerLiveBuffer'),
34
+ Contrast::Agent::Patching::Policy::AfterLoadPatch.new(
35
+ 'Rails::Application::Configuration',
36
+ 'contrast/framework/rails/patch/rails_application_configuration',
37
+ method_to_instrument: :session_store,
38
+ instrumenting_module:
39
+ 'Contrast::Framework::Rails::Patch::RailsApplicationConfiguration')
40
+ ])
73
41
  end
74
42
  end
75
43
  end
@@ -0,0 +1,87 @@
1
+ # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'contrast/agent/telemetry/events/exceptions/telemetry_exceptions'
5
+
6
+ module Contrast
7
+ module Logger
8
+ # Our decorator for the Ougai logger allowing for the catching, creating and saving Telemetry exceptions
9
+ module AliasedLogging
10
+ ALIASED_WARN = 'warn'.cs__freeze
11
+ ALIASED_ERROR = 'error'.cs__freeze
12
+ ALIASED_FATAL = 'fatal'.cs__freeze
13
+
14
+ # @param message [String] The message to log. Use default_message if not specified.
15
+ # @param exception [Exception] The exception or the error
16
+ # @param data [Object] Any structured data
17
+ def warn message = nil, exception = nil, data = nil, &block
18
+ # build Telemetry Exclusion
19
+ build_exclusion(ALIASED_WARN, message, exception, data)
20
+ super(message, exception, data, &block)
21
+ end
22
+
23
+ # @param message [String] The message to log. Use default_message if not specified.
24
+ # @param exception [Exception] The exception or the error
25
+ # @param data [Object] Any structured data
26
+ def error message = nil, exception = nil, data = nil, &block
27
+ # build Telemetry Exclusion
28
+ build_exclusion(ALIASED_ERROR, message, exception, data)
29
+ super(message, exception, data, &block)
30
+ end
31
+
32
+ # @param message [String] The message to log. Use default_message if not specified.
33
+ # @param exception [Exception] The exception or the error
34
+ # @param data [Object] Any structured data
35
+ def fatal message = nil, exception = nil, data = nil, &block
36
+ # build Telemetry Exclusion
37
+ build_exclusion(ALIASED_FATAL, message, exception, data)
38
+ super(message, exception, data, &block)
39
+ end
40
+
41
+ private
42
+
43
+ def build_exclusion type, message = nil, exception = nil, data = nil
44
+ start = caller_locations&.find_index { |stack| stack.to_s.include?(type) }
45
+ stack_trace = start ? caller_locations(start + 1, 20) : caller_locations(20, 20)
46
+ stack_frame_type = stack_trace[1].path.delete_prefix(Dir.pwd)
47
+ message_exception_type = exception ? exception.cs__class.to_s : stack_frame_type.split('/').last
48
+ stack_frame_function = stack_trace[1].label
49
+ key = "#{ stack_frame_type }|#{ stack_frame_function }|#{ message }"
50
+ if TELEMETRY_EXCEPTIONS[key]
51
+ TELEMETRY_EXCEPTIONS.increment key
52
+ return
53
+ end
54
+
55
+ event_message = create_message(stack_frame_function, stack_frame_type, message_exception_type, data, exception,
56
+ message)
57
+ TELEMETRY_EXCEPTIONS[key] = event_message
58
+ rescue StandardError => e
59
+ debug('Unable to report exception to telemetry', e)
60
+ end
61
+
62
+ def create_message stack_frame_function, stack_frame_type, message_exception_type, data, exception, message
63
+ message_for_exception = if exception
64
+ exception.cs__respond_to?(:message) ? exception.message : exception
65
+ else
66
+ message
67
+ end
68
+ module_name = exception ? exception.cs__class.to_s.split('::').first : nil
69
+ stack_frame = Contrast::Agent::Telemetry::TelemetryException::StackFrame.build stack_frame_function,
70
+ stack_frame_type,
71
+ module_name
72
+ message_exception = Contrast::Agent::Telemetry::TelemetryException::MessageException.build(
73
+ message_exception_type,
74
+ message_for_exception,
75
+ module_name,
76
+ stack_frame)
77
+ tags = if data
78
+ data
79
+ else
80
+ exception.cs__is_a?(Hash) ? exception : {}
81
+ end
82
+ message = Contrast::Agent::Telemetry::TelemetryException::Message.build tags, [message_exception]
83
+ Contrast::Agent::Telemetry::TelemetryException::Event.new message
84
+ end
85
+ end
86
+ end
87
+ end
@@ -1,8 +1,6 @@
1
1
  # Copyright (c) 2022 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
2
  # frozen_string_literal: true
3
3
 
4
- require 'contrast/utils/exclude_key'
5
-
6
4
  module Contrast
7
5
  module Logger
8
6
  # Our decorator for the Ougai logger allowing for the logging of the
@@ -19,7 +17,7 @@ module Contrast
19
17
  env_key = env_key.to_s
20
18
  next unless ENV_KEYS.include?(env_key) ||
21
19
  (env_key.start_with?(Contrast::Components::Config::CONTRAST_ENV_MARKER) &&
22
- !env_key.start_with?(Contrast::Components::Config::CONTRAST_ENV_MARKER + 'API'))
20
+ !env_key.start_with?("#{ Contrast::Components::Config::CONTRAST_ENV_MARKER }API"))
23
21
 
24
22
  info('Environment settings', key: env_key, value: env_value)
25
23
  end
@@ -31,8 +29,6 @@ module Contrast
31
29
  loggable = ::Contrast::CONFIG.loggable
32
30
  info('Current configuration', configuration: loggable)
33
31
  env_keys = ENV.keys.select do |env_key|
34
- next if Contrast::Utils::ExcludeKey.excludable? env_key.to_s
35
-
36
32
  env_key&.to_s&.start_with?(Contrast::Components::Config::CONTRAST_ENV_MARKER)
37
33
  end
38
34
  env_items = env_keys.map { |env_key| Contrast::Utils::EnvConfigurationItem.new(env_key, nil) }
@@ -142,7 +142,7 @@ module Contrast
142
142
  "as suspicious using #{ rule_id } - #{ input_value }"
143
143
  log [suspicious_attack_with, rule_id, outcome], ::Logger::WARN
144
144
  elsif input_value.present?
145
- suspicious_attack_without = 'Suspicious activity indicates a potential attack using ' + rule_id.to_s
145
+ suspicious_attack_without = "Suspicious activity indicates a potential attack using #{ rule_id }"
146
146
  log [suspicious_attack_without, rule_id, outcome], ::Logger::WARN
147
147
  end
148
148
  end