Skip to content

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

FIX: Let category moderators review deleted content safely

Régis Hanol7c3683b145c2
diff --git a/app/controllers/reviewables_controller.rb b/app/controllers/reviewables_controller.rb
index cec2ab58119..7bd6b31a78f 100644
--- a/app/controllers/reviewables_controller.rb
+++ b/app/controllers/reviewables_controller.rb
@@ -8,9 +8,7 @@ class ReviewablesController < ApplicationController
before_action :version_required, only: %i[update perform]
before_action :ensure_can_see, except: [:destroy]
- around_action :with_deleted_content,
- only: %i[index show],
- if: ->(controller) { controller.guardian.is_staff? }
+ around_action :with_deleted_content, only: %i[index show]
def index
offset = params[:offset].to_i
@@ -362,6 +360,6 @@ class ReviewablesController < ApplicationController
end
def with_deleted_content
- Post.unscoped { Topic.unscoped { PostAction.unscoped { yield } } }
+ Reviewable.with_deleted_content { yield }
end
end
diff --git a/app/models/reviewable.rb b/app/models/reviewable.rb
index 7fd25d7793b..bc4b251dac0 100644
--- a/app/models/reviewable.rb
+++ b/app/models/reviewable.rb
@@ -355,8 +355,10 @@ class Reviewable < ActiveRecord::Base
def actions_for(guardian, args = nil)
args ||= {}
- built_actions =
- Actions.new(self, guardian).tap { |actions| build_actions(actions, guardian, args) }
+ built_actions = Actions.new(self, guardian)
+ return built_actions if !can_review_target?(guardian)
+
+ build_actions(built_actions, guardian, args)
# Empty bundles can cause big issues on the client side, so we remove them
# here. It's not valid anyway to have a bundle with no actions, but you can
@@ -384,6 +386,7 @@ class Reviewable < ActiveRecord::Base
def update_fields(params, performed_by, version: nil)
return true if params.blank?
+ raise Discourse::InvalidAccess if !can_review_target?(performed_by.guardian)
(params[:payload] || {}).each { |k, v| payload[k] = v }
self.category_id = params[:category_id] if params.has_key?(:category_id)
@@ -487,6 +490,10 @@ class Reviewable < ActiveRecord::Base
.each { |r| r.perform(performed_by, action, args) }
end
+ def self.with_deleted_content
+ Post.unscoped { Topic.unscoped { PostAction.unscoped { yield } } }
+ end
+
def self.viewable_by(user, order: nil, preload: true)
return none if user.blank?
@@ -536,15 +543,26 @@ class Reviewable < ActiveRecord::Base
)
.where(
"reviewables.category_id IS NULL OR reviewables.category_id IN (?)",
- Guardian.new(user).allowed_category_ids,
+ user.guardian.allowed_category_ids,
)
- exclude_private_messages_hidden_from(result, user)
+ result = exclude_private_messages_hidden_from(result, user)
+ visible_target =
+ user.guardian.reviewable_post_scope.where("posts.id = reviewable_target.id").select(:id)
+
+ result.where(<<~SQL)
+ NOT EXISTS (
+ SELECT 1 FROM posts reviewable_target
+ WHERE reviewables.target_type = 'Post'
+ AND reviewable_target.id = reviewables.target_id
+ AND NOT EXISTS (#{visible_target.to_sql})
+ )
+ SQL
end
def self.exclude_private_messages_hidden_from(result, user)
visible_private_message_ids =
- Guardian.new(user).private_message_topic_scope(Topic.unscoped).select(:id)
+ user.guardian.private_message_topic_scope(Topic.unscoped).select(:id)
result.where(<<~SQL, private_message: Archetype.private_message)
NOT EXISTS (
@@ -979,6 +997,11 @@ class Reviewable < ActiveRecord::Base
private
+ def can_review_target?(guardian)
+ post = target_post
+ !post || guardian.can_review_post?(post)
+ end
+
def target_post
return if target_type != "Post"
diff --git a/lib/guardian/post_guardian.rb b/lib/guardian/post_guardian.rb
index 76a24b7a13c..c03440bc956 100644
--- a/lib/guardian/post_guardian.rb
+++ b/lib/guardian/post_guardian.rb
@@ -320,6 +320,23 @@ module PostGuardian
false
end
+ def reviewable_post_scope
+ return Post.with_deleted if is_admin?
+ return Post.none if !is_staff? && !category_group_moderation_allowed?
+
+ topics = visible_topic_scope(Topic.with_deleted)
+ topics = topics.where(category_id: category_group_moderator_scope.select(:id)) if !is_staff?
+
+ Post.with_deleted.secured(self).where(topic_id: topics.select(:id))
+ end
+
+ def can_review_post?(post)
+ return false if post.blank?
+ return true if is_admin?
+
+ reviewable_post_scope.exists?(id: post.id)
+ end
+
def can_see_deleted_post?(post)
return false if !post.trashed?
return false if @user.anonymous?
diff --git a/lib/post_destroyer.rb b/lib/post_destroyer.rb
index 58d0aa51953..594c740152c 100644
--- a/lib/post_destroyer.rb
+++ b/lib/post_destroyer.rb
@@ -395,7 +395,13 @@ class PostDestroyer
def agree(reviewable)
notify_deletion(reviewable)
- result = reviewable.perform(@user, :agree_and_keep, post_was_deleted: true)
+ result =
+ reviewable.perform(
+ @user,
+ :agree_and_keep,
+ post_was_deleted: true,
+ guardian: Discourse.system_user.guardian,
+ )
reviewable.transition_to(result.transition_to, @user)
end
diff --git a/spec/models/report_spec.rb b/spec/models/report_spec.rb
index 8714dc634d6..a5dbf981874 100644
--- a/spec/models/report_spec.rb
+++ b/spec/models/report_spec.rb
@@ -962,19 +962,20 @@ RSpec.describe Report do
result =
PostActionCreator.new(flagger, post, PostActionType.types[:spam], message: "bad").perform
- result.reviewable.perform(flagger, :agree_and_hide)
+ reviewer = Fabricate(:admin)
+ result.reviewable.perform(reviewer, :agree_and_hide)
expect(result.success).to eq(true)
expect(report.data).to be_present
exporter = Jobs::ExportCsvFile.new
exporter.entity = "report"
exporter.extra = ActiveSupport::HashWithIndifferentAccess.new(name: "flags_status")
- exporter.current_user = flagger
+ exporter.current_user = reviewer
exported_csv = []
exporter.report_export { |entry| exported_csv << entry }
expect(exported_csv[0]).to eq(["Type", "Assigned", "Poster", "Flagger", "Resolution time"])
expect(exported_csv[1]).to eq(
- ["spam", flagger.username, post.user.username, flagger.username, "0.0"],
+ ["spam", reviewer.username, post.user.username, flagger.username, "0.0"],
)
end
end
diff --git a/spec/models/reviewable_spec.rb b/spec/models/reviewable_spec.rb
index c606d01e5ba..8174f87ecbc 100644
--- a/spec/models/reviewable_spec.rb
+++ b/spec/models/reviewable_spec.rb
@@ -142,6 +142,13 @@ RSpec.describe Reviewable, type: :model do
expect(described_class.viewable_by(admin)).to exist(id: reviewable.id)
end
+ it "includes flagged private messages for moderators outside the conversation" do
+ flagged_reviewable =
+ Fabricate(:reviewable_flagged_post, target: pm_post, topic: pm_topic, category: nil)
+
+ expect(described_class.viewable_by(outsider_moderator)).to exist(id: flagged_reviewable.id)
+ end
+
it "includes private-message reviewables for participating moderators" do
participant_moderator = Fabricate(:moderator)
participant_topic =
@@ -160,6 +167,65 @@ RSpec.describe Reviewable, type: :model do
end
end
+ describe ".viewable_by with post targets" do
+ fab!(:group)
+ fab!(:reviewer) { Fabricate(:user, groups: [group]) }
+ fab!(:category)
+ fab!(:moderation_group) { Fabricate(:category_moderation_group, category:, group:) }
+ fab!(:topic) { Fabricate(:topic, category:) }
+ fab!(:post) { Fabricate(:post, topic:) }
+ fab!(:reviewable) { Fabricate(:reviewable_flagged_post, target: post, topic:, category:) }
+
+ before { SiteSetting.enable_category_group_moderation = true }
+
+ it "checks whisper group membership" do
+ post.update!(post_type: Post.types[:whisper])
+ expect(described_class.viewable_by(reviewer)).not_to exist(id: reviewable.id)
+
+ SiteSetting.whispers_allowed_groups = group.id.to_s
+ expect(described_class.viewable_by(reviewer)).to exist(id: reviewable.id)
+ end
+
+ it "excludes shared drafts the reviewer cannot see" do
+ Fabricate(:shared_draft, topic:)
+
+ expect(described_class.viewable_by(reviewer)).not_to exist(id: reviewable.id)
+ end
+
+ it "excludes targets moved to categories the reviewer does not moderate" do
+ [
+ Fabricate(:category),
+ Fabricate(:private_category, group: Fabricate(:group)),
+ ].each do |other_category|
+ topic.update_columns(category_id: other_category.id)
+
+ expect(described_class.viewable_by(reviewer)).not_to exist(id: reviewable.id)
+ end
+ end
+
+ it "checks the target's current private-message topic when reviewable metadata is stale" do
+ post.update_columns(topic_id: Fabricate(:private_message_topic).id)
+
+ expect(described_class.viewable_by(reviewer)).not_to exist(id: reviewable.id)
+ end
+
+ it "loads hidden and deleted targets only within an explicit deleted-content scope" do
+ post.update!(hidden: true)
+ post.trash!
+ topic.trash!
+
+ loaded =
+ described_class.with_deleted_content do
+ described_class.viewable_by(reviewer).find(reviewable.id)
+ end
+
+ expect(loaded.target).to eq(post)
+ expect(loaded.topic).to eq(topic)
+ expect(described_class.find(reviewable.id).target).to be_nil
+ expect(described_class.find(reviewable.id).topic).to be_nil
+ end
+ end
+
describe ".list_for" do
fab!(:user)
@@ -529,10 +595,45 @@ RSpec.describe Reviewable, type: :model do
end
end
+ describe "#update_fields" do
+ it "rejects edits to an inaccessible target before changing its payload" do
+ reviewable = Fabricate(:reviewable_flagged_post)
+ private_category = Fabricate(:private_category, group: Fabricate(:group))
+ reviewable.target.topic.update_columns(category_id: private_category.id)
+ original_payload = reviewable.payload.deep_dup
+
+ expect do
+ reviewable.update_fields({ payload: { raw: "Updated content" } }, Fabricate(:moderator))
+ end.to raise_error(Discourse::InvalidAccess)
+
+ expect(reviewable.reload.payload).to eq(original_payload)
+ end
+ end
+
describe "#perform" do
fab!(:moderator) { Fabricate(:moderator, refresh_auto_groups: true) }
let(:post) { Fabricate(:post) }
+ it "hides actions and denies execution for an inaccessible target" do
+ reviewable = Fabricate(:reviewable_flagged_post)
+ private_category = Fabricate(:private_category, group: Fabricate(:group))
+ reviewable.target.topic.update_columns(category_id: private_category.id)
+
+ expect(reviewable.actions_for(moderator.guardian).bundles).to be_empty
+ expect { reviewable.perform(moderator, :ignore) }.to raise_error(Reviewable::InvalidAction)
+ expect(reviewable.reload).to be_pending
+ end
+
+ it "allows moderators to review flagged private messages outside their conversations" do
+ topic = Fabricate(:private_message_topic)
+ reviewable =
+ Fabricate(:reviewable_flagged_post, target: Fabricate(:post, topic:), topic:, category: nil)
+
+ reviewable.perform(moderator, :ignore)
+
+ expect(reviewable.reload).to be_ignored
+ end
+
it "rolls back the transaction when the action fails" do
reviewable = Fabricate(:reviewable_queued_post)
diff --git a/spec/requests/reviewables_controller_spec.rb b/spec/requests/reviewables_controller_spec.rb
index 91dd5fc108c..a0cbd346aff 100644
--- a/spec/requests/reviewables_controller_spec.rb
+++ b/spec/requests/reviewables_controller_spec.rb
@@ -164,18 +164,35 @@ RSpec.describe ReviewablesController do
expect(topic_json["title"]).to eq(topic.title)
end
- it "does not return information for trashed topics and posts to category mods" do
+ it "returns information for trashed topics and posts to category mods" do
SiteSetting.enable_category_group_moderation = true
sign_in(category_mod)
post1.trash!
topic.trash!
get "/review.json"
- expect(response.code).to eq("200")
+ expect(response).to have_http_status(:ok)
json = response.parsed_body
reviewable_json = json["reviewables"].find { |r| r["id"] == reviewable.id }
- expect(reviewable_json["raw"]).to be_blank
+ expect(reviewable_json["raw"]).to eq(post1.raw)
+ expect(reviewable_json["cooked"]).to eq(post1.cooked)
+ expect(reviewable_json["deleted_at"]).to be_present
+ expect(reviewable_json).not_to have_key("blank_post")
+ expect(json["topics"].find { |item| item["id"] == topic.id }["title"]).to eq(topic.title)
+ end
+
+ it "excludes inaccessible whispers from the list and counts" do
+ SiteSetting.enable_category_group_moderation = true
+ post1.update!(post_type: Post.types[:whisper])
+ sign_in(category_mod)
+
+ get "/review.json"
+
+ expect(response).to have_http_status(:ok)
+ expect(response.parsed_body["reviewables"]).to be_empty
+ expect(response.parsed_body["meta"]["total_rows_reviewables"]).to eq(0)
+ expect(response.parsed_body["meta"]["unseen_reviewable_count"]).to eq(0)
end
end
@@ -734,8 +751,61 @@ RSpec.describe ReviewablesController do
end
end
+ context "with a category moderator and a trashed target" do
+ fab!(:post1, :post)
+ fab!(:reviewable) do
+ Fabricate(
+ :reviewable,
+ target_id: post1.id,
+ target_type: "Post",
+ topic: post1.topic,
+ type: "ReviewableFlaggedPost",
+ category: post1.topic.category,
+ )
+ end
+ fab!(:group_user)
+
+ before do
+ SiteSetting.enable_category_group_moderation = true
+ Fabricate(
+ :category_moderation_group,
+ category: post1.topic.category,
+ group: group_user.group,
+ )
+ sign_in(group_user.user)
+ end
+
+ it "returns the trashed content" do
+ post1.trash!
+ post1.topic.trash!
+
+ get "/review/#{reviewable.id}.json"
+
+ expect(response).to have_http_status(:ok)
+ expect(response.parsed_body["reviewable"]).to include(
+ "raw" => post1.raw,
+ "cooked" => post1.cooked,
+ "deleted_at" => be_present,
+ )
+
+ post1.delete
+ get "/review/#{reviewable.id}.json"
+
+ expect(response).to have_http_status(:ok)
+ expect(response.parsed_body["reviewable"]["blank_post"]).to eq(true)
+ end
+
+ it "returns 404 for an inaccessible whisper" do
+ post1.update!(post_type: Post.types[:whisper])
+
+ get "/review/#{reviewable.id}.json"
+
+ expect(response).to have_http_status(:not_found)
+ end
+ end
+
context "with an inaccessible conversation" do
- it "does not serialize the conversation" do
+ it "does not serialize the deleted conversation of a deleted target" do
SiteSetting.enable_category_group_moderation = true
category = Fabricate(:category)
@@ -770,6 +840,10 @@ RSpec.describe ReviewablesController do
expect(Guardian.new(group_user.user).can_see?(meta_post.topic)).to eq(false)
+ post.trash!
+ meta_post.trash!
+ meta_post.topic.trash!
+
sign_in(group_user.user)
get "/review/#{reviewable.id}.json"
@@ -811,6 +885,35 @@ RSpec.describe ReviewablesController do
fab!(:reviewable)
before { sign_in(Fabricate(:moderator)) }
+ it "returns 404 to category moderators for an inaccessible whisper" do
+ SiteSetting.enable_category_group_moderation = true
+ group_user = Fabricate(:group_user)
+ whisper = Fabricate(:post, post_type: Post.types[:whisper])
+ Fabricate(
+ :category_moderation_group,
+ category: whisper.topic.category,
+ group: group_user.group,
+ )
+ flagged_whisper =
+ Fabricate(
+ :reviewable,
+ target_id: whisper.id,
+ target_type: "Post",
+ topic: whisper.topic,
+ type: "ReviewableFlaggedPost",
+ category: whisper.topic.category,
+ )
+ sign_in(group_user.user)
+
+ put "/review/#{flagged_whisper.id}/perform/ignore.json",
+ params: {
+ version: flagged_whisper.version,
+ }
+
+ expect(response).to have_http_status(:not_found)
+ expect(flagged_whisper.reload).to be_pending
+ end
+
it "includes the statuses of the other reviewables resolved by the action in the response" do
sign_in(Fabricate(:admin))
spammer = Fabricate(:user, refresh_auto_groups: true)