A platform for community discussion. Free, open, simple.
SECURITY: Enforce first-post visibility for topic bookmarks (#42321)
Chris Albertibd15d025b2af
diff --git a/app/services/topic_bookmarkable.rb b/app/services/topic_bookmarkable.rbindex 8ac89a77cf7..c99a995e884 100644--- a/app/services/topic_bookmarkable.rb+++ b/app/services/topic_bookmarkable.rb@@ -25,22 +25,22 @@ class TopicBookmarkable < BaseBookmarkabledef self.list_query(user, guardian)topics = Topic.listable_topics.secured(guardian)pms = Topic.private_messages_for_user(user)+ first_posts = guardian.filter_hidden_posts(Post.secured(guardian).where(deleted_at: nil))topic_bookmarks =user.bookmarks_of_type("Topic").joins("INNER JOIN topics ON topics.id = bookmarks.bookmarkable_id AND bookmarks.bookmarkable_type = 'Topic'",)+ .joins("INNER JOIN posts ON posts.topic_id = topics.id AND posts.post_number = 1").joins("LEFT JOIN topic_users ON topic_users.topic_id = topics.id").where("topic_users.user_id = ?", user.id)- guardian.filter_allowed_categories(topic_bookmarks.merge(topics.or(pms)))+ guardian.filter_allowed_categories(topic_bookmarks.merge(topics.or(pms)).merge(first_posts))enddef self.search_query(bookmarks, query, ts_query, &bookmarkable_search)bookmarkable_search.call(- bookmarks.joins(- "LEFT JOIN posts ON posts.topic_id = topics.id AND posts.post_number = 1",- ).joins("LEFT JOIN post_search_data ON post_search_data.post_id = posts.id"),+ bookmarks.joins("LEFT JOIN post_search_data ON post_search_data.post_id = posts.id"),"#{ts_query} @@ post_search_data.search_data",)end@@ -66,7 +66,7 @@ class TopicBookmarkable < BaseBookmarkableenddef self.can_see_bookmarkable?(guardian, bookmarkable)- guardian.can_see_topic?(bookmarkable)+ guardian.can_see_post?(bookmarkable&.first_post)enddef self.bookmark_metadata(bookmark, user)@@ -74,7 +74,7 @@ class TopicBookmarkable < BaseBookmarkableenddef self.validate_before_create(guardian, bookmarkable)- raise Discourse::InvalidAccess if bookmarkable.blank? || !guardian.can_see_topic?(bookmarkable)+ raise Discourse::InvalidAccess if !can_see_bookmarkable?(guardian, bookmarkable)enddef self.after_create(guardian, bookmark, opts)diff --git a/spec/lib/bookmark_query_spec.rb b/spec/lib/bookmark_query_spec.rbindex 7b05e087a13..22e0a2a2e3d 100644--- a/spec/lib/bookmark_query_spec.rb+++ b/spec/lib/bookmark_query_spec.rb@@ -9,7 +9,7 @@ RSpec.describe BookmarkQuery dodescribe "#count_all" dofab!(:post_bookmark) { Fabricate(:bookmark, user:, bookmarkable: Fabricate(:post)) }- fab!(:topic_bookmark) { Fabricate(:bookmark, user:, bookmarkable: Fabricate(:topic)) }+ fab!(:topic_bookmark) { Fabricate(:bookmark, user:, bookmarkable: Fabricate(:topic_with_op)) }before doFabricate(:topic_user, user:, topic: post_bookmark.bookmarkable.topic)@@ -44,7 +44,7 @@ RSpec.describe BookmarkQuery dodescribe "#list_all" dofab!(:post_bookmark) { Fabricate(:bookmark, user:, bookmarkable: Fabricate(:post)) }- fab!(:topic_bookmark) { Fabricate(:bookmark, user:, bookmarkable: Fabricate(:topic)) }+ fab!(:topic_bookmark) { Fabricate(:bookmark, user:, bookmarkable: Fabricate(:topic_with_op)) }let(:user_bookmark) doFabricate(:bookmark, user:, bookmarkable: Fabricate(:user, username: "bookmarkqueen"))diff --git a/spec/lib/topics_filter_spec.rb b/spec/lib/topics_filter_spec.rbindex 549b385c281..11fef873a62 100644--- a/spec/lib/topics_filter_spec.rb+++ b/spec/lib/topics_filter_spec.rb@@ -363,10 +363,10 @@ RSpec.describe TopicsFilter doenddescribe "when filtering with the `in` filter" do- fab!(:topic)+ fab!(:topic, :topic_with_op)fab!(:pinned_topic) do- Fabricate(:topic, pinned_at: Time.zone.now, pinned_until: 1.hour.from_now)+ Fabricate(:topic_with_op, pinned_at: Time.zone.now, pinned_until: 1.hour.from_now)endfab!(:expired_pinned_topic) dodiff --git a/spec/models/user_bookmark_list_spec.rb b/spec/models/user_bookmark_list_spec.rbindex 05bf6d5c97e..237f48aacfa 100644--- a/spec/models/user_bookmark_list_spec.rb+++ b/spec/models/user_bookmark_list_spec.rb@@ -15,7 +15,7 @@ RSpec.describe UserBookmarkList doafter { DiscoursePluginRegistry.reset! }let(:post_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:post)) }- let(:topic_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:topic)) }+ let(:topic_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:topic_with_op)) }let(:user_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:user)) }it "returns all types of bookmarks" dodiff --git a/spec/models/user_summary_spec.rb b/spec/models/user_summary_spec.rbindex 7925ae6f9e0..901d93178f5 100644--- a/spec/models/user_summary_spec.rb+++ b/spec/models/user_summary_spec.rb@@ -94,7 +94,7 @@ RSpec.describe UserSummary dodescribe "#bookmark_count" dofab!(:user)fab!(:post_bookmark) { Fabricate(:bookmark, user:, bookmarkable: Fabricate(:post)) }- fab!(:topic_bookmark) { Fabricate(:bookmark, user:, bookmarkable: Fabricate(:topic)) }+ fab!(:topic_bookmark) { Fabricate(:bookmark, user:, bookmarkable: Fabricate(:topic_with_op)) }let(:summary) { UserSummary.new(user, Guardian.new(user)) }diff --git a/spec/requests/bookmarks_controller_spec.rb b/spec/requests/bookmarks_controller_spec.rbindex 20dd6a09093..035214fc9de 100644--- a/spec/requests/bookmarks_controller_spec.rb+++ b/spec/requests/bookmarks_controller_spec.rb@@ -5,7 +5,7 @@ RSpec.describe BookmarksController dolet(:user_2) { Fabricate(:user) }let(:bookmark_post) { Fabricate(:post) }let(:bookmark_post_2) { Fabricate(:post) }- let(:bookmark_topic) { Fabricate(:topic) }+ let(:bookmark_topic) { Fabricate(:topic_with_op) }let(:bookmark_user) { current_user }describe "#create" do@@ -92,6 +92,20 @@ RSpec.describe BookmarksController do)endend++ it "returns a 403 when the first post of a topic is hidden" do+ hidden_topic = Fabricate(:topic_with_op)+ hidden_topic.first_post.update!(hidden: true)++ post "/bookmarks.json",+ params: {+ bookmarkable_id: hidden_topic.id,+ bookmarkable_type: "Topic",+ }++ expect(response.status).to eq(403)+ expect(Bookmark.find_by(user: current_user, bookmarkable: hidden_topic)).to be_nil+ endenddescribe "#destroy" dodiff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rbindex 3a466f0c652..3c5ab534994 100644--- a/spec/requests/users_controller_spec.rb+++ b/spec/requests/users_controller_spec.rb@@ -7909,14 +7909,14 @@ RSpec.describe UsersController dodescribe "#bookmarks excerpts" dofab!(:user)let!(:topic) { Fabricate(:topic, user: user) }- let!(:post) { Fabricate(:post, topic: topic) }+ let!(:topic_post) { Fabricate(:post, topic: topic) }let!(:bookmark) { Fabricate(:bookmark, name: "Test", user: user, bookmarkable: topic) }it "uses the first post of the topic for the bookmarks excerpt" doTopicUser.change(user.id,bookmark.bookmarkable.id,- { last_read_post_number: post.post_number },+ { last_read_post_number: topic_post.post_number },)sign_in(user)@@ -7928,19 +7928,13 @@ RSpec.describe UsersController doexpect(bookmark_list.first["excerpt"]).to eq(expected_excerpt)end- it "does not expose excerpts for hidden bookmarked posts the user cannot see" do+ it "does not expose excerpts for hidden post bookmarks the user cannot see" dohidden_post_raw = "hidden post bookmark secret " * 20- hidden_topic_raw = "hidden topic bookmark secret " * 20hidden_post = Fabricate(:post, raw: hidden_post_raw)- hidden_topic = Fabricate(:topic)- hidden_first_post = Fabricate(:post, topic: hidden_topic, raw: hidden_topic_raw)hidden_post_bookmark = Fabricate(:bookmark, user: user, bookmarkable: hidden_post)- hidden_topic_bookmark = Fabricate(:bookmark, user: user, bookmarkable: hidden_topic)TopicUser.change(user.id, hidden_post.topic_id, total_msecs_viewed: 1)- TopicUser.change(user.id, hidden_topic.id, total_msecs_viewed: 1)hidden_post.update!(hidden: true)- hidden_first_post.update!(hidden: true)sign_in(user)@@ -7948,18 +7942,44 @@ RSpec.describe UsersController doexpect(response.status).to eq(200)bookmark_list = response.parsed_body["user_bookmark_list"]["bookmarks"]hidden_post_response = bookmark_list.find { |item| item["id"] == hidden_post_bookmark.id }- hidden_topic_response = bookmark_list.find { |item| item["id"] == hidden_topic_bookmark.id }expect(hidden_post_response).to be_presentexpect(hidden_post_response).not_to have_key("excerpt")expect(hidden_post_response).not_to have_key("truncated")expect(hidden_post_response).not_to have_key("cooked")- expect(hidden_topic_response).to be_present- expect(hidden_topic_response).not_to have_key("excerpt")- expect(hidden_topic_response).not_to have_key("truncated")- expect(hidden_topic_response).not_to have_key("cooked")- expect(response.body).not_to include("hidden post bookmark secret")- expect(response.body).not_to include("hidden topic bookmark secret")+ expect(response.body).not_to include(hidden_post.raw)+ end++ it "prevents hidden first posts from being listed or searched" do+ SearchIndexer.enable+ hidden_topic =+ Fabricate(:topic, title: "Hidden topic bookmark secret title", user: Fabricate(:user))+ hidden_first_post =+ Fabricate(+ :post,+ topic: hidden_topic,+ user: hidden_topic.user,+ raw: "hidden topic bookmark secret body",+ )+ hidden_topic_bookmark = Fabricate(:bookmark, user: user, bookmarkable: hidden_topic)++ sign_in(user)+ hidden_first_post.update!(hidden: true)++ get "/u/#{user.username}/bookmarks.json"+ expect(response.status).to eq(200)+ bookmark_list = response.parsed_body.dig("user_bookmark_list", "bookmarks") || []+ expect(bookmark_list.map { |bookmark_response| bookmark_response["id"] }).not_to include(+ hidden_topic_bookmark.id,+ )+ expect(response.body).not_to include(hidden_topic.title)++ get "/u/#{user.username}/bookmarks.json", params: { q: "hidden topic bookmark secret body" }+ expect(response.status).to eq(200)+ bookmark_list = response.parsed_body.dig("user_bookmark_list", "bookmarks") || []+ expect(bookmark_list.map { |bookmark_response| bookmark_response["id"] }).not_to include(+ hidden_topic_bookmark.id,+ )enddescribe "bookmarkable_url" do@@ -7968,7 +7988,7 @@ RSpec.describe UsersController doTopicUser.change(user.id,bookmark.bookmarkable.id,- { last_read_post_number: post.post_number },+ { last_read_post_number: topic_post.post_number },)sign_in(user)@@ -7978,7 +7998,7 @@ RSpec.describe UsersController dobookmark_list = response.parsed_body["bookmarks"]expect(bookmark_list.first["bookmarkable_url"]).to end_with(- "/t/#{topic.slug}/#{topic.id}/#{post.post_number + 1}",+ "/t/#{topic.slug}/#{topic.id}/#{topic_post.post_number + 1}",)end@@ -7986,7 +8006,7 @@ RSpec.describe UsersController doTopicUser.change(user.id,bookmark.bookmarkable.id,- { last_read_post_number: post.post_number },+ { last_read_post_number: topic_post.post_number },)sign_in(user)diff --git a/spec/serializers/user_bookmark_list_serializer_spec.rb b/spec/serializers/user_bookmark_list_serializer_spec.rbindex ed38233dcb4..0544f85062e 100644--- a/spec/serializers/user_bookmark_list_serializer_spec.rb+++ b/spec/serializers/user_bookmark_list_serializer_spec.rb@@ -14,7 +14,9 @@ RSpec.describe UserBookmarkListSerializer doafter { DiscoursePluginRegistry.reset_register!(:bookmarkables) }let(:post_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:post)) }- let(:topic_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:topic)) }+ let(:topic_bookmark) do+ Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:topic_with_op))+ endlet(:user_bookmark) { Fabricate(:bookmark, user: user, bookmarkable: Fabricate(:user)) }def run_serializerdiff --git a/spec/services/topic_bookmarkable_spec.rb b/spec/services/topic_bookmarkable_spec.rbindex 6ee7fdadfe8..7acd20536f4 100644--- a/spec/services/topic_bookmarkable_spec.rb+++ b/spec/services/topic_bookmarkable_spec.rb@@ -11,6 +11,7 @@ RSpec.describe TopicBookmarkable dolet!(:topic1) { Fabricate(:topic) }let!(:topic2) { Fabricate(:topic) }let!(:post) { Fabricate(:post, topic: topic1) }+ let!(:post2) { Fabricate(:post, topic: topic2) }let!(:bookmark1) doFabricate(:bookmark, user: user, bookmarkable: topic1, name: "something i gotta do")end@@ -96,6 +97,12 @@ RSpec.describe TopicBookmarkable doexpect(registered_bookmarkable.can_send_reminder?(bookmark1)).to eq(false)end+ it "cannot send a reminder if the first post is hidden" do+ expect(registered_bookmarkable.can_send_reminder?(bookmark1)).to eq(true)+ bookmark1.bookmarkable.first_post.update!(hidden: true)+ expect(registered_bookmarkable.can_send_reminder?(bookmark1)).to eq(false)+ end+it "cannot send reminder if the user cannot access the topic" doexpect(registered_bookmarkable.can_send_reminder?(bookmark1)).to eq(true)bookmark1.bookmarkable.update!(category: private_category)