Class: Rack::Protection::Base

### Home Classes Methods

### In Files

- lib/rack/protection/base.rb

### Parent

Object

### Methods

- ::default_options
- ::default_reaction
- ::new
- #accepts?
- #call
- #default_options
- #default_reaction
- #deny
- #drop_session
- #encrypt
- #html?
- #origin
- #random_string
- #react
- #referrer
- #report
- #safe?
- #session
- #session?
- #warn

### Class/Module Index

Quicksearch

- Rack
- Rack::Protection
- Rack::Protection::AuthenticityToken
- Rack::Protection::Base
- Rack::Protection::EscapedParams
- Rack::Protection::FormToken
- Rack::Protection::FrameOptions
- Rack::Protection::HttpOrigin
- Rack::Protection::IPSpoofing
- Rack::Protection::JsonCsrf
- Rack::Protection::PathTraversal
- Rack::Protection::RemoteReferrer
- Rack::Protection::RemoteToken
- Rack::Protection::SessionHijacking
- Rack::Protection::VERSION
- Rack::Protection::XSSHeader

No matching classes.

# Rack::Protection::Base

### Constants

DEFAULT_OPTIONS

### Attributes

app[R]
options[R]

### Public Class Methods

default_options(options) click to toggle source

    # File lib/rack/protection/base.rb, line 20
    def self.default_options(options)
      define_method(:default_options) { super().merge(options) }
    end

default_reaction(reaction) click to toggle source

    # File lib/rack/protection/base.rb, line 24
    def self.default_reaction(reaction)
      alias_method(:default_reaction, reaction)
    end

new(app, options = {}) click to toggle source

    # File lib/rack/protection/base.rb, line 32
    def initialize(app, options = {})
      @app, @options = app, default_options.merge(options)
    end

### Public Instance Methods

accepts?(env) click to toggle source

    # File lib/rack/protection/base.rb, line 40
    def accepts?(env)
      raise NotImplementedError, "#{self.class} implementation pending"
    end

call(env) click to toggle source

    # File lib/rack/protection/base.rb, line 44
    def call(env)
      unless accepts? env
        warn env, "attack prevented by #{self.class}"
        result = react env
      end
      result or app.call(env)
    end

default_options() click to toggle source

    # File lib/rack/protection/base.rb, line 28
    def default_options
      DEFAULT_OPTIONS
    end

default_reaction(env) click to toggle source
Alias for: deny
deny(env) click to toggle source

    # File lib/rack/protection/base.rb, line 63
    def deny(env)
      [options[:status], {'Content-Type' => 'text/plain'}, [options[:message]]]
    end

Also aliased as: default_reaction
drop_session(env) click to toggle source

    # File lib/rack/protection/base.rb, line 80
    def drop_session(env)
      session(env).clear if session? env
    end

encrypt(value) click to toggle source

    # File lib/rack/protection/base.rb, line 100
    def encrypt(value)
      options[:encryptor].hexdigest value.to_s
    end

html?(headers) click to toggle source

    # File lib/rack/protection/base.rb, line 106
    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) click to toggle source

    # File lib/rack/protection/base.rb, line 90
    def origin(env)
      env['HTTP_ORIGIN'] || env['HTTP_X_ORIGIN']
    end

random_string(secure = defined? SecureRandom) click to toggle source

    # File lib/rack/protection/base.rb, line 94
    def random_string(secure = defined? SecureRandom)
      secure ? SecureRandom.hex(32) : "%032x" % rand(2**128-1)
    rescue NotImplementedError
      random_string false
    end

react(env) click to toggle source

    # 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) click to toggle source

    # File lib/rack/protection/base.rb, line 84
    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

report(env) click to toggle source

    # File lib/rack/protection/base.rb, line 67
    def report(env)
      env[options[:report_key]] = true
    end

safe?(env) click to toggle source

    # File lib/rack/protection/base.rb, line 36
    def safe?(env)
      ]GET HEAD OPTIONS TRACE].include? env['REQUEST_METHOD']
    end

session(env) click to toggle source

    # File lib/rack/protection/base.rb, line 75
    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) click to toggle source

    # File lib/rack/protection/base.rb, line 71
    def session?(env)
      env.include? options[:session_key]
    end

warn(env, message) click to toggle source

    # 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

[Validate]

Generated with the Darkfish Rdoc Generator 2.