Class: Sekisyo::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/sekisyo/middleware.rb

Overview

Sekisyo Middleware is rack middleware for input validation.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

Returns a new instance of Middleware.



58
59
60
61
62
63
64
# File 'lib/sekisyo/middleware.rb', line 58

def initialize(app)
  @app = app
  @configuration = self.class.configuration.dup
  @whitelist = self.class.whitelist.dup
  @logger = @configuration.logger
  @allow_keys = @configuration.allow_keys.map(&:to_s)
end

Class Method Details

.configurationSekisyo::Configuration

Returns Configuration object, wrapper for Hashie::Mash.

Returns:



12
13
14
# File 'lib/sekisyo/middleware.rb', line 12

def configuration
  @configuration ||= Sekisyo::Configuration.new
end

.configure(**options) ⇒ void

This method returns an undefined value.

Options

  • :file_paths(Array<String>) — Whitelist yml file paths.

  • :undefined_request(Symbol, nil) — Specifies what to do with the validation results if whitelist does not contain a description. Available values are nil(default), :warning, :failure

  • :logger(Logger) — Specify log class instances.

  • :allow_keys(Array<Symbol, String>) — Specifies the top-level key of the parameter to be exempt from validation.

Yield

  • (Sekisyo::Configuration)

Parameters:

  • options (Hash)

    Override configuration with this value.



39
40
41
42
43
44
45
# File 'lib/sekisyo/middleware.rb', line 39

def configure(**options)
  if block_given?
    yield configuration
  else
    configuration.deep_merge!(options)
  end
end

.option(name, value = nil) ⇒ void

This method returns an undefined value.

Parameters:

  • name (Symbol)

    Configuration key

  • value (Any) (defaults to: nil)

    Configuration value



53
54
55
# File 'lib/sekisyo/middleware.rb', line 53

def option(name, value = nil)
  configuration[name] = value
end

.whitelistSekisyo::Whitelist

Returns Whitelist of parameters to be received per request.

Returns:



19
20
21
# File 'lib/sekisyo/middleware.rb', line 19

def whitelist
  @whitelist ||= Sekisyo::Whitelist.new.parse(*configuration.file_paths)
end

Instance Method Details

#call(env) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sekisyo/middleware.rb', line 71

def call(env)
  return @app.call(env) if valid?(env)

  code   = 400
  body   = ['Bad request']
  header = { 'Content-Type' => 'text/html;charset=utf-8',
             'Content-Length' => body.sum(&:bytesize).to_s,
             'X-XSS-Protection' => '1;mode=block',
             'X-Content-Type-Options' => 'nosniff',
             'X-Frame-Options' => 'SAMEORIGIN' }
  [code, header, body]
end