Skip to content

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

FIX: GET `preview` performs state changes and can publish artifacts even when sharing is denied (#38224)

Sam3a45cd902e41
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/shared_ai_conversations_controller.rb b/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/shared_ai_conversations_controller.rb
index 6aed3512e38..14033994840 100644
--- a/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/shared_ai_conversations_controller.rb
+++ b/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/shared_ai_conversations_controller.rb
@@ -101,7 +101,7 @@ module DiscourseAi
@shared_conversation = SharedAiConversation.find_by(target: @topic)
@error = DiscourseAi::AiBot::EntryPoint.ai_share_error(@topic, guardian)
- if @error == :not_allowed
+ if @error
raise Discourse::InvalidAccess.new(
nil,
nil,
diff --git a/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/shared_ai_conversations_controller.rb.lock b/plugins/discourse-ai/app/controllers/discourse_ai/ai_bot/shared_ai_conversations_controller.rb.lock
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/plugins/discourse-ai/app/models/shared_ai_conversation.rb b/plugins/discourse-ai/app/models/shared_ai_conversation.rb
index e99bdcf0e24..24f399d7a24 100644
--- a/plugins/discourse-ai/app/models/shared_ai_conversation.rb
+++ b/plugins/discourse-ai/app/models/shared_ai_conversation.rb
@@ -17,7 +17,8 @@ class SharedAiConversation < ActiveRecord::Base
raise "Target must be a topic for now" if !target.is_a?(Topic)
conversation = find_by(user: user, target: target)
- conversation_data = build_conversation_data(target, max_posts: max_posts)
+ conversation_data =
+ build_conversation_data(target, max_posts: max_posts, publish_artifacts: true)
conversation =
if conversation
@@ -135,7 +136,12 @@ class SharedAiConversation < ActiveRecord::Base
I18n.t("discourse_ai.share_ai.formatted_excerpt", llm_name: llm_name, excerpt: excerpt)
end
- def self.build_conversation_data(topic, max_posts: DEFAULT_MAX_POSTS, include_usernames: false)
+ def self.build_conversation_data(
+ topic,
+ max_posts: DEFAULT_MAX_POSTS,
+ include_usernames: false,
+ publish_artifacts: false
+ )
allowed_user_ids = topic.topic_allowed_users.pluck(:user_id)
ai_bot_participant = DiscourseAi::AiBot::EntryPoint.find_participant_in(allowed_user_ids)
@@ -168,7 +174,7 @@ class SharedAiConversation < ActiveRecord::Base
id: post.id,
user_id: post.user_id,
created_at: post.created_at,
- cooked: cook_artifacts(post),
+ cooked: cook_artifacts(post, publish: publish_artifacts),
}
mapped[:agent] = agent if ai_bot_participant&.id == post.user_id
@@ -178,7 +184,7 @@ class SharedAiConversation < ActiveRecord::Base
}
end
- def self.cook_artifacts(post)
+ def self.cook_artifacts(post, publish: false)
html = post.cooked
return html if !%w[lax hybrid strict].include?(SiteSetting.ai_artifact_security)
@@ -190,7 +196,7 @@ class SharedAiConversation < ActiveRecord::Base
version = node["data-ai-artifact-version"]
version_number = version.to_i if version
if id > 0
- AiArtifact.share_publicly(id: id, post: post)
+ AiArtifact.share_publicly(id: id, post: post) if publish
node.replace(AiArtifact.iframe_for(id, version_number))
end
end
diff --git a/plugins/discourse-ai/app/models/shared_ai_conversation.rb.lock b/plugins/discourse-ai/app/models/shared_ai_conversation.rb.lock
new file mode 100644
index 00000000000..e69de29bb2d
diff --git a/plugins/discourse-ai/spec/requests/ai_bot/shared_ai_conversations_controller_spec.rb b/plugins/discourse-ai/spec/requests/ai_bot/shared_ai_conversations_controller_spec.rb
index 488b88043ed..3d9c7ef279e 100644
--- a/plugins/discourse-ai/spec/requests/ai_bot/shared_ai_conversations_controller_spec.rb
+++ b/plugins/discourse-ai/spec/requests/ai_bot/shared_ai_conversations_controller_spec.rb
@@ -344,6 +344,53 @@ RSpec.describe DiscourseAi::AiBot::SharedAiConversationsController do
get "#{path}/preview/#{user_pm_share.id}.json"
expect(response).not_to have_http_status(:success)
end
+
+ it "denies preview when there are other people in the PM" do
+ other_user = Fabricate(:user)
+ pm_topic = Fabricate(:private_message_topic, user: user, recipient: bot_user)
+ pm_topic.topic_allowed_users.create!(user_id: other_user.id)
+ Fabricate(:post, topic: pm_topic, user: user)
+ Fabricate(:post, topic: pm_topic, user: bot_user)
+
+ get "#{path}/preview/#{pm_topic.id}.json"
+ expect(response).not_to have_http_status(:success)
+ end
+
+ it "denies preview when there is other content in the PM" do
+ pm_topic = Fabricate(:private_message_topic, user: user, recipient: bot_user)
+ Fabricate(:post, topic: pm_topic, user: user)
+ Fabricate(:post, topic: pm_topic)
+
+ get "#{path}/preview/#{pm_topic.id}.json"
+ expect(response).not_to have_http_status(:success)
+ end
+
+ it "does not share artifacts publicly when previewing a valid conversation" do
+ SiteSetting.ai_artifact_security = "lax"
+ first_post = user_pm_share.posts.first
+
+ artifact =
+ AiArtifact.create!(
+ user: bot_user,
+ post: first_post,
+ name: "test",
+ html: "<div>test</div>",
+ )
+
+ cooked_html =
+ "<p>Post with artifact</p>\n<div class=\"ai-artifact\" data-ai-artifact-id=\"#{artifact.id}\"></div>"
+ first_post.update_columns(
+ raw:
+ "Post with artifact\n<div class=\"ai-artifact\" data-ai-artifact-id=\"#{artifact.id}\"></div>",
+ cooked: cooked_html,
+ )
+
+ get "#{path}/preview/#{user_pm_share.id}.json"
+ expect(response).to have_http_status(:success)
+
+ artifact.reload
+ expect(artifact.metadata&.dig("public")).not_to eq(true)
+ end
end
end
diff --git a/plugins/discourse-ai/spec/requests/ai_bot/shared_ai_conversations_controller_spec.rb.lock b/plugins/discourse-ai/spec/requests/ai_bot/shared_ai_conversations_controller_spec.rb.lock
new file mode 100644
index 00000000000..e69de29bb2d