A platform for community discussion. Free, open, simple.
SECURITY: AI helper suggestions based on a topic should check user has access to it
Roman Rizzi49558abd9e58
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/ai_helper/assistant_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/ai_helper/assistant_controller.rbindex 8a65a187df8..b626172122c 100644--- a/plugins/discourse-ai/app/controllers/discourse_ai/ai_helper/assistant_controller.rb+++ b/plugins/discourse-ai/app/controllers/discourse_ai/ai_helper/assistant_controller.rb@@ -54,6 +54,7 @@ module DiscourseAidef suggest_titleif params[:topic_id]topic = Topic.find_by(id: params[:topic_id])+ guardian.ensure_can_see!(topic)input = DiscourseAi::Summarization::Strategies::TopicSummary.new(topic).targets_dataelseinput = get_text_param!@@ -75,7 +76,9 @@ module DiscourseAidef suggest_categoryif params[:topic_id]- opts = { topic_id: params[:topic_id] }+ topic = Topic.find_by(id: params[:topic_id])+ guardian.ensure_can_see!(topic)+ opts = { topic_id: topic.id }elseinput = get_text_param!opts = { text: input }@@ -87,7 +90,9 @@ module DiscourseAidef suggest_tagsif params[:topic_id]- opts = { topic_id: params[:topic_id] }+ topic = Topic.find_by(id: params[:topic_id])+ guardian.ensure_can_see!(topic)+ opts = { topic_id: topic.id }elseinput = get_text_param!opts = { text: input }diff --git a/plugins/discourse-ai/spec/requests/ai_helper/assistant_controller_spec.rb b/plugins/discourse-ai/spec/requests/ai_helper/assistant_controller_spec.rbindex 1c82b422dec..19393b63c0f 100644--- a/plugins/discourse-ai/spec/requests/ai_helper/assistant_controller_spec.rb+++ b/plugins/discourse-ai/spec/requests/ai_helper/assistant_controller_spec.rb@@ -223,7 +223,8 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController doenddescribe "#suggest_title" do- fab!(:topic)+ fab!(:category)+ fab!(:topic) { Fabricate(:topic, category: category) }fab!(:post_1) { Fabricate(:post, topic: topic, raw: "I love apples") }fab!(:post_3) { Fabricate(:post, topic: topic, raw: "I love mangos") }fab!(:post_2) { Fabricate(:post, topic: topic, raw: "I love bananas") }@@ -265,6 +266,14 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController doexpect(response.parsed_body["suggestions"]).to eq(title_suggestions_array)endend++ it "returns a 403 when the user cannot see the topic" do+ private_topic = Fabricate(:private_message_topic)++ post "/discourse-ai/ai-helper/suggest_title", params: { topic_id: private_topic.id }++ expect(response.status).to eq(403)+ endendcontext "when suggesting titles with input text" do@@ -490,4 +499,43 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController doendendend++ describe "semantic suggestions" do+ fab!(:embedding_definition)+ fab!(:private_message_topic)++ before do+ sign_in(user)+ user.group_ids = [Group::AUTO_GROUPS[:trust_level_1]]+ SiteSetting.composer_ai_helper_allowed_groups = Group::AUTO_GROUPS[:trust_level_1]+ SiteSetting.ai_embeddings_selected_model = embedding_definition.id+ SiteSetting.ai_embeddings_enabled = true+ end++ describe "#semantic_tags" do+ context "when passing a topic the user doesn't have access to" do+ it "returns a 403" do+ post "/discourse-ai/ai-helper/suggest_tags",+ params: {+ topic_id: private_message_topic.id,+ }++ expect(response.status).to eq(403)+ end+ end+ end++ describe "#semantic_categories" do+ context "when passing a topic the user doesn't have access to" do+ it "returns a 403" do+ post "/discourse-ai/ai-helper/suggest_category",+ params: {+ topic_id: private_message_topic.id,+ }++ expect(response.status).to eq(403)+ end+ end+ end+ endend