Skip to content

A platform for community discussion. Free, open, simple.

FIX: verify destination topic access when archiving chat channel to existing topic (#38248)

Renato Atilio4dbe3d293138
diff --git a/plugins/chat/app/controllers/chat/api/channels_archives_controller.rb b/plugins/chat/app/controllers/chat/api/channels_archives_controller.rb
index aaebe7536f5..8c77392ee23 100644
--- a/plugins/chat/app/controllers/chat/api/channels_archives_controller.rb
+++ b/plugins/chat/app/controllers/chat/api/channels_archives_controller.rb
@@ -15,6 +15,14 @@ class Chat::Api::ChannelsArchivesController < Chat::Api::ChannelsController
raise Discourse::InvalidParameters if new_topic && archive_params[:title].blank?
raise Discourse::InvalidParameters if !new_topic && archive_params[:topic_id].blank?
+ if !new_topic
+ destination_topic = Topic.find_by(id: archive_params[:topic_id])
+ if destination_topic.blank? || !guardian.can_see_topic?(destination_topic) ||
+ !guardian.can_create_post_on_topic?(destination_topic)
+ raise Discourse::InvalidAccess
+ end
+ end
+
if !guardian.can_change_channel_status?(channel_from_params, :read_only)
raise Discourse::InvalidAccess.new(I18n.t("chat.errors.channel_cannot_be_archived"))
end
diff --git a/plugins/chat/spec/requests/chat/api/channels_archives_controller_spec.rb b/plugins/chat/spec/requests/chat/api/channels_archives_controller_spec.rb
index 4ff346602ff..4d1eddc1d3d 100644
--- a/plugins/chat/spec/requests/chat/api/channels_archives_controller_spec.rb
+++ b/plugins/chat/spec/requests/chat/api/channels_archives_controller_spec.rb
@@ -86,6 +86,50 @@ RSpec.describe Chat::Api::ChannelsArchivesController do
expect(channel.reload.status).to eq("read_only")
end
+ context "when archiving to an existing topic the user cannot access" do
+ fab!(:moderator)
+ fab!(:private_category) { Fabricate(:private_category, group: Fabricate(:group)) }
+ fab!(:private_topic) { Fabricate(:topic, category: private_category) }
+
+ it "returns 403 when the staff user cannot see the destination topic" do
+ sign_in(moderator)
+ post "/chat/api/channels/#{channel.id}/archives",
+ params: {
+ archive: {
+ type: "existing_topic",
+ topic_id: private_topic.id,
+ },
+ }
+ expect(response.status).to eq(403)
+ end
+
+ it "returns 403 when the staff user cannot create posts on the destination topic" do
+ private_topic.update!(closed: true)
+ sign_in(moderator)
+ post "/chat/api/channels/#{channel.id}/archives",
+ params: {
+ archive: {
+ type: "existing_topic",
+ topic_id: private_topic.id,
+ },
+ }
+ expect(response.status).to eq(403)
+ end
+
+ it "does not create an archive record when destination topic is inaccessible" do
+ sign_in(moderator)
+ expect {
+ post "/chat/api/channels/#{channel.id}/archives",
+ params: {
+ archive: {
+ type: "existing_topic",
+ topic_id: private_topic.id,
+ },
+ }
+ }.not_to change { Chat::ChannelArchive.count }
+ end
+ end
+
it "does nothing if the chat channel archive already exists" do
sign_in(admin)
post "/chat/api/channels/#{channel.id}/archives", params: new_topic_params