A platform for community discussion. Free, open, simple.
FIX: Tighten chat inline-onebox channel scoping (#43654)
Isaac Janzene61a5a2c13e0
diff --git a/plugins/chat/lib/chat/inline_onebox_handler.rb b/plugins/chat/lib/chat/inline_onebox_handler.rbindex 79c40c82d93..e3cc98892fa 100644--- a/plugins/chat/lib/chat/inline_onebox_handler.rb+++ b/plugins/chat/lib/chat/inline_onebox_handler.rb@@ -23,7 +23,7 @@ module Chatreturn if !chat_channelif route[:thread_id].present?- thread = Chat::Thread.find_by(id: route[:thread_id])+ thread = chat_channel.threads.find_by(id: route[:thread_id])return if !threadtitle =@@ -44,7 +44,7 @@ module Chatendend- return if !Guardian.new.can_preview_chat_channel?(chat_channel)+ return if !Guardian.new.can_preview_anonymous_public_chat_channel?(chat_channel){ url: url, title: title }enddiff --git a/plugins/chat/lib/chat/onebox_handler.rb b/plugins/chat/lib/chat/onebox_handler.rbindex 272434f0663..d8007c40c20 100644--- a/plugins/chat/lib/chat/onebox_handler.rb+++ b/plugins/chat/lib/chat/onebox_handler.rb@@ -19,7 +19,7 @@ module Chatthread = chat_channel.threads.find_by(id: route[:thread_id]) if route[:thread_id]end- return if !Guardian.new.can_preview_chat_channel?(chat_channel)+ return if !Guardian.new.can_preview_anonymous_public_chat_channel?(chat_channel)args = build_args(url, chat_channel)diff --git a/plugins/chat/spec/lib/chat/inline_onebox_handler_spec.rb b/plugins/chat/spec/lib/chat/inline_onebox_handler_spec.rbindex 47147bce259..37b614a5d17 100644--- a/plugins/chat/spec/lib/chat/inline_onebox_handler_spec.rb+++ b/plugins/chat/spec/lib/chat/inline_onebox_handler_spec.rb@@ -10,6 +10,11 @@ describe Chat::InlineOneboxHandler dofab!(:user_3) { Fabricate(:user, staged: true) }fab!(:user_4) { Fabricate(:user, suspended_till: 3.weeks.from_now) }+ before do+ SiteSetting.chat_allowed_groups =+ "#{Group::AUTO_GROUPS[:everyone]}|#{Group::AUTO_GROUPS[:anonymous_users]}"+ end+let(:public_chat_url) { "#{Discourse.base_url}/chat/c/-/#{public_channel.id}" }let(:private_chat_url) { "#{Discourse.base_url}/chat/c/-/#{private_channel.id}" }let(:invalid_chat_url) { "#{Discourse.base_url}/chat/c/-/999" }@@ -27,6 +32,14 @@ describe Chat::InlineOneboxHandler do)end+ it "does not render an inline onebox when anonymous public chat access is disabled" do+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:staff]++ expect(+ Chat::InlineOneboxHandler.handle(public_chat_url, { channel_id: public_channel.id }),+ ).to be_nil+ end+it "does not render an inline onebox for a channel which does not exist" dopublic_channel.trash!expect(diff --git a/plugins/chat/spec/lib/chat/onebox_handler_spec.rb b/plugins/chat/spec/lib/chat/onebox_handler_spec.rbindex d287d9efb35..137f3297f5d 100644--- a/plugins/chat/spec/lib/chat/onebox_handler_spec.rb+++ b/plugins/chat/spec/lib/chat/onebox_handler_spec.rb@@ -9,6 +9,11 @@ describe Chat::OneboxHandler dofab!(:user_3) { Fabricate(:user, staged: true) }fab!(:user_4) { Fabricate(:user, suspended_till: 3.weeks.from_now) }+ before do+ SiteSetting.chat_allowed_groups =+ "#{Group::AUTO_GROUPS[:everyone]}|#{Group::AUTO_GROUPS[:anonymous_users]}"+ end+let(:public_chat_url) { "#{Discourse.base_url}/chat/c/-/#{public_channel.id}" }let(:private_chat_url) { "#{Discourse.base_url}/chat/c/-/#{private_channel.id}" }let(:invalid_chat_url) { "#{Discourse.base_url}/chat/c/-/999" }diff --git a/plugins/chat/spec/requests/core_ext/inline_onebox_controller_spec.rb b/plugins/chat/spec/requests/core_ext/inline_onebox_controller_spec.rbnew file mode 100644index 00000000000..26806383262--- /dev/null+++ b/plugins/chat/spec/requests/core_ext/inline_onebox_controller_spec.rb@@ -0,0 +1,35 @@+# frozen_string_literal: true++RSpec.describe InlineOneboxController do+ fab!(:current_user, :user)+ fab!(:direct_message_user_1, :user)+ fab!(:direct_message_user_2, :user)+ fab!(:public_channel, :category_channel)+ fab!(:private_channel) do+ Fabricate(+ :direct_message_channel,+ users: [direct_message_user_1, direct_message_user_2],+ threading_enabled: true,+ )+ end+ fab!(:private_thread) do+ Fabricate(:chat_thread, channel: private_channel, title: "Private thread title")+ end++ before do+ SiteSetting.chat_enabled = true+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]+ sign_in(current_user)+ end++ it "does not disclose a private thread title through a public channel URL" do+ get "/inline-onebox.json",+ params: {+ urls: ["#{Discourse.base_url}/chat/c/-/#{public_channel.id}/t/#{private_thread.id}"],+ }++ expect(response.status).to eq(200)+ expect(response.body).not_to include(private_thread.title)+ expect(response.parsed_body["inline-oneboxes"]).to eq([])+ end+enddiff --git a/plugins/chat/spec/requests/core_ext/onebox_controller_spec.rb b/plugins/chat/spec/requests/core_ext/onebox_controller_spec.rbindex c705e1e79b5..5b51aac3aeb 100644--- a/plugins/chat/spec/requests/core_ext/onebox_controller_spec.rb+++ b/plugins/chat/spec/requests/core_ext/onebox_controller_spec.rb@@ -27,7 +27,8 @@ RSpec.describe OneboxController dobefore doSiteSetting.chat_enabled = true- SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]+ SiteSetting.chat_allowed_groups =+ "#{Group::AUTO_GROUPS[:everyone]}|#{Group::AUTO_GROUPS[:anonymous_users]}"sign_in(current_user)end@@ -47,4 +48,28 @@ RSpec.describe OneboxController doexpect(response.body).to include(public_channel.name)expect(response.body).not_to include(private_original_message.message)end++ it "does not onebox a public channel message for a user excluded from chat" do+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:staff]+ restricted_message =+ Fabricate(+ :chat_message,+ chat_channel: public_channel,+ message: "restricted chat message content",+ )++ get "/chat/api/channels"++ expect(response.status).to eq(403)+ expect(response.parsed_body["errors"]).to be_present++ get "/onebox.json",+ params: {+ url: "#{Discourse.base_url}/chat/c/-/#{public_channel.id}/#{restricted_message.id}",+ refresh: "true",+ }++ expect(response.status).to eq(200)+ expect(response.body).not_to include(restricted_message.message)+ endend