A platform for community discussion. Free, open, simple.
FIX: Stop-stream endpoint allows unauthorized bot-control action via visibility-only check (#37860)
Samb570d9cd0187
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/bot_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/bot_controller.rbindex 231b611486e..89a4be94884 100644--- a/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/bot_controller.rb+++ b/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/bot_controller.rb@@ -32,6 +32,11 @@ module DiscourseAienddef stop_streaming_response+ raise Discourse::InvalidAccess unless SiteSetting.ai_bot_enabled+ unless current_user.in_any_groups?(SiteSetting.ai_bot_allowed_groups_map)+ raise Discourse::InvalidAccess+ end+post = Post.find(params[:post_id])guardian.ensure_can_see!(post)diff --git a/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rb b/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rbindex f9b20112479..3da520a6f6a 100644--- a/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rb+++ b/plugins/discourse-ai/spec/requests/ai_bot/bot_controller_spec.rb@@ -88,7 +88,10 @@ RSpec.describe DiscourseAi::AiBot::BotController dodescribe "#stop_streaming_response" dolet(:redis_stream_key) { "gpt_cancel:#{pm_post.id}" }- before { Discourse.redis.setex(redis_stream_key, 60, 1) }+ before do+ SiteSetting.ai_bot_enabled = true+ Discourse.redis.setex(redis_stream_key, 60, 1)+ endit "returns a 403 when the user cannot see the PM" dopost "/discourse-ai/ai-bot/post/#{pm_post.id}/stop-streaming"@@ -96,8 +99,36 @@ RSpec.describe DiscourseAi::AiBot::BotController doexpect(response.status).to eq(403)end+ it "returns a 403 when the user can see the PM but is not in ai_bot_allowed_groups" do+ allowed_user = pm_topic.topic_allowed_users.first.user+ sign_in(allowed_user)++ SiteSetting.ai_bot_allowed_groups = Group::AUTO_GROUPS[:staff].to_s++ post "/discourse-ai/ai-bot/post/#{pm_post.id}/stop-streaming"++ expect(response.status).to eq(403)+ expect(Discourse.redis.get(redis_stream_key)).to eq("1")+ end++ it "returns a 403 when ai_bot_enabled is false" do+ allowed_user = pm_topic.topic_allowed_users.first.user+ sign_in(allowed_user)++ SiteSetting.ai_bot_enabled = false++ post "/discourse-ai/ai-bot/post/#{pm_post.id}/stop-streaming"++ expect(response.status).to eq(403)+ expect(Discourse.redis.get(redis_stream_key)).to eq("1")+ end+it "deletes the key using to track the streaming" do- sign_in(pm_topic.topic_allowed_users.first.user)+ allowed_user = pm_topic.topic_allowed_users.first.user+ sign_in(allowed_user)++ Group.refresh_automatic_groups!+ SiteSetting.ai_bot_allowed_groups = allowed_user.groups.first.id.to_spost "/discourse-ai/ai-bot/post/#{pm_post.id}/stop-streaming"