Skip to content

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

SECURITY: Scope AI bot reply stream to the PM's participants

Alan Guo Xiang Tan1fb2026eb800
diff --git a/plugins/discourse-ai/lib/ai_bot/playground.rb b/plugins/discourse-ai/lib/ai_bot/playground.rb
index 80aed6aee0c..7c5c2acfeab 100644
--- a/plugins/discourse-ai/lib/ai_bot/playground.rb
+++ b/plugins/discourse-ai/lib/ai_bot/playground.rb
@@ -476,6 +476,8 @@ module DiscourseAi
reply = +""
post_streamer = nil
+ stream_user_ids = nil
+ stream_group_ids = nil
post_type =
(
@@ -568,7 +570,17 @@ module DiscourseAi
save_ai_custom_fields(reply_post)
- publish_update(reply_post, { raw: "" })
+ stream_user_ids = reply_post.topic.allowed_users.pluck(:id)
+ stream_group_ids = reply_post.topic.allowed_groups.pluck(:id)
+
+ publish_update(
+ reply_post,
+ payload: {
+ raw: "",
+ },
+ user_ids: stream_user_ids,
+ group_ids: stream_group_ids,
+ )
redis_stream_key = "gpt_cancel:#{reply_post.id}"
Discourse.redis.setex(redis_stream_key, MAX_STREAM_DELAY_SECONDS, 1)
@@ -622,7 +634,14 @@ module DiscourseAi
if post_streamer
post_streamer.run_later do
Discourse.redis.expire(redis_stream_key, MAX_STREAM_DELAY_SECONDS)
- publish_update(reply_post, { raw: raw })
+ publish_update(
+ reply_post,
+ payload: {
+ raw: raw,
+ },
+ user_ids: stream_user_ids,
+ group_ids: stream_group_ids,
+ )
end
end
end
@@ -640,7 +659,7 @@ module DiscourseAi
# land the final message prior to saving so we don't clash
reply_post.cooked = PrettyText.cook(reply)
- publish_final_update(reply_post)
+ publish_final_update(reply_post, user_ids: stream_user_ids, group_ids: stream_group_ids)
reply_post.revise(
bot.bot_user,
@@ -736,7 +755,9 @@ module DiscourseAi
reply_post.topic.update!(participant_count: 2)
end
post_streamer&.finish(skip_callback: true)
- publish_final_update(reply_post) if stream_reply
+ if stream_reply
+ publish_final_update(reply_post, user_ids: stream_user_ids, group_ids: stream_group_ids)
+ end
if reply_post && post.post_number == 1 && post.topic.private_message? && auto_set_title
title_playground(reply_post, post.user)
end
@@ -801,15 +822,30 @@ module DiscourseAi
User.joins("INNER JOIN llm_models llm ON llm.user_id = users.id").where(active: true)
end
- def publish_final_update(reply_post)
+ def publish_final_update(reply_post, user_ids:, group_ids:)
return if @published_final_update
if reply_post
- publish_update(reply_post, { cooked: reply_post.cooked, done: true })
+ publish_update(
+ reply_post,
+ payload: {
+ cooked: reply_post.cooked,
+ done: true,
+ },
+ user_ids: user_ids,
+ group_ids: group_ids,
+ )
# we subscribe at position -2 so we will always get this message
# moving all cooked on every page load is wasteful ... this means
# we have a benign message at the end, 2 is set to ensure last message
# is delivered
- publish_update(reply_post, { noop: true })
+ publish_update(
+ reply_post,
+ payload: {
+ noop: true,
+ },
+ user_ids: user_ids,
+ group_ids: group_ids,
+ )
@published_final_update = true
end
end
@@ -843,14 +879,18 @@ module DiscourseAi
}
end
- def publish_update(bot_reply_post, payload)
+ def publish_update(bot_reply_post, payload:, user_ids:, group_ids:)
+ return if user_ids.blank? && group_ids.blank?
+
payload = { post_id: bot_reply_post.id, post_number: bot_reply_post.post_number }.merge(
payload,
)
+
MessageBus.publish(
"discourse-ai/ai-bot/topic/#{bot_reply_post.topic_id}",
payload,
- user_ids: bot_reply_post.topic.allowed_user_ids,
+ user_ids: user_ids.presence,
+ group_ids: group_ids.presence,
max_backlog_size: 2,
max_backlog_age: MAX_STREAM_DELAY_SECONDS,
)
diff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb
index c1d6915be25..f1a62906706 100644
--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/playground_spec.rb
@@ -1177,6 +1177,53 @@ RSpec.describe DiscourseAi::AiBot::Playground do
end
end
+ it "only streams the reply to the PM's participants" do
+ messages =
+ DiscourseAi::Completions::Llm.with_prepared_responses(["the secret bot reply"]) do
+ MessageBus.track_publish("discourse-ai/ai-bot/topic/#{pm.id}") do
+ playground.reply_to(third_post)
+ end
+ end
+
+ message = messages.first
+ expect(message.user_ids).to contain_exactly(user.id, bot_user.id)
+ expect(message.group_ids).to be_nil
+ end
+
+ it "streams the reply to a participant authorized through an allowed group" do
+ group = Fabricate(:group)
+ group_pm =
+ Fabricate(
+ :private_message_topic,
+ user: user,
+ topic_allowed_users: [
+ Fabricate.build(:topic_allowed_user, user: user),
+ Fabricate.build(:topic_allowed_user, user: bot_user),
+ ],
+ topic_allowed_groups: [Fabricate.build(:topic_allowed_group, group: group)],
+ )
+ Fabricate(:post, topic: group_pm, user: user, post_number: 1, raw: "group opening message")
+ group_post =
+ Fabricate(
+ :post,
+ topic: group_pm,
+ user: user,
+ post_number: 2,
+ raw: "a private group question",
+ )
+
+ messages =
+ DiscourseAi::Completions::Llm.with_prepared_responses(["a reply to the group"]) do
+ MessageBus.track_publish("discourse-ai/ai-bot/topic/#{group_pm.id}") do
+ playground.reply_to(group_post)
+ end
+ end
+
+ message = messages.first
+ expect(message.user_ids).to contain_exactly(user.id, bot_user.id)
+ expect(message.group_ids).to contain_exactly(group.id)
+ end
+
it "supports multiple function calls" do
tool_call1 =
DiscourseAi::Completions::ToolCall.new(