Class: Rack::Protection::Base
Relationships & Source Files | |
Extension / Inclusion / Inheritance Descendants | |
Subclasses:
|
|
Inherits: | Object |
Defined in: | rack-protection/lib/rack/protection/base.rb |
Constant Summary
-
DEFAULT_OPTIONS =
# File 'rack-protection/lib/rack/protection/base.rb', line 12{ 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 text/xml application/xml] }
Class Method Summary
Instance Attribute Summary
Instance Method Summary
- #accepts?(env) ⇒ Boolean
- #call(env)
- #debug(env, message)
- #default_options
-
#default_reaction(env)
Alias for #deny.
- #deny(env) (also: #default_reaction, #react)
- #drop_session(env)
- #encrypt(value)
- #html?(headers) ⇒ Boolean
- #instrument(env)
- #origin(env)
- #random_string(secure = defined? SecureRandom))
- #react(env)
- #referrer(env)
- #report(env)
- #safe?(env) ⇒ Boolean
- #secure_compare(a, b)
- #session(env)
- #session?(env) ⇒ Boolean
- #warn(env, message)
Constructor Details
.new(app, options = {}) ⇒ Base
Class Method Details
.default_options(options)
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 23
def self. ( ) define_method(: ) { super().merge( ) } end
.default_reaction(reaction)
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 27
def self.default_reaction(reaction) alias_method(:default_reaction, reaction) end
Instance Attribute Details
#app (readonly)
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 21
attr_reader :app, :
#options (readonly)
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 21
attr_reader :app, :
Instance Method Details
#accepts?(env) ⇒ Boolean
# File 'rack-protection/lib/rack/protection/base.rb', line 44
def accepts?(env) raise NotImplementedError, "#{self.class} implementation pending" end
#call(env)
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 48
def call(env) unless accepts? env instrument env result = react env end result or app.call(env) end
#debug(env, message)
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 61
def debug(env, ) return unless [:logging] l = [:logger] || env['rack.logger'] || ::Logger.new(env['rack.errors']) l.debug( ) end
#default_options
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 31
def DEFAULT_OPTIONS end
#default_reaction(env)
Alias for #deny.
# File 'rack-protection/lib/rack/protection/base.rb', line 138
alias default_reaction deny
#deny(env) Also known as: #default_reaction, #react
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 82
def deny(env) warn env, "attack prevented by #{self.class}" [ [:status], { 'content-type' => 'text/plain' }, [ [: ]]] end
#drop_session(env)
[ GitHub ]#encrypt(value)
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 130
def encrypt(value) [:encryptor].hexdigest value.to_s end
#html?(headers) ⇒ Boolean
# File 'rack-protection/lib/rack/protection/base.rb', line 140
def html?(headers) return false unless (header = headers.detect { |k, _v| k.downcase == 'content-type' }) [:html_types].include? header.last[%r{^\w+/\w+}] end
#instrument(env)
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 75
def instrument(env) return unless (i = [:instrumenter]) env['rack.protection.attack'] = self.class.name.split('::').last.downcase i.instrument('rack.protection', env) end
#origin(env)
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 120
def origin(env) env['HTTP_ORIGIN'] || env['HTTP_X_ORIGIN'] end
#random_string(secure = defined? SecureRandom))
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 124
def random_string(secure = defined? SecureRandom) secure ? SecureRandom.hex(16) : '%032x' % rand((2**128) - 1) rescue NotImplementedError random_string false end
#react(env)
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 56
def react(env) result = send( [:reaction], env) result if (Array === result) && (result.size == 3) end
#referrer(env)
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 112
def referrer(env) ref = env['HTTP_REFERER'].to_s return if ! [:allow_empty_referrer] && ref.empty? URI.parse(ref).host || Request.new(env).host rescue URI::InvalidURIError end
#report(env)
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 87
def report(env) warn env, "attack reported by #{self.class}" env[ [:report_key]] = true end
#safe?(env) ⇒ Boolean
# File 'rack-protection/lib/rack/protection/base.rb', line 40
def safe?(env) %w[GET HEAD OPTIONS TRACE].include? env['REQUEST_METHOD'] end
#secure_compare(a, b)
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 134
def secure_compare(a, b) Rack::Utils.secure_compare(a.to_s, b.to_s) end
#session(env)
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 96
def session(env) return env[ [:session_key]] if session? env raise "you need to set up a session middleware *before* #{self.class}" end
#session?(env) ⇒ Boolean
# File 'rack-protection/lib/rack/protection/base.rb', line 92
def session?(env) env.include? [:session_key] end
#warn(env, message)
[ GitHub ]# File 'rack-protection/lib/rack/protection/base.rb', line 68
def warn(env, ) return unless [:logging] l = [:logger] || env['rack.logger'] || ::Logger.new(env['rack.errors']) l.warn( ) end