Class: Rack::Protection::MaximumCookie

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/protection/maximum_cookie.rb,
lib/rack/protection/maximum_cookie/version.rb

Constant Summary collapse

HEADER_SEP_RE =
%r{\r?\n|\0}.freeze
%r{;\s*[Dd][Oo][Mm][Aa][Ii][Nn]=([^;]+)}.freeze
%r{\A[^=]+}.freeze
VERSION =
'0.4.2'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}, &block) ⇒ MaximumCookie

Returns a new instance of MaximumCookie.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rack/protection/maximum_cookie.rb', line 66

def initialize(app, options={}, &block)
  @app = app
  @handler = block

  @options = {}.tap do |h|
    h[:limit] = Integer(options.fetch(:limit, 50))
    h[:bytesize_limit] = Integer(options.fetch(:bytesize_limit, 4_096))
    h[:overhead] = Integer(options.fetch(:overhead, 3))
    h[:stateful?] = !!options.fetch(:stateful?, options.fetch(:stateful, false))
    h[:strict?] = h[:stateful?] || !!options.fetch(:strict?, options.fetch(:strict, false))
    h[:per_domain?] = h[:strict?] || !!options.fetch(:per_domain?, options.fetch(:per_domain, true))

    h.freeze
  end

  if strict?
    # Allow non-ICANN domains to be handled the same as ICANN domains.
    @public_suffix_list = PublicSuffix::List.parse(::File.read(PublicSuffix::List::DEFAULT_LIST_PATH), :private_domains=>false)
  end

  unless limit? || bytesize_limit?
    abort 'No limits, nothing to do!'
  end
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



30
31
32
# File 'lib/rack/protection/maximum_cookie.rb', line 30

def app
  @app
end

#handlerObject (readonly)

Returns the value of attribute handler.



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

def handler
  @handler
end

#public_suffix_listObject (readonly)

Returns the value of attribute public_suffix_list.



32
33
34
# File 'lib/rack/protection/maximum_cookie.rb', line 32

def public_suffix_list
  @public_suffix_list
end

Instance Method Details

#bytesize_limitObject



42
43
44
# File 'lib/rack/protection/maximum_cookie.rb', line 42

def bytesize_limit
  @options[:bytesize_limit]
end

#bytesize_limit?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/rack/protection/maximum_cookie.rb', line 46

def bytesize_limit?
  @options[:bytesize_limit] >= 0
end

#call(env) ⇒ Object



91
92
93
94
95
96
97
98
# File 'lib/rack/protection/maximum_cookie.rb', line 91

def call(env)
  status, headers, body = app.call(env)
  if headers.key?(SET_COOKIE)
    check_cookies env, Rack::Request.new(env),
      normalize_cookie_header(headers[SET_COOKIE])
  end
  [status, headers, body]
end

#limitObject



34
35
36
# File 'lib/rack/protection/maximum_cookie.rb', line 34

def limit
  @options[:limit]
end

#limit?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/rack/protection/maximum_cookie.rb', line 38

def limit?
  @options[:limit] >= 0
end

#overheadObject



50
51
52
# File 'lib/rack/protection/maximum_cookie.rb', line 50

def overhead
  @options[:overhead]
end

#per_domain?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/rack/protection/maximum_cookie.rb', line 54

def per_domain?
  @options[:per_domain?]
end

#stateful?Boolean

Returns:

  • (Boolean)


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

def stateful?
  @options[:stateful?]
end

#strict?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rack/protection/maximum_cookie.rb', line 58

def strict?
  @options[:strict?]
end