Skip to content

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

SECURITY: Enforce mandatory human approval for ChangeSiteSetting, SuspendUser, and Sile... (#43702)

Penar Musaraj5debcbac334f
diff --git a/plugins/discourse-ai/lib/agents/bot.rb b/plugins/discourse-ai/lib/agents/bot.rb
index 4c0c6d2a180..43aee602f39 100644
--- a/plugins/discourse-ai/lib/agents/bot.rb
+++ b/plugins/discourse-ai/lib/agents/bot.rb
@@ -481,6 +481,8 @@ module DiscourseAi
end
def tool_requires_approval?(tool)
+ return true if tool.class.mandatory_approval?
+
tool.class.requires_approval? && @agent.class.require_approval
end
diff --git a/plugins/discourse-ai/lib/agents/tools/change_site_setting.rb b/plugins/discourse-ai/lib/agents/tools/change_site_setting.rb
index 43d04f29345..8011efeaf3a 100644
--- a/plugins/discourse-ai/lib/agents/tools/change_site_setting.rb
+++ b/plugins/discourse-ai/lib/agents/tools/change_site_setting.rb
@@ -53,6 +53,10 @@ module DiscourseAi
true
end
+ def self.mandatory_approval?
+ true
+ end
+
def self.attribute_to_approver?
true
end
diff --git a/plugins/discourse-ai/lib/agents/tools/silence_user.rb b/plugins/discourse-ai/lib/agents/tools/silence_user.rb
index 2133cf27c78..c8a03585ab0 100644
--- a/plugins/discourse-ai/lib/agents/tools/silence_user.rb
+++ b/plugins/discourse-ai/lib/agents/tools/silence_user.rb
@@ -50,6 +50,10 @@ module DiscourseAi
true
end
+ def self.mandatory_approval?
+ true
+ end
+
def self.attribute_to_approver?
true
end
diff --git a/plugins/discourse-ai/lib/agents/tools/suspend_user.rb b/plugins/discourse-ai/lib/agents/tools/suspend_user.rb
index 0c599aa884c..84737e688b2 100644
--- a/plugins/discourse-ai/lib/agents/tools/suspend_user.rb
+++ b/plugins/discourse-ai/lib/agents/tools/suspend_user.rb
@@ -50,6 +50,10 @@ module DiscourseAi
true
end
+ def self.mandatory_approval?
+ true
+ end
+
def self.attribute_to_approver?
true
end
diff --git a/plugins/discourse-ai/lib/agents/tools/tool.rb b/plugins/discourse-ai/lib/agents/tools/tool.rb
index e8a110773e6..0e906d1be9b 100644
--- a/plugins/discourse-ai/lib/agents/tools/tool.rb
+++ b/plugins/discourse-ai/lib/agents/tools/tool.rb
@@ -47,6 +47,10 @@ module DiscourseAi
false
end
+ def mandatory_approval?
+ false
+ end
+
# When true, the replayed tool (after approval) is given the
# approving moderator as context.user, so guardian checks and
# downstream audit logs (StaffActionLogger, UserHistory) credit
diff --git a/plugins/discourse-ai/spec/lib/agents/bot_spec.rb b/plugins/discourse-ai/spec/lib/agents/bot_spec.rb
index a1a6e769ad8..84a07e56d64 100644
--- a/plugins/discourse-ai/spec/lib/agents/bot_spec.rb
+++ b/plugins/discourse-ai/spec/lib/agents/bot_spec.rb
@@ -1171,6 +1171,52 @@ RSpec.describe DiscourseAi::Agents::Bot do
expect(result[:error]).not_to include(secret_value)
end
+ it "queues mandatory approval tools when require_approval is false" do
+ toggle_enabled_bots(bots: [fake])
+ Group.refresh_automatic_groups!
+ target_user = Fabricate(:user)
+ mandatory_approval_agent =
+ AiAgent.create!(
+ name: "MandatoryApprovalAgent",
+ system_prompt: "test",
+ description: "test",
+ allowed_group_ids: [Group::AUTO_GROUPS[:trust_level_0]],
+ require_approval: false,
+ tools: %w[SuspendUser SilenceUser ChangeSiteSetting],
+ )
+ agent_class = mandatory_approval_agent.class_instance
+ test_bot_user = DiscourseAi::AiBot::EntryPoint.find_user_from_model(fake.name)
+ bot = described_class.as(test_bot_user, agent: agent_class.new)
+ context = DiscourseAi::Agents::BotContext.new(user: admin)
+ tools = [
+ DiscourseAi::Agents::Tools::SuspendUser.new(
+ { username: target_user.username, duration_days: 3, reason: "Testing" },
+ bot_user: admin,
+ llm: bot.llm,
+ context: context,
+ ),
+ DiscourseAi::Agents::Tools::SilenceUser.new(
+ { username: target_user.username, duration_days: 3, reason: "Testing" },
+ bot_user: admin,
+ llm: bot.llm,
+ context: context,
+ ),
+ DiscourseAi::Agents::Tools::ChangeSiteSetting.new(
+ { setting_name: "min_post_length", value: "42", reason: "Testing" },
+ bot_user: admin,
+ llm: bot.llm,
+ context: context,
+ ),
+ ]
+
+ results = tools.map { |tool| bot.send(:invoke_tool, tool, context) { |*args| } }
+
+ expect(results).to all(include(status: "pending_approval"))
+ expect(target_user.reload).not_to be_suspended
+ expect(SiteSetting.min_post_length).not_to eq(42)
+ expect(ReviewableAiToolAction.count).to eq(3)
+ end
+
it "executes immediately when require_approval is false" do
toggle_enabled_bots(bots: [fake])
Group.refresh_automatic_groups!
diff --git a/plugins/discourse-ai/spec/requests/ai_bot/chat_tool_approval_spec.rb b/plugins/discourse-ai/spec/requests/ai_bot/chat_tool_approval_spec.rb
new file mode 100644
index 00000000000..0870c4405c7
--- /dev/null
+++ b/plugins/discourse-ai/spec/requests/ai_bot/chat_tool_approval_spec.rb
@@ -0,0 +1,52 @@
+# frozen_string_literal: true
+
+RSpec.describe "AI agent chat tool approval" do
+ fab!(:admin) { Fabricate(:admin, refresh_auto_groups: true) }
+
+ before do
+ enable_current_plugin
+ SiteSetting.ai_bot_enabled = true
+ SiteSetting.chat_enabled = true
+ SiteSetting.chat_allowed_groups = Group::AUTO_GROUPS[:everyone]
+ sign_in(admin)
+ end
+
+ it "queues a suspension requested through the chat API for human approval" do
+ fake_llm_model = Fabricate(:llm_model, name: "mandatory-approval-fake", provider: "fake")
+ target_user = Fabricate(:user)
+ agent =
+ Fabricate(
+ :ai_agent,
+ allowed_group_ids: [Group::AUTO_GROUPS[:admins]],
+ allow_chat_direct_messages: true,
+ default_llm_id: fake_llm_model.id,
+ require_approval: false,
+ tools: ["SuspendUser"],
+ )
+ agent.create_user!
+ channel = Fabricate(:direct_message_channel, users: [admin, agent.user])
+ tool_call =
+ DiscourseAi::Completions::ToolCall.new(
+ id: "suspend-user",
+ name: "suspend_user",
+ parameters: {
+ username: target_user.username,
+ duration_days: 3,
+ reason: "Requested in the chat conversation",
+ },
+ )
+
+ post "/chat/#{channel.id}.json", params: { message: "Suspend @#{target_user.username}." }
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["message_id"]).to be_present
+
+ job_args = Jobs::CreateAiChatReply.jobs.last["args"].first.symbolize_keys
+ DiscourseAi::Completions::Llm.with_prepared_responses([tool_call, "Awaiting approval."]) do
+ Jobs::CreateAiChatReply.new.execute(job_args)
+ end
+
+ expect(target_user.reload.suspended?).to eq(false)
+ expect(ReviewableAiToolAction.count).to eq(1)
+ end
+end