A platform for community discussion. Free, open, simple.
SECURITY: Chat authorization and disclosure fixes
Natea2971a5aac1
diff --git a/plugins/chat/app/controllers/chat/api/channel_threads_controller.rb b/plugins/chat/app/controllers/chat/api/channel_threads_controller.rbindex 016134bfe76..faed48572a1 100644--- a/plugins/chat/app/controllers/chat/api/channel_threads_controller.rb+++ b/plugins/chat/app/controllers/chat/api/channel_threads_controller.rb@@ -85,6 +85,7 @@ class Chat::Api::ChannelThreadsController < Chat::ApiControllerendon_model_not_found(:channel) { raise Discourse::NotFound }on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess }+ on_failed_policy(:can_create_thread_in_channel) { raise Discourse::InvalidAccess }on_failed_policy(:threading_enabled_for_channel) { raise Discourse::NotFound }on_model_errors(:thread) do |model|render json: failed_json.merge(errors: [model.errors.full_messages.join(", ")]),diff --git a/plugins/chat/app/serializers/chat/channel_serializer.rb b/plugins/chat/app/serializers/chat/channel_serializer.rbindex 088f12828f7..0b02436f1c2 100644--- a/plugins/chat/app/serializers/chat/channel_serializer.rb+++ b/plugins/chat/app/serializers/chat/channel_serializer.rb@@ -114,6 +114,10 @@ module Chat@current_user_membership.present?end+ def include_last_message?+ scope.can_preview_chat_channel?(object)+ end+def current_user_membership@current_user_membership.chat_channel = objectdiff --git a/plugins/chat/app/services/chat/create_thread.rb b/plugins/chat/app/services/chat/create_thread.rbindex ca39ff8cbca..877e8a22e74 100644--- a/plugins/chat/app/services/chat/create_thread.rb+++ b/plugins/chat/app/services/chat/create_thread.rb@@ -51,7 +51,11 @@ module Chatenddef can_create_thread_in_channel(guardian:, channel:)- guardian.can_create_channel_message?(channel)+ if channel.direct_message_channel?+ return Chat::Channel::Policy::MessageCreation.new(context).call+ end++ guardian.can_join_chat_channel?(channel) && guardian.can_create_channel_message?(channel)enddef threading_enabled_for_channel(channel:)diff --git a/plugins/chat/lib/chat/guardian_extensions.rb b/plugins/chat/lib/chat/guardian_extensions.rbindex b20235293c8..2dd15f6e721 100644--- a/plugins/chat/lib/chat/guardian_extensions.rb+++ b/plugins/chat/lib/chat/guardian_extensions.rb@@ -244,6 +244,8 @@ module Chatreturn false if !can_modify_channel_message?(message.chat_channel)if message.user_id == current_user.id+ return false if !can_preview_chat_channel?(message.chat_channel)+case chatablewhen Categoryreturn message.deleted_by_id == current_user.id || can_moderate_chat?(chatable)diff --git a/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb b/plugins/chat/spec/lib/chat/guardian_extensions_spec.rbindex 2e15758dca0..dbae64cbfa4 100644--- a/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb+++ b/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb@@ -605,6 +605,38 @@ RSpec.describe Chat::GuardianExtensions doexpect(guardian.can_restore_chat?(message, chatable)).to eq(false)endend++ context "when the owner has lost access to a private category channel" do+ fab!(:revoke_group, :group)+ fab!(:revoked_category) { Fabricate(:private_category, group: revoke_group) }+ fab!(:revoked_channel) { Fabricate(:chat_channel, chatable: revoked_category) }+ fab!(:message) { Fabricate(:chat_message, chat_channel: revoked_channel, user: user) }++ before do+ revoke_group.add(user)+ message.trash!(guardian.user)+ GroupUser.where(group: revoke_group, user: user).destroy_all+ end++ it "disallows owner to restore" do+ expect(guardian.can_restore_chat?(message, revoked_category)).to eq(false)+ end+ end++ context "when the owner is no longer in a direct message channel" do+ fab!(:other_user, :user)+ fab!(:dm_channel) { Fabricate(:direct_message_channel, users: [user, other_user]) }+ fab!(:message) { Fabricate(:chat_message, chat_channel: dm_channel, user: user) }++ before do+ message.trash!(guardian.user)+ dm_channel.chatable.direct_message_users.find_by!(user: user).destroy!+ end++ it "disallows owner to restore" do+ expect(guardian.can_restore_chat?(message, dm_channel.chatable)).to eq(false)+ end+ endendenddiff --git a/plugins/chat/spec/requests/chat/api/channel_messages_controller_spec.rb b/plugins/chat/spec/requests/chat/api/channel_messages_controller_spec.rbindex 123782ec4ab..14df3cb5ebf 100644--- a/plugins/chat/spec/requests/chat/api/channel_messages_controller_spec.rb+++ b/plugins/chat/spec/requests/chat/api/channel_messages_controller_spec.rb@@ -255,4 +255,48 @@ RSpec.describe Chat::Api::ChannelMessagesController doendendend++ describe "#restore" do+ context "when the user no longer has access to a private category channel" do+ fab!(:group)+ fab!(:private_category) { Fabricate(:private_category, group:) }+ fab!(:private_channel) { Fabricate(:chat_channel, chatable: private_category) }+ fab!(:message) { Fabricate(:chat_message, chat_channel: private_channel, user: current_user) }++ before do+ group.add(current_user)+ private_channel.add(current_user)+ message.trash!(current_user)+ GroupUser.where(group: group, user: current_user).destroy_all+ end++ it "does not restore their own deleted message" do+ put "/chat/api/channels/#{private_channel.id}/messages/#{message.id}/restore"++ expect(response.status).to eq(403)+ expect(message.reload).to be_trashed+ end+ end++ context "when the user is no longer a member of a direct message channel" do+ fab!(:other_user, :user)+ fab!(:third_user, :user)+ fab!(:dm_channel) do+ Fabricate(:direct_message_channel, users: [current_user, other_user, third_user])+ end+ fab!(:message) { Fabricate(:chat_message, chat_channel: dm_channel, user: current_user) }++ before do+ message.trash!(current_user)+ dm_channel.chatable.direct_message_users.find_by!(user: current_user).destroy!+ end++ it "does not restore their own deleted message" do+ put "/chat/api/channels/#{dm_channel.id}/messages/#{message.id}/restore"++ expect(response.status).to eq(403)+ expect(message.reload).to be_trashed+ end+ end+ endenddiff --git a/plugins/chat/spec/requests/chat/api/channel_threads_controller_spec.rb b/plugins/chat/spec/requests/chat/api/channel_threads_controller_spec.rbindex 058503b98f8..b55baa1e4ac 100644--- a/plugins/chat/spec/requests/chat/api/channel_threads_controller_spec.rb+++ b/plugins/chat/spec/requests/chat/api/channel_threads_controller_spec.rb@@ -328,6 +328,27 @@ RSpec.describe Chat::Api::ChannelThreadsController doendend+ context "when user can only see a readonly category channel" do+ fab!(:group) { Fabricate(:group, users: [current_user]) }+ fab!(:category) do+ Fabricate(+ :private_category,+ group: group,+ permission_type: CategoryGroup.permission_types[:readonly],+ )+ end+ fab!(:channel_1) do+ Fabricate(:category_channel, chatable: category, threading_enabled: true)+ end+ fab!(:message_1) { Fabricate(:chat_message, chat_channel: channel_1) }++ it "returns 403" do+ post "/chat/api/channels/#{channel_id}/threads", params: params++ expect(response.status).to eq(403)+ end+ end+context "when the title is too long" dolet(:title) { "x" * Chat::Thread::MAX_TITLE_LENGTH + "x" }diff --git a/plugins/chat/spec/requests/reviewables_controller_spec.rb b/plugins/chat/spec/requests/reviewables_controller_spec.rbnew file mode 100644index 00000000000..84f63d2c3f9--- /dev/null+++ b/plugins/chat/spec/requests/reviewables_controller_spec.rb@@ -0,0 +1,54 @@+# frozen_string_literal: true++RSpec.describe ReviewablesController do+ fab!(:moderator)+ fab!(:message_author) { Fabricate(:user, refresh_auto_groups: true) }+ fab!(:flagger) { Fabricate(:user, refresh_auto_groups: true) }+ fab!(:direct_message_channel) do+ Fabricate(:direct_message_channel, users: [message_author, flagger])+ end++ before do+ SiteSetting.chat_enabled = true+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]+ SiteSetting.chat_message_flag_allowed_groups = Group::AUTO_GROUPS[:everyone]+ end++ describe "#index" do+ it "does not expose unrelated direct-message last message content to moderators" do+ flagged_message_text = "flagged direct message content"+ unrelated_last_message_text = "unrelated later direct message secret"+ flagged_message =+ Fabricate(+ :chat_message,+ chat_channel: direct_message_channel,+ user: message_author,+ message: flagged_message_text,+ )++ result =+ Chat::ReviewQueue.new.flag_message(+ flagged_message,+ Guardian.new(flagger),+ ReviewableScore.types[:spam],+ )+ expect(result[:success]).to eq(true)++ unrelated_last_message =+ Fabricate(+ :chat_message,+ chat_channel: direct_message_channel,+ user: flagger,+ message: unrelated_last_message_text,+ )+ direct_message_channel.update!(last_message: unrelated_last_message)++ sign_in(moderator)+ get "/review.json", params: { type: "Chat::ReviewableMessage" }++ expect(response.status).to eq(200)+ expect(response.body).to include(flagged_message_text)+ expect(response.body).not_to include(unrelated_last_message_text)+ end+ end+enddiff --git a/plugins/chat/spec/serializer/chat/channel_serializer_spec.rb b/plugins/chat/spec/serializer/chat/channel_serializer_spec.rbindex ef54f3cdee2..3db0c451b12 100644--- a/plugins/chat/spec/serializer/chat/channel_serializer_spec.rb+++ b/plugins/chat/spec/serializer/chat/channel_serializer_spec.rb@@ -122,4 +122,32 @@ describe Chat::ChannelSerializer doexpect(serializer.as_json[:unicode_title]).to eq("🐈 Cats")end++ describe "#last_message" do+ fab!(:last_message) { Fabricate(:chat_message, chat_channel: chat_channel) }++ before { chat_channel.update!(last_message: last_message) }++ context "when the user can preview the channel" do+ let(:guardian_user) { admin }++ it "includes the last_message" do+ expect(serializer.as_json[:last_message]).to be_present+ expect(serializer.as_json[:last_message][:id]).to eq(last_message.id)+ end+ end++ context "when the user cannot preview the channel" do+ fab!(:private_channel, :private_category_channel)+ fab!(:last_private_message) { Fabricate(:chat_message, chat_channel: private_channel) }++ let(:chat_channel) { private_channel }++ before { private_channel.update!(last_message: last_private_message) }++ it "omits the last_message" do+ expect(serializer.as_json[:last_message]).to be_nil+ end+ end+ endenddiff --git a/plugins/chat/spec/services/chat/create_thread_spec.rb b/plugins/chat/spec/services/chat/create_thread_spec.rbindex 345006265fa..3082e4f0576 100644--- a/plugins/chat/spec/services/chat/create_thread_spec.rb+++ b/plugins/chat/spec/services/chat/create_thread_spec.rb@@ -28,6 +28,12 @@ RSpec.describe Chat::CreateThread dolet(:params) { { original_message_id: message_1.id, channel_id: channel_1.id, title: } }let(:dependencies) { { guardian: } }+ before do+ SiteSetting.chat_enabled = true+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]+ SiteSetting.direct_message_enabled_groups = Group::AUTO_GROUPS[:everyone]+ end+context "when all steps pass" doit { is_expected.to run_successfully }@@ -106,6 +112,21 @@ RSpec.describe Chat::CreateThread doit { is_expected.to fail_a_policy(:can_view_channel) }end+ context "when user can only see a readonly category channel" do+ fab!(:group) { Fabricate(:group, users: [current_user]) }+ fab!(:category) do+ Fabricate(+ :private_category,+ group: group,+ permission_type: CategoryGroup.permission_types[:readonly],+ )+ end+ fab!(:channel_1) { Fabricate(:category_channel, chatable: category, threading_enabled: true) }+ fab!(:message_1) { Fabricate(:chat_message, chat_channel: channel_1) }++ it { is_expected.to fail_a_policy(:can_create_thread_in_channel) }+ end+context "when channel is not open" docontext "when channel is read_only" dobefore { channel_1.update!(status: :read_only) }diff --git a/plugins/discourse-calendar/app/serializers/discourse_post_event/event_serializer.rb b/plugins/discourse-calendar/app/serializers/discourse_post_event/event_serializer.rbindex 3088fee8941..ac6081e8c61 100644--- a/plugins/discourse-calendar/app/serializers/discourse_post_event/event_serializer.rb+++ b/plugins/discourse-calendar/app/serializers/discourse_post_event/event_serializer.rb@@ -37,11 +37,17 @@ module DiscoursePostEventhas_one :image_upload, embed: :object, serializer: UploadSerializerdef channel- ::Chat::ChannelSerializer.new(object.chat_channel, root: false, scope:)+ ::Chat::ChannelSerializer.new(+ object.chat_channel,+ root: false,+ scope:,+ membership: object.chat_channel.membership_for(scope.current_user),+ )enddef include_channel?- object.chat_enabled && defined?(::Chat::ChannelSerializer) && object.chat_channel.present?+ object.chat_enabled && defined?(::Chat::ChannelSerializer) && object.chat_channel.present? &&+ scope.can_chat? && scope.can_preview_chat_channel?(object.chat_channel)enddef at_capacitydiff --git a/plugins/discourse-calendar/spec/requests/events_controller_spec.rb b/plugins/discourse-calendar/spec/requests/events_controller_spec.rbindex 93246f71b30..b8264c04142 100644--- a/plugins/discourse-calendar/spec/requests/events_controller_spec.rb+++ b/plugins/discourse-calendar/spec/requests/events_controller_spec.rb@@ -726,6 +726,79 @@ module DiscoursePostEventendend+ describe "#show" do+ before do+ SiteSetting.calendar_enabled = true+ SiteSetting.discourse_post_event_enabled = true+ end++ fab!(:admin_user) { Fabricate(:user, admin: true) }+ fab!(:category)+ fab!(:topic) { Fabricate(:topic, user: admin_user, category: category) }+ fab!(:post_1) { Fabricate(:post, user: admin_user, topic: topic) }+ fab!(:chat_channel) { Fabricate(:chat_channel, chatable: category) }+ fab!(:event) { Fabricate(:event, post: post_1, chat_enabled: true, chat_channel: chat_channel) }+ fab!(:chat_message) do+ Fabricate(+ :chat_message,+ chat_channel: chat_channel,+ user: admin_user,+ message: "private chat message body",+ )+ end++ before { chat_channel.update!(last_message: chat_message) }++ context "when the viewer is anonymous" do+ before do+ SiteSetting.chat_enabled = true+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]+ end++ it "does not include the chat channel block or last message body" do+ get "/discourse-post-event/events/#{event.id}.json"++ expect(response.status).to eq(200)+ expect(response.parsed_body["event"]).not_to have_key("channel")+ expect(response.body).not_to include("private chat message body")+ end+ end++ context "when the viewer cannot join the chat channel" do+ fab!(:viewer, :user)++ before do+ SiteSetting.chat_enabled = true+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:staff]+ sign_in(viewer)+ end++ it "does not include the chat channel block or last message body" do+ get "/discourse-post-event/events/#{event.id}.json"++ expect(response.status).to eq(200)+ expect(response.parsed_body["event"]).not_to have_key("channel")+ expect(response.body).not_to include("private chat message body")+ end+ end++ context "when the viewer can join the chat channel" do+ before do+ SiteSetting.chat_enabled = true+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]+ sign_in(admin_user)+ end++ it "includes the chat channel block with the last message body" do+ get "/discourse-post-event/events/#{event.id}.json"++ expect(response.status).to eq(200)+ expect(response.parsed_body["event"]["channel"]).to be_present+ expect(response.body).to include("private chat message body")+ end+ end+ end+describe "anonymous access to EventsController" dobefore doSiteSetting.calendar_enabled = true