A platform for community discussion. Free, open, simple.
SECURITY: Potential DoS on posts requests (#41092)
Penar Musaraj6b9dbabc89ba
diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rbindex 4926364b4b7..fe405526715 100644--- a/app/controllers/topics_controller.rb+++ b/app/controllers/topics_controller.rb@@ -401,6 +401,7 @@ class TopicsController < ApplicationControllertopic = Topic.find_by(id: params[:topic_id])guardian.ensure_can_edit!(topic)+ return if reject_oversized_tag_params!(:tags, :original_tags)original_title = params[:original_title]if original_title.present? && original_title != topic.title@@ -526,6 +527,7 @@ class TopicsController < ApplicationControllerdef update_tagstopic = Topic.find_by(id: params[:topic_id])guardian.ensure_can_edit_tags!(topic)+ return if reject_oversized_tag_params!(:tags)tags =if params[:tags].is_a?(ActionController::Parameters)@@ -1356,6 +1358,27 @@ class TopicsController < ApplicationControllerrender_json_dump(payload)end+ def reject_oversized_tag_params!(*param_names)+ param_names.each do |param_name|+ next if !params.has_key?(param_name)+ next if tag_param_size(params[param_name]) <= SiteSetting.max_tags_per_topic++ render_json_error(+ I18n.t("tags.too_many_tags_for_topic", count: SiteSetting.max_tags_per_topic),+ )+ return true+ end++ false+ end++ def tag_param_size(tags)+ return tags.length if tags.is_a?(Array)+ return tags.keys.length if tags.is_a?(ActionController::Parameters)++ 0+ end+def resolve_tag_names(topic)@resolved_tag_names ||=if params[:tags].present?diff --git a/config/locales/server.en.yml b/config/locales/server.en.ymlindex 72040858ea3..7236395f0e6 100644--- a/config/locales/server.en.yml+++ b/config/locales/server.en.yml@@ -5807,6 +5807,9 @@ en:minimum_required_tags:one: "You must select at least %{count} tag."other: "You must select at least %{count} tags."+ too_many_tags_for_topic:+ one: "You can apply up to %{count} tag to a topic."+ other: "You can apply up to %{count} tags to a topic."upload_row_too_long: "The CSV file should have one tag per line. Optionally the tag can be followed by a comma, then the tag group name."bulk_create:invalid_params: "Invalid parameters. Please provide an array of tag names."diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rbindex 83ba9e2294e..4cf08cc3c91 100644--- a/spec/requests/topics_controller_spec.rb+++ b/spec/requests/topics_controller_spec.rb@@ -2537,6 +2537,22 @@ RSpec.describe TopicsController doexpect(topic.tags.pluck(:id)).to contain_exactly(tag.id)end+ it "rejects tag arrays exceeding the configured per-topic limit" do+ SiteSetting.max_tags_per_topic = 1++ put "/t/#{topic.slug}/#{topic.id}.json",+ params: {+ tags: [{ id: tag.id, name: tag.name }, {}],+ },+ as: :json++ expect(response.status).to eq(422)+ expect(response.parsed_body["errors"]).to contain_exactly(+ I18n.t("tags.too_many_tags_for_topic", count: 1),+ )+ expect(topic.reload.tags).to be_empty+ end+it "can update tags when params are form-encoded as indexed hash" doexpect doput "/t/#{topic.id}/tags.json",