Skip to content

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

FEATURE: enforce guardian permissions in AI tool runner (#37709)

Sam6cb3b14d7f14
diff --git a/plugins/discourse-ai/lib/personas/tool_runner.rb b/plugins/discourse-ai/lib/personas/tool_runner.rb
index 8f9d3523e6a..99ee52d4160 100644
--- a/plugins/discourse-ai/lib/personas/tool_runner.rb
+++ b/plugins/discourse-ai/lib/personas/tool_runner.rb
@@ -48,10 +48,17 @@ module DiscourseAi
if username.present?
User.find_by(username: username)
else
- Discourse.system_user
+ @bot_user || Discourse.system_user
end
end
+ def resolve_guardian(username)
+ user = resolve_user(username)
+ return nil, nil if user.nil?
+ guardian = user.staged? ? system_guardian : Guardian.new(user)
+ [user, guardian]
+ end
+
def resolve_category(category_id_or_name)
if category_id_or_name.is_a?(Integer) ||
category_id_or_name.to_i.to_s == category_id_or_name.to_s
@@ -584,25 +591,17 @@ module DiscourseAi
username = params[:username]
message = params[:message]
- # Validate parameters
return { error: "Missing required parameter: channel_name" } if channel_name.blank?
- return { error: "Missing required parameter: username" } if username.blank?
return { error: "Missing required parameter: message" } if message.blank?
- # Find the user
- user = User.find_by(username: username)
+ user, guardian = resolve_guardian(username)
return { error: "User not found: #{username}" } if user.nil?
- # Find the channel
channel = Chat::Channel.find_by(name: channel_name)
- if channel.nil?
- # Try finding by slug if not found by name
- channel = Chat::Channel.find_by(slug: channel_name.parameterize)
- end
+ channel ||= Chat::Channel.find_by(slug: channel_name.parameterize)
return { error: "Channel not found: #{channel_name}" } if channel.nil?
begin
- guardian = Guardian.new(user)
message =
ChatSDK::Message.create(
raw: message,
@@ -678,12 +677,12 @@ module DiscourseAi
return { error: "Missing required parameter: title" } if title.blank?
return { error: "Missing required parameter: raw" } if raw.blank?
- user = resolve_user(username)
+ user, guardian = resolve_guardian(username)
return { error: "User not found: #{username}" } if user.nil?
category = resolve_category(category_id.presence || category_name)
-
return { error: "Category not found" } if category.nil?
+ return { error: "Permission denied" } unless guardian.can_create?(Topic, category)
begin
post_creator =
@@ -694,7 +693,7 @@ module DiscourseAi
category: category.id,
tags: tags,
skip_validations: true,
- guardian: system_guardian,
+ guardian: guardian,
)
post = post_creator.create
@@ -731,13 +730,12 @@ module DiscourseAi
return { error: "Missing required parameter: topic_id" } if topic_id.blank?
return { error: "Missing required parameter: raw" } if raw.blank?
- # Find the user
- user = resolve_user(username)
+ user, guardian = resolve_guardian(username)
return { error: "User not found: #{username}" } if user.nil?
- # Verify topic exists
topic = Topic.find_by(id: topic_id)
return { error: "Topic not found" } if topic.nil?
+ return { error: "Permission denied" } unless guardian.can_create?(Post, topic)
begin
post_creator =
@@ -747,7 +745,7 @@ module DiscourseAi
topic_id: topic_id,
reply_to_post_number: reply_to_post_number,
skip_validations: true,
- guardian: system_guardian,
+ guardian: guardian,
)
post = post_creator.create
@@ -883,15 +881,10 @@ module DiscourseAi
options ||= {}
edit_reason = options["edit_reason"]
- username = options["username"]
-
- user = resolve_user(username)
- return { error: "User not found: #{username}" } if user.nil?
- guardian = Guardian.new(user)
- unless guardian.can_edit?(post)
- return { error: "User is not allowed to edit this post" }
- end
+ user, guardian = resolve_guardian(options["username"])
+ return { error: "User not found: #{options["username"]}" } if user.nil?
+ return { error: "Permission denied" } unless guardian.can_edit?(post)
revisor = PostRevisor.new(post)
if revisor.revise!(user, { raw: raw, edit_reason: edit_reason })
@@ -912,10 +905,10 @@ module DiscourseAi
updates ||= {}
options ||= {}
- user = resolve_user(options["username"])
- return { error: "User not found: #{options["username"]}" } if user.nil?
- guardian = Guardian.new(user)
+ user, guardian = resolve_guardian(options["username"])
+ return { error: "User not found: #{options["username"]}" } if user.nil?
+ return { error: "Permission denied" } unless guardian.can_see?(topic)
# Handle category change
if updates.key?("category")
diff --git a/plugins/discourse-ai/spec/lib/personas/tool_runner_spec.rb b/plugins/discourse-ai/spec/lib/personas/tool_runner_spec.rb
index d466e4d862b..3966878c61f 100644
--- a/plugins/discourse-ai/spec/lib/personas/tool_runner_spec.rb
+++ b/plugins/discourse-ai/spec/lib/personas/tool_runner_spec.rb
@@ -3,8 +3,8 @@
require "rails_helper"
RSpec.describe DiscourseAi::Personas::ToolRunner do
- fab!(:user)
- fab!(:bot_user) { Fabricate(:user, admin: true) }
+ fab!(:user) { Fabricate(:user, refresh_auto_groups: true) }
+ fab!(:bot_user) { Fabricate(:user, admin: true, refresh_auto_groups: true) }
fab!(:tool) do
AiTool.create!(
name: "test_tool",
@@ -173,7 +173,7 @@ RSpec.describe DiscourseAi::Personas::ToolRunner do
expect(result["success"]).to eq(true)
expect(post.reload.raw).to eq("new raw content")
expect(post.edit_reason).to eq("AI edit")
- expect(post.last_editor_id).to eq(Discourse.system_user.id)
+ expect(post.last_editor_id).to eq(bot_user.id)
end
it "can edit a post as a specific user" do
@@ -215,7 +215,7 @@ RSpec.describe DiscourseAi::Personas::ToolRunner do
bot_user: bot_user,
tool: tool,
)
- expect { runner.invoke }.to raise_error(/Permission denied|not allowed to edit/)
+ expect { runner.invoke }.to raise_error(/Permission denied/)
expect(post.reload.raw).to eq(original_raw)
end
@@ -515,6 +515,104 @@ RSpec.describe DiscourseAi::Personas::ToolRunner do
expect(topic.reload.tags.pluck(:name)).to contain_exactly("tag1", "tag2")
end
+ describe "guardian permission checks" do
+ fab!(:private_category) do
+ Fabricate(:category).tap do |c|
+ c.set_permissions(staff: :full)
+ c.save!
+ end
+ end
+ fab!(:private_topic) { Fabricate(:topic, category: private_category) }
+
+ def run_tool(script, parameters)
+ tool.update!(script: script)
+ described_class.new(parameters: parameters, llm: llm, bot_user: bot_user, tool: tool).invoke
+ end
+
+ it "denies regular user access to restricted topics and categories" do
+ params = { username: user.username }
+
+ expect {
+ run_tool(
+ "function invoke(params) { return discourse.editTopic(params.topic_id, { visible: false }, { username: params.username }); }",
+ params.merge(topic_id: private_topic.id),
+ )
+ }.to raise_error(MiniRacer::RuntimeError, /Permission denied/)
+
+ expect {
+ run_tool(
+ 'function invoke(params) { return discourse.createTopic({ title: "Test title", raw: "Test body", category_id: params.category_id, username: params.username }); }',
+ params.merge(category_id: private_category.id),
+ )
+ }.to raise_error(MiniRacer::RuntimeError, /Permission denied/)
+
+ expect {
+ run_tool(
+ 'function invoke(params) { return discourse.createPost({ topic_id: params.topic_id, raw: "Test reply", username: params.username }); }',
+ params.merge(topic_id: private_topic.id),
+ )
+ }.to raise_error(MiniRacer::RuntimeError, /Permission denied/)
+ end
+
+ it "defaults to bot_user when no username is provided" do
+ new_category = Fabricate(:category)
+ result =
+ run_tool(
+ 'function invoke(params) { return discourse.createTopic({ title: "Bot topic title here", raw: "Bot topic body content", category_id: params.category_id }); }',
+ { category_id: new_category.id },
+ )
+ expect(result["success"]).to eq(true)
+ expect(Topic.find(result["topic_id"]).user_id).to eq(bot_user.id)
+
+ result =
+ run_tool(
+ 'function invoke(params) { return discourse.createPost({ topic_id: params.topic_id, raw: "Bot reply content" }); }',
+ { topic_id: topic.id },
+ )
+ expect(result["success"]).to eq(true)
+ expect(Post.find(result["post_id"]).user_id).to eq(bot_user.id)
+
+ result =
+ run_tool(
+ "function invoke(params) { return discourse.editTopic(params.topic_id, { visible: false }); }",
+ { topic_id: topic.id },
+ )
+ expect(result["success"]).to eq(true)
+ expect(topic.reload.visible).to eq(false)
+ end
+
+ it "allows moderator to toggle visibility on a topic" do
+ mod = Fabricate(:moderator)
+ result =
+ run_tool(
+ "function invoke(params) { return discourse.editTopic(params.topic_id, { visible: false }, { username: params.username }); }",
+ { topic_id: topic.id, username: mod.username },
+ )
+ expect(result["success"]).to eq(true)
+ expect(topic.reload.visible).to eq(false)
+ end
+
+ it "allows user to createTopic in a permitted category" do
+ result =
+ run_tool(
+ 'function invoke(params) { return discourse.createTopic({ title: "A valid topic title here", raw: "Some body content for the topic", category_id: params.category_id, username: params.username }); }',
+ { category_id: category.id, username: user.username },
+ )
+ expect(result["success"]).to eq(true)
+ expect(result["topic_id"]).to be_present
+ end
+
+ it "allows user to createPost in a permitted topic" do
+ result =
+ run_tool(
+ 'function invoke(params) { return discourse.createPost({ topic_id: params.topic_id, raw: "A reply to the topic", username: params.username }); }',
+ { topic_id: topic.id, username: user.username },
+ )
+ expect(result["success"]).to eq(true)
+ expect(result["post_id"]).to be_present
+ end
+ end
+
describe "custom fields" do
it "can get a custom field from a post" do
post = Fabricate(:post)