Class: Relintio::Middleware

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

Instance Method Summary collapse

Constructor Details

#initialize(app, agent) ⇒ Middleware

Returns a new instance of Middleware.



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

def initialize(app, agent)
  @app = app
  @agent = agent
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
# File 'lib/relintio/middleware.rb', line 10

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

  # Skip security check for internal challenge routes
  if request.path_info == "/_relintio/challenge" || request.path_info == "/_relintio/verify"
    return @app.call(env)
  end

  res = @agent.check_request(request)
  @agent.send_telemetry(request, res)

  if res[:action] == "block"
    return [403, { 'Content-Type' => 'text/html' }, [
      `<!DOCTYPE html><html><head><title>Access Denied</title><style>body{background:#000;color:#fff;font-family:sans-serif;padding:50px;text-align:center;}</style></head><body><h1>403 Forbidden</h1><p>Request blocked by Relintio WAF protection.</p></body></html>`
    ]]
  end

  if res[:action] == "challenge"
    challenge_url = "/_relintio/challenge?ref=#{URI.encode_www_form_component(request.path_info)}"
    return [403, {
      'Content-Type' => 'text/html',
      'X-Relintio-Action' => 'challenge',
      'X-Relintio-Challenge-URL' => challenge_url
    }, [
      `<!DOCTYPE html><html><head><title>Security Challenge</title></head><body><script>window.location.href="#{challenge_url}";</script></body></html>`
    ]]
  end

  @app.call(env)
end