A platform for community discussion. Free, open, simple.
SECURITY: Respect topic visibility in reviewable score serializer
Gabriel Grubba65f6d7626ac1
diff --git a/app/models/reviewable_score.rb b/app/models/reviewable_score.rbindex 3cfc2f3d908..a20acf592b5 100644--- a/app/models/reviewable_score.rb+++ b/app/models/reviewable_score.rb@@ -97,6 +97,21 @@ class ReviewableScore < ActiveRecord::Basereturn if meta_topic.blank?Reviewable::Conversation.new(meta_topic)end++ def notify_moderators_flag_message?+ return false unless meta_topic&.private_message? && meta_topic.notify_moderators?+ return false unless reviewable.target.is_a?(Post)++ PostAction+ .with_deleted+ .where(+ post_id: reviewable.target_id,+ user_id: user_id,+ post_action_type_id: reviewable_score_type,+ related_post_id: Post.where(topic_id: meta_topic_id).select(:id),+ )+ .exists?+ endend# == Schema Informationdiff --git a/app/models/topic.rb b/app/models/topic.rbindex 780503f7d6b..307878a5895 100644--- a/app/models/topic.rb+++ b/app/models/topic.rb@@ -544,6 +544,10 @@ class Topic < ActiveRecord::Basesubtype == TopicSubtype.moderator_warningend+ def notify_moderators?+ subtype == TopicSubtype.notify_moderators+ end+# all users (in groups or directly targeted) that are going to get the pmdef all_allowed_usersmoderators_sql = " UNION #{User.moderators.to_sql}" if private_message? &&diff --git a/app/serializers/reviewable_score_serializer.rb b/app/serializers/reviewable_score_serializer.rbindex 23c8d7040d1..870ca4ea915 100644--- a/app/serializers/reviewable_score_serializer.rb+++ b/app/serializers/reviewable_score_serializer.rb@@ -34,7 +34,8 @@ class ReviewableScoreSerializer < ApplicationSerializerhas_one :reviewed_by, serializer: BasicUserSerializer, root: "users"def include_reviewable_conversation?- object.meta_topic.present? && scope&.can_see?(object.meta_topic)+ return false if object.meta_topic.blank?+ scope&.can_see?(object.meta_topic) || object.notify_moderators_flag_message?enddef agree_statsdiff --git a/spec/models/reviewable_score_spec.rb b/spec/models/reviewable_score_spec.rbindex 271ee14f9e4..28e97b05c1c 100644--- a/spec/models/reviewable_score_spec.rb+++ b/spec/models/reviewable_score_spec.rb@@ -206,4 +206,49 @@ RSpec.describe ReviewableScore, type: :model doexpect(ReviewableScore.calculate_score(user, 2, 3)).to eq(flag_score + 2 + 3)endend++ describe "#notify_moderators_flag_message?" do+ subject(:score) { result.reviewable_score }++ fab!(:flagger) { Fabricate(:user, refresh_auto_groups: true) }+ fab!(:post)++ let(:result) { PostActionCreator.notify_moderators(flagger, post, "flag reason") }++ context "when the meta topic is the flag's own notify_moderators message" do+ it { is_expected.to be_notify_moderators_flag_message }+ end++ context "when there is no meta topic" do+ before { score.update!(meta_topic: nil) }++ it { is_expected.not_to be_notify_moderators_flag_message }+ end++ context "when the meta topic is a regular private message" do+ let(:regular_pm) { Fabricate(:private_message_topic) }++ before { score.update!(meta_topic: regular_pm) }++ it { is_expected.not_to be_notify_moderators_flag_message }+ end++ context "when the reviewable does not target a post" do+ let(:non_post_target) { Fabricate(:user) }++ before { score.reviewable.update!(target: non_post_target) }++ it { is_expected.not_to be_notify_moderators_flag_message }+ end++ context "when the meta topic belongs to a different flag" do+ let(:other_result) do+ PostActionCreator.notify_moderators(flagger, Fabricate(:post), "other flag reason")+ end++ before { score.update!(meta_topic: other_result.reviewable_score.meta_topic) }++ it { is_expected.not_to be_notify_moderators_flag_message }+ end+ endenddiff --git a/spec/requests/reviewables_controller_spec.rb b/spec/requests/reviewables_controller_spec.rbindex 16175d1e5a2..2f48dc4310e 100644--- a/spec/requests/reviewables_controller_spec.rb+++ b/spec/requests/reviewables_controller_spec.rb@@ -605,6 +605,86 @@ RSpec.describe ReviewablesController doendend+ context "when a category moderator cannot see the flag's PM topic" do+ subject(:show_reviewable) { get "/review/#{reviewable.id}.json" }++ fab!(:post)+ fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }+ fab!(:category_moderator, :user)+ fab!(:group)+ fab!(:category_moderation_group) do+ Fabricate(:category_moderation_group, category: post.topic.category, group:)+ end++ let(:flag_reason) { "this is the flag reason" }+ let(:result) { PostActionCreator.notify_moderators(user, post, flag_reason) }+ let(:reviewable) { result.reviewable }+ let(:reviewable_score) { result.reviewable_score }+ let(:meta_topic) { reviewable_score.meta_topic }++ let(:response_body) { response.parsed_body }+ let(:serialized_score) { response_body["reviewable_scores"].first }+ let(:conversation_id) { serialized_score["reviewable_conversation_id"] }+ let(:conversation) do+ response_body["reviewable_conversations"].find { |convo| convo["id"] == conversation_id }+ end+ let(:flagger_post) do+ response_body["conversation_posts"].find do |convo_post|+ convo_post["id"] == conversation["conversation_post_ids"].first+ end+ end++ before do+ SiteSetting.enable_category_group_moderation = false+ reviewable_score+ SiteSetting.enable_category_group_moderation = true+ group.add(category_moderator)+ sign_in(category_moderator)+ end++ it "cannot see the flag's PM topic directly" do+ expect(category_moderator.guardian.can_see_topic?(meta_topic)).to eq(false)+ end++ context "when the conversation belongs to the flag being reviewed" do+ it "returns the notify moderators conversation" do+ show_reviewable++ expect(response.code).to eq("200")+ expect(conversation_id).to eq(meta_topic.id)+ expect(conversation).to be_present+ expect(flagger_post).to include(+ "user_id" => user.id,+ "excerpt" => a_string_including(flag_reason),+ )+ end+ end++ context "when the score points at an unrelated notify_moderators message" do+ let(:unrelated_flag_reason) { "unrelated flag reason" }+ let(:unrelated_post) { Fabricate(:post) }+ let(:unrelated_result) do+ PostActionCreator.notify_moderators(user, unrelated_post, unrelated_flag_reason)+ end++ before do+ SiteSetting.enable_category_group_moderation = false+ reviewable_score.update!(meta_topic: unrelated_result.reviewable_score.meta_topic)+ SiteSetting.enable_category_group_moderation = true+ end++ it "does not return the conversation" do+ show_reviewable++ expect(response.code).to eq("200")+ expect(conversation_id).to be_blank+ expect(response_body["reviewable_conversations"]).to be_blank+ expect(response_body["conversation_posts"]).to be_blank+ expect(response.body).not_to include(unrelated_flag_reason)+ end+ end+ end+context "with an inaccessible conversation" doit "does not serialize the conversation" doSiteSetting.enable_category_group_moderation = true