A platform for community discussion. Free, open, simple.
SECURITY: DM communication-preference bypass when adding members
Martin Brennan46c812b96ef8
diff --git a/plugins/chat/app/controllers/chat/api/channels_memberships_controller.rb b/plugins/chat/app/controllers/chat/api/channels_memberships_controller.rbindex fa9d90c5285..d8f34c9f386 100644--- a/plugins/chat/app/controllers/chat/api/channels_memberships_controller.rb+++ b/plugins/chat/app/controllers/chat/api/channels_memberships_controller.rb@@ -36,6 +36,10 @@ class Chat::Api::ChannelsMembershipsController < Chat::Api::ChannelsControlleron_failed_policy(:can_add_users_to_channel) dorender_json_error(I18n.t("chat.errors.users_cant_be_added_to_channel"))end+ on_failed_policy(:actor_allows_dms) do+ render_json_error(I18n.t("chat.errors.actor_disallowed_dms"))+ end+ on_failed_policy(:targets_allow_dms_from_user) { |policy| render_json_error(policy.reason) }on_failed_policy(:satisfies_dms_max_users_limit) do |policy|render_json_dump({ error: policy.reason }, status: 400)enddiff --git a/plugins/chat/app/services/chat/add_users_to_channel.rb b/plugins/chat/app/services/chat/add_users_to_channel.rbindex 12192c5dfce..efbaba7a181 100644--- a/plugins/chat/app/services/chat/add_users_to_channel.rb+++ b/plugins/chat/app/services/chat/add_users_to_channel.rb@@ -44,6 +44,10 @@ module Chatmodel :channelpolicy :can_add_users_to_channelmodel :target_users, optional: true+ model :user_comm_screener+ policy :actor_allows_dms+ policy :targets_allow_dms_from_user,+ class_name: Chat::DirectMessageChannel::Policy::CanCommunicateAllPartiespolicy :satisfies_dms_max_users_limit,class_name: Chat::DirectMessageChannel::Policy::MaxUsersExcess@@ -60,6 +64,14 @@ module Chat::Chat::Channel.includes(:chatable).find_by(id: params.channel_id)end+ def fetch_user_comm_screener(target_users:, guardian:)+ UserCommScreener.new(acting_user: guardian.user, target_user_ids: target_users.map(&:id))+ end++ def actor_allows_dms(user_comm_screener:)+ !user_comm_screener.actor_disallowing_all_pms?+ end+def can_add_users_to_channel(guardian:, channel:)return false if !guardian.user.admin? && !channel.joined_by?(guardian.user)diff --git a/plugins/chat/spec/services/chat/add_users_to_channel_spec.rb b/plugins/chat/spec/services/chat/add_users_to_channel_spec.rbindex 1ba68b2e569..573680a3305 100644--- a/plugins/chat/spec/services/chat/add_users_to_channel_spec.rb+++ b/plugins/chat/spec/services/chat/add_users_to_channel_spec.rb@@ -203,5 +203,62 @@ RSpec.describe Chat::AddUsersToChannel doit { is_expected.to fail_a_policy(:can_add_users_to_channel) }end++ context "when the actor is not allowing anyone to message them" do+ before do+ channel.add(current_user)+ current_user.user_option.update!(allow_private_messages: false)+ end++ it { is_expected.to fail_a_policy(:actor_allows_dms) }+ end++ context "when one of the target users is ignoring the current user" do+ before do+ channel.add(current_user)+ IgnoredUser.create!(+ user: users.first,+ ignored_user: current_user,+ expiring_at: 1.day.from_now,+ )+ params[:usernames] = [users.first.username]+ end++ it { is_expected.to fail_a_policy(:targets_allow_dms_from_user) }+ end++ context "when one of the target users is muting the current user" do+ before do+ channel.add(current_user)+ MutedUser.create!(user: users.first, muted_user: current_user)+ params[:usernames] = [users.first.username]+ end++ it { is_expected.to fail_a_policy(:targets_allow_dms_from_user) }+ end++ context "when the current user is ignoring one of the target users" do+ before do+ channel.add(current_user)+ IgnoredUser.create!(+ user: current_user,+ ignored_user: users.first,+ expiring_at: 1.day.from_now,+ )+ params[:usernames] = [users.first.username]+ end++ it { is_expected.to fail_a_policy(:targets_allow_dms_from_user) }+ end++ context "when the current user is muting one of the target users" do+ before do+ channel.add(current_user)+ MutedUser.create!(user: current_user, muted_user: users.first)+ params[:usernames] = [users.first.username]+ end++ it { is_expected.to fail_a_policy(:targets_allow_dms_from_user) }+ endendend