Class: Profiler::Middleware::CorsMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/profiler/middleware/cors_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ CorsMiddleware

Returns a new instance of CorsMiddleware.



6
7
8
# File 'lib/profiler/middleware/cors_middleware.rb', line 6

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/profiler/middleware/cors_middleware.rb', line 10

def call(env)
  # Apply CORS and frame options to all profiler endpoints
  if env['PATH_INFO'].start_with?('/_profiler/')
    # Handle OPTIONS preflight request
    if env['REQUEST_METHOD'] == 'OPTIONS'
      return [
        200,
        cors_headers(env),
        ['']
      ]
    end

    status, headers, body = @app.call(env)

    # Add CORS headers
    cors_headers(env).each do |key, value|
      headers[key] = value
    end

    # Allow embedding in iframes from Chrome extensions
    headers.delete('X-Frame-Options')
    # Don't set CSP if controller requested to skip it
    unless env['profiler.skip_csp']
      # Allow embedding from standard web origins
      headers['Content-Security-Policy'] = "frame-ancestors 'self' http: https:"
    end

    [status, headers, body]
  else
    @app.call(env)
  end
end