Class: FunApi::Middleware::TrustedHost

Inherits:
Object
  • Object
show all
Defined in:
lib/funapi/middleware/trusted_host.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, **options) ⇒ TrustedHost

Returns a new instance of TrustedHost.



4
5
6
7
# File 'lib/funapi/middleware/trusted_host.rb', line 4

def initialize(app, **options)
  @app = app
  @allowed_hosts = Array(options[:allowed_hosts] || [])
end

Instance Method Details

#call(env) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/funapi/middleware/trusted_host.rb', line 9

def call(env)
  host = env["HTTP_HOST"]&.split(":")&.first

  unless host_allowed?(host)
    return [
      400,
      {"content-type" => "application/json"},
      [JSON.dump(detail: "Invalid host header")]
    ]
  end

  @app.call(env)
end