A platform for community discussion. Free, open, simple.
FIX: Chat channel creation skips category posting permission check for mods (#39823)
Natalie Tay69c7c6d489dc
diff --git a/plugins/chat/app/controllers/chat/api/channels_controller.rb b/plugins/chat/app/controllers/chat/api/channels_controller.rbindex c0930340ee2..ccbbea392d0 100644--- a/plugins/chat/app/controllers/chat/api/channels_controller.rb+++ b/plugins/chat/app/controllers/chat/api/channels_controller.rb@@ -80,6 +80,7 @@ class Chat::Api::ChannelsController < Chat::ApiControllerendon_model_not_found(:category) { raise ActiveRecord::RecordNotFound }on_failed_policy(:can_create_channel) { raise Discourse::InvalidAccess }+ on_failed_policy(:can_create_channel_in_category) { raise Discourse::InvalidAccess }on_failed_policy(:category_channel_does_not_exist) doraise Discourse::InvalidParameters.new(I18n.t("chat.errors.channel_exists_for_category"))enddiff --git a/plugins/chat/app/services/chat/create_category_channel.rb b/plugins/chat/app/services/chat/create_category_channel.rbindex 3091d81b753..0df045197e7 100644--- a/plugins/chat/app/services/chat/create_category_channel.rb+++ b/plugins/chat/app/services/chat/create_category_channel.rb@@ -52,6 +52,7 @@ module Chatendmodel :category+ policy :can_create_channel_in_categorypolicy :category_channel_does_not_existtransaction do@@ -75,6 +76,10 @@ module ChatCategory.find_by(id: params.category_id)end+ def can_create_channel_in_category(guardian:, category:)+ guardian.can_post_in_category?(category)+ end+def category_channel_does_not_exist(category:, params:)!Chat::Channel.exists?(chatable: category, name: params.name)enddiff --git a/plugins/chat/spec/requests/chat/api/channels_controller_spec.rb b/plugins/chat/spec/requests/chat/api/channels_controller_spec.rbindex db72623890a..685625cb12c 100644--- a/plugins/chat/spec/requests/chat/api/channels_controller_spec.rb+++ b/plugins/chat/spec/requests/chat/api/channels_controller_spec.rb@@ -347,6 +347,27 @@ RSpec.describe Chat::Api::ChannelsController doexpect(new_channel.chatable_id).to eq(category.id)end+ context "when the user cannot post in the category" do+ fab!(:moderator)+ fab!(:group)+ fab!(:private_category) { Fabricate(:private_category, group:) }++ before do+ sign_in(moderator)+ params[:channel][:chatable_id] = private_category.id+ end++ it "does not create a channel or membership" do+ expect {+ post "/chat/api/channels", params:, headers: { "ACCEPT" => "application/json" }+ }.to not_change { Chat::Channel.count }.and not_change {+ Chat::UserChatChannelMembership.count+ }+ expect(response.status).to eq(403)+ expect(response.parsed_body["errors"]).to include(I18n.t("invalid_access"))+ end+ end+it "creates a channel using the user-provided slug" donew_params = params.dupnew_params[:channel][:slug] = "wow-so-cool"