A platform for community discussion. Free, open, simple.
SECURITY: Respect PM read scope when listing MCP bookmarks (#43827)
Penar Musaraje81a2be18b46
diff --git a/lib/discourse_mcp/tools/bookmarks.rb b/lib/discourse_mcp/tools/bookmarks.rbindex fd349fdf396..ef16455b18d 100644--- a/lib/discourse_mcp/tools/bookmarks.rb+++ b/lib/discourse_mcp/tools/bookmarks.rb@@ -7,9 +7,19 @@ module DiscourseMcpOUTPUT_SCHEMA = OutputSchema.object(bookmarks: OutputSchema::OBJECT_ARRAY)def self.call(arguments:, request_context:)+ bookmarks = Bookmark.where(user_id: request_context.user_id)+ if !request_context.has_scopes?(Scopes::PRIVATE_MESSAGES_READ)+ private_topic_ids = Topic.where(archetype: Archetype.private_message).select(:id)+ bookmarks =+ bookmarks+ .where.not(bookmarkable_type: "Topic", bookmarkable_id: private_topic_ids)+ .where.not(+ bookmarkable_type: "Post",+ bookmarkable_id: Post.where(topic_id: private_topic_ids).select(:id),+ )+ endbookmarks =- Bookmark- .where(user_id: request_context.user_id)+ bookmarks.includes(:bookmarkable).order(updated_at: :desc).limit(arguments.fetch("limit", 50).to_i.clamp(1, 100))diff --git a/spec/lib/discourse_mcp/regular_user_tools_spec.rb b/spec/lib/discourse_mcp/regular_user_tools_spec.rbindex 41abfb6cdc6..dd8ef10cb8b 100644--- a/spec/lib/discourse_mcp/regular_user_tools_spec.rb+++ b/spec/lib/discourse_mcp/regular_user_tools_spec.rb@@ -220,11 +220,13 @@ describe DiscourseMcp::Tools doFabricate(:topic, category: Fabricate(:private_category, group: Fabricate(:group))),)hidden_bookmark = Fabricate(:bookmark, user:, bookmarkable: private_post)+ context = request_context(user)+ allow(context).to receive(:has_scopes?).with(+ DiscourseMcp::Scopes::PRIVATE_MESSAGES_READ,+ ).and_return(false)result =- described_class.call(arguments: {}, request_context: request_context(user)).fetch(- :structuredContent,- )+ described_class.call(arguments: {}, request_context: context).fetch(:structuredContent)bookmark_ids = result.fetch(:bookmarks).pluck(:id)expect(bookmark_ids).to include(visible_bookmark.id)diff --git a/spec/requests/mcp_content_access_spec.rb b/spec/requests/mcp_content_access_spec.rbindex 827837e6f73..5e86bf1c7da 100644--- a/spec/requests/mcp_content_access_spec.rb+++ b/spec/requests/mcp_content_access_spec.rb@@ -480,6 +480,41 @@ describe "MCP content access" doexpect(notifications.pluck("id")).to eq([private_notification.id, other_notification.id])end+ it "filters private-message bookmarks before the limit unless the read scope is granted" do+ public_bookmark = Fabricate(:bookmark, user:, updated_at: 3.minutes.ago)+ private_topic_bookmark =+ Fabricate(+ :bookmark,+ user:,+ bookmarkable: message.topic,+ name: "Private topic bookmark note",+ updated_at: 2.minutes.ago,+ )+ private_post_bookmark =+ Fabricate(+ :bookmark,+ user:,+ bookmarkable: message,+ name: "Private post bookmark note",+ updated_at: 1.minute.ago,+ )+ authorize++ call_tool("discourse_bookmark_list", { limit: 2 })++ expect(response.status).to eq(200)+ bookmarks = response.parsed_body.dig("result", "structuredContent", "bookmarks")+ expect(bookmarks.pluck("id")).to eq([public_bookmark.id])+ expect(response.body).not_to include(private_topic_bookmark.name, private_post_bookmark.name)++ authorize("mcp:private-messages:read")+ call_tool("discourse_bookmark_list", { limit: 2 })++ expect(response.status).to eq(200)+ bookmarks = response.parsed_body.dig("result", "structuredContent", "bookmarks")+ expect(bookmarks.pluck("id")).to eq([private_post_bookmark.id, private_topic_bookmark.id])+ end+it "filters hidden private-message posts when category moderation is enabled" doSiteSetting.enable_category_group_moderation = trueauthorize("mcp:private-messages:read")