Class: Rack::CorsGateOriginProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/classes/cors_gate_origin_processor.rb

Overview

CorsGateOriginProcessor allows:

  • referer header to be transformed to Origin header

  • removal of “Origin: null” (Chrome)

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ CorsGateOriginProcessor

Returns a new instance of CorsGateOriginProcessor.



7
8
9
10
# File 'lib/classes/cors_gate_origin_processor.rb', line 7

def initialize(app, opts = {})
  @app = app
  @remove_null_origin = opts[:remove_null_origin] || false
end

Instance Method Details

#call(env) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/classes/cors_gate_origin_processor.rb', line 12

def call(env)
  if @remove_null_origin
    # Consider Chrome's "null" origin the same as no origin being set at all

    env.delete('HTTP_ORIGIN') if env['HTTP_ORIGIN'] == 'null'
    env.delete('HTTP_X_ORIGIN') if env['HTTP_X_ORIGIN'] == 'null'
  end

  # Use referer header if no origin-header is present

  origin = env['HTTP_X_ORIGIN'] || env['HTTP_ORIGIN']
  referer = env['HTTP_REFERER']

  if origin.nil? && referer
    env['HTTP_ORIGIN'] = referer_to_origin(referer)
  end

  @app.call(env)
end