A platform for community discussion. Free, open, simple.
SECURITY: Scope chat record lookups to authorized channels (#43781)
Isaac Janzenb646934a7382
diff --git a/plugins/chat/app/services/chat/lookup_user_threads.rb b/plugins/chat/app/services/chat/lookup_user_threads.rbindex e13744781ed..6cab24575ea 100644--- a/plugins/chat/app/services/chat/lookup_user_threads.rb+++ b/plugins/chat/app/services/chat/lookup_user_threads.rb@@ -42,6 +42,7 @@ module Chatdef fetch_threads(guardian:, params:)::Chat::Thread.viewable_by_user(guardian.user)+ .where(channel_id: ::Chat::ChannelFetcher.all_secured_channel_ids(guardian)).includes(:channel,:user_chat_thread_memberships,diff --git a/plugins/chat/lib/chat/message_mover.rb b/plugins/chat/lib/chat/message_mover.rbindex a62c7074187..303dad3bf3a 100644--- a/plugins/chat/lib/chat/message_mover.rb+++ b/plugins/chat/lib/chat/message_mover.rb@@ -37,8 +37,8 @@ module Chatdef initialize(acting_user:, source_channel:, message_ids:)@source_channel = source_channel@acting_user = acting_user- @source_message_ids = message_ids- @source_messages = find_messages(@source_message_ids, source_channel)+ @source_messages = find_messages(message_ids, source_channel)+ @source_message_ids = message_ids & @source_messages.map(&:id)@ordered_source_message_ids = @source_messages.map(&:id)@source_thread_ids = @source_messages.pluck(:thread_id).uniq.compactenddiff --git a/plugins/chat/spec/requests/chat/api/channels_moves_controller_spec.rb b/plugins/chat/spec/requests/chat/api/channels_moves_controller_spec.rbindex 6f7fe008e43..46b2a58a67a 100644--- a/plugins/chat/spec/requests/chat/api/channels_moves_controller_spec.rb+++ b/plugins/chat/spec/requests/chat/api/channels_moves_controller_spec.rb@@ -39,6 +39,50 @@ RSpec.describe Chat::Api::ChannelsMessagesMovesController doendend+ context "when the user is a category group moderator" do+ fab!(:current_user, :user)+ fab!(:moderation_group, :group)+ fab!(:source_channel) { Fabricate(:private_category_channel, group: moderation_group) }+ fab!(:destination_channel, :category_channel)+ fab!(:source_message) { Fabricate(:chat_message, chat_channel: source_channel) }+ fab!(:restricted_channel, :private_category_channel)+ fab!(:restricted_message) { Fabricate(:chat_message, chat_channel: restricted_channel) }+ fab!(:category_moderation_group) do+ Fabricate(+ :category_moderation_group,+ category: source_channel.category,+ group: moderation_group,+ )+ end++ before do+ SiteSetting.enable_category_group_moderation = true+ moderation_group.add(current_user)+ source_channel.add(current_user)+ sign_in(current_user)+ end++ it "does not delete a message from an inaccessible channel" do+ expect(current_user.guardian.can_preview_chat_channel?(restricted_channel)).to eq(false)++ post "/chat/api/channels/#{source_channel.id}/messages/moves",+ params: {+ move: {+ destination_channel_id: destination_channel.id,+ message_ids: [source_message.id, restricted_message.id],+ },+ }++ expect(response.status).to eq(200)+ expect(response.parsed_body).to include(+ "success" => "OK",+ "destination_channel_id" => destination_channel.id,+ )+ expect(source_message.reload.deleted_at).not_to eq(nil)+ expect(restricted_message.reload.deleted_at).to eq(nil)+ end+ end+context "when the user is admin" dofab!(:current_user, :admin)diff --git a/plugins/chat/spec/requests/chat/api/current_user_threads_spec.rb b/plugins/chat/spec/requests/chat/api/current_user_threads_spec.rbindex a6455045e3e..d2c4acd462f 100644--- a/plugins/chat/spec/requests/chat/api/current_user_threads_spec.rb+++ b/plugins/chat/spec/requests/chat/api/current_user_threads_spec.rb@@ -26,6 +26,69 @@ describe Chat::Api::CurrentUserThreadsController doexpect(response).to have_http_status :okexpect(response.parsed_body[:threads]).not_to be_emptyend++ context "when the user has lost access to a private channel" do+ fab!(:authorized_user, :user)+ fab!(:private_group, :group)+ fab!(:private_channel) do+ Fabricate(:private_category_channel, group: private_group, threading_enabled: true)+ end+ fab!(:private_original_message) do+ Fabricate(+ :chat_message,+ chat_channel: private_channel,+ message: "Former member private thread content",+ )+ end+ fab!(:private_thread) do+ Fabricate(+ :chat_thread,+ channel: private_channel,+ original_message: private_original_message,+ with_replies: 1,+ )+ end+ let(:private_message_after_revocation) do+ Fabricate(+ :chat_message,+ chat_channel: private_channel,+ message: "Private thread content created after revocation",+ thread: private_thread,+ ).tap { |message| private_thread.update!(last_message: message) }+ end++ before do+ private_group.add(current_user)+ private_group.add(authorized_user)+ private_channel.add(current_user)+ private_channel.add(authorized_user)+ private_thread.add(current_user)+ private_thread.add(authorized_user)+ GroupUser.where(group: private_group, user: current_user).destroy_all+ private_message_after_revocation+ end++ it "excludes post-revocation private content while retaining it for authorized members" do+ get "/chat/api/me/threads"++ expect(response).to have_http_status :ok+ expect(response.body).not_to include(private_original_message.message)+ expect(response.body).not_to include(private_message_after_revocation.message)+ expect(response.parsed_body["threads"].map { |thread| thread["id"] }).to contain_exactly(+ thread.id,+ )++ sign_in(authorized_user)+ get "/chat/api/me/threads"++ expect(response).to have_http_status :ok+ expect(response.body).to include(private_original_message.message)+ expect(response.body).to include(private_message_after_revocation.message)+ expect(response.parsed_body["threads"].map { |thread| thread["id"] }).to contain_exactly(+ private_thread.id,+ )+ end+ endendcontext "when threads are not found" dodiff --git a/plugins/chat/spec/requests/core_ext/inline_onebox_controller_spec.rb b/plugins/chat/spec/requests/core_ext/inline_onebox_controller_spec.rbindex 26806383262..fee5f8e146d 100644--- a/plugins/chat/spec/requests/core_ext/inline_onebox_controller_spec.rb+++ b/plugins/chat/spec/requests/core_ext/inline_onebox_controller_spec.rb@@ -18,15 +18,21 @@ RSpec.describe InlineOneboxController dobefore doSiteSetting.chat_enabled = true- SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]+ SiteSetting.enable_public_channels = true+ SiteSetting.chat_allowed_groups =+ "#{Group::AUTO_GROUPS[:everyone]}|#{Group::AUTO_GROUPS[:anonymous_users]}"sign_in(current_user)endit "does not disclose a private thread title through a public channel URL" do- get "/inline-onebox.json",- params: {- urls: ["#{Discourse.base_url}/chat/c/-/#{public_channel.id}/t/#{private_thread.id}"],- }+ get "/chat/api/channels/#{private_channel.id}/threads/#{private_thread.id}"++ expect(response.status).to eq(403)++ chat_thread_url = "#{Discourse.base_url}/chat/c/-/#{public_channel.id}/t/#{private_thread.id}"+ InlineOneboxer.invalidate(chat_thread_url)++ get "/inline-onebox.json", params: { urls: [chat_thread_url] }expect(response.status).to eq(200)expect(response.body).not_to include(private_thread.title)