Skip to content

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

FIX: ensures deleted thread is not accessible via API (#38098)

Joffrey JAFFEUX9681c6d3fb63
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 a90fec4c545..016134bfe76 100644
--- a/plugins/chat/app/controllers/chat/api/channel_threads_controller.rb
+++ b/plugins/chat/app/controllers/chat/api/channel_threads_controller.rb
@@ -44,6 +44,7 @@ class Chat::Api::ChannelThreadsController < Chat::ApiController
end
on_failed_policy(:invalid_access) { raise Discourse::InvalidAccess }
on_failed_policy(:threading_enabled_for_channel) { raise Discourse::NotFound }
+ on_failed_policy(:original_message_not_deleted) { raise Discourse::NotFound }
on_model_not_found(:thread) { raise Discourse::NotFound }
on_failure { render(json: failed_json, status: :unprocessable_entity) }
on_failed_contract do |contract|
diff --git a/plugins/chat/app/services/chat/lookup_thread.rb b/plugins/chat/app/services/chat/lookup_thread.rb
index 01b9c6eeaf4..26851a6be56 100644
--- a/plugins/chat/app/services/chat/lookup_thread.rb
+++ b/plugins/chat/app/services/chat/lookup_thread.rb
@@ -27,6 +27,7 @@ module Chat
model :thread
policy :invalid_access
policy :threading_enabled_for_channel
+ policy :original_message_not_deleted
model :membership, optional: true
model :participants, optional: true
@@ -48,6 +49,11 @@ module Chat
thread.channel.threading_enabled || thread.force
end
+ def original_message_not_deleted(thread:, guardian:)
+ thread.original_message.deleted_at.blank? ||
+ guardian.can_moderate_chat?(thread.channel.chatable)
+ end
+
def fetch_membership(thread:, guardian:)
thread.membership_for(guardian.user)
end
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 ac43e6d4266..cb5ceb68ff8 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
@@ -41,6 +41,24 @@ RSpec.describe Chat::Api::ChannelThreadsController do
expect(response.parsed_body["thread"]["id"]).to eq(thread.id)
end
+ context "when the thread original message is deleted" do
+ before { thread.original_message.trash! }
+
+ it "returns 404" do
+ get "/chat/api/channels/#{thread.channel_id}/threads/#{thread.id}"
+ expect(response.status).to eq(404)
+ end
+
+ context "when the current user is a moderator" do
+ before { current_user.update!(moderator: true) }
+
+ it "returns 200" do
+ get "/chat/api/channels/#{thread.channel_id}/threads/#{thread.id}"
+ expect(response.status).to eq(200)
+ end
+ end
+ end
+
context "when the channel_id does not match the thread id" do
fab!(:other_channel, :chat_channel)
diff --git a/plugins/chat/spec/services/chat/lookup_thread_spec.rb b/plugins/chat/spec/services/chat/lookup_thread_spec.rb
index afb09d6ec3b..0f0b7b17e88 100644
--- a/plugins/chat/spec/services/chat/lookup_thread_spec.rb
+++ b/plugins/chat/spec/services/chat/lookup_thread_spec.rb
@@ -51,6 +51,18 @@ RSpec.describe Chat::LookupThread do
it { is_expected.to fail_a_policy(:invalid_access) }
end
+ context "when the original message is deleted" do
+ before { thread.original_message.trash! }
+
+ it { is_expected.to fail_a_policy(:original_message_not_deleted) }
+
+ context "when user is a moderator" do
+ before { current_user.update!(moderator: true) }
+
+ it { is_expected.to run_successfully }
+ end
+ end
+
context "when threading is not enabled for the channel" do
before { channel.update!(threading_enabled: false) }