Sha256: a84c4261c89f395a48702690b81f62a458b37fc3a751b9e46a1be03d7b7dfe08

Contents?: true

Size: 1.85 KB

Versions: 44

Compression:

Stored size: 1.85 KB

Contents

# Copyright (c) 2018 Sqreen. All Rights Reserved.
# Please refer to our terms for more information: https://www.sqreen.io/terms.html

require 'sqreen/rule_callback'
require 'sqreen/actions'
require 'sqreen/middleware'

module Sqreen
  module Rules
    # Runs actions concerned with whether the request ought to be served
    class RunReqStartActions < FrameworkCB
      PRIORITY = 95

      def initialize(framework)
        if defined?(Sqreen::Frameworks::SinatraFramework) &&
           framework.is_a?(Sqreen::Frameworks::SinatraFramework)
          super(Sinatra::ExtendedRack, :call)
        elsif defined?(Sqreen::Frameworks::RailsFramework) &&
              framework.is_a?(Sqreen::Frameworks::RailsFramework)
          super(Sqreen::RailsMiddleware, :call)
        else
          # last resort; we won't get nice errors
          super(Sqreen::Middleware, :call)
        end

        self.framework = framework
      end

      def whitelisted?
        whitelisted = SharedStorage.get(:whitelisted)
        return whitelisted unless whitelisted.nil?
        framework && !framework.whitelisted_match.nil?
      end

      def priority
        PRIORITY
      end

      def pre(_inst, _args, _budget = nil, &_block)
        return unless framework
        ip = framework.client_ip
        return unless ip

        actions = actions_repo.get(Sqreen::Actions::BlockIp, ip) +
                  actions_repo.get(Sqreen::Actions::RedirectIp, ip)

        actions.each do |act|
          res = run_client_ip_action(act, ip)
          return res unless res.nil?
        end
        nil
      end

      private

      # @param action [Sqreen::Actions::Base]
      def run_client_ip_action(action, client_ip)
        action.run client_ip
      end

      # @return [Sqreen::Actions::Repository]
      def actions_repo
        Sqreen::Actions::Repository.instance
      end
    end
  end
end

Version data entries

44 entries across 44 versions & 1 rubygems

Version Path
sqreen-1.18.1-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.18.1 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.18.0-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.18.0 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.17.2-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.17.2 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.17.2.rc1-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.17.2.rc1 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.17.2.beta4-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.17.2.beta4 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.17.2.beta3-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.17.2.beta3 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.17.2.beta2-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.17.2.beta2 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.17.2.beta1-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.17.2.beta1 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.17.0-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.17.0 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.16.2-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.16.2 lib/sqreen/rules_callbacks/run_req_start_actions.rb