A platform for community discussion. Free, open, simple.
SECURITY: Validate stream_suggestion mode/location context before AI helper dispatch (#43982)
Keegan George99f7f69f1a35
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 6df48fa1832..df0f3d1fcb4 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@@ -12,6 +12,11 @@ module DiscourseAibefore_action :rate_limiter_performed!RATE_LIMITS = { "default" => { amount: 6, interval: 3.minutes } }.freeze+ STREAM_LOCATION_CONTEXTS = {+ "composer" => "composer",+ "post" => "post",+ "helper" => "post",+ }.freezedef suggestinput = get_text_param!@@ -161,16 +166,15 @@ module DiscourseAidef stream_suggestiontext = get_text_param!-- location = params[:location]- raise Discourse::InvalidParameters.new(:location) if !location+ location = normalized_stream_locationraise Discourse::InvalidParameters.new(:mode) if params[:mode].blank?assistant = DiscourseAi::AiHelper::Assistant.new+ assistant.ensure_mode_access_for_location!(params[:mode], current_user, location)+if params[:mode] == DiscourseAi::AiHelper::Assistant::ILLUSTRATE_POST- assistant.ensure_mode_access!(params[:mode], current_user)return suggest_thumbnails(text)end@@ -182,11 +186,6 @@ module DiscourseAi# otherwise we may end up streaming the data to the wrong clientraise 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_idprogress_channel = "discourse_ai_helper/stream_suggestions/#{channel_id}"@@ -272,11 +271,8 @@ module DiscourseAienddef ensure_can_request_stream_suggestions- location = params[:location]- raise Discourse::InvalidParameters.new(:location) if location.blank?-allowed_groups =- if location == "composer"+ if normalized_stream_location == "composer"SiteSetting.composer_ai_helper_allowed_groups_mapelseSiteSetting.post_ai_helper_allowed_groups_map@@ -285,6 +281,12 @@ module DiscourseAiensure_user_is_in_any_allowed_group!(allowed_groups)end+ def normalized_stream_location+ STREAM_LOCATION_CONTEXTS.fetch(params[:location]) do+ raise Discourse::InvalidParameters.new(:location)+ end+ end+def ensure_user_is_in_any_allowed_group!(allowed_groups)raise Discourse::InvalidAccess if !current_user.in_any_groups?(allowed_groups)enddiff --git a/plugins/discourse-ai/lib/ai_helper/assistant.rb b/plugins/discourse-ai/lib/ai_helper/assistant.rbindex 46829362cfe..a4c7e5d326f 100644--- a/plugins/discourse-ai/lib/ai_helper/assistant.rb+++ b/plugins/discourse-ai/lib/ai_helper/assistant.rb@@ -312,6 +312,11 @@ module DiscourseAiai_agentend+ def ensure_mode_access_for_location!(helper_mode, user, location)+ ensure_mode_access!(helper_mode, user)+ raise Discourse::InvalidAccess if !location_map(helper_mode).include?(location)+ end+privatedef agent_has_image_generation_tool?(agent)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 8d6931b3a32..68694d915a6 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@@ -175,6 +175,35 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController doexpect(response.parsed_body["thumbnails"].first["id"]).to eq(upload.id)end+ context "when a post-only user invokes a composer-only mode" do+ fab!(:post_only_group, :group)+ fab!(:post_only_user) { Fabricate(:user, refresh_auto_groups: true) }++ before do+ SiteSetting.composer_ai_helper_allowed_groups = restricted_group.id.to_s+ SiteSetting.post_ai_helper_allowed_groups = post_only_group.id.to_s+ post_only_group.add(post_only_user)+ post_illustrator_agent.update!(allowed_group_ids: [post_only_group.id])+ end++ it "returns 403 without generating thumbnails for post location aliases" do+ sign_in(post_only_user)++ %w[post helper].each do |location|+ post "/discourse-ai/ai-helper/stream_suggestion.json",+ params: {+ text: "A beautiful sunset",+ location:,+ mode: DiscourseAi::AiHelper::Assistant::ILLUSTRATE_POST,+ }++ expect(response.status).to eq(403)+ expect(response.parsed_body["errors"]).to be_present+ expect(response.parsed_body["thumbnails"]).to be_nil+ end+ end+ end+it "returns a 403 when the user cannot access the post illustrator agent" dosign_in(user)post_illustrator_agent.update!(allowed_group_ids: [restricted_group.id])@@ -236,6 +265,9 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController doSiteSetting.post_ai_helper_allowed_groups = post_group.id.to_scomposer_group.add(composer_only_user)post_group.add(post_only_user)+ AiAgent.find_by(id: SiteSetting.ai_helper_proofreader_agent).update!(+ allowed_group_ids: [composer_group.id, post_group.id],+ )endit "returns 403 when a composer-only user tries to use post helper" do@@ -267,6 +299,24 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController doexpect(response.parsed_body["errors"].join).to include("location")end+ it "returns 400 with a location error for an unrecognized location" do+ sign_in(post_only_user)++ DiscourseAi::Completions::Llm.with_prepared_responses([["hello ", "world"]]) do+ post "/discourse-ai/ai-helper/stream_suggestion.json",+ params: {+ text: "hello",+ location: "unknown",+ post_id: my_post.id,+ mode: DiscourseAi::AiHelper::Assistant::PROOFREAD,+ client_id: "test123",+ }+ end++ expect(response.status).to eq(400)+ expect(response.parsed_body["errors"].join).to include("location")+ end+it "returns 403 when a post-only user tries to use composer helper" dosign_in(post_only_user)@@ -298,21 +348,24 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController doendend- it "allows a post-only user to use the post helper" do+ it "allows a post-only user to use post helper aliases" dosign_in(post_only_user)- results = [["hello ", "world"]]+ results = [["hello ", "world"], ["hello ", "world"]]DiscourseAi::Completions::Llm.with_prepared_responses(results) do- post "/discourse-ai/ai-helper/stream_suggestion.json",- params: {- text: "hello",- location: "post",- post_id: my_post.id,- mode: DiscourseAi::AiHelper::Assistant::PROOFREAD,- client_id: "test123",- }+ %w[post helper].each do |location|+ post "/discourse-ai/ai-helper/stream_suggestion.json",+ params: {+ text: "hello",+ location:,+ post_id: my_post.id,+ mode: DiscourseAi::AiHelper::Assistant::PROOFREAD,+ client_id: "test123",+ }- expect(response.status).to eq(200)+ expect(response.status).to eq(200)+ expect(response.parsed_body["success"]).to eq(true)+ endendendend