Skip to content

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

FIX: Composer/Post group policy can be bypassed via unioned auth check (#38283)

Keegan George1859da2037ad
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 66012a1c69e..b8fdd105280 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
@@ -7,7 +7,8 @@ module DiscourseAi
requires_plugin PLUGIN_NAME
requires_login
- before_action :ensure_can_request_suggestions
+ before_action :ensure_can_request_composer_suggestions, except: :stream_suggestion
+ before_action :ensure_can_request_stream_suggestions, only: :stream_suggestion
before_action :rate_limiter_performed!
include SecureUploadEndpointHelpers
@@ -285,13 +286,25 @@ module DiscourseAi
).performed!
end
- def ensure_can_request_suggestions
+ def ensure_can_request_composer_suggestions
+ ensure_user_is_in_any_allowed_group!(SiteSetting.composer_ai_helper_allowed_groups_map)
+ end
+
+ def ensure_can_request_stream_suggestions
+ location = params[:location]
+ raise Discourse::InvalidParameters.new(:location) if location.blank?
+
allowed_groups =
- (
- SiteSetting.composer_ai_helper_allowed_groups_map |
- SiteSetting.post_ai_helper_allowed_groups_map
- )
+ if location == "composer"
+ SiteSetting.composer_ai_helper_allowed_groups_map
+ else
+ SiteSetting.post_ai_helper_allowed_groups_map
+ end
+
+ ensure_user_is_in_any_allowed_group!(allowed_groups)
+ end
+ def ensure_user_is_in_any_allowed_group!(allowed_groups)
raise Discourse::InvalidAccess if !current_user.in_any_groups?(allowed_groups)
end
end
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 33e9e483213..d2a78b8c15d 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,99 @@ RSpec.describe DiscourseAi::AiHelper::AssistantController do
expect(last_message.data[:result]).to eq("hello world")
expect(last_message.data[:done]).to eq(true)
end
+
+ context "when enforcing context-specific group permissions" do
+ fab!(:composer_group, :group)
+ fab!(:post_group, :group)
+ fab!(:composer_only_user) { Fabricate(:user, refresh_auto_groups: true) }
+ fab!(:post_only_user) { Fabricate(:user, refresh_auto_groups: true) }
+ fab!(:my_post, :post)
+
+ before do
+ SiteSetting.composer_ai_helper_allowed_groups = composer_group.id.to_s
+ SiteSetting.post_ai_helper_allowed_groups = post_group.id.to_s
+ composer_group.add(composer_only_user)
+ post_group.add(post_only_user)
+ end
+
+ it "returns 403 when a composer-only user tries to use post helper" do
+ sign_in(composer_only_user)
+
+ 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",
+ }
+
+ expect(response.status).to eq(403)
+ end
+
+ it "returns 400 with a location error when location is missing" do
+ sign_in(composer_only_user)
+
+ post "/discourse-ai/ai-helper/stream_suggestion.json",
+ params: {
+ text: "hello",
+ mode: DiscourseAi::AiHelper::Assistant::PROOFREAD,
+ client_id: "test123",
+ }
+
+ 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" do
+ sign_in(post_only_user)
+
+ post "/discourse-ai/ai-helper/stream_suggestion.json",
+ params: {
+ text: "hello",
+ location: "composer",
+ mode: DiscourseAi::AiHelper::Assistant::PROOFREAD,
+ client_id: "test123",
+ }
+
+ expect(response.status).to eq(403)
+ end
+
+ it "allows a composer-only user to use the composer helper" do
+ sign_in(composer_only_user)
+
+ results = [["hello ", "world"]]
+ DiscourseAi::Completions::Llm.with_prepared_responses(results) do
+ post "/discourse-ai/ai-helper/stream_suggestion.json",
+ params: {
+ text: "hello",
+ location: "composer",
+ mode: DiscourseAi::AiHelper::Assistant::PROOFREAD,
+ client_id: "test123",
+ }
+
+ expect(response.status).to eq(200)
+ end
+ end
+
+ it "allows a post-only user to use the post helper" do
+ sign_in(post_only_user)
+
+ results = [["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",
+ }
+
+ expect(response.status).to eq(200)
+ end
+ end
+ end
end
describe "#suggest" do