Class: Rack::Protection::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/protection/base.rb

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :reaction    => :default_reaction, :logging   => true,
  :message     => 'Forbidden',       :encryptor => Digest::SHA1,
  :session_key => 'rack.session',    :status    => 403,
  :allow_empty_referrer => true,
  :html_types           => %w[text/html application/xhtml]
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Base

Returns a new instance of Base.



31
32
33
# File 'lib/rack/protection/base.rb', line 31

def initialize(app, options = {})
  @app, @options = app, default_options.merge(options)
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



17
18
19
# File 'lib/rack/protection/base.rb', line 17

def app
  @app
end

#optionsObject (readonly)

Returns the value of attribute options.



17
18
19
# File 'lib/rack/protection/base.rb', line 17

def options
  @options
end

Class Method Details

.default_options(options) ⇒ Object



19
20
21
# File 'lib/rack/protection/base.rb', line 19

def self.default_options(options)
  define_method(:default_options) { super().merge(options) }
end

.default_reaction(reaction) ⇒ Object



23
24
25
# File 'lib/rack/protection/base.rb', line 23

def self.default_reaction(reaction)
  alias_method(:default_reaction, reaction)
end

Instance Method Details

#accepts?(env) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (NotImplementedError)


39
40
41
# File 'lib/rack/protection/base.rb', line 39

def accepts?(env)
  raise NotImplementedError, "#{self.class} implementation pending"
end

#call(env) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/rack/protection/base.rb', line 43

def call(env)
  unless accepts? env
    warn env, "attack prevented by #{self.class}"
    result = react env
  end
  result or app.call(env)
end

#default_optionsObject



27
28
29
# File 'lib/rack/protection/base.rb', line 27

def default_options
  DEFAULT_OPTIONS
end

#deny(env) ⇒ Object Also known as: default_reaction



62
63
64
# File 'lib/rack/protection/base.rb', line 62

def deny(env)
  [options[:status], {'Content-Type' => 'text/plain'}, [options[:message]]]
end

#drop_session(env) ⇒ Object



75
76
77
# File 'lib/rack/protection/base.rb', line 75

def drop_session(env)
  session(env).clear if session? env
end

#encrypt(value) ⇒ Object



95
96
97
# File 'lib/rack/protection/base.rb', line 95

def encrypt(value)
  options[:encryptor].hexdigest value.to_s
end

#html?(headers) ⇒ Boolean

Returns:

  • (Boolean)


101
102
103
104
# File 'lib/rack/protection/base.rb', line 101

def html?(headers)
  return false unless header = headers.detect { |k,v| k.downcase == 'content-type' }
  options[:html_types].include? header.last[/^\w+\/\w+/]
end

#origin(env) ⇒ Object



85
86
87
# File 'lib/rack/protection/base.rb', line 85

def origin(env)
  env['HTTP_ORIGIN'] || env['HTTP_X_ORIGIN']
end

#random_string(secure = defined? SecureRandom)) ⇒ Object



89
90
91
92
93
# File 'lib/rack/protection/base.rb', line 89

def random_string(secure = defined? SecureRandom)
  secure ? SecureRandom.hex(32) : "%032x" % rand(2**128-1)
rescue NotImplementedError
  random_string false
end

#react(env) ⇒ Object



51
52
53
54
# File 'lib/rack/protection/base.rb', line 51

def react(env)
  result = send(options[:reaction], env)
  result if Array === result and result.size == 3
end

#referrer(env) ⇒ Object



79
80
81
82
83
# File 'lib/rack/protection/base.rb', line 79

def referrer(env)
  ref = env['HTTP_REFERER'].to_s
  return if !options[:allow_empty_referrer] and ref.empty?
  URI.parse(ref).host || Request.new(env).host
end

#safe?(env) ⇒ Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/rack/protection/base.rb', line 35

def safe?(env)
  %w[GET HEAD OPTIONS TRACE].include? env['REQUEST_METHOD']
end

#session(env) ⇒ Object



70
71
72
73
# File 'lib/rack/protection/base.rb', line 70

def session(env)
  return env[options[:session_key]] if session? env
  fail "you need to set up a session middleware *before* #{self.class}"
end

#session?(env) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/rack/protection/base.rb', line 66

def session?(env)
  env.include? options[:session_key]
end

#warn(env, message) ⇒ Object



56
57
58
59
60
# File 'lib/rack/protection/base.rb', line 56

def warn(env, message)
  return unless options[:logging]
  l = options[:logger] || env['rack.logger'] || ::Logger.new(env['rack.errors'])
  l.warn(message)
end