diff options
author | Grey Baker <greysteil@gmail.com> | 2015-10-18 19:27:54 +0100 |
---|---|---|
committer | Grey Baker <greysteil@gmail.com> | 2015-10-23 14:56:47 +0100 |
commit | 59ab2d1ee5995d9ea27ca60e92576518c1898c59 (patch) | |
tree | 5bbae87d4879f32c6914607d261a05bcdef7a77a /actionpack/lib | |
parent | 6f62ace65a720f515da82f5fc5f3e7eab1df8658 (diff) | |
download | rails-59ab2d1ee5995d9ea27ca60e92576518c1898c59.tar.gz rails-59ab2d1ee5995d9ea27ca60e92576518c1898c59.tar.bz2 rails-59ab2d1ee5995d9ea27ca60e92576518c1898c59.zip |
Catch invalid UTF-8 querystring values and respond with BadRequest
Diffstat (limited to 'actionpack/lib')
-rw-r--r-- | actionpack/lib/action_dispatch/http/request.rb | 5 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/request/utils.rb | 15 |
2 files changed, 19 insertions, 1 deletions
diff --git a/actionpack/lib/action_dispatch/http/request.rb b/actionpack/lib/action_dispatch/http/request.rb index c6ab4dbc9a..35e3ac304f 100644 --- a/actionpack/lib/action_dispatch/http/request.rb +++ b/actionpack/lib/action_dispatch/http/request.rb @@ -338,7 +338,10 @@ module ActionDispatch # Override Rack's GET method to support indifferent access def GET fetch_header("action_dispatch.request.query_parameters") do |k| - set_header k, Request::Utils.normalize_encode_params(super || {}) + rack_query_params = super || {} + # Check for non UTF-8 parameter values, which would cause errors later + Request::Utils.check_param_encoding(rack_query_params) + set_header k, Request::Utils.normalize_encode_params(rack_query_params) end rescue Rack::Utils::ParameterTypeError, Rack::Utils::InvalidParameterError => e raise ActionController::BadRequest.new("Invalid query parameters: #{e.message}", e) diff --git a/actionpack/lib/action_dispatch/request/utils.rb b/actionpack/lib/action_dispatch/request/utils.rb index a8151a8224..bb3df3c311 100644 --- a/actionpack/lib/action_dispatch/request/utils.rb +++ b/actionpack/lib/action_dispatch/request/utils.rb @@ -13,6 +13,21 @@ module ActionDispatch end end + def self.check_param_encoding(params) + case params + when Array + params.each { |element| check_param_encoding(element) } + when Hash + params.each_value { |value| check_param_encoding(value) } + when String + unless params.valid_encoding? + # Raise Rack::Utils::InvalidParameterError for consistency with Rack. + # ActionDispatch::Request#GET will re-raise as a BadRequest error. + raise Rack::Utils::InvalidParameterError, "Non UTF-8 value: #{params}" + end + end + end + class ParamEncoder # :nodoc: # Convert nested Hash to HashWithIndifferentAccess. # |