Class: Rack::Protection::Base
- Inherits:
-
Object
- Object
- Rack::Protection::Base
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,
:report_key => "protection.failed",
: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.
32
33
34
|
# File 'lib/rack/protection/base.rb', line 32
def initialize(app, options = {})
@app, @options = app, default_options.merge(options)
end
|
Instance Attribute Details
#app ⇒ Object
Returns the value of attribute app.
18
19
20
|
# File 'lib/rack/protection/base.rb', line 18
def app
@app
end
|
#options ⇒ Object
Returns the value of attribute options.
18
19
20
|
# File 'lib/rack/protection/base.rb', line 18
def options
@options
end
|
Class Method Details
.default_options(options) ⇒ Object
20
21
22
|
# File 'lib/rack/protection/base.rb', line 20
def self.default_options(options)
define_method(:default_options) { super().merge(options) }
end
|
.default_reaction(reaction) ⇒ Object
24
25
26
|
# File 'lib/rack/protection/base.rb', line 24
def self.default_reaction(reaction)
alias_method(:default_reaction, reaction)
end
|
Instance Method Details
#accepts?(env) ⇒ Boolean
40
41
42
|
# File 'lib/rack/protection/base.rb', line 40
def accepts?(env)
raise NotImplementedError, "#{self.class} implementation pending"
end
|
#bytesize(string) ⇒ Object
128
129
130
|
# File 'lib/rack/protection/base.rb', line 128
def bytesize(string)
string.bytesize
end
|
#call(env) ⇒ Object
44
45
46
47
48
49
50
|
# File 'lib/rack/protection/base.rb', line 44
def call(env)
unless accepts? env
instrument env
result = react env
end
result or app.call(env)
end
|
#default_options ⇒ Object
28
29
30
|
# File 'lib/rack/protection/base.rb', line 28
def default_options
DEFAULT_OPTIONS
end
|
#deny(env) ⇒ Object
Also known as:
default_reaction
69
70
71
72
|
# File 'lib/rack/protection/base.rb', line 69
def deny(env)
warn env, "attack prevented by #{self.class}"
[options[:status], {'Content-Type' => 'text/plain'}, [options[:message]]]
end
|
#drop_session(env) ⇒ Object
88
89
90
|
# File 'lib/rack/protection/base.rb', line 88
def drop_session(env)
session(env).clear if session? env
end
|
#encrypt(value) ⇒ Object
109
110
111
|
# File 'lib/rack/protection/base.rb', line 109
def encrypt(value)
options[:encryptor].hexdigest value.to_s
end
|
#html?(headers) ⇒ Boolean
139
140
141
142
|
# File 'lib/rack/protection/base.rb', line 139
def html?()
return false unless = .detect { |k,v| k.downcase == 'content-type' }
options[:html_types].include? .last[/^\w+\/\w+/]
end
|
#instrument(env) ⇒ Object
63
64
65
66
67
|
# File 'lib/rack/protection/base.rb', line 63
def instrument(env)
return unless i = options[:instrumenter]
env['rack.protection.attack'] = self.class.name.split('::').last.downcase
i.instrument('rack.protection', env)
end
|
#origin(env) ⇒ Object
99
100
101
|
# File 'lib/rack/protection/base.rb', line 99
def origin(env)
env['HTTP_ORIGIN'] || env['HTTP_X_ORIGIN']
end
|
#random_string(secure = defined? SecureRandom)) ⇒ Object
103
104
105
106
107
|
# File 'lib/rack/protection/base.rb', line 103
def random_string(secure = defined? SecureRandom)
secure ? SecureRandom.hex(16) : "%032x" % rand(2**128-1)
rescue NotImplementedError
random_string false
end
|
#react(env) ⇒ Object
52
53
54
55
|
# File 'lib/rack/protection/base.rb', line 52
def react(env)
result = send(options[:reaction], env)
result if Array === result and result.size == 3
end
|
#referrer(env) ⇒ Object
92
93
94
95
96
97
|
# File 'lib/rack/protection/base.rb', line 92
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
rescue URI::InvalidURIError
end
|
#report(env) ⇒ Object
74
75
76
77
|
# File 'lib/rack/protection/base.rb', line 74
def report(env)
warn env, "attack reported by #{self.class}"
env[options[:report_key]] = true
end
|
#safe?(env) ⇒ Boolean
36
37
38
|
# File 'lib/rack/protection/base.rb', line 36
def safe?(env)
%w[GET HEAD OPTIONS TRACE].include? env['REQUEST_METHOD']
end
|
#secure_compare(a, b) ⇒ Object
The implementations of secure_compare and bytesize are taken from Rack::Utils to be able to support rack older than XXXX.
115
116
117
118
119
120
121
122
123
|
# File 'lib/rack/protection/base.rb', line 115
def secure_compare(a, b)
return false unless bytesize(a) == bytesize(b)
l = a.unpack("C*")
r, i = 0, -1
b.each_byte { |v| r |= v ^ l[i+=1] }
r == 0
end
|
#session(env) ⇒ Object
83
84
85
86
|
# File 'lib/rack/protection/base.rb', line 83
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
79
80
81
|
# File 'lib/rack/protection/base.rb', line 79
def session?(env)
env.include? options[:session_key]
end
|
#warn(env, message) ⇒ Object
57
58
59
60
61
|
# File 'lib/rack/protection/base.rb', line 57
def warn(env, message)
return unless options[:logging]
l = options[:logger] || env['rack.logger'] || ::Logger.new(env['rack.errors'])
l.warn(message)
end
|