A platform for community discussion. Free, open, simple.
FIX: add a basic validator for topic params
Samad5f502332aa
diff --git a/app/controllers/list_controller.rb b/app/controllers/list_controller.rbindex a2e94b26176..1661a1fe5eb 100644--- a/app/controllers/list_controller.rb+++ b/app/controllers/list_controller.rb@@ -371,7 +371,12 @@ class ListController < ApplicationControllerparams[:tags] = [params[:tag_id].parameterize] if params[:tag_id].present? && guardian.can_tag_pms?TopicQuery.public_valid_options.each do |key|- options[key] = params[key]+ if params.key?(key)+ val = options[key] = params[key]+ if !TopicQuery.validate?(key, val)+ raise Discourse::InvalidParameters.new key+ end+ endend# hacky columns get special handlingdiff --git a/lib/topic_query.rb b/lib/topic_query.rbindex ad73a7848fd..bea49045b87 100644--- a/lib/topic_query.rb+++ b/lib/topic_query.rb@@ -10,6 +10,34 @@ require_dependency 'avatar_lookup'class TopicQuery+ def self.validators+ @validators ||= begin++ zero_or_more = lambda do |x|+ Integer === x && x >= 0+ end++ array_zero_or_more = lambda do |x|+ Array === x && x.length > 0 && x.all? { |i| Integer === i && i >= 0 }+ end++ {+ max_posts: zero_or_more,+ exclude_category_ids: array_zero_or_more,+ min_posts: zero_or_more,+ }+ end+ end++ def self.validate?(option, value)++ if fn = validators[option.to_sym]+ fn.call(value)+ else+ true+ end+ end+def self.public_valid_options@public_valid_options ||=%i(pagediff --git a/spec/requests/list_controller_spec.rb b/spec/requests/list_controller_spec.rbindex 0f72cedfa03..50530d10e84 100644--- a/spec/requests/list_controller_spec.rb+++ b/spec/requests/list_controller_spec.rb@@ -16,6 +16,17 @@ RSpec.describe ListController doexpect(response.status).to eq(200)end+ it "does not return a 500 for invalid input" do+ get "/latest?exclude_category_ids[]=bob"+ expect(response.status).to eq(400)++ get "/latest?min_posts=bob"+ expect(response.status).to eq(400)++ get "/latest?max_posts=bob"+ expect(response.status).to eq(400)+ end+it "doesn't throw an error with page params as an array" doget "/#{Discourse.anonymous_filters[1]}", params: { page: ['7'] }expect(response.status).to eq(200)