Skip to content

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

FIX: Enforce silence policy across chat mutations (#43653)

Isaac Janzen35a9a45b61e5
diff --git a/plugins/chat/app/controllers/chat/api/channel_threads_controller.rb b/plugins/chat/app/controllers/chat/api/channel_threads_controller.rb
index 10f246d483e..3ec92d0dce3 100644
--- a/plugins/chat/app/controllers/chat/api/channel_threads_controller.rb
+++ b/plugins/chat/app/controllers/chat/api/channel_threads_controller.rb
@@ -55,6 +55,7 @@ class Chat::Api::ChannelThreadsController < Chat::ApiController
def update
::Chat::UpdateThread.call(service_params) do
+ on_failed_policy(:no_silenced_user) { raise Discourse::InvalidAccess }
on_failed_policy(:threading_enabled_for_channel) { raise Discourse::NotFound }
on_failed_policy(:can_view_channel) { raise Discourse::InvalidAccess }
on_failed_policy(:can_edit_thread) { raise Discourse::InvalidAccess }
@@ -72,6 +73,7 @@ class Chat::Api::ChannelThreadsController < Chat::ApiController
def create
::Chat::CreateThread.call(service_params) do
+ on_failed_policy(:no_silenced_user) { raise Discourse::InvalidAccess }
on_success do |thread:|
render_serialized(
thread,
diff --git a/plugins/chat/app/services/chat/create_thread.rb b/plugins/chat/app/services/chat/create_thread.rb
index 877e8a22e74..aac47e1f2e4 100644
--- a/plugins/chat/app/services/chat/create_thread.rb
+++ b/plugins/chat/app/services/chat/create_thread.rb
@@ -26,6 +26,7 @@ module Chat
validates :title, length: { maximum: Chat::Thread::MAX_TITLE_LENGTH }
end
+ policy :no_silenced_user
model :channel
policy :can_view_channel
policy :can_create_thread_in_channel
@@ -42,6 +43,10 @@ module Chat
private
+ def no_silenced_user(guardian:)
+ !guardian.is_silenced?
+ end
+
def fetch_channel(params:)
::Chat::Channel.find_by(id: params.channel_id)
end
diff --git a/plugins/chat/app/services/chat/update_thread.rb b/plugins/chat/app/services/chat/update_thread.rb
index 46f88cd8b7f..90641cfb232 100644
--- a/plugins/chat/app/services/chat/update_thread.rb
+++ b/plugins/chat/app/services/chat/update_thread.rb
@@ -27,6 +27,7 @@ module Chat
validates :title, length: { maximum: Chat::Thread::MAX_TITLE_LENGTH }
end
+ policy :no_silenced_user
model :thread
policy :can_view_channel
policy :can_edit_thread
@@ -36,6 +37,10 @@ module Chat
private
+ def no_silenced_user(guardian:)
+ !guardian.is_silenced?
+ end
+
def fetch_thread(params:)
Chat::Thread.find_by(id: params.thread_id)
end
diff --git a/plugins/chat/lib/chat/guardian_extensions.rb b/plugins/chat/lib/chat/guardian_extensions.rb
index f0856bd91cc..6b9a7e02cb6 100644
--- a/plugins/chat/lib/chat/guardian_extensions.rb
+++ b/plugins/chat/lib/chat/guardian_extensions.rb
@@ -261,6 +261,7 @@ module Chat
end
def can_restore_chat?(message, chatable)
+ return false if @user.silenced?
return false if !can_modify_channel_message?(message.chat_channel)
return false if !is_admin? && !can_preview_chat_channel?(message.chat_channel)
diff --git a/plugins/chat/lib/chat_sdk/thread.rb b/plugins/chat/lib/chat_sdk/thread.rb
index 2b0efc8e379..b491e34af1d 100644
--- a/plugins/chat/lib/chat_sdk/thread.rb
+++ b/plugins/chat/lib/chat_sdk/thread.rb
@@ -102,6 +102,7 @@ module ChatSDK
end
on_failed_policy(:can_view_channel) { raise "Guardian can't view channel" }
on_failed_policy(:can_edit_thread) { raise "Guardian can't edit thread" }
+ on_failed_policy(:no_silenced_user) { raise "Guardian is silenced" }
on_failed_policy(:threading_enabled_for_channel) do
raise "Threading is not enabled for this channel"
end
diff --git a/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb b/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb
index 2809d678200..0b054bc2207 100644
--- a/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb
+++ b/plugins/chat/spec/lib/chat/guardian_extensions_spec.rb
@@ -594,6 +594,19 @@ RSpec.describe Chat::GuardianExtensions do
context "when group moderation is enabled" do
before { SiteSetting.enable_category_group_moderation = true }
+ it "disallows a silenced group moderator from restoring" do
+ moderator = Fabricate(:user)
+ mods = Fabricate(:group)
+ mods.add(moderator)
+ Fabricate(:category_moderation_group, category: chatable, group: mods)
+
+ expect(Guardian.new(moderator).can_restore_chat?(message, chatable)).to eq(true)
+
+ UserSilencer.new(moderator).silence
+
+ expect(Guardian.new(moderator).can_restore_chat?(message, chatable)).to eq(false)
+ end
+
it "allows a group moderator to restore" do
moderator = Fabricate(:user)
mods = Fabricate(:group)
diff --git a/plugins/chat/spec/lib/chat_sdk/thread_spec.rb b/plugins/chat/spec/lib/chat_sdk/thread_spec.rb
index fb2873e337e..8f186238eff 100644
--- a/plugins/chat/spec/lib/chat_sdk/thread_spec.rb
+++ b/plugins/chat/spec/lib/chat_sdk/thread_spec.rb
@@ -24,6 +24,20 @@ describe ChatSDK::Thread do
end
end
+ context "when guardian is silenced" do
+ fab!(:current_user, :user)
+ fab!(:thread_1) { Fabricate(:chat_thread, original_message_user: current_user) }
+
+ before do
+ params[:guardian] = current_user.guardian
+ UserSilencer.new(current_user).silence
+ end
+
+ it "raises a silenced guardian error" do
+ expect { described_class.update_title(**params) }.to raise_error("Guardian is silenced")
+ end
+ end
+
context "when guardian can't see the channel" do
fab!(:thread_1) { Fabricate(:chat_thread, channel: Fabricate(:private_category_channel)) }
diff --git a/plugins/chat/spec/requests/chat/api/channel_messages_controller_spec.rb b/plugins/chat/spec/requests/chat/api/channel_messages_controller_spec.rb
index bf27c04434c..24325c8eebe 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
@@ -614,6 +614,36 @@ RSpec.describe Chat::Api::ChannelMessagesController do
end
describe "#restore" do
+ context "when current user is silenced" do
+ fab!(:recipient, :user)
+ fab!(:channel) { Fabricate(:direct_message_channel, users: [current_user, recipient]) }
+ fab!(:message) { Fabricate(:chat_message, chat_channel: channel, user: current_user) }
+
+ before do
+ message.trash!(current_user)
+ UserSilencer.new(current_user).silence
+ end
+
+ it "does not restore or publish their self-deleted direct message" do
+ publications = nil
+ events =
+ DiscourseEvent.track_events do
+ publications =
+ MessageBus.track_publish do
+ put "/chat/api/channels/#{channel.id}/messages/#{message.id}/restore"
+ end
+ end
+
+ aggregate_failures do
+ expect(response).to have_http_status(:forbidden)
+ expect(response.parsed_body["errors"]).to include(I18n.t("invalid_access"))
+ expect(message.reload).to be_trashed
+ expect(events).not_to include(include(event_name: :chat_message_restored))
+ expect(publications).to be_empty
+ end
+ end
+ end
+
context "when the user no longer has access to a private category channel" do
fab!(:group)
fab!(:private_category) { Fabricate(:private_category, group:) }
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.rb
index 75f022daf76..d2ede7b467c 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
@@ -485,4 +485,83 @@ RSpec.describe Chat::Api::ChannelThreadsController do
end
end
end
+
+ describe "silenced users" do
+ fab!(:direct_message_recipient, :user)
+ fab!(:direct_message_channel) do
+ Fabricate(
+ :direct_message_channel,
+ users: [current_user, direct_message_recipient],
+ threading_enabled: true,
+ )
+ end
+ fab!(:category_creation_message) do
+ Fabricate(:chat_message, chat_channel: public_channel, user: current_user)
+ end
+ fab!(:direct_message_creation_message) do
+ Fabricate(:chat_message, chat_channel: direct_message_channel, user: current_user)
+ end
+ fab!(:category_thread) do
+ Fabricate(
+ :chat_thread,
+ channel: public_channel,
+ original_message_user: current_user,
+ title: "Category thread",
+ )
+ end
+ fab!(:direct_message_thread) do
+ Fabricate(
+ :chat_thread,
+ channel: direct_message_channel,
+ original_message_user: current_user,
+ title: "Direct-message thread",
+ )
+ end
+
+ it "cannot create or rename category or direct-message threads" do
+ UserSilencer.new(current_user).silence
+ responses = []
+
+ events =
+ DiscourseEvent.track_events(:chat_thread_created) do
+ post "/chat/api/channels/#{public_channel.id}/threads",
+ params: {
+ original_message_id: category_creation_message.id,
+ title: "New category thread",
+ }
+ responses << { status: response.status, errors: response.parsed_body["errors"] }
+
+ post "/chat/api/channels/#{direct_message_channel.id}/threads",
+ params: {
+ original_message_id: direct_message_creation_message.id,
+ title: "New direct-message thread",
+ }
+ responses << { status: response.status, errors: response.parsed_body["errors"] }
+
+ put "/chat/api/channels/#{public_channel.id}/threads/#{category_thread.id}",
+ params: {
+ title: "Renamed category thread",
+ }
+ responses << { status: response.status, errors: response.parsed_body["errors"] }
+
+ put "/chat/api/channels/#{direct_message_channel.id}/threads/#{direct_message_thread.id}",
+ params: {
+ title: "Renamed direct-message thread",
+ }
+ responses << { status: response.status, errors: response.parsed_body["errors"] }
+ end
+
+ aggregate_failures do
+ expect(responses.map { |response| response[:status] }).to all(eq(403))
+ expect(responses.map { |response| response[:errors] }).to all(
+ include(I18n.t("invalid_access")),
+ )
+ expect(events).to be_empty
+ expect(category_creation_message.reload.thread).to be_nil
+ expect(direct_message_creation_message.reload.thread).to be_nil
+ expect(category_thread.reload.title).to eq("Category thread")
+ expect(direct_message_thread.reload.title).to eq("Direct-message thread")
+ end
+ end
+ end
end