A platform for community discussion. Free, open, simple.
SECURITY: Restrict category chat MessageBus events to eligible users
Natba74bb404ab4
diff --git a/plugins/chat/app/services/chat/publisher.rb b/plugins/chat/app/services/chat/publisher.rbindex ed943b6a393..453e329d79a 100644--- a/plugins/chat/app/services/chat/publisher.rb+++ b/plugins/chat/app/services/chat/publisher.rb@@ -509,28 +509,24 @@ module Chatprivatedef self.permissions(channel)+ if channel.category_channel? && channel.read_restricted?+ group_ids =+ channel.allowed_group_ids.map do |group_id|+ Chat.restricted_category_message_bus_audience_id(group_id)+ end+ return { group_ids: }+ end+group_ids = channel.allowed_group_ids.presenceif group_ids.blank? && channel.category_channel? && !channel.read_restricted?return {} if Chat.anonymous_public_channel_access_allowed?- group_ids = chat_allowed_group_ids+ group_ids = Chat.message_bus_allowed_group_idsend{ user_ids: channel.allowed_user_ids.presence, group_ids: group_ids }.compactend- def self.chat_allowed_group_ids- pseudo_everyone_ids = [Group::AUTO_GROUPS[:everyone], Group::AUTO_GROUPS[:logged_in_users]]- excluded_group_ids = [Group::AUTO_GROUPS[:anonymous_users]]- Chat- .allowed_group_ids- .reject { |group_id| excluded_group_ids.include?(group_id) }- .map do |group_id|- pseudo_everyone_ids.include?(group_id) ? Group::AUTO_GROUPS[:trust_level_0] : group_id- end- .uniq- end-def self.anonymous_guardianGuardian.new(nil)enddiff --git a/plugins/chat/lib/chat/engine.rb b/plugins/chat/lib/chat/engine.rbindex ced8449bb20..579a6ec2d7c 100644--- a/plugins/chat/lib/chat/engine.rb+++ b/plugins/chat/lib/chat/engine.rb@@ -32,6 +32,28 @@ module Chatallowed_group_ids.include?(Group::AUTO_GROUPS[:anonymous_users])end+ def self.message_bus_group_ids_for(group_ids)+ return group_ids if group_ids.blank? || !SiteSetting.chat_enabled+ return group_ids if (group_ids & message_bus_allowed_group_ids).blank?++ group_ids + group_ids.map { |group_id| restricted_category_message_bus_audience_id(group_id) }+ end++ def self.restricted_category_message_bus_audience_id(group_id)+ "chat-restricted-category-group-#{group_id}"+ end++ def self.message_bus_allowed_group_ids+ pseudo_everyone_ids = [Group::AUTO_GROUPS[:everyone], Group::AUTO_GROUPS[:logged_in_users]]++ allowed_group_ids+ .reject { |group_id| group_id == Group::AUTO_GROUPS[:anonymous_users] }+ .map do |group_id|+ pseudo_everyone_ids.include?(group_id) ? Group::AUTO_GROUPS[:trust_level_0] : group_id+ end+ .uniq+ end+def self.message_onebox_templatereturn File.read(MESSAGE_ONEBOX_TEMPLATE_PATH) if Rails.env.development?diff --git a/plugins/chat/plugin.rb b/plugins/chat/plugin.rbindex b9c2ef6a088..835f54042eb 100644--- a/plugins/chat/plugin.rb+++ b/plugins/chat/plugin.rb@@ -47,6 +47,14 @@ endrequire_relative "lib/chat/engine"after_initialize do+ original_message_bus_group_ids_lookup = MessageBus.group_ids_lookup+ MessageBus.group_ids_lookup do |env|+ host = RailsMultisite::ConnectionManagement.host(env)+ RailsMultisite::ConnectionManagement.with_hostname(host) do+ Chat.message_bus_group_ids_for(original_message_bus_group_ids_lookup&.call(env))+ end+ end+register_seedfu_fixtures(Rails.root.join("plugins/chat/db/fixtures"))UserNotifications.append_view_path(File.expand_path("../app/views", __FILE__))diff --git a/plugins/chat/spec/services/chat/publisher_spec.rb b/plugins/chat/spec/services/chat/publisher_spec.rbindex d0b98f32c45..7df48203c23 100644--- a/plugins/chat/spec/services/chat/publisher_spec.rb+++ b/plugins/chat/spec/services/chat/publisher_spec.rb@@ -248,6 +248,29 @@ describe Chat::Publisher doexpect(messages.first.user_ids).to eq(nil)end+ it "publishes restricted category messages to bounded category permission groups" do+ category_group = Fabricate(:group)+ chat_group = Fabricate(:group)+ category = Fabricate(:private_category, group: category_group)+ restricted_channel = Fabricate(:category_channel, chatable: category)+ message = Fabricate(:chat_message, chat_channel: restricted_channel)+ SiteSetting.chat_allowed_groups = chat_group.id++ published_message =+ MessageBus+ .track_publish(described_class.root_message_bus_channel(restricted_channel.id)) do+ described_class.publish_new!(restricted_channel, message, staged_id)+ end+ .first++ expect(published_message.user_ids).to eq(nil)+ expect(published_message.group_ids).to contain_exactly(+ *restricted_channel.allowed_group_ids.map do |group_id|+ "chat-restricted-category-group-#{group_id}"+ end,+ )+ end+it "publishes to trust level 0 when chat is allowed for everyone" doSiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]