A platform for community discussion. Free, open, simple.
FIX: prevents exception when search q params is a hash (#7437)
Joffrey JAFFEUXfe86941cb64d
diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rbindex 621788f7ecd..1502270235c 100644--- a/app/controllers/search_controller.rb+++ b/app/controllers/search_controller.rb@@ -9,8 +9,18 @@ class SearchController < ApplicationControllerenddef show- @search_term = params[:q]- raise Discourse::InvalidParameters.new(:q) if @search_term.present? && @search_term.length < SiteSetting.min_search_term_length+ @search_term = params.permit(:q)[:q]++ # a q param has been given but it's not in the correct format+ # eg: ?q[foo]=bar+ if params[:q].present? && !@search_term.present?+ raise Discourse::InvalidParameters.new(:q)+ end++ if @search_term.present? &&+ @search_term.length < SiteSetting.min_search_term_length+ raise Discourse::InvalidParameters.new(:q)+ endsearch_args = {type_filter: 'topic',diff --git a/spec/requests/search_controller_spec.rb b/spec/requests/search_controller_spec.rbindex ca3e20fa848..b453026a931 100644--- a/spec/requests/search_controller_spec.rb+++ b/spec/requests/search_controller_spec.rb@@ -137,6 +137,11 @@ describe SearchController doexpect(response.status).to eq(400)end+ it "raises an error when search term is a hash" do+ get "/search.json?q[foo]"+ expect(response.status).to eq(400)+ end+it "logs the search term" doSiteSetting.log_search_queries = trueget "/search.json", params: { q: 'bantha' }