Skip to content

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

FIX: Persona-level `allowed_group_ids` is not enforced when executing modes (#39839)

Samb929fbaaf6b2
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.rb
index 04cc5dbccc9..6295eba3ab4 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
@@ -34,13 +34,16 @@ module DiscourseAi
raise Discourse::InvalidParameters.new(:custom_prompt) if params[:custom_prompt].blank?
end
+ assistant = DiscourseAi::AiHelper::Assistant.new
+ assistant.ensure_mode_access!(params[:mode], current_user)
+
if params[:mode] == DiscourseAi::AiHelper::Assistant::ILLUSTRATE_POST
return suggest_thumbnails(input)
end
hijack do
render json:
- DiscourseAi::AiHelper::Assistant.new.generate_and_send_prompt(
+ assistant.generate_and_send_prompt(
params[:mode],
input,
current_user,
@@ -170,7 +173,11 @@ module DiscourseAi
raise Discourse::InvalidParameters.new(:location) if !location
raise Discourse::InvalidParameters.new(:mode) if params[:mode].blank?
+
+ assistant = DiscourseAi::AiHelper::Assistant.new
+
if params[:mode] == DiscourseAi::AiHelper::Assistant::ILLUSTRATE_POST
+ assistant.ensure_mode_access!(params[:mode], current_user)
return suggest_thumbnails(text)
end
@@ -182,6 +189,11 @@ module DiscourseAi
# otherwise we may end up streaming the data to the wrong client
raise Discourse::InvalidParameters.new(:client_id) if params[:client_id].blank?
+ # The UI only renders modes from `current_user.ai_helper_prompts`, but a crafted
+ # API request can still hit this endpoint directly. Enforce the selected agent's
+ # group restrictions here before enqueueing async work.
+ assistant.ensure_mode_access!(params[:mode], current_user)
+
channel_id = next_channel_id
progress_channel = "discourse_ai_helper/stream_suggestions/#{channel_id}"
@@ -240,9 +252,11 @@ module DiscourseAi
check_secure_upload_permission(image) if image.secure?
user = current_user
+ assistant = DiscourseAi::AiHelper::Assistant.new
+ assistant.ensure_mode_access!(DiscourseAi::AiHelper::Assistant::IMAGE_CAPTION, user)
hijack do
- caption = DiscourseAi::AiHelper::Assistant.new.generate_image_caption(image, user)
+ caption = assistant.generate_image_caption(image, user)
render json: {
caption:
"#{caption} (#{I18n.t("discourse_ai.ai_helper.image_caption.attribution")})",
diff --git a/plugins/discourse-ai/lib/ai_helper/assistant.rb b/plugins/discourse-ai/lib/ai_helper/assistant.rb
index 35de8a7a419..f6c810a492b 100644
--- a/plugins/discourse-ai/lib/ai_helper/assistant.rb
+++ b/plugins/discourse-ai/lib/ai_helper/assistant.rb
@@ -287,6 +287,15 @@ module DiscourseAi
raw_caption.delete("|").squish.truncate_words(IMAGE_CAPTION_MAX_WORDS)
end
+ def ensure_mode_access!(helper_mode, user)
+ ai_agent = ai_agent_for_mode(helper_mode)
+ return if ai_agent.nil?
+
+ raise Discourse::InvalidAccess if !user.in_any_groups?(ai_agent.allowed_group_ids.to_a)
+
+ ai_agent
+ end
+
private
def agent_has_image_generation_tool?(agent)
@@ -298,11 +307,18 @@ module DiscourseAi
false
end
- def build_bot(helper_mode, user)
+ def ai_agent_for_mode(helper_mode)
agent_id = agents_prompt_map(include_image_caption: true).invert[helper_mode]
raise Discourse::InvalidParameters.new(:mode) if agent_id.blank?
- agent_klass = AiAgent.find_by(id: agent_id)&.class_instance
+ AiAgent.find_by(id: agent_id)
+ end
+
+ def build_bot(helper_mode, user)
+ ai_agent = ensure_mode_access!(helper_mode, user)
+ return if ai_agent.nil?
+
+ agent_klass = ai_agent.class_instance
return if agent_klass.nil?
llm_model = find_ai_helper_model(helper_mode, agent_klass)
diff --git a/plugins/discourse-ai/spec/lib/modules/ai_helper/assistant_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_helper/assistant_spec.rb
index 52ea153556b..1ae8a60dfa5 100644
--- a/plugins/discourse-ai/spec/lib/modules/ai_helper/assistant_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/ai_helper/assistant_spec.rb
@@ -293,7 +293,12 @@ RSpec.describe DiscourseAi::AiHelper::Assistant do
context "when the agent is not using structured outputs" do
it "still works" do
- regular_agent = Fabricate(:ai_agent, response_format: nil)
+ regular_agent =
+ Fabricate(
+ :ai_agent,
+ response_format: nil,
+ allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],
+ )
SiteSetting.ai_helper_translator_agent = regular_agent.id
response =
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.rb
index cd9d0eb71da..e645a88f2f1 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
@@ -3,6 +3,7 @@
RSpec.describe DiscourseAi::AiHelper::AssistantController do
fab!(:newuser)
fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
+ fab!(:restricted_group, :group)
before do
enable_current_plugin
@@ -19,6 +20,10 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
it "returns 403 when user cannot see the post" do
sign_in(user)
+ AiAgent.find_by(id: SiteSetting.ai_helper_custom_prompt_agent).update!(
+ allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],
+ )
+
group = Fabricate(:group)
private_category = Fabricate(:private_category, group: group)
topic = Fabricate(:topic, category: private_category)
@@ -40,6 +45,10 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
it "is able to stream suggestions to helper" do
sign_in(user)
+ AiAgent.find_by(id: SiteSetting.ai_helper_custom_prompt_agent).update!(
+ allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],
+ )
+
category = Fabricate(:category)
category.set_permissions(everyone: :full)
category.save!
@@ -81,6 +90,30 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
expect(last_message.data[:done]).to eq(true)
end
+ context "when the user is not in the helper mode agent's allowed_group_ids" do
+ before { Jobs.run_later! }
+
+ it "returns a 403 before enqueuing the streamed suggestion" do
+ sign_in(user)
+
+ AiAgent.find_by(id: SiteSetting.ai_helper_custom_prompt_agent).update!(
+ allowed_group_ids: [restricted_group.id],
+ )
+
+ post "/discourse-ai/ai-helper/stream_suggestion.json",
+ params: {
+ text: "hello wrld",
+ location: "composer",
+ client_id: "1234",
+ custom_prompt: "Translate to Spanish",
+ mode: DiscourseAi::AiHelper::Assistant::CUSTOM_PROMPT,
+ }
+
+ expect(response.status).to eq(403)
+ expect(response.parsed_body["errors"]).to be_present
+ end
+ end
+
context "when mode is illustrate_post" do
let(:image_tool) do
AiTool.create!(
@@ -141,6 +174,21 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
expect(response.parsed_body["thumbnails"].length).to eq(1)
expect(response.parsed_body["thumbnails"].first["id"]).to eq(upload.id)
end
+
+ it "returns a 403 when the user cannot access the post illustrator agent" do
+ sign_in(user)
+ post_illustrator_agent.update!(allowed_group_ids: [restricted_group.id])
+
+ post "/discourse-ai/ai-helper/stream_suggestion.json",
+ params: {
+ text: "A beautiful sunset",
+ location: "composer",
+ mode: DiscourseAi::AiHelper::Assistant::ILLUSTRATE_POST,
+ }
+
+ expect(response.status).to eq(403)
+ expect(response.parsed_body["errors"]).to be_present
+ end
end
it "is able to stream suggestions to composer" do
@@ -299,7 +347,6 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
context "when logged in as an allowed user" do
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]
end
@@ -346,6 +393,10 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
end
it "uses custom instruction when using custom_prompt mode" do
+ AiAgent.find_by(id: SiteSetting.ai_helper_custom_prompt_agent).update!(
+ allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],
+ )
+
translated_text = "Un usuario escribio esto"
expected_diff =
"<div class=\"inline-diff\"><p><ins>Un </ins><ins>usuario </ins><ins>escribio </ins><ins>esto</ins><del>A </del><del>user </del><del>wrote </del><del>this</del></p></div>"
@@ -364,6 +415,26 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
end
end
+ context "when the user is not in the helper mode agent's allowed_group_ids" do
+ it "returns a 403 response" do
+ AiAgent.find_by(id: SiteSetting.ai_helper_custom_prompt_agent).update!(
+ allowed_group_ids: [restricted_group.id],
+ )
+
+ DiscourseAi::Completions::Llm.with_prepared_responses(["Un usuario escribio esto"]) do
+ post "/discourse-ai/ai-helper/suggest",
+ params: {
+ mode: DiscourseAi::AiHelper::Assistant::CUSTOM_PROMPT,
+ text: "A user wrote this",
+ custom_prompt: "Translate to Spanish",
+ }
+ end
+
+ expect(response.status).to eq(403)
+ expect(response.parsed_body["errors"]).to be_present
+ end
+ end
+
it "returns error when PostIllustrator agent has no image generation tool" do
post "/discourse-ai/ai-helper/suggest",
params: {
@@ -503,7 +574,6 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
context "when logged in as an allowed user" do
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]
end
@@ -612,6 +682,17 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
end
end
+ it "returns a 403 when the user cannot access the image caption agent" do
+ AiAgent.find_by(id: SiteSetting.ai_helper_image_caption_agent).update!(
+ allowed_group_ids: [restricted_group.id],
+ )
+
+ request_caption({ image_url: image_url, image_url_type: "long_url" }) do |r|
+ expect(r.status).to eq(403)
+ expect(r.parsed_body["errors"]).to be_present
+ end
+ end
+
it "returns a cleaned up caption from the LLM" do
request_caption({ image_url: image_url, image_url_type: "long_url" }, bad_caption) do |r|
expect(r.status).to eq(200)
@@ -777,7 +858,6 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
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