From 28a88cfa85658aa51dcba7f47364011d45fb73a4 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 13 Aug 2019 21:30:20 -0500 Subject: [PATCH] application controller: fix errors in normalize_search. Fix exceptions in `normalize_search` on e.g. `https://danbooru.donmai.us/users?search=blah`. Caused when the `search` param is not a hash. --- app/controllers/application_controller.rb | 8 +++++--- test/functional/application_controller_test.rb | 5 +++++ 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 143efe5a4..01022f20b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -189,8 +189,8 @@ class ApplicationController < ActionController::Base # /tags?search[name]=touhou&search[category]=&search[order]= # => /tags?search[name]=touhou def normalize_search - return unless request.get? - params[:search] ||= ActionController::Parameters.new + return unless request.get? && params[:action] == "index" + params[:search] = search_params deep_reject_blank = lambda do |hash| hash.reject { |k, v| v.blank? || (v.is_a?(Hash) && deep_reject_blank.call(v).blank?) } @@ -204,7 +204,9 @@ class ApplicationController < ActionController::Base end def search_params - params.fetch(:search, {}).permit! + search_params = params.fetch(:search, {}) + search_params = ActionController::Parameters.new unless search_params.is_a?(ActionController::Parameters) + search_params.permit! end def set_safe_mode diff --git a/test/functional/application_controller_test.rb b/test/functional/application_controller_test.rb index 4d3e9b8f9..47338564a 100644 --- a/test/functional/application_controller_test.rb +++ b/test/functional/application_controller_test.rb @@ -35,5 +35,10 @@ class ApplicationControllerTest < ActionDispatch::IntegrationTest assert_response 410 end end + + should "normalize search params" do + get tags_path, params: { search: { name: "bkub", post_count: "" } } + assert_redirected_to tags_path(search: { name: "bkub" }) + end end end