Skip to content

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

FIX: Filter unlinked topic visibility in topic-link reflect queries (#42195)

Isaac Janzenb2cdadd34022
diff --git a/app/models/topic_link.rb b/app/models/topic_link.rb
index d895125d286..038b0445710 100644
--- a/app/models/topic_link.rb
+++ b/app/models/topic_link.rb
@@ -228,6 +228,7 @@ class TopicLink < ActiveRecord::Base
def self.apply_link_visibility_filters(builder, link:, target_topic:, target_posts:)
builder.where(<<~SQL)
#{target_topic}.deleted_at IS NULL
+ AND (#{target_topic}.id IS NULL OR #{target_topic}.visible = true)
AND (#{link}.internal = false OR #{target_topic}.id IS NOT NULL)
AND (#{link}.link_post_id IS NULL OR (#{target_posts}.id IS NOT NULL AND #{target_posts}.deleted_at IS NULL))
SQL
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 36b16f99e01..7cb813d4a80 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -3239,6 +3239,49 @@ RSpec.describe TopicsController do
)
end
+ it "does not expose links from unlisted topics to anonymous viewers" do
+ public_post = Fabricate(:post, user: post_author1)
+ public_topic = public_post.topic
+ public_source_topic = Fabricate(:topic, user: post_author1, title: "Public source topic")
+ public_source_post =
+ Fabricate(
+ :post,
+ topic: public_source_topic,
+ user: post_author1,
+ raw:
+ "#{Discourse.base_url_no_prefix}#{public_topic.relative_url(public_post.post_number)}",
+ )
+ unlisted_source_topic = Fabricate(:topic, user: post_author1, title: "Unlisted source topic")
+ unlisted_source_post =
+ Fabricate(
+ :post,
+ topic: unlisted_source_topic,
+ user: post_author1,
+ raw:
+ "#{Discourse.base_url_no_prefix}#{public_topic.relative_url(public_post.post_number)}",
+ )
+
+ TopicLink.extract_from(public_source_post)
+ TopicLink.extract_from(unlisted_source_post)
+ unlisted_source_topic.update_column(:visible, false)
+
+ get "/t/#{public_topic.slug}/#{public_topic.id}.json"
+
+ expect(response).to have_http_status(:ok)
+ serialized_public_post =
+ response
+ .parsed_body
+ .dig("post_stream", "posts")
+ .find { |post| post["id"] == public_post.id }
+ expect(serialized_public_post["link_counts"]).to contain_exactly(
+ a_hash_including(
+ "url" =>
+ "#{Discourse.base_url_no_prefix}#{public_source_topic.relative_url(public_source_post.post_number)}",
+ "title" => public_source_topic.title,
+ ),
+ )
+ end
+
it "does not expose hidden post link counts", :aggregate_failures do
first_post = Fabricate(:post, user: post_author1)
test_topic = first_post.topic