A platform for community discussion. Free, open, simple.
SECURITY: Restrict chat history reads to joinable channels (#42687)
Juan David Martínez Cubillos98579a3b2031
diff --git a/plugins/chat/app/services/chat/list_channel_messages.rb b/plugins/chat/app/services/chat/list_channel_messages.rbindex 75fbf546b6b..6401a660b54 100644--- a/plugins/chat/app/services/chat/list_channel_messages.rb+++ b/plugins/chat/app/services/chat/list_channel_messages.rb@@ -70,12 +70,8 @@ module Chatenddef can_view_channel(guardian:, channel:)- if guardian.anonymous?- return false if !Chat.anonymous_public_channel_access_allowed?- return false if !channel&.category_channel?- end-- guardian.can_preview_chat_channel?(channel)+ guardian.can_join_chat_channel?(channel) ||+ guardian.can_preview_anonymous_public_chat_channel?(channel)enddef fetch_membership(channel:, guardian:)diff --git a/plugins/chat/app/services/chat/list_channel_pins.rb b/plugins/chat/app/services/chat/list_channel_pins.rbindex 77a1005ce4a..fa02b10c630 100644--- a/plugins/chat/app/services/chat/list_channel_pins.rb+++ b/plugins/chat/app/services/chat/list_channel_pins.rb@@ -22,7 +22,8 @@ module Chatenddef can_view_channel(guardian:, channel:)- guardian.can_preview_chat_channel?(channel)+ guardian.can_join_chat_channel?(channel) ||+ guardian.can_preview_anonymous_public_chat_channel?(channel)enddef fetch_membership(channel:, guardian:)diff --git a/plugins/chat/app/services/chat/list_channel_thread_messages.rb b/plugins/chat/app/services/chat/list_channel_thread_messages.rbindex 3866b26da96..f652d7787eb 100644--- a/plugins/chat/app/services/chat/list_channel_thread_messages.rb+++ b/plugins/chat/app/services/chat/list_channel_thread_messages.rb@@ -75,7 +75,10 @@ module Chatenddef can_view_thread(guardian:, thread:)- guardian.user == Discourse.system_user || guardian.can_preview_chat_channel?(thread.channel)+ return true if guardian.user == Discourse.system_user++ guardian.can_join_chat_channel?(thread.channel) ||+ guardian.can_preview_anonymous_public_chat_channel?(thread.channel)enddef threading_enabled_for_channel(thread:)diff --git a/plugins/chat/app/services/chat/lookup_channel_threads.rb b/plugins/chat/app/services/chat/lookup_channel_threads.rbindex 415bb0e8ac7..52f378d1191 100644--- a/plugins/chat/app/services/chat/lookup_channel_threads.rb+++ b/plugins/chat/app/services/chat/lookup_channel_threads.rb@@ -68,7 +68,7 @@ module Chatenddef can_view_channel(guardian:, channel:)- guardian.can_preview_chat_channel?(channel)+ guardian.can_join_chat_channel?(channel)enddef fetch_threads(guardian:, channel:, params:)diff --git a/plugins/chat/app/services/chat/lookup_thread.rb b/plugins/chat/app/services/chat/lookup_thread.rbindex 12332037371..6d9823feb83 100644--- a/plugins/chat/app/services/chat/lookup_thread.rb+++ b/plugins/chat/app/services/chat/lookup_thread.rb@@ -48,7 +48,8 @@ module Chatenddef invalid_access(guardian:, thread:)- guardian.can_preview_chat_channel?(thread.channel)+ guardian.can_join_chat_channel?(thread.channel) ||+ guardian.can_preview_anonymous_public_chat_channel?(thread.channel)enddef threading_enabled_for_channel(thread:)diff --git a/plugins/chat/app/services/chat/search_message.rb b/plugins/chat/app/services/chat/search_message.rbindex 6b92cd4c314..1ee61cefd71 100644--- a/plugins/chat/app/services/chat/search_message.rb+++ b/plugins/chat/app/services/chat/search_message.rb@@ -62,7 +62,8 @@ module Chatenddef can_view_channel(guardian:, channel:)- guardian.can_preview_chat_channel?(channel)+ guardian.can_join_chat_channel?(channel) ||+ guardian.can_preview_anonymous_public_chat_channel?(channel)enddef fetch_metadata(params:, guardian:)diff --git a/plugins/chat/lib/chat/guardian_extensions.rb b/plugins/chat/lib/chat/guardian_extensions.rbindex c4b78472648..9e0847fb447 100644--- a/plugins/chat/lib/chat/guardian_extensions.rb+++ b/plugins/chat/lib/chat/guardian_extensions.rb@@ -150,9 +150,17 @@ module Chatdef can_preview_chat_channel?(chat_channel)return false if !chat_channel&.chatable+can_see_chatable?(chat_channel.chatable)end+ def can_preview_anonymous_public_chat_channel?(chat_channel)+ return false if !Chat.anonymous_public_channel_access_allowed?+ return false if !chat_channel&.category_channel?++ Guardian.new(nil).can_preview_chat_channel?(chat_channel)+ end+def can_see_chat_message?(message)return false if !can_preview_chat_channel?(message.chat_channel)return true if !message.trashed?diff --git a/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb b/plugins/chat/spec/lib/chat/guardian_extensions_spec.rbindex 0dceeb41652..2809d678200 100644--- a/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb+++ b/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb@@ -225,6 +225,31 @@ RSpec.describe Chat::GuardianExtensions doendend+ describe "#can_preview_anonymous_public_chat_channel?" do+ before do+ SiteSetting.enable_public_channels = true+ SiteSetting.chat_allowed_groups =+ "#{Group::AUTO_GROUPS[:everyone]}|#{Group::AUTO_GROUPS[:anonymous_users]}"+ end++ it "returns true for channels backed by categories anonymous users can see" do+ expect(guardian.can_preview_anonymous_public_chat_channel?(channel)).to eq(true)+ end++ it "returns false for private category channels visible only to the user" do+ category =+ Fabricate(+ :private_category,+ group: Fabricate(:group, users: [user]),+ permission_type: CategoryGroup.permission_types[:readonly],+ )+ channel.update!(chatable: category)++ expect(guardian.can_preview_chat_channel?(channel)).to eq(true)+ expect(guardian.can_preview_anonymous_public_chat_channel?(channel)).to eq(false)+ end+ end+describe "#can_post_in_chatable?" doalias_matcher :be_able_to_post_in_chatable, :be_can_post_in_chatablediff --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 d6e7751e9c0..d4b98218b11 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@@ -114,6 +114,63 @@ RSpec.describe Chat::Api::ChannelMessagesController doendend+ context "when user can only see a readonly category channel" do+ fab!(:readonly_group) { Fabricate(:group, users: [current_user]) }+ fab!(:channel) do+ category =+ Fabricate(+ :private_category,+ group: readonly_group,+ permission_type: CategoryGroup.permission_types[:readonly],+ )+ Fabricate(:category_channel, chatable: category)+ end+ fab!(:restricted_message) do+ Fabricate(+ :chat_message,+ chat_channel: channel,+ message: "Confidential restricted channel history",+ )+ end++ it "does not expose the channel messages" do+ get "/chat/api/channels/#{channel.id}/messages"++ expect(response.status).to eq(403)+ expect(response.parsed_body["error_type"]).to eq("invalid_access")+ expect(response.body).not_to include(restricted_message.message)+ end+ end++ context "when logged-in user can only read an anonymous-public category channel" do+ fab!(:channel) do+ category = Fabricate(:category)+ category.set_permissions(everyone: :readonly)+ category.save!+ Fabricate(:category_channel, chatable: category)+ end+ fab!(:message) { Fabricate(:chat_message, chat_channel: channel) }++ before do+ SiteSetting.chat_allowed_groups =+ "#{Group::AUTO_GROUPS[:everyone]}|#{Group::AUTO_GROUPS[:anonymous_users]}"+ end++ it "exposes the same channel messages anonymous users can read" do+ expect(current_user.guardian.can_join_chat_channel?(channel)).to eq(false)+ expect(current_user.guardian.can_preview_anonymous_public_chat_channel?(channel)).to eq(+ true,+ )++ get "/chat/api/channels/#{channel.id}/messages"++ expect(response.status).to eq(200)+ expect(response.parsed_body["messages"].map { |message| message["id"] }).to contain_exactly(+ message.id,+ )+ end+ end+context "when page_size is above limit" dofab!(:messages) { Fabricate.times(5, :chat_message, chat_channel: channel) }diff --git a/plugins/chat/spec/requests/chat/api/channel_pins_controller_spec.rb b/plugins/chat/spec/requests/chat/api/channel_pins_controller_spec.rbindex 227d9cd8612..48f9a4204f5 100644--- a/plugins/chat/spec/requests/chat/api/channel_pins_controller_spec.rb+++ b/plugins/chat/spec/requests/chat/api/channel_pins_controller_spec.rb@@ -72,6 +72,38 @@ RSpec.describe Chat::Api::ChannelPinsController doexpect(response.status).to eq(403)endend++ context "when user can only see a readonly category channel" do+ fab!(:readonly_group) { Fabricate(:group, users: [user]) }+ fab!(:channel) do+ category =+ Fabricate(+ :private_category,+ group: readonly_group,+ permission_type: CategoryGroup.permission_types[:readonly],+ )+ Fabricate(:category_channel, chatable: category)+ end+ fab!(:message) do+ Fabricate(+ :chat_message,+ chat_channel: channel,+ message: "Confidential restricted pinned message",+ )+ end++ before { sign_in(user) }++ it "does not expose pinned messages" do+ Fabricate(:chat_pinned_message, chat_channel: channel, chat_message: message)++ get "/chat/api/channels/#{channel.id}/pins"++ expect(response.status).to eq(403)+ expect(response.parsed_body["error_type"]).to eq("invalid_access")+ expect(response.body).not_to include(message.message)+ end+ endenddescribe "#mark_read" dodiff --git a/plugins/chat/spec/requests/chat/api/channel_thread_messages_controller_spec.rb b/plugins/chat/spec/requests/chat/api/channel_thread_messages_controller_spec.rbindex 4ab7a38439b..77c91649dae 100644--- a/plugins/chat/spec/requests/chat/api/channel_thread_messages_controller_spec.rb+++ b/plugins/chat/spec/requests/chat/api/channel_thread_messages_controller_spec.rb@@ -91,6 +91,36 @@ RSpec.describe Chat::Api::ChannelThreadMessagesController doendend+ context "when user can only see a readonly category channel" do+ fab!(:readonly_group) { Fabricate(:group, users: [current_user]) }+ fab!(:thread) do+ category =+ Fabricate(+ :private_category,+ group: readonly_group,+ permission_type: CategoryGroup.permission_types[:readonly],+ )+ channel = Fabricate(:category_channel, chatable: category, threading_enabled: true)+ Fabricate(:chat_thread, channel:)+ end+ fab!(:restricted_message) do+ Fabricate(+ :chat_message,+ chat_channel: thread.channel,+ thread:,+ message: "Confidential restricted thread history",+ )+ end++ it "does not expose the thread messages" do+ get "/chat/api/channels/#{thread.channel.id}/threads/#{thread.id}/messages"++ expect(response.status).to eq(403)+ expect(response.parsed_body["error_type"]).to eq("invalid_access")+ expect(response.body).not_to include(restricted_message.message)+ end+ end+context "when page_size is above limit" dofab!(:message_3) { Fabricate(:chat_message, thread:, chat_channel: thread.channel) }diff --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 9deab720889..2e994025bfc 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@@ -138,6 +138,37 @@ RSpec.describe Chat::Api::ChannelThreadsController doendend+ context "when user can only see a readonly category channel" do+ fab!(:readonly_group) { Fabricate(:group, users: [current_user]) }+ fab!(:readonly_channel) do+ category =+ Fabricate(+ :private_category,+ group: readonly_group,+ permission_type: CategoryGroup.permission_types[:readonly],+ )+ Fabricate(:category_channel, chatable: category, threading_enabled: true)+ end+ fab!(:restricted_message) do+ Fabricate(+ :chat_message,+ chat_channel: readonly_channel,+ message: "Confidential restricted thread lookup",+ )+ end+ fab!(:thread) do+ Fabricate(:chat_thread, channel: readonly_channel, original_message: restricted_message)+ end++ it "does not expose the thread" do+ get "/chat/api/channels/#{readonly_channel.id}/threads/#{thread.id}"++ expect(response.status).to eq(403)+ expect(response.parsed_body["error_type"]).to eq("invalid_access")+ expect(response.body).not_to include(restricted_message.message)+ end+ end+context "when user cannot chat" dobefore { SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:trust_level_4] }@@ -228,6 +259,43 @@ RSpec.describe Chat::Api::ChannelThreadsController doendend+ context "when user can only see a readonly category channel" do+ fab!(:readonly_group) { Fabricate(:group, users: [current_user]) }+ fab!(:readonly_channel) do+ category =+ Fabricate(+ :private_category,+ group: readonly_group,+ permission_type: CategoryGroup.permission_types[:readonly],+ )+ Fabricate(:category_channel, chatable: category, threading_enabled: true)+ end+ fab!(:restricted_message) do+ Fabricate(+ :chat_message,+ chat_channel: readonly_channel,+ message: "Confidential restricted thread list",+ )+ end++ before do+ Fabricate(+ :chat_thread,+ channel: readonly_channel,+ original_message: restricted_message,+ with_replies: 1,+ )+ end++ it "does not expose thread list messages" do+ get "/chat/api/channels/#{readonly_channel.id}/threads"++ expect(response.status).to eq(403)+ expect(response.parsed_body["error_type"]).to eq("invalid_access")+ expect(response.body).not_to include(restricted_message.message)+ end+ end+context "when channel does not have threading enabled" dobefore { public_channel.update!(threading_enabled: false) }diff --git a/plugins/chat/spec/requests/chat/api/search_controller_spec.rb b/plugins/chat/spec/requests/chat/api/search_controller_spec.rbindex 8bfbf3f0d76..81d384a0fd7 100644--- a/plugins/chat/spec/requests/chat/api/search_controller_spec.rb+++ b/plugins/chat/spec/requests/chat/api/search_controller_spec.rb@@ -140,6 +140,37 @@ RSpec.describe Chat::Api::SearchController doexpect(response.status).to eq(200)end++ context "when channel_id refers to a readonly category channel" do+ fab!(:readonly_group) { Fabricate(:group, users: [current_user]) }+ fab!(:readonly_channel) do+ category =+ Fabricate(+ :private_category,+ group: readonly_group,+ permission_type: CategoryGroup.permission_types[:readonly],+ )+ Fabricate(:category_channel, chatable: category)+ end+ fab!(:restricted_message) do+ Fabricate(+ :chat_message,+ chat_channel: readonly_channel,+ message: "Confidential restricted search message",+ )+ end++ it "does not expose scoped search results" do+ get "/chat/api/search.json",+ params: {+ query: restricted_message.message,+ channel_id: readonly_channel.id,+ }++ expect(response.status).to eq(404)+ expect(response.body).not_to include(restricted_message.message)+ end+ endendendenddiff --git a/plugins/chat/spec/services/chat/list_channel_messages_spec.rb b/plugins/chat/spec/services/chat/list_channel_messages_spec.rbindex dd1d26fb9a2..37febd4d4b0 100644--- a/plugins/chat/spec/services/chat/list_channel_messages_spec.rb+++ b/plugins/chat/spec/services/chat/list_channel_messages_spec.rb@@ -66,7 +66,10 @@ RSpec.describe Chat::ListChannelMessages dolet(:params) { { channel_id:, **optional_params } }let(:dependencies) { { guardian: } }- before { channel.add(user) }+ before do+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]+ channel.add(user)+ endcontext "when data is not valid" dolet(:channel_id) { nil }diff --git a/plugins/chat/spec/services/chat/list_channel_pins_spec.rb b/plugins/chat/spec/services/chat/list_channel_pins_spec.rbindex 74c5813f709..4f29e4ac014 100644--- a/plugins/chat/spec/services/chat/list_channel_pins_spec.rb+++ b/plugins/chat/spec/services/chat/list_channel_pins_spec.rb@@ -45,6 +45,21 @@ RSpec.describe Chat::ListChannelPins doit { is_expected.to fail_a_policy(:can_view_channel) }end+ context "when anonymous public channel access is allowed" do+ let(:guardian) { Guardian.new(nil) }++ before do+ SiteSetting.chat_allowed_groups =+ "#{Group::AUTO_GROUPS[:everyone]}|#{Group::AUTO_GROUPS[:anonymous_users]}"+ end++ it { is_expected.to run_successfully }++ it "returns pinned messages" do+ expect(result[:pins]).to contain_exactly(pin_1, pin_2)+ end+ end+context "when all conditions are met" doit { is_expected.to run_successfully }diff --git a/plugins/chat/spec/services/chat/list_channel_thread_messages_spec.rb b/plugins/chat/spec/services/chat/list_channel_thread_messages_spec.rbindex 7cfd7fd47b9..162dde8ac3d 100644--- a/plugins/chat/spec/services/chat/list_channel_thread_messages_spec.rb+++ b/plugins/chat/spec/services/chat/list_channel_thread_messages_spec.rb@@ -94,7 +94,10 @@ RSpec.describe Chat::ListChannelThreadMessages dolet(:params) { { thread_id:, channel_id: thread.channel_id, **optional_params } }let(:dependencies) { { guardian: } }- before { thread.channel.add(user) }+ before do+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]+ thread.channel.add(user)+ endcontext "when data is not valid" dolet(:thread_id) { nil }diff --git a/plugins/chat/spec/services/chat/lookup_channel_threads_spec.rb b/plugins/chat/spec/services/chat/lookup_channel_threads_spec.rbindex 90192f71a55..042e1f6b5bf 100644--- a/plugins/chat/spec/services/chat/lookup_channel_threads_spec.rb+++ b/plugins/chat/spec/services/chat/lookup_channel_threads_spec.rb@@ -92,6 +92,8 @@ RSpec.describe ::Chat::LookupChannelThreads dolet(:params) { { channel_id:, limit:, offset: } }let(:dependencies) { { guardian: } }+ before { SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone] }+context "when data is invalid" dolet(:channel_id) { nil }@@ -116,6 +118,21 @@ RSpec.describe ::Chat::LookupChannelThreads doit { is_expected.to fail_a_policy(:can_view_channel) }end+ context "when the user can only see a readonly category channel" do+ fab!(:readonly_group) { Fabricate(:group, users: [current_user]) }+ fab!(:channel) do+ category =+ Fabricate(+ :private_category,+ group: readonly_group,+ permission_type: CategoryGroup.permission_types[:readonly],+ )+ Fabricate(:category_channel, chatable: category, threading_enabled: true)+ end++ it { is_expected.to fail_a_policy(:can_view_channel) }+ end+context "when channel has no threads" doit { is_expected.to fail_to_find_a_model(:threads) }enddiff --git a/plugins/chat/spec/services/chat/lookup_thread_spec.rb b/plugins/chat/spec/services/chat/lookup_thread_spec.rbindex 0f0b7b17e88..4e685489ebe 100644--- a/plugins/chat/spec/services/chat/lookup_thread_spec.rb+++ b/plugins/chat/spec/services/chat/lookup_thread_spec.rb@@ -19,6 +19,8 @@ RSpec.describe Chat::LookupThread dolet(:params) { { thread_id: thread.id, channel_id: thread.channel_id } }let(:dependencies) { { guardian: } }+ before { SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone] }+context "when all steps pass" doit { is_expected.to run_successfully }@@ -51,6 +53,23 @@ RSpec.describe Chat::LookupThread doit { is_expected.to fail_a_policy(:invalid_access) }end+ context "when the user can only see a readonly category channel" do+ fab!(:readonly_group) { Fabricate(:group, users: [current_user]) }+ fab!(:readonly_channel) do+ category =+ Fabricate(+ :private_category,+ group: readonly_group,+ permission_type: CategoryGroup.permission_types[:readonly],+ )+ Fabricate(:category_channel, chatable: category, threading_enabled: true)+ end++ before { thread.update!(channel: readonly_channel) }++ it { is_expected.to fail_a_policy(:invalid_access) }+ end+context "when the original message is deleted" dobefore { thread.original_message.trash! }diff --git a/plugins/chat/spec/services/chat/search_message_spec.rb b/plugins/chat/spec/services/chat/search_message_spec.rbindex 93a7e0fda2f..5e0578ec4d4 100644--- a/plugins/chat/spec/services/chat/search_message_spec.rb+++ b/plugins/chat/spec/services/chat/search_message_spec.rb@@ -24,6 +24,7 @@ RSpec.describe Chat::SearchMessage dobefore doSearchIndexer.enableSiteSetting.chat_enabled = true+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]endcontext "when contract is not valid" do@@ -44,6 +45,21 @@ RSpec.describe Chat::SearchMessage doit { is_expected.to fail_a_policy(:can_view_channel) }end++ context "when the user can only see a readonly category channel" do+ fab!(:readonly_group) { Fabricate(:group, users: [current_user]) }+ fab!(:channel) do+ category =+ Fabricate(+ :private_category,+ group: readonly_group,+ permission_type: CategoryGroup.permission_types[:readonly],+ )+ Fabricate(:category_channel, chatable: category)+ end++ it { is_expected.to fail_a_policy(:can_view_channel) }+ endendcontext "when everything is ok" do