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.



11
12
13
# File 'lib/rack/contrib/enforce_valid_encoding.rb', line 11

def initialize app
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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

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