Skip to content

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

SECURITY: Scope sentiment posts endpoint to allowed categories

Isaac Janzene1bb146a1fad
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/sentiment/sentiment_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/sentiment/sentiment_controller.rb
index 2e1f54264c0..4702a667a9e 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/sentiment/sentiment_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/sentiment/sentiment_controller.rb
@@ -65,6 +65,7 @@ module DiscourseAi
((:start_date IS NULL OR p.created_at > :start_date) AND (:end_date IS NULL OR p.created_at < :end_date))
AND p.deleted_at IS NULL
AND t.deleted_at IS NULL
+ AND (t.category_id IS NULL OR t.category_id IN (:allowed_category_ids))
ORDER BY p.created_at DESC
LIMIT :limit OFFSET :offset
SQL
@@ -74,6 +75,7 @@ module DiscourseAi
threshold: threshold,
limit: limit + 1,
offset: offset,
+ allowed_category_ids: guardian.allowed_category_ids,
)
has_more = posts.length > limit
diff --git a/plugins/discourse-ai/spec/requests/sentiment/sentiment_controller_spec.rb b/plugins/discourse-ai/spec/requests/sentiment/sentiment_controller_spec.rb
index c6dda7bc488..f9fdc86e617 100644
--- a/plugins/discourse-ai/spec/requests/sentiment/sentiment_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/sentiment/sentiment_controller_spec.rb
@@ -7,7 +7,7 @@ RSpec.describe DiscourseAi::Sentiment::SentimentController do
fab!(:topic) { Fabricate(:topic, category: category) }
fab!(:post) { Fabricate(:post, user: admin, topic: topic) }
fab!(:post_2) { Fabricate(:post, user: admin, topic: topic) }
- fab!(:classification_result) { Fabricate(:classification_result, target: post) }
+ fab!(:classification_result) { Fabricate(:sentiment_classification, target: post) }
before do
enable_current_plugin
@@ -36,6 +36,51 @@ RSpec.describe DiscourseAi::Sentiment::SentimentController do
end
end
+ context "when signed in as a moderator" do
+ fab!(:moderator)
+
+ it "excludes posts from categories the moderator cannot see when group_by is category" do
+ restricted_category = Fabricate(:category)
+ restricted_category.set_permissions(admins: :full)
+ restricted_category.save!
+
+ restricted_topic = Fabricate(:topic, category: restricted_category)
+ restricted_post = Fabricate(:post, user: admin, topic: restricted_topic)
+ Fabricate(:sentiment_classification, target: restricted_post)
+
+ sign_in(moderator)
+
+ get "/discourse-ai/sentiment/posts",
+ params: {
+ group_by: "category",
+ group_value: restricted_category.name,
+ }
+
+ expect(response).to be_successful
+ post_ids = JSON.parse(response.body)["posts"].map { |p| p["post_id"] }
+ expect(post_ids).not_to include(restricted_post.id)
+ end
+
+ it "excludes posts from categories the moderator cannot see when group_by is tag" do
+ restricted_category = Fabricate(:category)
+ restricted_category.set_permissions(admins: :full)
+ restricted_category.save!
+
+ tag = Fabricate(:tag, name: "secret-tag")
+ restricted_topic = Fabricate(:topic, category: restricted_category, tags: [tag])
+ restricted_post = Fabricate(:post, user: admin, topic: restricted_topic)
+ Fabricate(:sentiment_classification, target: restricted_post)
+
+ sign_in(moderator)
+
+ get "/discourse-ai/sentiment/posts", params: { group_by: "tag", group_value: "secret-tag" }
+
+ expect(response).to be_successful
+ post_ids = JSON.parse(response.body)["posts"].map { |p| p["post_id"] }
+ expect(post_ids).not_to include(restricted_post.id)
+ end
+ end
+
it "excludes posts from soft-deleted topics" do
deleted_topic = Fabricate(:topic, category: category, deleted_at: Time.current)
deleted_topic_post = Fabricate(:post, user: admin, topic: deleted_topic)