A platform for community discussion. Free, open, simple.
FIX: prevent chat reactions from non channel members (#37078)
David Battersby2b24fc91c1f4
diff --git a/plugins/chat/config/locales/server.en.yml b/plugins/chat/config/locales/server.en.ymlindex 63b7ae68475..93fe7665fdf 100644--- a/plugins/chat/config/locales/server.en.yml+++ b/plugins/chat/config/locales/server.en.yml@@ -68,6 +68,7 @@ en:closed: "The channel is closed, no messages can be edited or deleted"read_only: "The channel is read only, no messages can be edited or deleted"user_cannot_send_message: "You cannot send messages at this time."+ user_not_in_channel: "You must be a member of this channel."rate_limit_exceeded: "Exceeded the limit of chat messages that can be sent within 30 seconds"auto_silence_from_flags: "Chat message flagged with score high enough to silence user."channel_cannot_be_archived: "The channel cannot be archived at this time, it must be either closed or open to archive."diff --git a/plugins/chat/lib/chat/message_reactor.rb b/plugins/chat/lib/chat/message_reactor.rbindex 74dcdc2c8c5..0cb9ce14399 100644--- a/plugins/chat/lib/chat/message_reactor.rb+++ b/plugins/chat/lib/chat/message_reactor.rb@@ -22,7 +22,7 @@ module Chatreaction = nilActiveRecord::Base.transaction do- enforce_channel_membership!+ enforce_channel_membership!(react_action)reaction = create_reaction(message, react_action, emoji)end@@ -45,8 +45,14 @@ module Chatendend- def enforce_channel_membership!- Chat::ChannelMembershipManager.new(@chat_channel).follow(@user)+ def enforce_channel_membership!(action)+ return if action == REMOVE_REACTION || @chat_channel.membership_for(@user)&.following++ raise Discourse::InvalidAccess.new(+ nil,+ nil,+ custom_message: "chat.errors.user_not_in_channel",+ )enddef validate_channel_status!diff --git a/plugins/chat/spec/lib/chat/message_reactor_spec.rb b/plugins/chat/spec/lib/chat/message_reactor_spec.rbindex d8797b648e2..b32aeb3073c 100644--- a/plugins/chat/spec/lib/chat/message_reactor_spec.rb+++ b/plugins/chat/spec/lib/chat/message_reactor_spec.rb@@ -4,11 +4,15 @@ describe Chat::MessageReactor dosubject(:message_reactor) { described_class.new(reacting_user, channel) }fab!(:reacting_user, :user)+ fab!(:other_user, :user)fab!(:channel, :category_channel)fab!(:reactor) { described_class.new(reacting_user, channel) }fab!(:message_1) { Fabricate(:chat_message, chat_channel: channel, user: reacting_user) }- before { SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone] }+ before do+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]+ channel.add(reacting_user)+ endit "calls guardian ensure_can_join_chat_channel!" doGuardian.any_instance.expects(:ensure_can_join_chat_channel!).once@@ -91,19 +95,48 @@ describe Chat::MessageReactor doendend- it "creates a membership when not present" do+ it "raises an error when user is not a member of the channel" do+ channel.membership_for(reacting_user).destroy!+expect {reactor.react!(message_id: message_1.id, react_action: :add, emoji: ":heart:")- }.to change(Chat::UserChatChannelMembership, :count).by(1)+ }.to raise_error(Discourse::InvalidAccess)+ end++ it "raises an error when user is not following the channel" do+ channel.membership_for(reacting_user).update!(following: false)++ expect {+ reactor.react!(message_id: message_1.id, react_action: :add, emoji: ":heart:")+ }.to raise_error(Discourse::InvalidAccess)+ end++ it "allows removing a reaction when not following" do+ Chat::MessageReaction.create!(chat_message: message_1, user: reacting_user, emoji: ":heart:")++ channel.membership_for(reacting_user).update!(following: false)++ expect {+ reactor.react!(message_id: message_1.id, react_action: :remove, emoji: ":heart:")+ }.to change(Chat::MessageReaction, :count).by(-1)end- it "doesn’t create a membership when present" do- Chat::UserChatChannelMembership.create!(- user: reacting_user,- chat_channel: channel,- following: true,+ it "does not allow reacting to an existing emoji when not following" do+ channel.add(other_user)+ described_class.new(other_user, channel).react!(+ message_id: message_1.id,+ react_action: :add,+ emoji: ":heart:",)+ channel.membership_for(reacting_user).update!(following: false)++ expect {+ reactor.react!(message_id: message_1.id, react_action: :add, emoji: ":heart:")+ }.to raise_error(Discourse::InvalidAccess)+ expect(message_1.reactions.where(emoji: ":heart:").count).to eq(1)+ end+ it "doesn't create a membership when present" doexpect {reactor.react!(message_id: message_1.id, react_action: :add, emoji: ":heart:")}.not_to change(Chat::UserChatChannelMembership, :count)diff --git a/plugins/chat/spec/requests/chat_controller_spec.rb b/plugins/chat/spec/requests/chat_controller_spec.rbindex c08c846af30..aca6fe419cf 100644--- a/plugins/chat/spec/requests/chat_controller_spec.rb+++ b/plugins/chat/spec/requests/chat_controller_spec.rb@@ -206,7 +206,7 @@ RSpec.describe Chat::ChatController doexpect(response.status).to eq(400)end- it "creates a membership when reacting to channel without a membership record" do+ it "errors when reacting to channel without a membership record" dosign_in(user)expect {@@ -215,8 +215,9 @@ RSpec.describe Chat::ChatController doemoji: ":heart:",react_action: "add",}- }.to change { Chat::UserChatChannelMembership.count }.by(1)- expect(response.status).to eq(200)+ }.not_to change { Chat::UserChatChannelMembership.count }+ expect(response.status).to eq(403)+ expect(response.parsed_body["errors"]).to include(I18n.t("chat.errors.user_not_in_channel"))endit "errors when user tries to react to private channel they can't access" dodiff --git a/plugins/chat/spec/system/react_to_message_spec.rb b/plugins/chat/spec/system/react_to_message_spec.rbindex c41faef264f..3097cdcabd4 100644--- a/plugins/chat/spec/system/react_to_message_spec.rb+++ b/plugins/chat/spec/system/react_to_message_spec.rb@@ -16,7 +16,7 @@ RSpec.describe "React to message", type: :system doendcontext "when other user has reacted" do- fab!(:reaction_1) do+ let!(:reaction_1) doChat::MessageReactor.new(other_user, category_channel_1).react!(message_id: message_1.id,react_action: :add,@@ -49,7 +49,7 @@ RSpec.describe "React to message", type: :system doendcontext "when current user reacts" do- fab!(:reaction_1) do+ let!(:reaction_1) doChat::MessageReactor.new(other_user, category_channel_1).react!(message_id: message_1.id,react_action: :add,@@ -147,9 +147,11 @@ RSpec.describe "React to message", type: :system doendcontext "when current user and another have reacted" do- fab!(:other_user) { Fabricate(:user, group_ids: [Group::AUTO_GROUPS[:trust_level_1]]) }+ fab!(:another_user) { Fabricate(:user, group_ids: [Group::AUTO_GROUPS[:trust_level_1]]) }- fab!(:reaction_1) do+ before { category_channel_1.add(another_user) }++ let!(:reaction_1) doChat::MessageReactor.new(current_user, category_channel_1).react!(message_id: message_1.id,react_action: :add,@@ -157,8 +159,8 @@ RSpec.describe "React to message", type: :system do)end- fab!(:reaction_2) do- Chat::MessageReactor.new(other_user, category_channel_1).react!(+ let!(:reaction_2) do+ Chat::MessageReactor.new(another_user, category_channel_1).react!(message_id: message_1.id,react_action: :add,emoji: "woman_detective",@@ -180,7 +182,7 @@ RSpec.describe "React to message", type: :system doendcontext "when current user has reacted" do- fab!(:reaction_1) do+ let!(:reaction_1) doChat::MessageReactor.new(current_user, category_channel_1).react!(message_id: message_1.id,react_action: :add,@@ -214,7 +216,9 @@ RSpec.describe "React to message", type: :system docontext "when receiving a duplicate reaction event" dofab!(:user_1) { Fabricate(:user, group_ids: [Group::AUTO_GROUPS[:trust_level_1]]) }- fab!(:reaction_2) do+ before { category_channel_1.add(user_1) }++ let!(:reaction_2) doChat::MessageReactor.new(user_1, category_channel_1).react!(message_id: message_1.id,react_action: :add,