Class: Rack::Prevoty::ProtectInterceptor

Inherits:
Interceptor show all
Defined in:
lib/rack/protect_interceptor.rb

Instance Method Summary collapse

Methods inherited from Interceptor

build_result

Constructor Details

#initialize(app, opts) ⇒ ProtectInterceptor



8
9
10
# File 'lib/rack/protect_interceptor.rb', line 8

def initialize(app, opts)
  super(app, opts)
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
31
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rack/protect_interceptor.rb', line 12

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

  # passthru if not listed in paths
  return @app.call(env) if @paths.detect {|p| req.path.start_with?(p)}.nil?

  # passthru if blacklisted
  return @app.call(env) unless @blacklist.detect {|p| req.path.start_with?(p)}.nil?

  case req.request_method
  when "GET", "DELETE"
    unless env['QUERY_STRING'] === ''
      querystring = URI.unescape(env['QUERY_STRING'])
      begin
        Timeout::timeout(@timeout) do
          resp = @client.bulk_filter(querystring, @configuration_key)
          env['QUERY_STRING'] = URI.escape(resp.output)
          case @log_destination
          when 'log'
            ::Prevoty::LOGGER << self.class.build_result(@mode, req, querystring, resp).to_json if resp.statistics.javascript_attributes > 0 || resp.statistics.javascript_protocols > 0 || resp.statistics.javascript_tags > 0
          when 'callback'
            @callback.call(self.class.build_result(@mode, req, querystring, resp).to_json) if !@callback.nil? && (resp.statistics.javascript_attributes > 0 || resp.statistics.javascript_protocols > 0 || resp.statistics.javascript_tags > 0)
          end
        end
      rescue Exception => e
        env['QUERY_STRING'] = escape_query(CGI::parse(querystring))
        Rails.logger.warn e.message
      end
    end
  when "POST", "PUT"
    if req.media_type === 'multipart/form-data'
      # TODO: implement support for multipart. The Rack multipart
      # implementation doesn't support parsing and re-creating the
      # mutlipart data so a custom implementation needs to be written
    else
      body = URI.unescape(req.body.read.encode('utf-8'))
      unless body === ''
        begin
          Timeout::timeout(@timeout) do
            resp = @client.bulk_filter(body, @configuration_key)
            env['rack.input'] = StringIO.new(resp.output)
            case @log_destination
            when 'log'
              ::Prevoty::LOGGER << self.class.build_result(@mode, req, body, resp).to_json if resp.statistics.javascript_attributes > 0 || resp.statistics.javascript_protocols > 0 || resp.statistics.javascript_tags > 0
            when 'callback'
              @callback.call(self.class.build_result(@mode, req, body, resp).to_json) if !@callback.nil? && (resp.statistics.javascript_attributes > 0 || resp.statistics.javascript_protocols > 0 || resp.statistics.javascript_tags > 0)
            end
          end
        rescue Exception => e
          env['rack.input'] = StringIO.new(escape_query(CGI::parse(body)))
          Rails.logger.warn e.message
        end
      end
    end

    # clean any GET data passed
    unless env['QUERY_STRING'] === ''
      querystring = URI.unescape(env['QUERY_STRING'])
      begin
        Timeout::timeout(@timeout) do
          resp = @client.bulk_filter(querystring, @configuration_key)
          env['QUERY_STRING'] = URI.escape(resp.output)
          case @log_destination
          when 'log'
            ::Prevoty::LOGGER << self.class.build_result(@mode, req, querystring, resp).to_json if resp.statistics.javascript_attributes > 0 || resp.statistics.javascript_protocols > 0 || resp.statistics.javascript_tags > 0
          when 'callback'
            @callback.call(self.class.build_result(@mode, req, querystring, resp).to_json) if !@callback.nil? && (resp.statistics.javascript_attributes > 0 || resp.statistics.javascript_protocols > 0 || resp.statistics.javascript_tags > 0)
          end
        end
      rescue Exception => e
        env['QUERY_STRING'] = escape_query(CGI::parse(querystring))
        Rails.logger.warn e.message
      end
    end
  else Rails.logger.warn "unknown method #{req.request_method}"
  end

  @app.call(env)
end