Class: QueryGuard::Middleware

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

Defined Under Namespace

Classes: BodyProxy

Instance Method Summary collapse

Constructor Details

#initialize(app, config) ⇒ Middleware

Returns a new instance of Middleware.



27
28
29
30
# File 'lib/query_guard/middleware.rb', line 27

def initialize(app, config)
  @app = app
  @config = config
end

Instance Method Details

#call(env) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/query_guard/middleware.rb', line 32

def call(env)
  return @app.call(env) unless @config.enabled?(rails_env)

  # Initialize new Context for this request
  context = Core::Context.new
  Thread.current[:query_guard_context] = context
  
  # Legacy: Also initialize old stats hash for backward compatibility
  Thread.current[:query_guard_stats] = { 
    count: 0, 
    total_duration_ms: 0.0, 
    violations: [],
    response_bytes: 0 
  }

  status, headers, body = @app.call(env)
  
  # Wrap body to track response bytes
  proxied_body = BodyProxy.new(body, Thread.current[:query_guard_stats])
  
  # Run all registered analyzers
  findings = @config.analyzer_registry.analyze(context, @config)
  
  # Check and report findings
  check_and_report!(context, findings)
  
  [status, headers, proxied_body]
ensure
  Thread.current[:query_guard_context] = nil
  Thread.current[:query_guard_stats] = nil
end