Skip to content

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

FIX: Prevent hidden tags from leaking in AI summaries (#36167)

Rafael dos Santos Silvac841c6d53631
diff --git a/plugins/discourse-ai/db/migrate/20251121131136_delete_summaries_with_hidden_tags.rb b/plugins/discourse-ai/db/migrate/20251121131136_delete_summaries_with_hidden_tags.rb
new file mode 100644
index 00000000000..67fd62a159f
--- /dev/null
+++ b/plugins/discourse-ai/db/migrate/20251121131136_delete_summaries_with_hidden_tags.rb
@@ -0,0 +1,48 @@
+# frozen_string_literal: true
+
+class DeleteSummariesWithHiddenTags < ActiveRecord::Migration[7.1]
+ def up
+ # Get all hidden tag names (tags that are restricted and NOT visible to everyone)
+ # Group::AUTO_GROUPS[:everyone] is 0
+ hidden_tag_names_sql = <<~SQL
+ SELECT DISTINCT tags.name
+ FROM tags
+ INNER JOIN tag_group_memberships ON tags.id = tag_group_memberships.tag_id
+ INNER JOIN tag_groups ON tag_group_memberships.tag_group_id = tag_groups.id
+ INNER JOIN tag_group_permissions ON tag_groups.id = tag_group_permissions.tag_group_id
+ WHERE tags.id NOT IN (
+ SELECT tgm.tag_id
+ FROM tag_group_memberships tgm
+ INNER JOIN tag_group_permissions tgp ON tgm.tag_group_id = tgp.tag_group_id
+ WHERE tgp.group_id = 0
+ )
+ SQL
+
+ hidden_tag_names = DB.query_single(hidden_tag_names_sql)
+
+ return if hidden_tag_names.empty?
+
+ # Delete summaries that might contain hidden tags
+ # We err on the side of caution - summaries are cheap to regenerate
+ # Use regex for efficient matching of multiple tags in a single query
+
+ # Escape special regex characters and build alternation pattern
+ # Example: (tag1|tag2|tag3) matches any of the tags
+ escaped_tags = hidden_tag_names.map { |tag| Regexp.escape(tag) }
+ regex_pattern = "(#{escaped_tags.join("|")})"
+
+ deleted_count = DB.exec(<<~SQL, regex_pattern)
+ DELETE FROM ai_summaries
+ WHERE target_type = 'Topic'
+ AND summarized_text ~* ?
+ SQL
+
+ if deleted_count > 0
+ Rails.logger.warn("Deleted #{deleted_count} AI summaries that may contain hidden tags.")
+ end
+ end
+
+ def down
+ raise ActiveRecord::IrreversibleMigration
+ end
+end
diff --git a/plugins/discourse-ai/lib/summarization/strategies/topic_summary.rb b/plugins/discourse-ai/lib/summarization/strategies/topic_summary.rb
index 69e982d424a..7f6a508208d 100644
--- a/plugins/discourse-ai/lib/summarization/strategies/topic_summary.rb
+++ b/plugins/discourse-ai/lib/summarization/strategies/topic_summary.rb
@@ -42,7 +42,9 @@ module DiscourseAi
resource_path = "#{Discourse.base_path}/t/-/#{target.id}"
content_title = target.title
category_name = target.category&.name
- tags = target.tags&.map(&:name)&.sort
+ # Only include public tags in summaries since summaries are cached and shared across users
+ # Use anonymous guardian to get most restrictive tag visibility
+ tags = target.visible_tags(Guardian.new(nil)).map(&:name).sort
input =
contents.map { |item| "(#{item[:id]} #{item[:poster]} said: #{item[:text]} " }.join
diff --git a/plugins/discourse-ai/spec/lib/modules/summarization/strategies/topic_summary_spec.rb b/plugins/discourse-ai/spec/lib/modules/summarization/strategies/topic_summary_spec.rb
index 89b388b3128..994592e57e8 100644
--- a/plugins/discourse-ai/spec/lib/modules/summarization/strategies/topic_summary_spec.rb
+++ b/plugins/discourse-ai/spec/lib/modules/summarization/strategies/topic_summary_spec.rb
@@ -136,6 +136,25 @@ RSpec.describe DiscourseAi::Summarization::Strategies::TopicSummary do
expect(content).to include("Tags: tag1, tag2")
end
+
+ context "with hidden tags" do
+ fab!(:hidden_tag) { Fabricate(:tag, name: "hidden") }
+
+ before do
+ Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: [hidden_tag.name])
+ topic.tags = [tag1, hidden_tag, tag2]
+ end
+
+ it "excludes hidden tags from summaries (summaries are cached and shared)" do
+ # Summaries are cached and shared across all users, so hidden tags should never appear
+ # regardless of who generates them
+ messages = topic_summary.as_llm_messages(contents)
+ content = messages.first[:content]
+
+ expect(content).to include("Tags: tag1, tag2")
+ expect(content).not_to include("hidden")
+ end
+ end
end
context "when topic has no category or tags" do