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.rbindex 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::ApiControllerendon_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.rbindex 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 Chatmodel :threadpolicy :invalid_accesspolicy :threading_enabled_for_channel+ policy :original_message_not_deletedmodel :membership, optional: truemodel :participants, optional: true@@ -48,6 +49,11 @@ module Chatthread.channel.threading_enabled || thread.forceend+ 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)enddiff --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 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 doexpect(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" dofab!(: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.rbindex 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 doit { 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" dobefore { channel.update!(threading_enabled: false) }