Class: TCellAgent::Instrumentation::Rails::Middleware::HeadersMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/tcell_agent/rails/middleware/headers_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ HeadersMiddleware

Returns a new instance of HeadersMiddleware.



11
12
13
# File 'lib/tcell_agent/rails/middleware/headers_middleware.rb', line 11

def initialize(app)
  @app = app
end

Instance Method Details

#_handle_appsensor_js_agent(request, response) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/tcell_agent/rails/middleware/headers_middleware.rb', line 79

def _handle_appsensor_js_agent(request, response)
  TCellAgent::Instrumentation.safe_block('Handling AppSensor, JS Agent, and DLP') do
    status_code, response_headers, response_body = response

    js_agent_handler, script_insert =
      TCellAgent::Instrumentation::Rails::JSAgent.get_handler_and_script_insert(request, response_headers)

    content_length = 0
    defer_appfw_due_to_streaming = false

    if TCellAgent::Utils::Rails.empty_content?(status_code, response_headers)
      content_length = 0

    elsif response_headers['Content-Length']
      content_length = response_headers['Content-Length'].to_i

      # when content length is present it can be inferred that the
      # response is not a streaming response, so js agent insertion
      # can be done up front
      response_body, content_length =
        TCellAgent::Instrumentation::Rails::JSAgent.insert_now(js_agent_handler,
                                                               script_insert,
                                                               response_body,
                                                               content_length)

      response_headers['Content-Length'] = content_length.to_s

    elsif response_body.is_a?(Rack::BodyProxy)
      response_body = TCellAgent::Instrumentation::Rails::TCellBodyProxy.new(
        response_body,
        js_agent_handler,
        script_insert,
        response_headers
      )
      defer_appfw_due_to_streaming = true
    end

    appfirewall_policy = TCellAgent.policy(TCellAgent::PolicyTypes::APPSENSOR)
    if appfirewall_policy.enabled
       = TCellAgent::MetaData.for_appfirewall(
        request, content_length, status_code, response_headers
      )
      if defer_appfw_due_to_streaming
        response_body. = 
      else
        appfirewall_policy.check_appfirewall_injections()
      end
    end

    return [status_code, response_headers, response_body]
  end

  response
end

#_handle_redirect(request, response) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/tcell_agent/rails/middleware/headers_middleware.rb', line 60

def _handle_redirect(request, response)
  TCellAgent::Instrumentation.safe_block('Handling Redirect Headers') do
    status, headers, active_response = response
    if headers.key?('Location')
      http_redirect_policy = TCellAgent.policy(TCellAgent::PolicyTypes::HTTPREDIRECT)
      tcell_context = request.env[TCellAgent::Instrumentation::TCELL_ID]
      from_domain = URI.parse(tcell_context.uri).host
      new_location = http_redirect_policy.check_redirect(
        headers['Location'], from_domain, status, tcell_context
      )

      # Enforcement
      headers['Location'] = new_location if new_location
    end
    response = [status, headers, active_response]
  end
  response
end

#_set_headers(request, response) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/tcell_agent/rails/middleware/headers_middleware.rb', line 35

def _set_headers(request, response)
  status, headers, active_response = response

  TCellAgent::Instrumentation.safe_block('Handling headers') do
    headers_policy = TCellAgent.policy(TCellAgent::PolicyTypes::HEADERS)
    policy_headers = headers_policy.get_headers(
      headers['Content-Type'],
      request.env[TCellAgent::Instrumentation::TCELL_ID]
    )
    policy_headers.each do |header_info|
      header_name = header_info['name']
      header_value = header_info['value']
      existing_header_value = headers[header_name]
      headers[header_name] = if existing_header_value
                               "#{existing_header_value}, #{header_value}"
                             else
                               header_value
                             end
    end
    response = [status, headers, active_response]
  end

  response
end

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/tcell_agent/rails/middleware/headers_middleware.rb', line 15

def call(env)
  request = Rack::Request.new(env)

  response = @app.call(env)

  if TCellAgent.configuration.should_intercept_requests?
    TCellAgent::Instrumentation.safe_block('Handling Request') do
      tcell_response = response
      unless request.env[TCellAgent::Instrumentation::TCELL_ID].patches_blocking_triggered
        tcell_response = _handle_appsensor_js_agent(request, tcell_response)
      end
      tcell_response = _handle_redirect(request, tcell_response)
      tcell_response = _set_headers(request, tcell_response)
      response = tcell_response
    end
  end

  response
end