A platform for community discussion. Free, open, simple.
SECURITY: Duplicate lookup reveals restricted topic titles through canonicalized URLs
Arpit Jalane54ba27eacee
diff --git a/app/models/topic_link.rb b/app/models/topic_link.rbindex 228ccf0fe85..d895125d286 100644--- a/app/models/topic_link.rb+++ b/app/models/topic_link.rb@@ -145,6 +145,7 @@ class TopicLink < ActiveRecord::Basedef self.extract_from(post)return if post.blank? || post.whisper? || post.user_id.blank? || post.deleted_at.present?+ guardian = post.acting_user.guardiancurrent_urls = []reflected_ids = []@@ -158,7 +159,7 @@ class TopicLink < ActiveRecord::Base.uniq { |_, p| p }.each do |link, parsed|TopicLink.transaction do- url, reflected_id = ensure_entry_for(post, link, parsed)+ url, reflected_id = ensure_entry_for(post, link, parsed, guardian)current_urls << url unless url.nil?reflected_ids << reflected_id unless reflected_id.nil?rescue URI::Error@@ -182,7 +183,7 @@ class TopicLink < ActiveRecord::Basedef self.duplicate_lookup(topic, guardian = Guardian.new)results =TopicLink- .includes(:post, :user)+ .includes(:post, :user, :link_topic, link_post: :topic).joins(:post, :user).where("posts.id IS NOT NULL AND users.id IS NOT NULL").where(topic_id: topic.id, reflection: false)@@ -197,6 +198,14 @@ class TopicLink < ActiveRecord::Baselookup = {}results.each do |tl|+ if tl.internal? &&+ (+ (tl.link_topic_id && !guardian.can_see?(tl.link_topic)) ||+ (tl.link_post_id && !guardian.can_see?(tl.link_post))+ )+ next+ end+normalized = tl.url.downcase.sub(%r{\Ahttps?://}, "").sub(%r{/\z}, "")lookup[normalized] = {domain: tl.domain,@@ -325,7 +334,7 @@ class TopicLink < ActiveRecord::Basetopic_link_idend- def self.ensure_entry_for(post, link, parsed)+ def self.ensure_entry_for(post, link, parsed, guardian)url = link.urlinternal = falsetopic_id = nil@@ -357,11 +366,12 @@ class TopicLink < ActiveRecord::Basetopic = Topic.find_by(id: topic_id) if topic_idtopic ||= Topic.find_by(slug: topic_slug) if topic_slug.present?+ target_visible = topic.present? && guardian.can_see?(topic)- if topic.present?+ if target_visibleurl = +"#{Discourse.base_url_no_prefix}#{topic.relative_url}"url << "/#{post_number}" if post_number.to_i > 1- else+ elsif topic.blank?topic_id = nilendend@@ -396,7 +406,7 @@ class TopicLink < ActiveRecord::Basereflected_id = nil# Create the reflection if we can- if topic && post.topic && topic.archetype != "private_message" &&+ if target_visible && post.topic && topic.archetype != "private_message" &&post.topic.archetype != "private_message" && post.topic.visible?prefix = Discourse.base_url_no_prefixreflected_url = "#{prefix}#{post.topic.relative_url(post.post_number)}"diff --git a/spec/requests/composer_messages_controller_spec.rb b/spec/requests/composer_messages_controller_spec.rbindex a1d71a50d09..d28f0af72ee 100644--- a/spec/requests/composer_messages_controller_spec.rb+++ b/spec/requests/composer_messages_controller_spec.rb@@ -48,6 +48,53 @@ RSpec.describe ComposerMessagesController doexpect(duplicate_lookup).not_to have_key("example.com/private-doc")end+ it "does not include restricted internal links in duplicate_lookup" do+ user.change_trust_level!(TrustLevel[1])+ restricted_category = Fabricate(:category)+ restricted_category.set_permissions(staff: :full)+ restricted_category.save!+ restricted_topic =+ Fabricate(:topic, category: restricted_category, title: "Secured category secret title")+ Fabricate(:post, topic: restricted_topic, user: restricted_topic.user)+ private_message = Fabricate(:private_message_topic, title: "Private message secret title")+ Fabricate(:post, topic: private_message, user: private_message.user)+ visible_topic = Fabricate(:topic)+ Fabricate(:post, topic: visible_topic, user: visible_topic.user)+ hidden_target_post = Fabricate(:post, topic: visible_topic, user: visible_topic.user)+ hidden_target_post.hide!(PostActionType.types[:spam])++ restricted_url = "#{Discourse.base_url}/t/#{restricted_topic.id}"+ private_message_url = "#{Discourse.base_url}/t/#{private_message.id}"+ hidden_target_url = "#{Discourse.base_url}#{hidden_target_post.url}"+ reply =+ create_post(+ user: user,+ topic: topic,+ raw: "Check out #{restricted_url}, #{private_message_url}, and #{hidden_target_url}",+ )++ expect(TopicLink.where(post: reply).pluck(:url)).to contain_exactly(+ restricted_url,+ private_message_url,+ hidden_target_url,+ )+ expect(+ TopicLink.where(+ topic: [restricted_topic, private_message],+ link_post: reply,+ reflection: true,+ ),+ ).to be_empty++ get "/composer_messages.json", params: { topic_id: topic.id, composer_action: "reply" }+ expect(response.status).to eq(200)++ duplicate_lookup = response.parsed_body["extras"]["duplicate_lookup"]+ expect(duplicate_lookup.keys).to be_empty+ expect(response.body).not_to include(restricted_topic.slug)+ expect(response.body).not_to include(private_message.slug)+ end+it "does not include links from posts converted to whispers in duplicate_lookup" doSiteSetting.whispers_allowed_groups = Group::AUTO_GROUPS[:staff].to_sstaff_user = Fabricate(:admin)