Class: Contrast::Agent::RequestContext

Inherits:
Object
  • Object
show all
Includes:
RequestContextExtend, Components::Logger::InstanceMethods, Components::Scope::InstanceMethods, Utils::RequestUtils
Defined in:
lib/contrast/agent/request/request_context.rb

Overview

This class acts to encapsulate information about the currently executed request, making it available to the Agent for the duration of the request in a standardized and normalized format which the Agent understands.

Constant Summary collapse

INPUT_ANALYSIS =
Contrast::Agent::Reporting::InputAnalysis.new

Constants included from Utils::CEFLogUtils

Utils::CEFLogUtils::AGENT_VERSION, Utils::CEFLogUtils::DATE_TIME_FORMAT, Utils::CEFLogUtils::DEFAULT_CEF_NAME, Utils::CEFLogUtils::DEFAULT_LEVEL, Utils::CEFLogUtils::DEFAULT_METADATA, Utils::CEFLogUtils::EVENT_TYPE, Utils::CEFLogUtils::PROGNAME, Utils::CEFLogUtils::VALID_LEVELS

Constants included from Utils::LogUtils

Utils::LogUtils::DATE_TIME_FORMAT, Utils::LogUtils::DEFAULT_LEVEL, Utils::LogUtils::DEFAULT_NAME, Utils::LogUtils::PROGNAME, Utils::LogUtils::STDERR_STR, Utils::LogUtils::STDOUT_STR, Utils::LogUtils::VALID_LEVELS

Constants included from Utils::RequestUtils

Utils::RequestUtils::END_PATTERN, Utils::RequestUtils::HASH_PATTERN, Utils::RequestUtils::ID_, Utils::RequestUtils::MEDIA_TYPE_MARKERS, Utils::RequestUtils::NUM_, Utils::RequestUtils::NUM_PATTERN, Utils::RequestUtils::STATIC_SUFFIXES, Utils::RequestUtils::UUID_PATTERN, Utils::RequestUtils::WIN_PATTERN

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from RequestContextExtend

#append_to_observed_route, #extract_after, #protect_input_analysis, #protect_postfilter_ia

Methods included from Components::Logger::InstanceMethods

#cef_logger, #logger

Methods included from Utils::LogUtils

#write_permission?

Methods included from Utils::RequestUtils

#normalize_params, #read_body, #traverse_parsed_multipart

Methods included from Components::Scope::InstanceMethods

#contrast_enter_method_scopes!, #contrast_exit_method_scopes!, #with_app_scope, #with_contrast_scope, #with_deserialization_scope, #with_split_scope

Constructor Details

#initialize(rack_request, app_loaded: true) ⇒ RequestContext

Returns a new instance of RequestContext.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/contrast/agent/request/request_context.rb', line 50

def initialize rack_request, app_loaded: true
  with_contrast_scope do
    # all requests get a timer and hash
    @timer = Contrast::Utils::Timer.new
    @logging_hash = { request_id: __id__ }

    # instantiate helper for request and response
    @request = Contrast::Agent::Request.new(rack_request) if rack_request
    @activity = Contrast::Agent::Reporting::ApplicationActivity.new

    # build analyzer
    @do_not_track = false
    @agent_input_analysis = INPUT_ANALYSIS
    agent_input_analysis.request = request

    # flag to indicate whether the app is fully loaded
    @app_loaded = !!app_loaded

    # generic holder for properties that can be set throughout this request
    @_properties = {}

    # count of propagation events
    @propagation_event_count = 0

    # count of source events
    @source_event_count = 0

    if ::Contrast::ASSESS.enabled?
      @sample_req, @sample_res = Contrast::Utils::Assess::SamplingUtil.instance.sample?(@request)
    end

    @reported_findings = []

    handle_routes
  end
end

Instance Attribute Details

#activityContrast::Agent::Reporting:ApplicationActivity (readonly)

Returns the application activity found in this request.

Returns:

  • (Contrast::Agent::Reporting:ApplicationActivity)

    the application activity found in this request



29
30
31
# File 'lib/contrast/agent/request/request_context.rb', line 29

def activity
  @activity
end

#agent_input_analysisContrast::Agent::Reporting::InputAnalysis (readonly)



42
43
44
# File 'lib/contrast/agent/request/request_context.rb', line 42

def agent_input_analysis
  @agent_input_analysis
end

#discovered_routeContrast::Agent::Reporting::RouteDiscovery (readonly)

Returns the route, used for findings, of this request.

Returns:



40
41
42
# File 'lib/contrast/agent/request/request_context.rb', line 40

def discovered_route
  @discovered_route
end

#logging_hashHash (readonly)

Returns context used to log the request.

Returns:

  • (Hash)

    context used to log the request



31
32
33
# File 'lib/contrast/agent/request/request_context.rb', line 31

def logging_hash
  @logging_hash
end

#observed_routeContrast::Agent::Reporting::ObservedRoute (readonly)

Returns the route, used for coverage, of this request.

Returns:



33
34
35
# File 'lib/contrast/agent/request/request_context.rb', line 33

def observed_route
  @observed_route
end

#propagation_event_countObject

Returns the value of attribute propagation_event_count.



48
49
50
# File 'lib/contrast/agent/request/request_context.rb', line 48

def propagation_event_count
  @propagation_event_count
end

#reported_findingsArray<String> (readonly)

Returns the hash of findings already reported fro this request.

Returns:

  • (Array<String>)

    the hash of findings already reported fro this request



44
45
46
# File 'lib/contrast/agent/request/request_context.rb', line 44

def reported_findings
  @reported_findings
end

#requestContrast::Agent::Request (readonly)

Returns our wrapper around the Rack::Request for this context.

Returns:



35
36
37
# File 'lib/contrast/agent/request/request_context.rb', line 35

def request
  @request
end

#responseContrast::Agent::Response (readonly)

Returns our wrapper around the Rack::Response or Array for this context, only available after the application has finished its processing.

Returns:

  • (Contrast::Agent::Response)

    our wrapper around the Rack::Response or Array for this context, only available after the application has finished its processing



38
39
40
# File 'lib/contrast/agent/request/request_context.rb', line 38

def response
  @response
end

#source_event_countObject

Returns the value of attribute source_event_count.



48
49
50
# File 'lib/contrast/agent/request/request_context.rb', line 48

def source_event_count
  @source_event_count
end

#timerContrast::Utils::Timer (readonly)

Returns when the context was created.

Returns:



46
47
48
# File 'lib/contrast/agent/request/request_context.rb', line 46

def timer
  @timer
end

Instance Method Details

#add_property(key, value) ⇒ Object



119
120
121
# File 'lib/contrast/agent/request/request_context.rb', line 119

def add_property key, value
  @_properties[key] = value
end

#analyze_req_res_assess?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/contrast/agent/request/request_context.rb', line 115

def analyze_req_res_assess?
  ::Contrast::ASSESS.enabled?
end

#analyze_req_res_protect?Boolean

Returns:

  • (Boolean)


99
100
101
# File 'lib/contrast/agent/request/request_context.rb', line 99

def analyze_req_res_protect?
  ::Contrast::PROTECT.enabled?
end

#analyze_request?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/contrast/agent/request/request_context.rb', line 91

def analyze_request?
  analyze_request_assess? || analyze_req_res_protect?
end

#analyze_request_assess?Boolean

Returns:

  • (Boolean)


103
104
105
106
107
# File 'lib/contrast/agent/request/request_context.rb', line 103

def analyze_request_assess?
  return false unless analyze_req_res_assess?

  @sample_req
end

#analyze_response?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/contrast/agent/request/request_context.rb', line 95

def analyze_response?
  analyze_response_assess? || analyze_req_res_protect?
end

#analyze_response_assess?Boolean

Returns:

  • (Boolean)


109
110
111
112
113
# File 'lib/contrast/agent/request/request_context.rb', line 109

def analyze_response_assess?
  return false unless analyze_req_res_assess?

  @sample_res &&= ::Contrast::ASSESS.scan_response?
end

#app_loaded?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/contrast/agent/request/request_context.rb', line 87

def app_loaded?
  @app_loaded
end

#get_property(key) ⇒ Object



123
124
125
# File 'lib/contrast/agent/request/request_context.rb', line 123

def get_property key
  @_properties[key]
end

#reset_activityObject



127
128
129
130
# File 'lib/contrast/agent/request/request_context.rb', line 127

def reset_activity
  @activity = Contrast::Agent::Reporting::ApplicationActivity.new
  @observed_route = Contrast::Agent::Reporting::ObservedRoute.new
end