contrast-agent 4.11.0 → 4.14.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (145) hide show
  1. checksums.yaml +4 -4
  2. data/.simplecov +1 -0
  3. data/ext/cs__assess_module/cs__assess_module.c +48 -0
  4. data/ext/cs__assess_module/cs__assess_module.h +7 -0
  5. data/ext/cs__common/cs__common.c +24 -7
  6. data/ext/cs__common/cs__common.h +12 -2
  7. data/ext/cs__contrast_patch/cs__contrast_patch.c +48 -11
  8. data/ext/cs__contrast_patch/cs__contrast_patch.h +5 -2
  9. data/ext/cs__os_information/cs__os_information.c +31 -0
  10. data/ext/cs__os_information/cs__os_information.h +7 -0
  11. data/ext/{cs__protect_kernel → cs__os_information}/extconf.rb +0 -0
  12. data/lib/contrast/agent/assess/contrast_event.rb +1 -1
  13. data/lib/contrast/agent/assess/contrast_object.rb +1 -1
  14. data/lib/contrast/agent/assess/policy/dynamic_source_factory.rb +2 -0
  15. data/lib/contrast/agent/assess/policy/policy_node.rb +6 -6
  16. data/lib/contrast/agent/assess/policy/policy_scanner.rb +5 -0
  17. data/lib/contrast/agent/assess/policy/preshift.rb +19 -6
  18. data/lib/contrast/agent/assess/policy/propagation_method.rb +2 -116
  19. data/lib/contrast/agent/assess/policy/propagation_node.rb +4 -4
  20. data/lib/contrast/agent/assess/policy/propagator/center.rb +1 -1
  21. data/lib/contrast/agent/assess/policy/propagator/database_write.rb +2 -0
  22. data/lib/contrast/agent/assess/policy/propagator/substitution.rb +2 -154
  23. data/lib/contrast/agent/assess/policy/source_method.rb +2 -71
  24. data/lib/contrast/agent/assess/policy/trigger_method.rb +45 -110
  25. data/lib/contrast/agent/assess/policy/trigger_node.rb +62 -21
  26. data/lib/contrast/agent/assess/policy/trigger_validation/xss_validator.rb +1 -1
  27. data/lib/contrast/agent/assess/property/tagged.rb +66 -189
  28. data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +40 -6
  29. data/lib/contrast/agent/deadzone/policy/policy.rb +6 -0
  30. data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +1 -0
  31. data/lib/contrast/agent/metric_telemetry_event.rb +26 -0
  32. data/lib/contrast/agent/middleware.rb +14 -62
  33. data/lib/contrast/agent/patching/policy/after_load_patcher.rb +0 -1
  34. data/lib/contrast/agent/patching/policy/method_policy.rb +3 -44
  35. data/lib/contrast/agent/patching/policy/method_policy_extend.rb +111 -0
  36. data/lib/contrast/agent/patching/policy/patch.rb +37 -238
  37. data/lib/contrast/agent/patching/policy/patcher.rb +15 -50
  38. data/lib/contrast/agent/reporting/report.rb +21 -0
  39. data/lib/contrast/agent/reporting/reporter.rb +142 -0
  40. data/lib/contrast/agent/reporting/reporting_events/finding.rb +90 -0
  41. data/lib/contrast/agent/reporting/reporting_events/preflight.rb +25 -0
  42. data/lib/contrast/agent/reporting/reporting_events/preflight_message.rb +56 -0
  43. data/lib/contrast/agent/reporting/reporting_events/reporting_event.rb +37 -0
  44. data/lib/contrast/agent/reporting/reporting_utilities/audit.rb +127 -0
  45. data/lib/contrast/agent/reporting/reporting_utilities/reporter_client.rb +168 -0
  46. data/lib/contrast/agent/reporting/reporting_utilities/reporting_storage.rb +66 -0
  47. data/lib/contrast/agent/request.rb +2 -81
  48. data/lib/contrast/agent/request_context.rb +18 -126
  49. data/lib/contrast/agent/request_context_extend.rb +138 -0
  50. data/lib/contrast/agent/request_handler.rb +7 -3
  51. data/lib/contrast/agent/response.rb +2 -73
  52. data/lib/contrast/agent/rule_set.rb +2 -4
  53. data/lib/contrast/agent/startup_metrics_telemetry_event.rb +94 -0
  54. data/lib/contrast/agent/static_analysis.rb +5 -3
  55. data/lib/contrast/agent/telemetry.rb +137 -0
  56. data/lib/contrast/agent/telemetry_event.rb +33 -0
  57. data/lib/contrast/agent/thread_watcher.rb +66 -11
  58. data/lib/contrast/agent/version.rb +1 -1
  59. data/lib/contrast/agent.rb +21 -1
  60. data/lib/contrast/api/communication/connection_status.rb +10 -7
  61. data/lib/contrast/api/communication/messaging_queue.rb +37 -3
  62. data/lib/contrast/api/communication/response_processor.rb +15 -8
  63. data/lib/contrast/api/communication/service_lifecycle.rb +13 -3
  64. data/lib/contrast/api/communication/socket.rb +6 -8
  65. data/lib/contrast/api/communication/socket_client.rb +29 -12
  66. data/lib/contrast/api/communication/speedracer.rb +37 -1
  67. data/lib/contrast/api/communication/tcp_socket.rb +4 -3
  68. data/lib/contrast/api/communication/unix_socket.rb +1 -0
  69. data/lib/contrast/api/decorators/finding.rb +45 -0
  70. data/lib/contrast/components/api.rb +90 -0
  71. data/lib/contrast/components/app_context.rb +10 -41
  72. data/lib/contrast/components/app_context_extend.rb +78 -0
  73. data/lib/contrast/components/assess.rb +7 -0
  74. data/lib/contrast/components/base.rb +23 -0
  75. data/lib/contrast/components/config.rb +92 -13
  76. data/lib/contrast/components/contrast_service.rb +11 -0
  77. data/lib/contrast/components/sampling.rb +2 -2
  78. data/lib/contrast/config/agent_configuration.rb +1 -1
  79. data/lib/contrast/config/api_configuration.rb +27 -0
  80. data/lib/contrast/config/api_proxy_configuration.rb +14 -0
  81. data/lib/contrast/config/application_configuration.rb +2 -3
  82. data/lib/contrast/config/assess_configuration.rb +3 -2
  83. data/lib/contrast/config/base_configuration.rb +17 -28
  84. data/lib/contrast/config/certification_configuration.rb +15 -0
  85. data/lib/contrast/config/env_variables.rb +18 -0
  86. data/lib/contrast/config/heap_dump_configuration.rb +6 -6
  87. data/lib/contrast/config/inventory_configuration.rb +1 -5
  88. data/lib/contrast/config/protect_rule_configuration.rb +1 -1
  89. data/lib/contrast/config/request_audit_configuration.rb +18 -0
  90. data/lib/contrast/config/root_configuration.rb +1 -0
  91. data/lib/contrast/config/ruby_configuration.rb +6 -6
  92. data/lib/contrast/config/service_configuration.rb +2 -2
  93. data/lib/contrast/config.rb +1 -1
  94. data/lib/contrast/configuration.rb +4 -2
  95. data/lib/contrast/extension/assess/array.rb +5 -7
  96. data/lib/contrast/framework/manager.rb +22 -44
  97. data/lib/contrast/framework/manager_extend.rb +50 -0
  98. data/lib/contrast/framework/rails/patch/action_controller_live_buffer.rb +9 -6
  99. data/lib/contrast/framework/rails/patch/support.rb +31 -29
  100. data/lib/contrast/framework/rails/railtie.rb +1 -1
  101. data/lib/contrast/framework/sinatra/support.rb +2 -1
  102. data/lib/contrast/logger/application.rb +4 -0
  103. data/lib/contrast/logger/log.rb +8 -103
  104. data/lib/contrast/utils/assess/propagation_method_utils.rb +129 -0
  105. data/lib/contrast/utils/assess/property/tagged_utils.rb +165 -0
  106. data/lib/contrast/utils/assess/source_method_utils.rb +83 -0
  107. data/lib/contrast/utils/assess/tracking_util.rb +20 -15
  108. data/lib/contrast/utils/assess/trigger_method_utils.rb +138 -0
  109. data/lib/contrast/utils/class_util.rb +65 -54
  110. data/lib/contrast/utils/exclude_key.rb +20 -0
  111. data/lib/contrast/utils/findings.rb +62 -0
  112. data/lib/contrast/utils/hash_digest.rb +10 -73
  113. data/lib/contrast/utils/hash_digest_extend.rb +86 -0
  114. data/lib/contrast/utils/head_dump_utils_extend.rb +74 -0
  115. data/lib/contrast/utils/heap_dump_util.rb +2 -65
  116. data/lib/contrast/utils/invalid_configuration_util.rb +29 -0
  117. data/lib/contrast/utils/io_util.rb +1 -1
  118. data/lib/contrast/utils/log_utils.rb +108 -0
  119. data/lib/contrast/utils/lru_cache.rb +4 -2
  120. data/lib/contrast/utils/metrics_hash.rb +59 -0
  121. data/lib/contrast/utils/middleware_utils.rb +87 -0
  122. data/lib/contrast/utils/net_http_base.rb +158 -0
  123. data/lib/contrast/utils/object_share.rb +1 -0
  124. data/lib/contrast/utils/os.rb +23 -0
  125. data/lib/contrast/utils/patching/policy/patch_utils.rb +232 -0
  126. data/lib/contrast/utils/patching/policy/patcher_utils.rb +54 -0
  127. data/lib/contrast/utils/request_utils.rb +88 -0
  128. data/lib/contrast/utils/response_utils.rb +97 -0
  129. data/lib/contrast/utils/substitution_utils.rb +167 -0
  130. data/lib/contrast/utils/tag_util.rb +9 -9
  131. data/lib/contrast/utils/telemetry.rb +79 -0
  132. data/lib/contrast/utils/telemetry_client.rb +90 -0
  133. data/lib/contrast/utils/telemetry_identifier.rb +130 -0
  134. data/lib/contrast.rb +19 -1
  135. data/resources/assess/policy.json +12 -6
  136. data/resources/deadzone/policy.json +86 -5
  137. data/ruby-agent.gemspec +7 -6
  138. data/service_executables/VERSION +1 -1
  139. data/service_executables/linux/contrast-service +0 -0
  140. data/service_executables/mac/contrast-service +0 -0
  141. metadata +68 -26
  142. data/ext/cs__protect_kernel/cs__protect_kernel.c +0 -47
  143. data/ext/cs__protect_kernel/cs__protect_kernel.h +0 -12
  144. data/lib/contrast/config/default_value.rb +0 -17
  145. data/lib/contrast/extension/protect/kernel.rb +0 -29
@@ -0,0 +1,97 @@
1
+ # Copyright (c) 2021 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 Utils
6
+ # used in Contrast::Agent::Response
7
+ module ResponseUtils
8
+ private
9
+
10
+ # From the dtm for normalized_response_headers:
11
+ # Key is UPPERCASE_UNDERSCORE
12
+ #
13
+ # Example: Content-Type: text/html; charset=utf-8
14
+ # "CONTENT_TYPE" => Content-Type,["text/html; charset=utf8"]
15
+ def append_pair map, key, value
16
+ return unless key && value
17
+ return if value.is_a?(Hash)
18
+
19
+ safe_key = Contrast::Utils::StringUtils.force_utf8(key)
20
+ hash_key = Contrast::Utils::StringUtils.normalized_key(safe_key)
21
+ map[hash_key] ||= Contrast::Api::Dtm::Pair.new
22
+ map[hash_key].key = safe_key
23
+ map[hash_key].values << Contrast::Utils::StringUtils.force_utf8(value)
24
+ end
25
+
26
+ HTTP_PREFIX = /^[Hh][Tt][Tt][Pp][_-]/i.cs__freeze
27
+
28
+ # Given some holder of the content of the response's body, extract that
29
+ # content and return it as a String
30
+ #
31
+ # @param body [String, Rack::File, Rack::BodyProxy,
32
+ # ActionDispatch::Response::RackBody, Rack::Response] Something that
33
+ # holds, wraps, or is the body of the Response
34
+ # @return [nil, String] the content of the body
35
+ def extract_body body
36
+ return unless body
37
+ return if defined?(Rack::File) && body.is_a?(Rack::File)
38
+
39
+ case body
40
+ when Rack::BodyProxy
41
+ handle_rack_body_proxy(body)
42
+ when ActionDispatch::Response::RackBody
43
+ extract_body(body.body) if defined?(ActionDispatch::Response::RackBody)
44
+ when Rack::Response
45
+ extract_body(body.body)
46
+ when Contrast::Utils::DuckUtils.quacks_to?(body, :each)
47
+ acc = []
48
+ body.each { |tmp| acc << read_or_string(tmp) }
49
+ acc.compact.join(Contrast::Utils::ObjectShare::NEW_LINE)
50
+ when ActionView::OutputBuffer
51
+ # https://stackoverflow.com/questions/15654676/how-to-convert-activesupportsafebuffer-to-string
52
+ body.to_str
53
+ else
54
+ read_or_string(body)
55
+ end
56
+ # if body.is_a?(Rack::BodyProxy)
57
+ # handle_rack_body_proxy(body)
58
+ # elsif (defined?(ActionDispatch::Response::RackBody) && body.is_a?(ActionDispatch::Response::RackBody)) ||
59
+ # body.is_a?(Rack::Response)
60
+ #
61
+ # extract_body(body.body)
62
+ # elsif Contrast::Utils::DuckUtils.quacks_to?(body, :each)
63
+ # acc = []
64
+ # body.each { |tmp| acc << read_or_string(tmp) }
65
+ # acc.compact.join(Contrast::Utils::ObjectShare::NEW_LINE)
66
+ # elsif ActionView::OutputBuffer
67
+ # # https://stackoverflow.com/questions/15654676/how-to-convert-activesupportsafebuffer-to-string
68
+ # body.to_str
69
+ # else
70
+ # read_or_string(body)
71
+ # end
72
+ end
73
+
74
+ def handle_rack_body_proxy body
75
+ next_body = body.instance_variable_get(:@body)
76
+ case next_body
77
+ when Array
78
+ extract_body(next_body[0])
79
+ else
80
+ extract_body(next_body)
81
+ end
82
+ end
83
+
84
+ def read_or_string obj
85
+ return unless obj
86
+
87
+ if Contrast::Utils::DuckUtils.quacks_to?(obj, :read)
88
+ tmp = obj.read
89
+ obj.rewind
90
+ tmp
91
+ else
92
+ obj.to_s
93
+ end
94
+ end
95
+ end
96
+ end
97
+ end
@@ -0,0 +1,167 @@
1
+ # Copyright (c) 2021 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 Utils
6
+ # used in Contrast::Agent::Assesss::Policy::Propagator::Substitution
7
+ module SubstitutionUtils
8
+ CAPTURE_GROUP_REGEXP = /\\[[:digit:]]/.cs__freeze
9
+ CAPTURE_NAME_REGEXP = /\\k<[[:alpha:]]/.cs__freeze
10
+
11
+ private
12
+
13
+ def substitution_tagger patcher, preshift, ret, block, global = true
14
+ return ret unless ret
15
+
16
+ begin
17
+ source = preshift.object
18
+ self_tracked = Contrast::Agent::Assess::Tracker.tracked?(source)
19
+ args = preshift.args[1]
20
+ incoming_tracked = args && determine_tracked(args)
21
+ return ret unless self_tracked || incoming_tracked
22
+
23
+ parent_events = []
24
+ if block
25
+ block_sub(self_tracked, source, ret)
26
+ elsif args.is_a?(String)
27
+ string_sub(parent_events, self_tracked, preshift, ret, args, incoming_tracked, global)
28
+ elsif args.is_a?(Hash)
29
+ hash_sub(self_tracked, source, ret)
30
+ else # Enumerator, only for gsub
31
+ pattern_gsub(parent_events, preshift, ret)
32
+ end
33
+
34
+ self_tracked_handle self_tracked, source, parent_events
35
+ string_build_event(parent_events, patcher, preshift, ret)
36
+ rescue StandardError => e
37
+ logger.error('Unable to apply gsub propagator', e)
38
+ end
39
+ ret
40
+ end
41
+
42
+ def self_tracked_handle self_tracked, source, parent_events
43
+ return unless self_tracked
44
+
45
+ source_properties = Contrast::Agent::Assess::Tracker.properties(source)
46
+ parent_event = source_properties&.event
47
+ parent_events.prepend(parent_event) if parent_event
48
+ end
49
+
50
+ def determine_tracked args
51
+ # if there's no arg, it can't be tracked
52
+ return false unless args
53
+
54
+ # if it's a string, just ask if it's tracked
55
+ case args
56
+ when String
57
+ Contrast::Agent::Assess::Tracker.tracked?(args)
58
+ # if it's a hash, ask if it has a tracked string
59
+ when Hash
60
+ args.values.any? { |value| value.is_a?(String) && Contrast::Agent::Assess::Tracker.tracked?(value) }
61
+ # this should never happen
62
+ else
63
+ false
64
+ end
65
+ end
66
+
67
+ def string_sub parent_events, self_tracked, preshift, ret, incoming, incoming_tracked, global
68
+ return unless (properties = Contrast::Agent::Assess::Tracker.properties!(ret))
69
+
70
+ incoming_properties = Contrast::Agent::Assess::Tracker.properties(incoming)
71
+ parent_events << incoming_properties&.event if incoming_properties&.event
72
+
73
+ source = preshift.object
74
+
75
+ # We can't efficiently find the places that things were
76
+ # copied from regexp / captures. Trading accuracy for
77
+ # performance
78
+ if incoming.match?(CAPTURE_GROUP_REGEXP) || incoming.match?(CAPTURE_NAME_REGEXP)
79
+ properties.splat_from(source, ret) if self_tracked
80
+ return
81
+ end
82
+
83
+ # if it's just a straight insert, that we can do
84
+ # Copy the tags from us to the return
85
+ ranges = find_string_sub_insert(properties, preshift, incoming, ret, global)
86
+
87
+ properties.delete_tags_at_ranges(ranges)
88
+ properties.shift_tags(ranges)
89
+ return unless incoming_tracked
90
+ return unless incoming_properties
91
+
92
+ tags = incoming_properties.tag_keys
93
+ ranges.each do |range|
94
+ tags.each do |tag|
95
+ properties.add_tag(tag, range)
96
+ end
97
+ end
98
+ end
99
+
100
+ # Find the points at which the new String was placed into the original
101
+ #
102
+ # @param properties [Contrast::Agent::Assess::Properties] the Properties of the ret
103
+ # @param preshift [Contrast::Agent::Assess::PreShift] the capture of the state of the code just prior to
104
+ # the invocation of the patched method
105
+ # @param incoming [String] the new String going into the substitution
106
+ # @param ret [String] the result of the substitution
107
+ # @param global [Boolean] if this was a global or single substitution
108
+ # @return [Array<Range>] the Ranges where substitution occurred
109
+ def find_string_sub_insert properties, preshift, incoming, ret, global
110
+ pattern = preshift.args[0]
111
+ source = preshift.object
112
+
113
+ properties.copy_from(source, ret)
114
+ # Figure out where inserts occurred
115
+ last_idx = 0
116
+ ranges = []
117
+ # For each insert, move the tag ranges
118
+ while last_idx
119
+ idx = source.index(pattern, last_idx)
120
+ break unless idx
121
+
122
+ last_idx = idx ? idx + 1 : nil
123
+ start_index = idx
124
+ end_index = idx + incoming.length
125
+ ranges << (start_index...end_index)
126
+ break unless global
127
+ end
128
+ ranges
129
+ end
130
+
131
+ def block_sub self_tracked, source, ret
132
+ return unless self_tracked
133
+
134
+ properties = Contrast::Agent::Assess::Tracker.properties!(ret)
135
+ properties&.splat_from(source, ret)
136
+ end
137
+
138
+ def hash_sub self_tracked, source, ret
139
+ return unless self_tracked
140
+
141
+ properties = Contrast::Agent::Assess::Tracker.properties!(ret)
142
+ properties&.splat_from(source, ret)
143
+ end
144
+
145
+ def pattern_gsub parent_events, preshift, ret
146
+ source = preshift.object
147
+ return unless (source_properties = Contrast::Agent::Assess::Tracker.properties(source))
148
+ return unless (properties = Contrast::Agent::Assess::Tracker.properties!(ret))
149
+
150
+ source_properties.tag_keys.each do |key|
151
+ properties.add_tag(key, 0...1)
152
+ end
153
+ parent_event = source_properties.event
154
+ parent_events << parent_event if parent_event
155
+ end
156
+
157
+ def string_build_event parent_events, patcher, preshift, ret
158
+ return unless Contrast::Agent::Assess::Tracker.tracked?(ret)
159
+
160
+ properties = Contrast::Agent::Assess::Tracker.properties(ret)
161
+ args = preshift.args
162
+ properties.build_event(patcher, ret, preshift.object, ret, args, 2)
163
+ properties.event.instance_variable_set(:@_parent_events, parent_events)
164
+ end
165
+ end
166
+ end
167
+ end
@@ -56,20 +56,20 @@ module Contrast
56
56
  # Any overlapping ranges are merged before returning
57
57
  #
58
58
  # arr: the array of tags to which we are adding
59
- # new_arr: misnomer. either an array of or a single Tag to be added
60
- def ordered_merge arr, new_arr
59
+ # add_arr: array of Tags or a single Tag to be added
60
+ def ordered_merge arr, add_arr
61
61
  # [Contrast::Agent::Assess::Tag, ...]
62
- if new_arr.is_a?(Array)
63
- return arr unless new_arr.any?
64
- return new_arr unless arr&.any?
62
+ if add_arr.is_a?(Array)
63
+ return arr unless add_arr.any?
64
+ return add_arr unless arr&.any?
65
65
 
66
- new_arr.each { |new_element| single_ordered_merge(arr, new_element) }
66
+ add_arr.each { |new_element| single_ordered_merge(arr, new_element) }
67
67
  # Contrast::Agent::Assess::Tag
68
68
  else
69
- return arr unless new_arr
70
- return [new_arr] unless arr
69
+ return arr unless add_arr
70
+ return [add_arr] unless arr
71
71
 
72
- single_ordered_merge(arr, new_arr)
72
+ single_ordered_merge(arr, add_arr)
73
73
  end
74
74
  smallerize(arr)
75
75
  end
@@ -0,0 +1,79 @@
1
+ # Copyright (c) 2021 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'
5
+ require 'contrast/utils/telemetry_identifier'
6
+
7
+ module Contrast
8
+ module Utils
9
+ # Tools for supporting the Telemetry feature
10
+ module Telemetry
11
+ DIR = '/ect/contrast/ruby-agent/'.cs__freeze
12
+ FILE = '.telemetry'.cs__freeze
13
+ CURRENT_DIR = Dir.pwd.cs__freeze
14
+ CONFIG_DIR = CURRENT_DIR + '/config/contrast/'.cs__freeze
15
+ MESSAGE = {
16
+ disclaimer:
17
+ "\n===================================================================================================" \
18
+ "\n\nThe [Contrast Security] [Ruby Agent] " \
19
+ "collects usage data in order to help us improve compatibility\n" \
20
+ "and security coverage. The data is anonymous and does not contain application data. It is collected\n" \
21
+ "by Contrast and is never shared. You can opt-out of telemetry by setting the\n" \
22
+ "'CONTRAST_AGENT_TELEMETRY_OPTOUT' environment variable to '1' or 'true'.\n" \
23
+ 'Read more about [Contrast Security] [Ruby Agent] telemetry: ' \
24
+ "https://docs.contrastsecurity.com/en/ruby-telemetry.html \n\n" \
25
+ "===================================================================================================\n\n"
26
+ }.cs__freeze
27
+
28
+ # Here we create the .telemetry file. If the file exist we do nothing.
29
+ #
30
+ # @return[Boolean, nil] true if file is created, false if file already exist
31
+ # and nil if Telemetry is disabled or on unsupported OS.
32
+ def self.create_telemetry_file
33
+ write_mark_file DIR, FILE, CONFIG_DIR
34
+ end
35
+
36
+ def self.disclaimer
37
+ @_disclaimer = MESSAGE[:disclaimer]
38
+ end
39
+
40
+ class << self
41
+ private
42
+
43
+ # Create the mark file
44
+ #
45
+ # @param dir [String] Directory in which the file is to be created
46
+ # @param file [String] filename of the mark file
47
+ # @param config_dir [String] path for the config folder in working directory
48
+ # @return[Boolean, nil] true if file is created, false if file already exist
49
+ # and nil if Telemetry is disabled or on unsupported OS.
50
+ def write_mark_file dir, file, config_dir
51
+ return unless Contrast::Agent::Telemetry.enabled?
52
+ return if Contrast::Utils::OS.windows?
53
+
54
+ @dir = dir
55
+ # After macOS Catalina, you can no longer store files or data in the read-only system volume,
56
+ # nor can we write to the "root" directory ( / ). This results in Errno::EROFS exception.
57
+ # So for the MacOS we would use the config directory of the instrumented application.
58
+ @dir = config_dir if Contrast::Utils::OS.mac?
59
+ return false if File.file? @dir + file
60
+ return true if touch_marker(@dir, file)
61
+
62
+ # If we don't have permission to write to root directory use config instead
63
+ touch_marker(config_dir, file)
64
+ end
65
+
66
+ # Touches .telemetry file
67
+ #
68
+ # @return[Boolean] true if success, false if fails
69
+ def touch_marker dir, file
70
+ FileUtils.mkdir_p dir unless Dir.exist? dir
71
+ FileUtils.touch dir + file
72
+ File.file? dir + file
73
+ rescue StandardError => _e
74
+ false
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,90 @@
1
+ # Copyright (c) 2021 Contrast Security, Inc. See https://www.contrastsecurity.com/enduser-terms-0317a for more details.
2
+ # frozen_string_literal: true
3
+
4
+ require 'net/http'
5
+ require 'contrast/utils/net_http_base'
6
+ require 'contrast/components/logger'
7
+ require 'contrast/utils/object_share'
8
+ require 'contrast/agent/version'
9
+ require 'json'
10
+
11
+ module Contrast
12
+ module Utils
13
+ # This module creates a Net::HTTP client and initiates a connection to the provided result
14
+ class TelemetryClient < NetHttpBase
15
+ ENDPOINT = 'api/v1/telemetry/metrics' # /TelemetryEvent.path
16
+ SERVICE_NAME = 'Telemetry'
17
+ include Contrast::Components::Logger::InstanceMethods
18
+ # This method initializes the Net::HTTP client we'll need. it will validate
19
+ # the connection and make the first request. If connection is valid and response
20
+ # is available then the open connection is returned.
21
+ #
22
+ # @param url [String]
23
+ # @return [Net::HTTP, nil] Return open connection or nil
24
+ def initialize_connection url
25
+ super(SERVICE_NAME, url, false, false)
26
+ end
27
+
28
+ # This method will be responsible for building the request. Because the telemetry collector expects to receive
29
+ # multiple events in a single request, we must always wrap the event in an array, even if there is only one.
30
+ #
31
+ # @param event [Contrast::Agent::TelemetryEvent,Contrast::Agent::StartupMetricsTelemetryEvent]
32
+ # @return [Net::HTTP::Post]
33
+ def build_request event
34
+ return unless valid_event? event
35
+
36
+ string_body = [event.to_hash].to_json
37
+ header = {
38
+ 'User-Agent' => "<#{ Contrast::Utils::ObjectShare::RUBY }>-<#{ Contrast::Agent::VERSION }>",
39
+ 'Content-Type' => 'application/json'
40
+ }
41
+ request = Net::HTTP::Post.new(build_path(event.path), header)
42
+ request.body = string_body
43
+ request
44
+ end
45
+
46
+ # This method will create the actual request and send it
47
+ # @param event[Contrast::Agent::TelemetryEvent]
48
+ # @param connection[Net::HTTP]
49
+ def send_request event, connection
50
+ return if connection.nil? || event.nil?
51
+ return unless valid_event? event
52
+
53
+ req = build_request event
54
+ connection.request req
55
+ end
56
+
57
+ # This method will handle the response from the tenant
58
+ # @param res [Net::HTTPResponse]
59
+ # @return sleep_time [Integer, nil]
60
+ def handle_response res
61
+ status_code = res.code.to_i
62
+ ready_after = if res.to_hash.keys.map(&:downcase).include?('ready-after')
63
+ res['Ready-After']
64
+ else
65
+ 60
66
+ end
67
+ ready_after if status_code == 429
68
+ end
69
+
70
+ # This method will be responsible for validating the event
71
+ # @param event[Contrast::Agent::TelemetryEvent,Contrast::Agent::StartupMetricsTelemetryEvent]
72
+ def valid_event? event
73
+ return true if event.cs__is_a?(Contrast::Agent::TelemetryEvent)
74
+ return true if event.cs__is_a?(Contrast::Agent::StartupMetricsTelemetryEvent)
75
+
76
+ false
77
+ end
78
+
79
+ private
80
+
81
+ # The telemetry instance accepts any path to #{ Contrast::Agent::Telemetry::URL }#{ ENDPOINT }, using the
82
+ # remainder of the path to segregate messages.
83
+ #
84
+ # @return [String] the fully qualified path to send the request
85
+ def build_path event_path
86
+ "#{ Contrast::Agent::Telemetry::URL }#{ ENDPOINT }#{ event_path }"
87
+ end
88
+ end
89
+ end
90
+ end
@@ -0,0 +1,130 @@
1
+ # Copyright (c) 2021 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'
5
+ require 'contrast/utils/os'
6
+ require 'socket'
7
+
8
+ module Contrast
9
+ module Utils
10
+ # Tools for supporting the Telemetry feature
11
+ module Telemetry
12
+ # Gets info about the instrumented application required to build unique identifiers,
13
+ # used in the agent's Telemetry.
14
+ module Identifier
15
+ MAC_REGEX = /^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/.cs__freeze
16
+ LINUX_OS_REG = /hwaddr=.*?(([A-F0-9]{2}:){5}[A-F0-9]{2})/im.cs__freeze
17
+ MAC_OS_PRIMARY = 'en0'.cs__freeze
18
+ LINUX_PRIMARY = 'enp'.cs__freeze
19
+
20
+ # Sinatra and Grape both use similar approach to identify the app_name.
21
+ # Rails has a different way of doing it, but to unify this we'll use this one.
22
+ # If app_name is changed/renamed during production it would still get the
23
+ # new folder's name.
24
+ #
25
+ # @ return [String] name of the application from the current working directory
26
+ def self.app_name
27
+ @_app_name ||= File.basename(Dir.pwd)
28
+ end
29
+
30
+ # Returns the MAC address of the primary network interface, depending on the used OS.
31
+ # If the primary is unknown it finds the first available network interface and gets it's
32
+ # MAC address instead.
33
+ #
34
+ # @return [String, nil] MAC address of the primary network interface or
35
+ # the first available one, or nil if nothing found
36
+ def self.mac
37
+ primary = Contrast::Utils::OS.mac? ? MAC_OS_PRIMARY : LINUX_PRIMARY
38
+ @_mac = find_mac(primary) || find_mac
39
+ end
40
+
41
+ class << self
42
+ private
43
+
44
+ # Finds the primary MAC address of all listed network adapters.
45
+ # If primary is not set or unknown, use the first MAC address found
46
+ # from the listed adapters.
47
+ #
48
+ # @param primary [nil, String] optional param if set look only for primary
49
+ # network adapter's name
50
+ # @return [String, nil] MAC address of the first listed network adapter or
51
+ # nil if not found
52
+ def find_mac primary = nil
53
+ result = nil
54
+ idx = 0
55
+ return if interfaces.empty?
56
+
57
+ while idx < interfaces.length
58
+ addr = interfaces[idx].addr
59
+ name = interfaces[idx].name # rubocop:disable Security/Module/Name
60
+ idx += 1
61
+ next if primary && !name.include?(primary)
62
+
63
+ # retrieving MAC address from primary network interface or first available
64
+ mac = retrieve_mac addr
65
+ next unless mac
66
+
67
+ result = mac if mac && (mac.match? MAC_REGEX)
68
+ break if result
69
+ end
70
+ result
71
+ end
72
+
73
+ # Retrieves MAC address for primary or any network interface.
74
+ # This is OS dependent search.
75
+ #
76
+ # @param addr [String] address info
77
+ # example: #<Addrinfo: LINK[en0 aa:bb:cc:00:11:22]>
78
+ # @return mac [nil, String] MAC address of primary network interface,
79
+ # any network interface, or nil if no interface is found.
80
+ def retrieve_mac addr
81
+ # Mac OS allow us to use getnameinfo(sockaddr [, flags]) => [hostname, servicename]
82
+ #
83
+ # returned address:
84
+ # <Socket::Ifaddr en0 UP,BROADCAST,RUNNING,NOTRAILERS,SIMPLEX,MULTICAST LINK[en0 aa:bb:cc:00:11:22]>
85
+ return addr.getnameinfo[0] if Contrast::Utils::OS.mac?
86
+
87
+ # In Linux using Socket::addr#getnameinfo results in ai_family not supported exception.
88
+ # In this case we are relying on match filtering of addresses.
89
+ #
90
+ # returned address:
91
+ # #<Socket::Ifaddr eth0 UP,BROADCAST,RUNNING,MULTICAST,0x10000
92
+ # PACKET[protocol=0 eth0 hatype=1 HOST hwaddr=aa:bb:cc:00:11:22]>
93
+ return Regexp.last_match(1) if addr.inspect =~ LINUX_OS_REG
94
+
95
+ nil
96
+ end
97
+
98
+ # Returns array of network interfaces.
99
+ # This is OS dependent search.
100
+ #
101
+ # @return interfaces [Array] Returns an array of interface addresses.
102
+ # Socket::Ifaddr - represents a result of getifaddrs().
103
+ def interfaces
104
+ @_interfaces ||= []
105
+
106
+ return @_interfaces unless @_interfaces.empty?
107
+
108
+ arr = Socket.getifaddrs
109
+ idx = 0
110
+ check_family = 0
111
+ while idx < arr.length
112
+ # We need only network adapters MACs. Checking for pfamily of every socket address:
113
+ # 18 for Mac OS and 17 for Linux.
114
+ # family should be an address family such as: :INET, :INET6, :UNIX, etc.
115
+ check_family = 18 if Contrast::Utils::OS.mac?
116
+ check_family = 17 if Contrast::Utils::OS.linux?
117
+ if arr[idx].addr.pfamily != check_family
118
+ idx += 1
119
+ next
120
+ end
121
+ @_interfaces << arr[idx]
122
+ idx += 1
123
+ end
124
+ @_interfaces
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
130
+ end
data/lib/contrast.rb CHANGED
@@ -23,7 +23,7 @@ end
23
23
 
24
24
  if RUBY_VERSION >= '3.0.0'
25
25
  # This fixes Ruby 3.0 issues with Module#(some instance method) patching by preventing the prepending of
26
- # a JSON helper on protobuf load. String.instance_method(:+) is one of the most noticable.
26
+ # a JSON helper on protobuf load. String.instance_method(:+) is one of the most noticeable.
27
27
  # TODO: RUBY-1132 Remove this once Ruby 3 is fixed.
28
28
  # See bug here: https://bugs.ruby-lang.org/issues/17725
29
29
  class Class
@@ -35,6 +35,7 @@ if RUBY_VERSION >= '3.0.0'
35
35
  end
36
36
 
37
37
  require 'contrast/components/agent'
38
+ require 'contrast/components/api'
38
39
  require 'contrast/components/app_context'
39
40
  require 'contrast/components/assess'
40
41
  require 'contrast/components/config'
@@ -47,6 +48,7 @@ require 'contrast/components/scope'
47
48
  require 'contrast/components/settings'
48
49
 
49
50
  module Contrast
51
+ API = Contrast::Components::Api::Interface.new
50
52
  SCOPE = Contrast::Components::Scope::Interface.new
51
53
  CONFIG = Contrast::Components::Config::Interface.new
52
54
  SETTINGS = Contrast::Components::Settings::Interface.new
@@ -76,3 +78,19 @@ if RUBY_VERSION >= '3.0.0'
76
78
  Class.alias_method(:prepend, :cs__orig_prepend)
77
79
  Class.remove_method(:cs__orig_prepend)
78
80
  end
81
+
82
+ if RUBY_VERSION < '3.0.0'
83
+ # Better handles ancestors for older ruby versions.
84
+ # This is called from C, tread lightly.
85
+ class Module
86
+ @_included_in = []
87
+ # Returns array with modules including this instance
88
+ def included_in
89
+ @_included_in ||= [] unless cs__frozen?
90
+ end
91
+
92
+ def self.included_in
93
+ @_included_in ||= [] unless cs__frozen?
94
+ end
95
+ end
96
+ end
@@ -50,7 +50,7 @@
50
50
  "target": "R",
51
51
  "type": "COOKIE",
52
52
  "tags":["NO_NEWLINES", "CROSS_SITE"]
53
- }, {
53
+ }, {
54
54
  "class_name":"Rack::Request::Helpers",
55
55
  "instance_method": true,
56
56
  "method_visibility": "public",
@@ -200,8 +200,15 @@
200
200
  "source": "O",
201
201
  "target": "R",
202
202
  "action": "KEEP"
203
- },
204
- {
203
+ }, {
204
+ "class_name": "String",
205
+ "instance_method": true,
206
+ "method_visibility": "public",
207
+ "method_name": "force_encoding",
208
+ "source": "O",
209
+ "target": "R",
210
+ "action": "SPLAT"
211
+ }, {
205
212
  "class_name": "String",
206
213
  "instance_method": true,
207
214
  "method_visibility": "public",
@@ -209,8 +216,7 @@
209
216
  "source": "O",
210
217
  "target": "R",
211
218
  "action": "KEEP"
212
- },
213
- {
219
+ }, {
214
220
  "class_name": "String",
215
221
  "instance_method": true,
216
222
  "method_visibility": "public",
@@ -218,7 +224,7 @@
218
224
  "source": "O,P0",
219
225
  "target": "R",
220
226
  "action": "SPLIT"
221
- },{
227
+ }, {
222
228
  "class_name": "String",
223
229
  "instance_method": true,
224
230
  "method_visibility": "public",