Class: Wafris::Middleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



5
6
7
8
9
# File 'lib/wafris/middleware.rb', line 5

def initialize(app)
  @app = app
  @notifier = ActiveSupport::Notifications if defined?(ActiveSupport::Notifications)
  ProxyFilter.set_filter
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/wafris/middleware.rb', line 11

def call(env)
  wafris_request = WafrisRequest.new(
    Rack::Request.new(env),
    env
  )

  treatment = Wafris.evaluate(wafris_request)

  @notifier&.instrument("#{treatment}.wafris", request: wafris_request, treatment: treatment)

  # These values match what the client tests expect (200, 404, 403, 500)
  if treatment == "Allowed" || treatment == "Passed"
    @app.call(env)
  elsif treatment == "Blocked"
    [403, {"content-type" => "text/plain"}, ["Blocked"]]
  else
    [500, {"content-type" => "text/plain"}, ["Error"]]
  end
rescue => e
  LogSuppressor.puts_log "[Wafris] Detailed Error: #{e.class} - #{e.message}"
  LogSuppressor.puts_log "[Wafris] Backtrace: #{e.backtrace.join("\n")}"
  @app.call(env)
end