123456789_123456789_123456789_123456789_123456789_

Class: Rack::Protection::EscapedParams

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Rack::Utils, Base
Instance Chain:
self, Base
Inherits: Rack::Protection::Base
Defined in: rack-protection/lib/rack/protection/escaped_params.rb

Overview

Prevented attack:: XSS Supported browsers:: all More infos:: http://en.wikipedia.org/wiki/Cross-site_scripting

Automatically escapes Rack::Request#params so they can be embedded in HTML or JavaScript without any further issues.

Options: escape:: What escaping modes to use, should be Symbol or Array of Symbols. Available: :html (default), :javascript, :url

Constant Summary

Base - Inherited

DEFAULT_OPTIONS

Class Method Summary

Instance Attribute Summary

Base - Inherited

Instance Method Summary

Constructor Details

.newEscapedParams

[ GitHub ]

  
# File 'rack-protection/lib/rack/protection/escaped_params.rb', line 36

def initialize(*)
  super

  modes       = Array options[:escape]
  @escaper    = options[:escaper]
  @html       = modes.include? :html
  @javascript = modes.include? :javascript
  @url        = modes.include? :url

  return unless @javascript && (!@escaper.respond_to? :escape_javascript)

  raise('Use EscapeUtils for JavaScript escaping.')
end

Class Method Details

.escape_url

[ GitHub ]

  
# File 'rack-protection/lib/rack/protection/escaped_params.rb', line 29

alias escape_url escape

Instance Method Details

#call(env)

[ GitHub ]

  
# File 'rack-protection/lib/rack/protection/escaped_params.rb', line 50

def call(env)
  request  = Request.new(env)
  get_was  = handle(request.GET)
  post_was = begin
    handle(request.POST)
  rescue StandardError
    nil
  end
  app.call env
ensure
  request.GET.replace  get_was  if get_was
  request.POST.replace post_was if post_was
end

#escape(object)

[ GitHub ]

  
# File 'rack-protection/lib/rack/protection/escaped_params.rb', line 70

def escape(object)
  case object
  when Hash   then escape_hash(object)
  when Array  then object.map { |o| escape(o) }
  when String then escape_string(object)
  when Tempfile then object
  end
end

#escape_hash(hash)

[ GitHub ]

  
# File 'rack-protection/lib/rack/protection/escaped_params.rb', line 79

def escape_hash(hash)
  hash = hash.dup
  hash.each { |k, v| hash[k] = escape(v) }
  hash
end

#escape_string(str)

[ GitHub ]

  
# File 'rack-protection/lib/rack/protection/escaped_params.rb', line 85

def escape_string(str)
  str = @escaper.escape_url(str)        if @url
  str = @escaper.escape_html(str)       if @html
  str = @escaper.escape_javascript(str) if @javascript
  str
end

#handle(hash)

[ GitHub ]

  
# File 'rack-protection/lib/rack/protection/escaped_params.rb', line 64

def handle(hash)
  was = hash.dup
  hash.replace escape(hash)
  was
end