A platform for community discussion. Free, open, simple.
DEV - handle malformed `page` param (#11093)
jbrw9ff7f2510666
diff --git a/app/controllers/search_controller.rb b/app/controllers/search_controller.rbindex ee64025a7ab..c909fc09145 100644--- a/app/controllers/search_controller.rb+++ b/app/controllers/search_controller.rb@@ -11,7 +11,8 @@ class SearchController < ApplicationControllerenddef show- @search_term = params.permit(:q)[:q]+ permitted_params = params.permit(:q, :page)+ @search_term = permitted_params[:q]# a q param has been given but it's not in the correct format# eg: ?q[foo]=bar@@ -28,6 +29,12 @@ class SearchController < ApplicationControllerraise Discourse::InvalidParameters.new("string contains null byte")end+ page = permitted_params[:page]+ # check for a malformed page parameter+ if page && (!page.is_a?(String) || page.to_i.to_s != page)+ raise Discourse::InvalidParameters+ end+rate_limit_errors = rate_limit_searchdiscourse_expires_in 1.minute@@ -36,8 +43,8 @@ class SearchController < ApplicationControllertype_filter: 'topic',guardian: guardian,blurb_length: 300,- page: if params[:page].to_i <= 10- [params[:page].to_i, 1].max+ page: if page.to_i <= 10+ [page.to_i, 1].maxend}diff --git a/spec/requests/search_controller_spec.rb b/spec/requests/search_controller_spec.rbindex a407eef8d1c..0988c061179 100644--- a/spec/requests/search_controller_spec.rb+++ b/spec/requests/search_controller_spec.rb@@ -330,6 +330,21 @@ describe SearchController doexpect(response.status).to eq(400)end+ it "doesn't raise an error if the page is a string number" do+ get "/search.json", params: { q: 'kittens', page: '3' }+ expect(response.status).to eq(200)+ end++ it "doesn't raise an error if the page is a integer number" do+ get "/search.json", params: { q: 'kittens', page: 3 }+ expect(response.status).to eq(200)+ end++ it "returns a 400 error if the page parameter is invalid" do+ get "/search.json?page=xawesome%27\"</a\&"+ expect(response.status).to eq(400)+ end+it "logs the search term" doSiteSetting.log_search_queries = trueget "/search.json", params: { q: 'bantha' }