contrast-agent 4.8.0 → 4.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +6 -1
- data/.rspec +0 -1
- data/.rspec_parallel +6 -0
- data/.simplecov +1 -0
- data/ext/cs__contrast_patch/cs__contrast_patch.c +0 -1
- data/ext/cs__contrast_patch/cs__contrast_patch.h +0 -2
- data/lib/contrast/agent/assess/contrast_event.rb +1 -5
- data/lib/contrast/agent/assess/contrast_object.rb +0 -3
- data/lib/contrast/agent/assess/finalizers/hash.rb +2 -5
- data/lib/contrast/agent/assess/policy/patcher.rb +5 -4
- data/lib/contrast/agent/assess/policy/policy.rb +1 -1
- data/lib/contrast/agent/assess/policy/policy_scanner.rb +2 -6
- data/lib/contrast/agent/assess/policy/preshift.rb +16 -12
- data/lib/contrast/agent/assess/policy/propagation_method.rb +102 -59
- data/lib/contrast/agent/assess/policy/propagator/database_write.rb +2 -7
- data/lib/contrast/agent/assess/policy/propagator/match_data.rb +31 -11
- data/lib/contrast/agent/assess/policy/propagator/remove.rb +4 -9
- data/lib/contrast/agent/assess/policy/propagator/split.rb +10 -6
- data/lib/contrast/agent/assess/policy/propagator/substitution.rb +3 -3
- data/lib/contrast/agent/assess/policy/rewriter_patch.rb +6 -7
- data/lib/contrast/agent/assess/policy/source_method.rb +18 -22
- data/lib/contrast/agent/assess/policy/trigger/xpath.rb +0 -4
- data/lib/contrast/agent/assess/policy/trigger_method.rb +62 -88
- data/lib/contrast/agent/assess/policy/trigger_node.rb +1 -1
- data/lib/contrast/agent/assess/property/evented.rb +2 -1
- data/lib/contrast/agent/assess/rule/provider/hardcoded_value_rule.rb +3 -4
- data/lib/contrast/agent/at_exit_hook.rb +3 -3
- data/lib/contrast/agent/class_reopener.rb +6 -5
- data/lib/contrast/agent/disable_reaction.rb +4 -5
- data/lib/contrast/agent/exclusion_matcher.rb +2 -7
- data/lib/contrast/agent/inventory/database_config.rb +117 -0
- data/lib/contrast/agent/inventory/dependency_analysis.rb +2 -6
- data/lib/contrast/agent/inventory/dependency_usage_analysis.rb +9 -10
- data/lib/contrast/agent/inventory/policy/datastores.rb +5 -6
- data/lib/contrast/agent/inventory/policy/policy.rb +1 -1
- data/lib/contrast/agent/middleware.rb +15 -13
- data/lib/contrast/agent/patching/policy/after_load_patch.rb +6 -3
- data/lib/contrast/agent/patching/policy/after_load_patcher.rb +21 -16
- data/lib/contrast/agent/patching/policy/module_policy.rb +2 -4
- data/lib/contrast/agent/patching/policy/patch.rb +13 -8
- data/lib/contrast/agent/patching/policy/patch_status.rb +3 -7
- data/lib/contrast/agent/patching/policy/patcher.rb +14 -14
- data/lib/contrast/agent/patching/policy/policy.rb +2 -4
- data/lib/contrast/agent/patching/policy/policy_node.rb +2 -3
- data/lib/contrast/agent/protect/policy/applies_no_sqli_rule.rb +1 -1
- data/lib/contrast/agent/protect/policy/policy.rb +1 -1
- data/lib/contrast/agent/protect/policy/rule_applicator.rb +3 -5
- data/lib/contrast/agent/protect/rule/base.rb +10 -10
- data/lib/contrast/agent/protect/rule/cmd_injection.rb +4 -5
- data/lib/contrast/agent/protect/rule/no_sqli.rb +7 -53
- data/lib/contrast/agent/protect/rule/path_traversal.rb +1 -5
- data/lib/contrast/agent/protect/rule/sql_sample_builder.rb +137 -0
- data/lib/contrast/agent/protect/rule/sqli.rb +7 -70
- data/lib/contrast/agent/reaction_processor.rb +3 -4
- data/lib/contrast/agent/request.rb +13 -7
- data/lib/contrast/agent/request_context.rb +36 -34
- data/lib/contrast/agent/request_handler.rb +5 -3
- data/lib/contrast/agent/response.rb +2 -3
- data/lib/contrast/agent/rewriter.rb +4 -3
- data/lib/contrast/agent/rule_set.rb +5 -4
- data/lib/contrast/agent/scope.rb +32 -20
- data/lib/contrast/agent/service_heartbeat.rb +2 -3
- data/lib/contrast/agent/static_analysis.rb +7 -6
- data/lib/contrast/agent/thread.rb +2 -4
- data/lib/contrast/agent/thread_watcher.rb +3 -4
- data/lib/contrast/agent/tracepoint_hook.rb +20 -7
- data/lib/contrast/agent/version.rb +1 -1
- data/lib/contrast/api/communication/messaging_queue.rb +16 -11
- data/lib/contrast/api/communication/response_processor.rb +11 -11
- data/lib/contrast/api/communication/service_lifecycle.rb +9 -5
- data/lib/contrast/api/communication/socket_client.rb +18 -14
- data/lib/contrast/api/communication/speedracer.rb +5 -6
- data/lib/contrast/api/decorators/address.rb +2 -3
- data/lib/contrast/api/decorators/agent_startup.rb +7 -9
- data/lib/contrast/api/decorators/application_startup.rb +9 -10
- data/lib/contrast/api/decorators/application_update.rb +0 -4
- data/lib/contrast/api/decorators/http_request.rb +3 -7
- data/lib/contrast/api/decorators/instrumentation_mode.rb +3 -5
- data/lib/contrast/api/decorators/message.rb +7 -7
- data/lib/contrast/api/decorators/route_coverage.rb +24 -1
- data/lib/contrast/api/decorators/trace_event_object.rb +2 -3
- data/lib/contrast/components/agent.rb +13 -15
- data/lib/contrast/components/app_context.rb +7 -11
- data/lib/contrast/components/assess.rb +19 -16
- data/lib/contrast/components/base.rb +40 -0
- data/lib/contrast/components/config.rb +1 -2
- data/lib/contrast/components/contrast_service.rb +8 -11
- data/lib/contrast/components/heap_dump.rb +5 -4
- data/lib/contrast/components/inventory.rb +2 -7
- data/lib/contrast/components/logger.rb +14 -10
- data/lib/contrast/components/protect.rb +10 -13
- data/lib/contrast/components/sampling.rb +5 -5
- data/lib/contrast/components/scope.rb +9 -32
- data/lib/contrast/components/settings.rb +1 -5
- data/lib/contrast/config/base_configuration.rb +14 -6
- data/lib/contrast/configuration.rb +22 -19
- data/lib/contrast/extension/assess/array.rb +3 -15
- data/lib/contrast/extension/assess/eval_trigger.rb +2 -23
- data/lib/contrast/extension/assess/fiber.rb +6 -16
- data/lib/contrast/extension/assess/hash.rb +3 -13
- data/lib/contrast/extension/assess/kernel.rb +3 -14
- data/lib/contrast/extension/assess/marshal.rb +6 -14
- data/lib/contrast/extension/assess/regexp.rb +5 -15
- data/lib/contrast/extension/assess/string.rb +6 -31
- data/lib/contrast/extension/extension.rb +61 -0
- data/lib/contrast/extension/kernel.rb +2 -4
- data/lib/contrast/extension/protect/kernel.rb +0 -15
- data/lib/contrast/framework/grape/support.rb +174 -0
- data/lib/contrast/framework/manager.rb +44 -9
- data/lib/contrast/framework/rack/patch/session_cookie.rb +6 -6
- data/lib/contrast/framework/rack/support.rb +1 -1
- data/lib/contrast/framework/rails/patch/assess_configuration.rb +5 -8
- data/lib/contrast/framework/rails/patch/support.rb +43 -36
- data/lib/contrast/framework/rails/railtie.rb +8 -6
- data/lib/contrast/framework/rails/rewrite/active_record_named.rb +4 -4
- data/lib/contrast/framework/rails/support.rb +60 -13
- data/lib/contrast/framework/sinatra/support.rb +1 -1
- data/lib/contrast/funchook/funchook.rb +4 -3
- data/lib/contrast/logger/application.rb +1 -6
- data/lib/contrast/logger/log.rb +103 -13
- data/lib/contrast/logger/request.rb +0 -4
- data/lib/contrast/tasks/config.rb +0 -1
- data/lib/contrast/tasks/service.rb +1 -6
- data/lib/contrast/utils/assess/sampling_util.rb +2 -3
- data/lib/contrast/utils/assess/tracking_util.rb +2 -4
- data/lib/contrast/utils/class_util.rb +26 -19
- data/lib/contrast/utils/heap_dump_util.rb +5 -3
- data/lib/contrast/utils/invalid_configuration_util.rb +4 -3
- data/lib/contrast/utils/io_util.rb +46 -40
- data/lib/contrast/utils/job_servers_running.rb +4 -3
- data/lib/contrast/utils/lru_cache.rb +43 -0
- data/lib/contrast/utils/os.rb +2 -3
- data/lib/contrast/utils/ruby_ast_rewriter.rb +16 -13
- data/lib/contrast/utils/string_utils.rb +2 -3
- data/lib/contrast/utils/tag_util.rb +26 -19
- data/lib/contrast.rb +24 -14
- data/resources/assess/policy.json +197 -2
- data/resources/deadzone/policy.json +10 -0
- data/ruby-agent.gemspec +13 -3
- data/service_executables/VERSION +1 -1
- data/service_executables/linux/contrast-service +0 -0
- data/service_executables/mac/contrast-service +0 -0
- metadata +91 -25
- data/lib/contrast/components/interface.rb +0 -196
- data/lib/contrast/delegators/input_analysis.rb +0 -12
- data/lib/contrast/utils/inventory_util.rb +0 -114
@@ -7,7 +7,8 @@ require 'timeout'
|
|
7
7
|
require 'contrast/utils/object_share'
|
8
8
|
require 'contrast/utils/string_utils'
|
9
9
|
require 'contrast/utils/hash_digest'
|
10
|
-
require 'contrast/components/
|
10
|
+
require 'contrast/components/logger'
|
11
|
+
require 'contrast/components/scope'
|
11
12
|
|
12
13
|
module Contrast
|
13
14
|
module Agent
|
@@ -16,8 +17,8 @@ module Contrast
|
|
16
17
|
# data in a format that the Agent expects, caching those transformations in
|
17
18
|
# order to avoid repeatedly creating Strings & thrashing GC.
|
18
19
|
class Request
|
19
|
-
include Contrast::Components::
|
20
|
-
|
20
|
+
include Contrast::Components::Logger::InstanceMethods
|
21
|
+
include Contrast::Components::Scope::InstanceMethods
|
21
22
|
|
22
23
|
extend Forwardable
|
23
24
|
|
@@ -91,10 +92,12 @@ module Contrast
|
|
91
92
|
defined?(Rack::Multipart::UploadedFile) &&
|
92
93
|
body.is_a?(Rack::Multipart::UploadedFile)
|
93
94
|
|
94
|
-
logger.trace(
|
95
|
+
logger.trace('not parsing uploaded file body',
|
96
|
+
file_name: body.original_filename,
|
97
|
+
content_type: body.content_type)
|
95
98
|
@_body = nil
|
96
99
|
else
|
97
|
-
logger.trace(
|
100
|
+
logger.trace('parsing body from request', body_type: body.cs__class.cs__name)
|
98
101
|
@_body = Contrast::Utils::StringUtils.force_utf8(read_body(body))
|
99
102
|
end
|
100
103
|
|
@@ -182,8 +185,11 @@ module Contrast
|
|
182
185
|
res[prefix] = Contrast::Utils::ObjectShare::EMPTY_STRING if prefix
|
183
186
|
res
|
184
187
|
when Enumerable
|
185
|
-
|
186
|
-
|
188
|
+
idx = 0
|
189
|
+
res = {}
|
190
|
+
while idx < val.length
|
191
|
+
res.merge! normalize_params(val[idx], prefix: "#{ prefix }[#{ idx }]")
|
192
|
+
idx += 1
|
187
193
|
end
|
188
194
|
res[prefix] = Contrast::Utils::ObjectShare::EMPTY_STRING if prefix
|
189
195
|
res
|
@@ -4,15 +4,14 @@
|
|
4
4
|
require 'contrast/utils/timer'
|
5
5
|
require 'contrast/agent/request'
|
6
6
|
require 'contrast/agent/response'
|
7
|
-
require 'contrast/
|
8
|
-
require 'contrast/components/
|
9
|
-
require 'contrast/
|
7
|
+
require 'contrast/agent/inventory/database_config'
|
8
|
+
require 'contrast/components/logger'
|
9
|
+
require 'contrast/components/scope'
|
10
10
|
|
11
11
|
module Contrast
|
12
12
|
module Agent
|
13
|
-
# This class acts to encapsulate information about the currently executed
|
14
|
-
#
|
15
|
-
# in a standardized and normalized format which the Agent understands.
|
13
|
+
# This class acts to encapsulate information about the currently executed request, making it available to the Agent
|
14
|
+
# for the duration of the request in a standardized and normalized format which the Agent understands.
|
16
15
|
#
|
17
16
|
# @attr_reader timer [Contrast::Utils::Timer] when the context was created
|
18
17
|
# @attr_reader logging_hash [Hash] context used to log the request
|
@@ -26,8 +25,8 @@ module Contrast
|
|
26
25
|
# @attr_reader route [Contrast::Api::Dtm::RouteCoverage] the route, used for findings, of this request
|
27
26
|
# @attr_reader observed_route [Contrast::Api::Dtm::ObservedRoute] the route, used for coverage, of this request
|
28
27
|
class RequestContext
|
29
|
-
include Contrast::Components::
|
30
|
-
|
28
|
+
include Contrast::Components::Logger::InstanceMethods
|
29
|
+
include Contrast::Components::Scope::InstanceMethods
|
31
30
|
|
32
31
|
EMPTY_INPUT_ANALYSIS_PB = Contrast::Api::Settings::InputAnalysis.new
|
33
32
|
|
@@ -63,11 +62,11 @@ module Contrast
|
|
63
62
|
|
64
63
|
@sample = true
|
65
64
|
|
66
|
-
if ASSESS.enabled?
|
65
|
+
if ::Contrast::ASSESS.enabled?
|
67
66
|
@sample_request, @sample_response = Contrast::Utils::Assess::SamplingUtil.instance.sample?(@request)
|
68
67
|
end
|
69
68
|
|
70
|
-
@sample_response &&= ASSESS.scan_response?
|
69
|
+
@sample_response &&= ::Contrast::ASSESS.scan_response?
|
71
70
|
|
72
71
|
append_route_coverage(Contrast::Agent.framework_manager.get_route_dtm(@request))
|
73
72
|
end
|
@@ -85,9 +84,11 @@ module Contrast
|
|
85
84
|
@sample_response
|
86
85
|
end
|
87
86
|
|
88
|
-
# Convert the discovered route for this request to appropriate forms and
|
89
|
-
#
|
90
|
-
#
|
87
|
+
# Convert the discovered route for this request to appropriate forms and disseminate it to those locations
|
88
|
+
# where it is necessary for our route coverage and finding vulnerability discovery features to function.
|
89
|
+
#
|
90
|
+
# @param route [Contrast::Api::Dtm::RouteCoverage, nil] the route of the current request, as determined from the
|
91
|
+
# framework
|
91
92
|
def append_route_coverage route
|
92
93
|
return unless route
|
93
94
|
|
@@ -108,8 +109,8 @@ module Contrast
|
|
108
109
|
# Collect the results for the given rule with the given action
|
109
110
|
#
|
110
111
|
# @param rule [String] the id of the rule to which the results apply
|
111
|
-
# @param response_type [Symbol] the result of the response, matching a
|
112
|
-
#
|
112
|
+
# @param response_type [Symbol] the result of the response, matching a value of
|
113
|
+
# Contrast::Api::Dtm::AttackResult::ResponseType
|
113
114
|
# @return [Array<Contrast::Api::Dtm::AttackResult>]
|
114
115
|
def results_for rule, response_type = nil
|
115
116
|
if response_type.nil?
|
@@ -120,8 +121,8 @@ module Contrast
|
|
120
121
|
end
|
121
122
|
|
122
123
|
def service_extract_request
|
123
|
-
return false unless AGENT.enabled?
|
124
|
-
return false unless PROTECT.enabled?
|
124
|
+
return false unless ::Contrast::AGENT.enabled?
|
125
|
+
return false unless ::Contrast::PROTECT.enabled?
|
125
126
|
return false if @do_not_track
|
126
127
|
|
127
128
|
service_response = Contrast::Agent.messaging_queue.send_event_immediately(@activity.http_request)
|
@@ -130,8 +131,10 @@ module Contrast
|
|
130
131
|
handle_protect_state(service_response)
|
131
132
|
ia = service_response.input_analysis
|
132
133
|
if ia
|
133
|
-
logger.trace
|
134
|
-
|
134
|
+
if logger.trace?
|
135
|
+
logger.trace('Analysis from Contrast Service', evaluations: ia.results.length)
|
136
|
+
logger.trace('Results', input_analysis: ia.inspect)
|
137
|
+
end
|
135
138
|
@speedracer_input_analysis = ia
|
136
139
|
speedracer_input_analysis.request = request
|
137
140
|
else
|
@@ -145,10 +148,9 @@ module Contrast
|
|
145
148
|
false
|
146
149
|
end
|
147
150
|
|
148
|
-
# NOTE: this method is only used as a backstop if Speedracer sends Input Evaluations
|
149
|
-
#
|
150
|
-
#
|
151
|
-
# Speedracer for any attacks detected during prefilter.
|
151
|
+
# NOTE: this method is only used as a backstop if Speedracer sends Input Evaluations when the protect state
|
152
|
+
# indicates a security exception should be thrown. This method ensures that the attack reports are generated.
|
153
|
+
# Normally these should be generated on Speedracer for any attacks detected during prefilter.
|
152
154
|
#
|
153
155
|
# @param agent_settings [Contrast::Api::Settings::AgentSettings]
|
154
156
|
def handle_protect_state agent_settings
|
@@ -165,9 +167,8 @@ module Contrast
|
|
165
167
|
raise Contrast::SecurityException.new(nil, (state.security_message || 'Blocking suspicious behavior'))
|
166
168
|
end
|
167
169
|
|
168
|
-
# append anything we've learned to the request seen message
|
169
|
-
#
|
170
|
-
# been accumulated since the last request
|
170
|
+
# append anything we've learned to the request seen message this is the sum-total of all inventory information
|
171
|
+
# that has been accumulated since the last request
|
171
172
|
def extract_after rack_response
|
172
173
|
@response = Contrast::Agent::Response.new(rack_response)
|
173
174
|
activity.http_response = @response.dtm if @sample_response
|
@@ -185,14 +186,13 @@ module Contrast
|
|
185
186
|
|
186
187
|
def reset_activity
|
187
188
|
@activity = Contrast::Api::Dtm::Activity.new(http_request: request.dtm)
|
188
|
-
@server_activity = Contrast::Api::Dtm::ServerActivity.new
|
189
|
+
@server_activity = Contrast::Api::Dtm::ServerActivity.new
|
189
190
|
@observed_route = Contrast::Api::Dtm::ObservedRoute.new
|
190
191
|
end
|
191
192
|
|
192
193
|
private
|
193
194
|
|
194
|
-
# Generate attack results directly from any evaluations on the
|
195
|
-
# agent settings object.
|
195
|
+
# Generate attack results directly from any evaluations on the agent settings object.
|
196
196
|
#
|
197
197
|
# @param agent_settings [Contrast::Api::Settings::AgentSettings]
|
198
198
|
def build_attack_results agent_settings
|
@@ -201,15 +201,17 @@ module Contrast
|
|
201
201
|
attack_results_by_rule = {}
|
202
202
|
agent_settings.input_analysis.results.each do |ia_result|
|
203
203
|
rule_id = ia_result.rule_id
|
204
|
-
rule = PROTECT.rule(rule_id)
|
204
|
+
rule = ::Contrast::PROTECT.rule(rule_id)
|
205
205
|
next unless rule
|
206
206
|
|
207
|
-
logger.debug
|
207
|
+
if logger.debug?
|
208
|
+
logger.debug('Building attack result from Contrast Service input analysis result',
|
209
|
+
result: ia_result.inspect)
|
210
|
+
end
|
208
211
|
|
209
212
|
attack_result = if rule.mode == :BLOCK
|
210
|
-
# special case for rules (like reflected xss)
|
211
|
-
#
|
212
|
-
# mode but now are just block at perimeter
|
213
|
+
# special case for rules (like reflected xss) that used to have an infilter / block mode
|
214
|
+
# but now are just block at perimeter
|
213
215
|
rule.build_attack_with_match(self, ia_result, attack_results_by_rule[rule_id],
|
214
216
|
ia_result.value)
|
215
217
|
else
|
@@ -1,20 +1,22 @@
|
|
1
1
|
# Copyright (c) 2021 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/components/logger'
|
5
|
+
require 'contrast/components/scope'
|
6
|
+
|
4
7
|
module Contrast
|
5
8
|
module Agent
|
6
9
|
# This class is instantiated when we receive a request and the agent is enabled to process
|
7
10
|
# that request. It holds the ruleset that we perform filtering operations on (currently
|
8
11
|
# prefilter and postfilter).
|
9
12
|
class RequestHandler
|
10
|
-
include Contrast::Components::
|
11
|
-
access_component :agent, :logging, :scope
|
13
|
+
include Contrast::Components::Logger::InstanceMethods
|
12
14
|
|
13
15
|
attr_reader :ruleset, :context
|
14
16
|
|
15
17
|
def initialize context
|
16
18
|
@context = context
|
17
|
-
@ruleset = AGENT.ruleset
|
19
|
+
@ruleset = ::Contrast::AGENT.ruleset
|
18
20
|
end
|
19
21
|
|
20
22
|
def send_activity_messages
|
@@ -7,7 +7,7 @@ require 'timeout'
|
|
7
7
|
require 'contrast/utils/object_share'
|
8
8
|
require 'contrast/utils/string_utils'
|
9
9
|
require 'contrast/utils/hash_digest'
|
10
|
-
require 'contrast/components/
|
10
|
+
require 'contrast/components/logger'
|
11
11
|
|
12
12
|
module Contrast
|
13
13
|
module Agent
|
@@ -16,8 +16,7 @@ module Contrast
|
|
16
16
|
# data in a format that the Agent expects, caching those transformations in
|
17
17
|
# order to avoid repeatedly creating Strings & thrashing GC.
|
18
18
|
class Response
|
19
|
-
include Contrast::Components::
|
20
|
-
access_component :logging
|
19
|
+
include Contrast::Components::Logger::InstanceMethods
|
21
20
|
|
22
21
|
extend Forwardable
|
23
22
|
|
@@ -7,7 +7,8 @@ return unless RUBY_VERSION < '2.6.0' # TODO: RUBY-714 remove guard w/ EOL of 2.5
|
|
7
7
|
|
8
8
|
require 'contrast/agent/class_reopener'
|
9
9
|
require 'contrast/agent/patching/policy/patch_status'
|
10
|
-
require 'contrast/components/
|
10
|
+
require 'contrast/components/logger'
|
11
|
+
require 'contrast/components/scope'
|
11
12
|
require 'contrast/utils/ruby_ast_rewriter'
|
12
13
|
|
13
14
|
module Contrast
|
@@ -19,8 +20,8 @@ module Contrast
|
|
19
20
|
# @deprecated Changes to this class are discouraged as this approach is
|
20
21
|
# being phased out with support for those language versions.
|
21
22
|
class Rewriter
|
22
|
-
|
23
|
-
|
23
|
+
extend Contrast::Components::Logger::InstanceMethods
|
24
|
+
extend Contrast::Components::Scope::InstanceMethods
|
24
25
|
|
25
26
|
SELF_DEFINITION = 'def self.'
|
26
27
|
DEFINITION = 'def '
|
@@ -1,13 +1,14 @@
|
|
1
1
|
# Copyright (c) 2021 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/components/logger'
|
5
|
+
|
4
6
|
module Contrast
|
5
7
|
module Agent
|
6
8
|
# This class is responsible for holding our ruleset and performing filtering operations on all
|
7
9
|
# rules when asked by the middleware.
|
8
10
|
class RuleSet < Set
|
9
|
-
include Contrast::Components::
|
10
|
-
access_component :analysis, :logging
|
11
|
+
include Contrast::Components::Logger::InstanceMethods
|
11
12
|
|
12
13
|
# The filtering that needs to happen before the application gets access to the request object.
|
13
14
|
# The main action here is snapshotting the request as provided to the application from the
|
@@ -16,7 +17,7 @@ module Contrast
|
|
16
17
|
def prefilter
|
17
18
|
context = Contrast::Agent::REQUEST_TRACKER.current
|
18
19
|
# TODO: RUBY-801 We shouldn't be responsible for knowing what modes are enabled
|
19
|
-
return unless context&.analyze_request? || PROTECT.enabled?
|
20
|
+
return unless context&.analyze_request? || ::Contrast::PROTECT.enabled?
|
20
21
|
|
21
22
|
logger.trace_with_time('Running prefilter...') do
|
22
23
|
map { |rule| rule.prefilter(context) }
|
@@ -33,7 +34,7 @@ module Contrast
|
|
33
34
|
def postfilter
|
34
35
|
context = Contrast::Agent::REQUEST_TRACKER.current
|
35
36
|
# TODO: RUBY-801 We shouldn't be responsible for knowing what modes are enabled
|
36
|
-
return unless context&.analyze_response? || PROTECT.enabled?
|
37
|
+
return unless context&.analyze_response? || ::Contrast::PROTECT.enabled?
|
37
38
|
|
38
39
|
logger.trace_with_time('Running postfilter...') do
|
39
40
|
map { |rule| rule.postfilter(context) }
|
data/lib/contrast/agent/scope.rb
CHANGED
@@ -104,39 +104,51 @@ module Contrast
|
|
104
104
|
exit_split_scope!
|
105
105
|
end
|
106
106
|
|
107
|
-
#
|
108
|
-
#
|
109
|
-
# Prefer the static methods if you know what scope you need at the call site.
|
107
|
+
# Static methods to be used, the cases are defined by the usage from the above methods
|
108
|
+
# if more methods are added - please extend the case statements as they are no longed dynamic
|
110
109
|
def in_scope? name
|
111
|
-
|
112
|
-
|
113
|
-
|
110
|
+
case name
|
111
|
+
when :contrast
|
112
|
+
in_contrast_scope?
|
113
|
+
when :deserialization
|
114
|
+
in_deserialization_scope?
|
115
|
+
when :split
|
116
|
+
in_split_scope?
|
117
|
+
else
|
118
|
+
raise NoMethodError, "Scope '#{ name.inspect }' is not registered as a scope."
|
119
|
+
end
|
114
120
|
end
|
115
121
|
|
116
122
|
def enter_scope! name
|
117
|
-
|
118
|
-
|
119
|
-
|
123
|
+
case name
|
124
|
+
when :contrast
|
125
|
+
enter_contrast_scope!
|
126
|
+
when :deserialization
|
127
|
+
enter_deserialization_scope!
|
128
|
+
when :split
|
129
|
+
enter_split_scope!
|
130
|
+
else
|
131
|
+
raise NoMethodError, "Scope '#{ name.inspect }' is not registered as a scope."
|
132
|
+
end
|
120
133
|
end
|
121
134
|
|
122
135
|
def exit_scope! name
|
123
|
-
|
124
|
-
|
125
|
-
|
136
|
+
case name
|
137
|
+
when :contrast
|
138
|
+
exit_contrast_scope!
|
139
|
+
when :deserialization
|
140
|
+
exit_deserialization_scope!
|
141
|
+
when :split
|
142
|
+
exit_split_scope!
|
143
|
+
else
|
144
|
+
raise NoMethodError, "Scope '#{ name.inspect }' is not registered as a scope."
|
145
|
+
end
|
126
146
|
end
|
127
147
|
|
128
148
|
class << self
|
129
149
|
def valid_scope? scope_sym
|
130
150
|
Contrast::Agent::Scope::SCOPE_LIST.include? scope_sym
|
131
151
|
end
|
132
|
-
|
133
|
-
def ensure_valid_scope! scope_sym
|
134
|
-
unless valid_scope? scope_sym # rubocop:disable Style/GuardClause
|
135
|
-
with_contrast_scope do
|
136
|
-
raise NoMethodError, "Scope '#{ scope_sym.inspect }' is not registered as a scope."
|
137
|
-
end
|
138
|
-
end
|
139
|
-
end
|
140
152
|
end
|
141
153
|
end
|
142
154
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# Copyright (c) 2021 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/components/
|
4
|
+
require 'contrast/components/logger'
|
5
5
|
require 'contrast/agent/worker_thread'
|
6
6
|
|
7
7
|
module Contrast
|
@@ -9,8 +9,7 @@ module Contrast
|
|
9
9
|
# The ServiceHeartbeat functions to keep the Contrast Service alive and
|
10
10
|
# ensure that it maintains this Agent's ApplicationContext.
|
11
11
|
class ServiceHeartbeat < WorkerThread
|
12
|
-
include Contrast::Components::
|
13
|
-
access_component :logging
|
12
|
+
include Contrast::Components::Logger::InstanceMethods
|
14
13
|
|
15
14
|
# Spec recommends 30 seconds, we're going with 15.
|
16
15
|
REFRESH_INTERVAL_SEC = 15
|
@@ -1,8 +1,8 @@
|
|
1
1
|
# Copyright (c) 2021 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/components/
|
5
|
-
require 'contrast/
|
4
|
+
require 'contrast/components/logger'
|
5
|
+
require 'contrast/components/scope'
|
6
6
|
require 'contrast/api/decorators/application_update'
|
7
7
|
|
8
8
|
module Contrast
|
@@ -10,8 +10,9 @@ module Contrast
|
|
10
10
|
# this module handles one time static analysis tasks
|
11
11
|
class StaticAnalysis
|
12
12
|
include Singleton
|
13
|
-
include Contrast::Components::
|
14
|
-
|
13
|
+
include Contrast::Components::Logger::InstanceMethods
|
14
|
+
include Contrast::Components::Scope::InstanceMethods
|
15
|
+
|
15
16
|
class << self
|
16
17
|
# After the first request is complete, we do a one-time manual catchup to review and
|
17
18
|
# report the already-loaded gems.
|
@@ -23,11 +24,11 @@ module Contrast
|
|
23
24
|
end
|
24
25
|
|
25
26
|
def send_inventory_message
|
26
|
-
return unless INVENTORY.enabled?
|
27
|
+
return unless ::Contrast::INVENTORY.enabled?
|
27
28
|
|
28
29
|
app_update_msg = Contrast::Api::Dtm::ApplicationUpdate.build
|
29
30
|
|
30
|
-
Contrast::
|
31
|
+
Contrast::Agent::Inventory::DatabaseConfig.append_db_config(app_update_msg)
|
31
32
|
Contrast::Agent.messaging_queue.send_event_eventually(app_update_msg)
|
32
33
|
end
|
33
34
|
|
@@ -1,15 +1,13 @@
|
|
1
1
|
# Copyright (c) 2021 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/components/
|
4
|
+
require 'contrast/components/scope'
|
5
5
|
|
6
6
|
module Contrast
|
7
7
|
module Agent
|
8
8
|
# Threads used by Contrast. Any long running thread should be created and managed by our ThreadWatcher class.
|
9
9
|
class Thread < ::Thread
|
10
|
-
include Contrast::Components::
|
11
|
-
|
12
|
-
access_component :scope
|
10
|
+
include Contrast::Components::Scope::InstanceMethods
|
13
11
|
|
14
12
|
# Make our internal code run in Contrast scope.
|
15
13
|
def initialize *args
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# Copyright (c) 2021 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/components/
|
4
|
+
require 'contrast/components/logger'
|
5
5
|
require 'contrast/agent/service_heartbeat'
|
6
6
|
require 'contrast/api/communication/messaging_queue'
|
7
7
|
|
@@ -13,8 +13,7 @@ module Contrast
|
|
13
13
|
# @attr_reader heartbeat [Contrast::Agent::ServiceHeartbeat]
|
14
14
|
# @attr_reader messaging_queue [Contrast::Api::Communication::MessagingQueue]
|
15
15
|
class ThreadWatcher
|
16
|
-
include Contrast::Components::
|
17
|
-
access_component :agent, :logging
|
16
|
+
include Contrast::Components::Logger::InstanceMethods
|
18
17
|
|
19
18
|
attr_reader :heapdump_util, :heartbeat, :messaging_queue
|
20
19
|
|
@@ -26,7 +25,7 @@ module Contrast
|
|
26
25
|
end
|
27
26
|
|
28
27
|
def startup!
|
29
|
-
return unless AGENT.enabled?
|
28
|
+
return unless ::Contrast::AGENT.enabled?
|
30
29
|
|
31
30
|
unless heartbeat.running?
|
32
31
|
logger.debug('Attempting to start heartbeat thread')
|
@@ -1,15 +1,15 @@
|
|
1
1
|
# Copyright (c) 2021 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/components/
|
4
|
+
require 'contrast/components/logger'
|
5
|
+
require 'contrast/components/scope'
|
5
6
|
|
6
7
|
module Contrast
|
7
8
|
module Agent
|
8
9
|
# This module is used to apply instrumentation to classes as they are required
|
9
10
|
module TracePointHook
|
10
|
-
|
11
|
-
|
12
|
-
access_component :logging, :scope
|
11
|
+
extend Contrast::Components::Logger::InstanceMethods
|
12
|
+
extend Contrast::Components::Scope::InstanceMethods
|
13
13
|
|
14
14
|
class << self
|
15
15
|
def enable!
|
@@ -27,18 +27,31 @@ module Contrast
|
|
27
27
|
|
28
28
|
private
|
29
29
|
|
30
|
+
# Use the TracePoint from the :end event, meaning the completion of a definition of a Class or Module (or
|
31
|
+
# really the completion of that piece of a definition, as determined by an `end` statement since there could be
|
32
|
+
# definitions across multiple files) to carry out actions required on definition. This typically involves
|
33
|
+
# patching and usage analysis
|
34
|
+
#
|
35
|
+
# @param tracepoint_event [TracePoint] the TracePoint from the :end
|
30
36
|
def process tracepoint_event
|
31
37
|
with_contrast_scope do
|
32
|
-
|
33
|
-
|
38
|
+
# the Module or Class that was loaded during this event
|
34
39
|
loaded_module = tracepoint_event.self
|
40
|
+
# the file being loaded that contained this definition
|
35
41
|
path = tracepoint_event.path
|
36
42
|
return if path&.include?('contrast')
|
37
43
|
|
44
|
+
logger.trace('Received TracePoint end event', module: loaded_module, path: path)
|
45
|
+
|
46
|
+
Contrast::Agent.framework_manager.register_late_framework(loaded_module)
|
38
47
|
Contrast::Agent::Inventory::DependencyUsageAnalysis.instance.associate_file(path) if path
|
39
48
|
Contrast::Agent::Patching::Policy::Patcher.patch_specific_module(loaded_module)
|
40
|
-
|
49
|
+
if RUBY_VERSION < '2.6.0' # TODO: RUBY-714 remove guard w/ EOL of 2.5
|
50
|
+
Contrast::Agent::Assess::Policy::RewriterPatch.rewrite_interpolation(loaded_module)
|
51
|
+
end
|
41
52
|
Contrast::Agent::Assess::Policy::PolicyScanner.scan(tracepoint_event)
|
53
|
+
rescue StandardError => e
|
54
|
+
logger.error('Unable to complete TracePoint analysis', e, module: loaded_module, path: path)
|
42
55
|
end
|
43
56
|
end
|
44
57
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
# Copyright (c) 2021 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/components/
|
4
|
+
require 'contrast/components/logger'
|
5
5
|
require 'contrast/agent/worker_thread'
|
6
6
|
|
7
7
|
module Contrast
|
@@ -9,29 +9,31 @@ module Contrast
|
|
9
9
|
module Communication
|
10
10
|
# Top level gateway to messaging with speedracer
|
11
11
|
class MessagingQueue < Contrast::Agent::WorkerThread
|
12
|
-
include Contrast::Components::
|
13
|
-
access_component :agent, :analysis, :logging, :settings
|
12
|
+
include Contrast::Components::Logger::InstanceMethods
|
14
13
|
|
15
|
-
attr_reader :
|
14
|
+
attr_reader :speedracer
|
16
15
|
|
17
16
|
def initialize
|
18
|
-
@queue = Queue.new
|
19
17
|
@speedracer = Contrast::Api::Communication::Speedracer.new
|
20
18
|
super
|
21
19
|
end
|
22
20
|
|
23
21
|
# Use this to bypass the messaging queue and leave response processing to the caller
|
24
22
|
def send_event_immediately event
|
25
|
-
if AGENT.disabled?
|
23
|
+
if ::Contrast::AGENT.disabled?
|
26
24
|
logger.warn('Attempted to send event immediately with Agent disabled', caller: caller, event: event)
|
27
25
|
return
|
28
26
|
end
|
29
27
|
speedracer.return_response(event)
|
30
28
|
end
|
31
29
|
|
30
|
+
def queue
|
31
|
+
@_queue ||= Queue.new
|
32
|
+
end
|
33
|
+
|
32
34
|
# Use this to add a message to the queue and process the response internally
|
33
35
|
def send_event_eventually event
|
34
|
-
if AGENT.disabled?
|
36
|
+
if ::Contrast::AGENT.disabled?
|
35
37
|
logger.warn('Attempted to queue event with Agent disabled', caller: caller, event: event)
|
36
38
|
return
|
37
39
|
end
|
@@ -43,7 +45,6 @@ module Contrast
|
|
43
45
|
speedracer.ensure_startup!
|
44
46
|
return if running?
|
45
47
|
|
46
|
-
@queue ||= Queue.new
|
47
48
|
@_thread = Contrast::Agent::Thread.new do
|
48
49
|
loop do
|
49
50
|
event = queue.pop
|
@@ -59,13 +60,17 @@ module Contrast
|
|
59
60
|
logger.debug('Started background sending thread.')
|
60
61
|
end
|
61
62
|
|
63
|
+
def delete_queue!
|
64
|
+
@_queue&.clear
|
65
|
+
@_queue&.close
|
66
|
+
@_queue = nil
|
67
|
+
end
|
68
|
+
|
62
69
|
def stop!
|
63
70
|
return unless running?
|
64
71
|
|
65
72
|
super
|
66
|
-
|
67
|
-
@queue&.close
|
68
|
-
@queue = nil
|
73
|
+
delete_queue!
|
69
74
|
end
|
70
75
|
end
|
71
76
|
end
|