Skip to content

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

FIX: topic_map exposes hidden-post URLs and titles through public topic details (#39815)

Sam14a1fb77e2f1
diff --git a/app/models/topic_link.rb b/app/models/topic_link.rb
index 30c9f6c7a7f..f5481d2fcf1 100644
--- a/app/models/topic_link.rb
+++ b/app/models/topic_link.rb
@@ -46,6 +46,7 @@ class TopicLink < ActiveRecord::Base
MIN(ftl.user_id) AS user_id,
SUM(clicks) AS clicks
FROM topic_links AS ftl
+ JOIN posts AS source_posts ON ftl.post_id = source_posts.id
LEFT JOIN topics AS ft ON ftl.link_topic_id = ft.id
LEFT JOIN categories AS c ON c.id = ft.category_id
LEFT JOIN posts AS target_posts ON ftl.link_post_id = target_posts.id
@@ -56,6 +57,7 @@ class TopicLink < ActiveRecord::Base
SQL
builder.where("ftl.topic_id = :topic_id", topic_id: topic_id)
+ apply_source_post_visibility_filters(builder, guardian, source_post: "source_posts")
apply_link_visibility_filters(
builder,
link: "ftl",
@@ -209,6 +211,16 @@ class TopicLink < ActiveRecord::Base
end
private_class_method :apply_link_visibility_filters
+ def self.apply_source_post_visibility_filters(builder, guardian, source_post:)
+ builder.where("#{source_post}.deleted_at IS NULL")
+ builder.where(
+ "#{source_post}.post_type IN (:visible_post_types)",
+ visible_post_types: Topic.visible_post_types(guardian.user),
+ )
+ builder.where("#{source_post}.hidden = false") unless guardian.is_staff?
+ end
+ private_class_method :apply_source_post_visibility_filters
+
private
# This pattern is used to create topic links very efficiently with minimal
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 4c2ea1283bf..cb3d72cf63f 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -2908,6 +2908,47 @@ RSpec.describe TopicsController do
expect(response.parsed_body).not_to have_key("tags_descriptions")
end
+ it "does not expose links from hidden posts in topic details to non-staff viewers" do
+ test_topic = Fabricate(:topic, user: post_author1)
+ visible_post = Fabricate(:post, topic: test_topic, user: post_author1)
+ hidden_post = Fabricate(:post, topic: test_topic, user: post_author1)
+
+ Fabricate(
+ :topic_link,
+ post: visible_post,
+ url: "https://visible-link.example.com",
+ domain: "visible-link.example.com",
+ clicks: 1,
+ title: "Visible title",
+ )
+ Fabricate(
+ :topic_link,
+ post: hidden_post,
+ url: "https://hidden-link.example.com",
+ domain: "hidden-link.example.com",
+ clicks: 1,
+ title: "Hidden title",
+ )
+
+ hidden_post.hide!(PostActionType.types[:off_topic])
+
+ get "/t/#{test_topic.slug}/#{test_topic.id}.json"
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["details"]["links"]).to contain_exactly(
+ a_hash_including("url" => "https://visible-link.example.com", "title" => "Visible title"),
+ )
+
+ sign_in(moderator)
+ get "/t/#{test_topic.slug}/#{test_topic.id}.json"
+
+ expect(response.status).to eq(200)
+ expect(response.parsed_body["details"]["links"]).to contain_exactly(
+ a_hash_including("url" => "https://visible-link.example.com", "title" => "Visible title"),
+ a_hash_including("url" => "https://hidden-link.example.com", "title" => "Hidden title"),
+ )
+ end
+
it "shows a blank-slug topic without redirecting" do
topic.update_columns(title: "", slug: nil)
topic.reload