Class: Rack::EnforceValidEncoding

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/contrib/enforce_valid_encoding.rb

Overview

Ensure that the path and query string presented to the application contains only valid characters. If the validation fails, then a 400 Bad Request response is returned immediately.

Requires: Ruby 1.9

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ EnforceValidEncoding

Returns a new instance of EnforceValidEncoding.



9
10
11
# File 'lib/rack/contrib/enforce_valid_encoding.rb', line 9

def initialize app
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/rack/contrib/enforce_valid_encoding.rb', line 13

def call env
  full_path = (env.fetch('PATH_INFO', '') + env.fetch('QUERY_STRING', ''))
  if full_path.force_encoding("US-ASCII").valid_encoding? &&
     Rack::Utils.unescape(full_path).valid_encoding?
    @app.call env
  else
    [400, {'Content-Type'=>'text/plain'}, ['Bad Request']]
  end
end