A platform for community discussion. Free, open, simple.
FIX: Authorize Retried AI Bot Replies Against Topic Creator (#42325)
Mark VanLandinghamc2a47a6675f7
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 f8e7c315451..34832efd97f 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@@ -81,11 +81,17 @@ module DiscourseAiagent_id = retry_agent_id(post, prompt_post)llm_model_id = post.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD]+ authorization_user_id =+ post.custom_fields[DiscourseAi::AiBot::POST_AI_AGENT_AUTHORIZATION_USER_ID_FIELD].presence+ authorization_user_id = authorization_user_id.to_i if authorization_user_id+ authorization_user_id ||= prompt_post.topic.user_id+ raise Discourse::InvalidAccess if authorization_user_id.blank?args = {post_id: prompt_post.id,bot_user_id: post.user_id,agent_id: agent_id,+ authorization_user_id: authorization_user_id,reply_post_id: post.id,}diff --git a/plugins/discourse-ai/app/jobs/regular/create_ai_reply.rb b/plugins/discourse-ai/app/jobs/regular/create_ai_reply.rbindex 9e317ac5984..56bf8ef9424 100644--- a/plugins/discourse-ai/app/jobs/regular/create_ai_reply.rb+++ b/plugins/discourse-ai/app/jobs/regular/create_ai_reply.rb@@ -11,20 +11,31 @@ module Jobsreturn if args[:reply_post_id].present? && reply_post.nil?return if reply_post && reply_post.topic_id != post.topic_idagent_id = args[:agent_id]+ authorization_user =+ if args.key?(:authorization_user_id)+ User.find_by(id: args[:authorization_user_id])+ else+ # jobs enqueued before authorization provenance was recorded+ post.user+ end+ return if authorization_user.nil?+llm_model_id = args[:llm_model_id]begin- agent = DiscourseAi::Agents::Agent.find_by(user: post.user, id: agent_id)+ agent = DiscourseAi::Agents::Agent.find_by(user: authorization_user, id: agent_id)raise DiscourseAi::Agents::Bot::BOT_NOT_FOUND if agent.nil?llm_model = LlmModel.find_by(id: llm_model_id.to_i) if !llm_model_id.to_i.zero?bot = DiscourseAi::Agents::Bot.as(bot_user, agent: agent.new, model: llm_model)- DiscourseAi::AiBot::Playground.new(bot).reply_to(- post,- feature_name: "bot",- existing_reply_post: reply_post,- )+ reply_post =+ DiscourseAi::AiBot::Playground.new(bot).reply_to(+ post,+ feature_name: "bot",+ existing_reply_post: reply_post,+ authorization_user_id: authorization_user.id,+ )rescue DiscourseAi::Agents::Bot::BOT_NOT_FOUNDRails.logger.warn("Bot not found for post #{post.id} - perhaps agent was deleted or bot was disabled",diff --git a/plugins/discourse-ai/lib/ai_bot.rb b/plugins/discourse-ai/lib/ai_bot.rbindex baa9456ef83..e511e1af279 100644--- a/plugins/discourse-ai/lib/ai_bot.rb+++ b/plugins/discourse-ai/lib/ai_bot.rb@@ -7,6 +7,7 @@ module DiscourseAiPOST_AI_LLM_NAME_FIELD = "ai_llm_name"POST_AI_LLM_MODEL_ID_FIELD = "ai_llm_model_id"POST_AI_AGENT_ID_FIELD = "ai_agent_id"+ POST_AI_AGENT_AUTHORIZATION_USER_ID_FIELD = "ai_agent_authorization_user_id"TOPIC_AI_AGENT_ID_FIELD = "ai_agent_id"TOPIC_AI_AGENT_ID_MAX_LENGTH = 20PERSONAL_MESSAGE_CONTEXT = "discourse_ai.ai_bot_personal_message"diff --git a/plugins/discourse-ai/lib/ai_bot/playground.rb b/plugins/discourse-ai/lib/ai_bot/playground.rbindex 0a1ab9fdc68..dc9e93b50a6 100644--- a/plugins/discourse-ai/lib/ai_bot/playground.rb+++ b/plugins/discourse-ai/lib/ai_bot/playground.rb@@ -147,14 +147,29 @@ module DiscourseAitopic_agent_id = post.topic.custom_fields["ai_agent_id"]topic_agent_id = topic_agent_id.to_i if topic_agent_id.present?- agent_id = mentioned&.dig(:id) || topic_agent_id+ authorization_user = post.user+ if mentioned+ agent_id = mentioned[:id]+ else+ agent_id = topic_agent_id+ authorization_user = post.topic.user if topic_agent_id+ endagent = nil- agent = DiscourseAi::Agents::Agent.find_by(user: post.user, id: agent_id.to_i) if agent_id+ agent =+ DiscourseAi::Agents::Agent.find_by(+ user: authorization_user,+ id: agent_id.to_i,+ ) if agent_id && authorization_userif !agent && (agent_name = post.topic.custom_fields["ai_agent"])- agent = DiscourseAi::Agents::Agent.find_by(user: post.user, name: agent_name)+ authorization_user = post.topic.user+ agent =+ DiscourseAi::Agents::Agent.find_by(+ user: authorization_user,+ name: agent_name,+ ) if authorization_userend# edge case, llm was mentioned in an ai agent conversation@@ -170,12 +185,15 @@ module DiscourseAiendend- agent ||= DiscourseAi::Agents::General+ if !agent+ agent = DiscourseAi::Agents::General+ authorization_user = post.user+ endbot_user = User.find(agent.user_id) if agent && agent.force_default_llmbot = DiscourseAi::Agents::Bot.as(bot_user, agent: agent.new)- new(bot).update_playground_with(post)+ new(bot).update_playground_with(post, authorization_user: authorization_user)endend@@ -222,8 +240,8 @@ module DiscourseAi@bot = botend- def update_playground_with(post)- schedule_bot_reply(post) if can_attach?(post)+ def update_playground_with(post, authorization_user: post.user)+ schedule_bot_reply(post, authorization_user: authorization_user) if can_attach?(post)enddef title_playground(post, user)@@ -462,6 +480,7 @@ module DiscourseAicancel_manager: nil,attributed_user: nil,feature_context: nil,+ authorization_user_id: nil,&blk)# this is a multithreading issue@@ -560,15 +579,11 @@ module DiscourseAiskip_jobs: true,post_type: post_type,skip_guardian: true,- custom_fields: {- DiscourseAi::AiBot::POST_AI_LLM_NAME_FIELD => bot.llm.llm_model.display_name,- DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD => bot.llm.llm_model.id,- DiscourseAi::AiBot::POST_AI_AGENT_ID_FIELD => bot.agent.id,- },+ custom_fields: ai_custom_fields(authorization_user_id: authorization_user_id),)end- save_ai_custom_fields(reply_post)+ save_ai_custom_fields(reply_post, authorization_user_id: authorization_user_id)stream_user_ids = reply_post.topic.allowed_users.pluck(:id)stream_group_ids = reply_post.topic.allowed_groups.pluck(:id)@@ -677,7 +692,7 @@ module DiscourseAiskip_validations: true,force_new_version: true,)- save_ai_custom_fields(reply_post)+ save_ai_custom_fields(reply_post, authorization_user_id: authorization_user_id)elsereply_post =PostCreator.create!(@@ -687,11 +702,7 @@ module DiscourseAiskip_validations: true,post_type: post_type,skip_guardian: true,- custom_fields: {- DiscourseAi::AiBot::POST_AI_LLM_NAME_FIELD => bot.llm.llm_model.display_name,- DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD => bot.llm.llm_model.id,- DiscourseAi::AiBot::POST_AI_AGENT_ID_FIELD => bot.agent.id,- },+ custom_fields: ai_custom_fields(authorization_user_id: authorization_user_id),)end@@ -729,6 +740,7 @@ module DiscourseAiraw: error_message,skip_validations: true,skip_guardian: true,+ custom_fields: ai_custom_fields(authorization_user_id: authorization_user_id),)end@@ -787,16 +799,22 @@ module DiscourseAiprivate- def save_ai_custom_fields(reply_post)- reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_NAME_FIELD] = bot- .llm- .llm_model- .display_name- reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD] = bot- .llm- .llm_model- .id- reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_AGENT_ID_FIELD] = bot.agent.id+ def ai_custom_fields(authorization_user_id: nil)+ fields = {+ DiscourseAi::AiBot::POST_AI_LLM_NAME_FIELD => bot.llm.llm_model.display_name,+ DiscourseAi::AiBot::POST_AI_LLM_MODEL_ID_FIELD => bot.llm.llm_model.id,+ DiscourseAi::AiBot::POST_AI_AGENT_ID_FIELD => bot.agent.id,+ }+ if authorization_user_id+ fields[+ DiscourseAi::AiBot::POST_AI_AGENT_AUTHORIZATION_USER_ID_FIELD+ ] = authorization_user_id+ end+ fields+ end++ def save_ai_custom_fields(reply_post, authorization_user_id: nil)+ reply_post.custom_fields.merge!(ai_custom_fields(authorization_user_id:))reply_post.save_custom_fieldsend@@ -860,13 +878,14 @@ module DiscourseAitrueend- def schedule_bot_reply(post)+ def schedule_bot_reply(post, authorization_user: post.user)agent_id = DiscourseAi::Agents::Agent.system_agents[bot.agent.class] || bot.agent.class.id::Jobs.enqueue(:create_ai_reply,post_id: post.id,bot_user_id: bot.bot_user.id,agent_id: agent_id,+ authorization_user_id: authorization_user&.id,)enddiff --git a/plugins/discourse-ai/spec/lib/modules/ai_bot/jobs/regular/create_ai_reply_spec.rb b/plugins/discourse-ai/spec/lib/modules/ai_bot/jobs/regular/create_ai_reply_spec.rbindex 5e131ded5d9..b7351067447 100644--- a/plugins/discourse-ai/spec/lib/modules/ai_bot/jobs/regular/create_ai_reply_spec.rb+++ b/plugins/discourse-ai/spec/lib/modules/ai_bot/jobs/regular/create_ai_reply_spec.rb@@ -23,13 +23,80 @@ RSpec.describe Jobs::CreateAiReply doit "adds a reply from the bot" doagent_id = AiAgent.find_by(name: "Forum Helper").id+ bot_user = DiscourseAi::AiBot::EntryPoint.find_user_from_model("gpt-3.5-turbo")+ authorization_user = topic.first_post.user++ DiscourseAi::Completions::Llm.with_prepared_responses([expected_response]) do+ job.execute(+ post_id: topic.first_post.id,+ bot_user_id: bot_user.id,+ agent_id: agent_id,+ authorization_user_id: authorization_user.id,+ )+ end++ bot_reply = topic.posts.last++ aggregate_failures do+ expect(bot_reply.raw).to eq(expected_response)+ expect(+ bot_reply.custom_fields[+ DiscourseAi::AiBot::POST_AI_AGENT_AUTHORIZATION_USER_ID_FIELD+ ].to_i,+ ).to eq(authorization_user.id)+ end+ end++ it "does not reply when an explicit authorization user is missing" do+ agent_id = AiAgent.find_by(name: "Forum Helper").id+ bot_user = DiscourseAi::AiBot::EntryPoint.find_user_from_model("gpt-3.5-turbo")+ expect {+ DiscourseAi::Completions::Llm.with_prepared_responses([expected_response]) do+ job.execute(+ post_id: topic.first_post.id,+ bot_user_id: bot_user.id,+ agent_id: agent_id,+ authorization_user_id: nil,+ )+ end+ }.not_to change { topic.posts.count }+ end++ it "falls back to the post author for jobs enqueued without authorization provenance" do+ agent_id = AiAgent.find_by(name: "Forum Helper").idbot_user = DiscourseAi::AiBot::EntryPoint.find_user_from_model("gpt-3.5-turbo")+DiscourseAi::Completions::Llm.with_prepared_responses([expected_response]) dojob.execute(post_id: topic.first_post.id, bot_user_id: bot_user.id, agent_id: agent_id)endexpect(topic.posts.last.raw).to eq(expected_response)end++ it "records authorization provenance before streaming generation fails" do+ agent_id = AiAgent.find_by(name: "Forum Helper").id+ bot_user = DiscourseAi::AiBot::EntryPoint.find_user_from_model("gpt-3.5-turbo")+ authorization_user = Fabricate(:user, refresh_auto_groups: true)+ pm_topic = Fabricate(:private_message_topic, user: authorization_user, recipient: bot_user)+ prompt_post = Fabricate(:post, topic: pm_topic, user: authorization_user)++ expect {+ DiscourseAi::Completions::Llm.with_prepared_responses([]) do+ job.execute(+ post_id: prompt_post.id,+ bot_user_id: bot_user.id,+ agent_id: agent_id,+ authorization_user_id: authorization_user.id,+ )+ end+ }.to raise_error(DiscourseAi::Completions::Endpoints::CannedResponse::CANNED_RESPONSE_ERROR)++ bot_reply = pm_topic.posts.order(:post_number).last++ expect(+ bot_reply.custom_fields[DiscourseAi::AiBot::POST_AI_AGENT_AUTHORIZATION_USER_ID_FIELD].to_i,+ ).to eq(authorization_user.id)+ endendenddiff --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 e63f167f5e8..fbb4287e553 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@@ -480,6 +480,102 @@ RSpec.describe DiscourseAi::AiBot::BotController doexpect(response.status).to eq(403)end+ it "uses the topic creator to authorize a legacy restricted agent retry" do+ admin = Fabricate(:admin, refresh_auto_groups: true)+ restricted_agent =+ Fabricate(+ :ai_agent,+ user: bot_user,+ default_llm_id: llm_model.id,+ allowed_group_ids: [Group::AUTO_GROUPS[:admins]],+ )+ topic = Fabricate(:private_message_topic, user: user, recipient: bot_user)+ topic.topic_allowed_users.create!(user: admin)+ prompt_post =+ Fabricate(:post, topic: topic, user: admin, raw: "Please use the restricted agent")+ reply_post = Fabricate(:post, topic: topic, user: bot_user, raw: "original restricted reply")+ reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_AGENT_ID_FIELD] = restricted_agent.id+ reply_post.save_custom_fields+ AiAgent.agent_cache.flush!++ DiscourseAi::Completions::Llm.with_prepared_responses(+ ["restricted retry response", "restricted retry title"],+ ) do+ post "/discourse-ai/ai-bot/post/#{reply_post.id}/retry"++ job_args = Jobs::CreateAiReply.jobs.last["args"].first.symbolize_keys+ Jobs::CreateAiReply.new.execute(job_args)+ end++ aggregate_failures do+ expect(response.status).to eq(200)+ expect(response.parsed_body).to eq("success" => "OK")+ expect(Jobs::CreateAiReply.jobs.last["args"].first["authorization_user_id"]).to eq(user.id)+ expect(reply_post.reload.raw).to eq("original restricted reply")+ end+ end++ it "reuses the recorded authorization user when retrying" do+ admin = Fabricate(:admin, refresh_auto_groups: true)+ restricted_agent =+ Fabricate(+ :ai_agent,+ user: bot_user,+ default_llm_id: llm_model.id,+ allowed_group_ids: [Group::AUTO_GROUPS[:admins]],+ )+ topic = Fabricate(:private_message_topic, user: user, recipient: bot_user)+ topic.topic_allowed_users.create!(user: admin)+ prompt_post = Fabricate(:post, topic: topic, user: admin, raw: "Use the restricted agent")+ reply_post = Fabricate(:post, topic: topic, user: bot_user, raw: "original reply")+ reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_AGENT_ID_FIELD] = restricted_agent.id+ reply_post.custom_fields[+ DiscourseAi::AiBot::POST_AI_AGENT_AUTHORIZATION_USER_ID_FIELD+ ] = admin.id+ reply_post.save_custom_fields+ AiAgent.agent_cache.flush!++ DiscourseAi::Completions::Llm.with_prepared_responses(+ ["retried response", "retried title"],+ ) do+ post "/discourse-ai/ai-bot/post/#{reply_post.id}/retry"++ job_args = Jobs::CreateAiReply.jobs.last["args"].first.symbolize_keys+ Jobs::CreateAiReply.new.execute(job_args)+ end++ aggregate_failures do+ expect(response.status).to eq(200)+ expect(Jobs::CreateAiReply.jobs.last["args"].first["authorization_user_id"]).to eq(admin.id)+ expect(reply_post.reload.raw).to eq("retried response")+ end+ end++ it "fails closed when a legacy retry has no topic creator to authorize against" do+ admin = Fabricate(:admin, refresh_auto_groups: true)+ restricted_agent =+ Fabricate(+ :ai_agent,+ user: bot_user,+ default_llm_id: llm_model.id,+ allowed_group_ids: [Group::AUTO_GROUPS[:admins]],+ )+ topic = Fabricate(:private_message_topic, user: user, recipient: bot_user)+ topic.topic_allowed_users.create!(user: admin)+ prompt_post = Fabricate(:post, topic: topic, user: admin, raw: "Use the restricted agent")+ reply_post = Fabricate(:post, topic: topic, user: bot_user, raw: "original reply")+ reply_post.custom_fields[DiscourseAi::AiBot::POST_AI_AGENT_ID_FIELD] = restricted_agent.id+ reply_post.save_custom_fields+ topic.update_columns(user_id: nil)+ AiAgent.agent_cache.flush!++ expect_not_enqueued_with(job: :create_ai_reply) do+ post "/discourse-ai/ai-bot/post/#{reply_post.id}/retry"+ end++ expect(response.status).to eq(403)+ end+it "streams a replacement into the existing bot reply" doretry_text = "second attempt"messages = nildiff --git a/plugins/discourse-ai/spec/requests/ai_bot/posts_controller_spec.rb b/plugins/discourse-ai/spec/requests/ai_bot/posts_controller_spec.rbnew file mode 100644index 00000000000..44f9c04f62e--- /dev/null+++ b/plugins/discourse-ai/spec/requests/ai_bot/posts_controller_spec.rb@@ -0,0 +1,78 @@+# frozen_string_literal: true++RSpec.describe PostsController do+ fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }+ fab!(:admin) { Fabricate(:admin, refresh_auto_groups: true) }+ fab!(:llm_model)++ let(:bot_user) { llm_model.reload.user }++ fab!(:restricted_agent) do+ Fabricate(+ :ai_agent,+ allowed_group_ids: [Group::AUTO_GROUPS[:admins]],+ allow_personal_messages: true,+ )+ end++ before do+ enable_current_plugin+ toggle_enabled_bots(bots: [llm_model])+ SiteSetting.ai_bot_allowed_groups = Group::AUTO_GROUPS[:trust_level_0].to_s+ SiteSetting.min_personal_message_post_length = 5+ end++ describe "#create" do+ it "uses the topic creator permissions for stored agent ids" do+ topic = Fabricate(:private_message_topic, user: user, recipient: bot_user)+ topic.topic_allowed_users.create!(user: admin)+ topic.custom_fields["ai_agent_id"] = restricted_agent.id+ topic.save_custom_fields+ sign_in(admin)++ expect_not_enqueued_with(job: :create_ai_reply, args: { agent_id: restricted_agent.id }) do+ post "/posts.json", params: { raw: "I'll look into this.", topic_id: topic.id }+ end++ aggregate_failures do+ expect(response.status).to eq(200)+ expect(response.parsed_body["topic_id"]).to eq(topic.id)+ end+ end++ it "uses the topic creator permissions for legacy stored agent names" do+ topic = Fabricate(:private_message_topic, user: user, recipient: bot_user)+ topic.topic_allowed_users.create!(user: admin)+ topic.custom_fields["ai_agent"] = restricted_agent.name+ topic.save_custom_fields+ sign_in(admin)++ expect_not_enqueued_with(job: :create_ai_reply, args: { agent_id: restricted_agent.id }) do+ post "/posts.json", params: { raw: "I'll look into this.", topic_id: topic.id }+ end++ aggregate_failures do+ expect(response.status).to eq(200)+ expect(response.parsed_body["topic_id"]).to eq(topic.id)+ end+ end++ it "fails closed when the topic creator no longer exists" do+ topic = Fabricate(:private_message_topic, user: user, recipient: bot_user)+ topic.topic_allowed_users.create!(user: admin)+ topic.custom_fields["ai_agent_id"] = restricted_agent.id+ topic.save_custom_fields+ topic.update_columns(user_id: nil)+ sign_in(admin)++ expect_not_enqueued_with(job: :create_ai_reply, args: { agent_id: restricted_agent.id }) do+ post "/posts.json", params: { raw: "I'll look into this.", topic_id: topic.id }+ end++ aggregate_failures do+ expect(response.status).to eq(200)+ expect(response.parsed_body["topic_id"]).to eq(topic.id)+ end+ end+ end+end