Skip to content

A platform for community discussion. Free, open, simple.

SECURITY: unauthorized channel membership inference

David Battersby81fd89e74405
diff --git a/plugins/chat/app/services/chat/search_chatable.rb b/plugins/chat/app/services/chat/search_chatable.rb
index 03fb5c08629..8fa1e332a5d 100644
--- a/plugins/chat/app/services/chat/search_chatable.rb
+++ b/plugins/chat/app/services/chat/search_chatable.rb
@@ -85,11 +85,15 @@ module Chat
user_search = user_search.includes(:user_option)
if params.excluded_memberships_channel_id
- user_search =
- user_search.where(
- "NOT EXISTS (SELECT 1 FROM user_chat_channel_memberships WHERE user_id = users.id AND chat_channel_id = ?)",
- params.excluded_memberships_channel_id,
- )
+ channel =
+ Chat::Channel.includes(:chatable).find_by(id: params.excluded_memberships_channel_id)
+ if channel && guardian.can_preview_chat_channel?(channel)
+ user_search =
+ user_search.where(
+ "NOT EXISTS (SELECT 1 FROM user_chat_channel_memberships WHERE user_id = users.id AND chat_channel_id = ?)",
+ params.excluded_memberships_channel_id,
+ )
+ end
end
filter_term = params.term.to_s
diff --git a/plugins/chat/spec/requests/chat/api/chatables_controller_spec.rb b/plugins/chat/spec/requests/chat/api/chatables_controller_spec.rb
index 78e34b6ae84..a4e440db77c 100644
--- a/plugins/chat/spec/requests/chat/api/chatables_controller_spec.rb
+++ b/plugins/chat/spec/requests/chat/api/chatables_controller_spec.rb
@@ -42,5 +42,56 @@ RSpec.describe Chat::Api::ChatablesController do
)
end
end
+
+ describe "excluded_memberships_channel_id" do
+ fab!(:target_user, :user)
+ fab!(:private_dm_channel) do
+ Fabricate(:direct_message_channel, users: [Fabricate(:user), target_user])
+ end
+
+ let(:base_params) do
+ {
+ term: target_user.username,
+ include_users: true,
+ include_groups: false,
+ include_category_channels: false,
+ include_direct_message_channels: false,
+ }
+ end
+
+ before do
+ SiteSetting.direct_message_enabled_groups = Group::AUTO_GROUPS[:everyone]
+ sign_in(current_user)
+ end
+
+ it "does not apply the exclusion filter when the user cannot access the channel" do
+ get "/chat/api/chatables",
+ params: base_params.merge(excluded_memberships_channel_id: private_dm_channel.id)
+
+ expect(response.status).to eq(200)
+ identifiers = response.parsed_body["users"].map { |u| u["identifier"] }
+ expect(identifiers).to include("u-#{target_user.id}")
+ end
+
+ it "does not apply the exclusion filter for a non-existent channel" do
+ get "/chat/api/chatables", params: base_params.merge(excluded_memberships_channel_id: -1)
+
+ expect(response.status).to eq(200)
+ identifiers = response.parsed_body["users"].map { |u| u["identifier"] }
+ expect(identifiers).to include("u-#{target_user.id}")
+ end
+
+ it "applies the exclusion filter when the user can access the channel" do
+ accessible_dm_channel =
+ Fabricate(:direct_message_channel, users: [current_user, target_user])
+
+ get "/chat/api/chatables",
+ params: base_params.merge(excluded_memberships_channel_id: accessible_dm_channel.id)
+
+ expect(response.status).to eq(200)
+ identifiers = response.parsed_body["users"].map { |u| u["identifier"] }
+ expect(identifiers).not_to include("u-#{target_user.id}")
+ end
+ end
end
end