require 'sqreen/rules/attrs' require 'sqreen/rules/rule_cb' module Sqreen module Rules class NotFoundCB < RuleCB IGNORED_EXTENSIONS = ['.css', '.gif', '.jpg', '.jpeg', '.png', '.svg', '.ico', '.webp', '.pdf', '.woff'].freeze def post(rv, _inst, args, _budget = nil, &_block) return if rv[0].to_i != 404 env = args[0] ua = env['HTTP_USER_AGENT'] script_name = env['SCRIPT_NAME'] path_info = env['PATH_INFO'] verb = env['REQUEST_METHOD'] host = env['SERVER_NAME'] override = env['action_dispatch.original_path'] exception = env['action_dispatch.exception'] record_from_env(ua, script_name, path_info, verb, override, host, exception) end def record_from_env(ua, script_name, path_info, verb, override, host, exception) path = path_from_variables(script_name, path_info, override) return if extension?(path, IGNORED_EXTENSIONS) if !override && exception && !exception.to_s.empty? record_from_exception({ 'ua' => ua, 'verb' => verb, 'host' => host, 'script_name' => script_name, 'path_info' => path_info }, exception.exception) end record_event({ 'path' => path, 'ua' => ua, 'verb' => verb, 'host' => host }) end def record_from_exception(payload, exception) message = exception.to_s if message && !message.empty? override = message =~ /No route matches\s+\[[a-z]+\]\s+"(.*)"/i && $1 end payload['path'] = path_from_variables(payload['script_name'], payload['path_info'], override) return if extension?(payload['path'], IGNORED_EXTENSIONS) record = payload.reject { |k, v| v.nil? || ['path_info', 'script_name'].include?(k) } payload.delete('path') # remove added claim record_event(record) end def path_from_variables(script_name, path_info, override) path = script_name if path.nil? path = override || path_info elsif override path += override elsif path_info path += path_info end path end def extension?(path, extensions) return false if path.nil? candidate = File.extname(path).downcase extensions.include?(candidate) end end end end