Class: Rack::Prevoty::ContentMiddleware
- Inherits:
-
Object
- Object
- Rack::Prevoty::ContentMiddleware
- Defined in:
- lib/rack/content_middleware.rb
Class Method Summary collapse
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, opts) ⇒ ContentMiddleware
constructor
A new instance of ContentMiddleware.
Constructor Details
#initialize(app, opts) ⇒ ContentMiddleware
Returns a new instance of ContentMiddleware.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rack/content_middleware.rb', line 9 def initialize(app, opts) @app = app @base = opts[:api_base] ||= 'https://api.prevoty.com' @client = ::Prevoty::Client.new(opts[:api_key], @base) @policy_key = opts[:policy_key] ||= '' @mode = opts[:mode] ||= 'monitor' @log_verbosity = opts[:log_verbosity] ||= 'incident' @paths = opts[:paths] ||= ['/'] @blacklist = opts[:blacklist] ||= [] @traffic_percentage = opts[:traffic_percentage] ||= 100 if @mode === 'monitor' @monitor = ::Prevoty::ContentMonitor.new(@client, opts) end end |
Class Method Details
.build_result(mode, request, input, result) ⇒ Object
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/rack/content_middleware.rb', line 109 def self.build_result(mode, request, input, result) data = { product: 'content', mode: mode, version: '1', input: input, timestamp: Time.now.utc.strftime('%b %d %Y %H:%M:%S %Z'), request_url: request.path, session_id: request.session["session_id"], cookies: request., http_method: request.request_method, src_ip: request.ip, dest_host: request.host, dest_port: request.port } # these are hacks due to differences between protect and monitor if mode === 'protect' data[:statistics] = result.statistics data[:output] = CGI::unescape(result.output) else data[:statistics] = result end ::Prevoty::ContentPayload.new(data) end |
Instance Method Details
#call(env) ⇒ Object
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/rack/content_middleware.rb', line 24 def call(env) req = Rack::Request.new(env) # Thread local storage for communicating between content and query Thread.current[:request_storage] = {} # only wrapping this in a begin so we can use ensure to clean out # thread storage begin if rand(100) > @traffic_percentage Thread.current[:request_storage][:skip_processing] = true return @app.call(env) end # 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? # 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 return @app.call(env) if req.media_type === 'multipart/form-data' unless env['QUERY_STRING'].empty? querystring = env['QUERY_STRING'] if @mode === 'protect' begin Timeout::timeout(@timeout) do resp = nil if defined? ActiveSupport::Notifications ActiveSupport::Notifications.instrument('prevoty:content:protect') do |payload| resp = payload[:response] = @client.bulk_filter(querystring, @policy_key) end end env['QUERY_STRING'] = resp.output result = self.class.build_result(@mode, req, querystring, resp) if resp.statistics.is_significant? || @log_verbosity === 'all' ::Prevoty::LOGGER << result.to_json + "\n" end end rescue Exception => e env['QUERY_STRING'] = escape_query(CGI::parse(env['QUERY_STRING'])) Rails.logger.warn e. end else @monitor.process({mode: @mode, input: env['QUERY_STRING'], request: req}) end end if ['POST', 'PUT', 'PATCH'].member?(req.request_method) body = URI.unescape(req.body.read.encode('utf-8')) unless body.empty? if @mode === 'protect' begin resp = nil Timeout::timeout(@timeout) do if defined? ActiveSupport::Notifications ActiveSupport::Notifications.instrument('prevoty:content:protect') do |payload| resp = payload[:response] = @client.bulk_filter(body, @policy_key) end end env['rack.input'] = StringIO.new(resp.output) result = self.class.build_result(@mode, req, body, resp) if resp.statistics.is_significant? || @log_verbosity === 'all' ::Prevoty::LOGGER << result.to_json + "\n" end end rescue Exception => e env['rack.input'] = StringIO.new(escape_query(CGI::parse(body))) Rails.logger.warn e. end else @monitor.process({mode: @mode, input: body, request: req}) end end end @app.call(env) ensure Thread.current[:request_storage] = {} end end |