Sha256: 67bafb280eee02041fac1aa96c3923414680ff7809bd9ea8762f407e726865bd

Contents?: true

Size: 1.76 KB

Versions: 27

Compression:

Stored size: 1.76 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
      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 pre(_inst, _args, _budget = nil, &_block)
        return unless framework
        ip = framework.client_ip
        return unless ip

        actions = actions_repo[Sqreen::Actions::BlockIp] +
                  actions_repo[Sqreen::Actions::RedirectIp]

        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

27 entries across 27 versions & 2 rubygems

Version Path
sqreen-1.15.0-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.15.0 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.14.2-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.14.2 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.14.1-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.14.1 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.14.0-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.14.0 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.14.0.beta3-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.14.0.beta3 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.14.0.beta2 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.14.0.beta1 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.13.4-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-alt-1.13.4 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.13.4 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.13.2-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-alt-1.13.2 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.13.2 lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-1.13.1-java lib/sqreen/rules_callbacks/run_req_start_actions.rb
sqreen-alt-1.13.1 lib/sqreen/rules_callbacks/run_req_start_actions.rb