A platform for community discussion. Free, open, simple.
SECURITY: Prevent stale Rewind reports from exposing restricted content (#43656)
Isaac Janzen514b49bb5a0b
diff --git a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rb b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rbindex de47a106eeb..6cc55b34e31 100644--- a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rb+++ b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_report.rb@@ -42,7 +42,7 @@ module DiscourseRewindTime.zone.local(year).all_yearend- def fetch_report(params:, for_user:, year:, date:)+ def fetch_report(params:, for_user:, year:, date:, guardian:)report_class = FetchReports::REPORTS[params.index]return if !report_class@@ -52,7 +52,60 @@ module DiscourseRewindreport = report_class.call(date:, user: for_user)cache_single_report(for_user.id, year, report_name, report.as_json)end++ report = filter_report_for_viewer(report, guardian, report_class) if report_class.in?(+ FetchReports::VISIBILITY_FILTERED_REPORTS,+ )reportend++ def filter_report_for_viewer(report, guardian, report_class)+ case report_class.name+ when Action::BestTopics.name+ filter_best_topics_for_viewer(report, guardian)+ when Action::BestPosts.name+ filter_best_posts_for_viewer(report, guardian)+ end+ end++ def filter_best_topics_for_viewer(report, guardian)+ topic_ids = report[:data].pluck(:topic_id)+ visible_topic_ids = guardian.can_see_topic_ids(topic_ids:)+ eligible_topic_ids =+ Topic+ .visible+ .where(id: visible_topic_ids, deleted_at: nil)+ .where.not(archetype: Archetype.private_message)+ .joins(:category)+ .where("NOT categories.read_restricted")+ .pluck(:id)++ report.merge(data: report[:data].select { |topic| topic[:topic_id].in?(eligible_topic_ids) })+ end++ def filter_best_posts_for_viewer(report, guardian)+ topic_ids = report[:data].pluck(:topic_id)+ visible_topic_ids = guardian.can_see_topic_ids(topic_ids:)+ eligible_post_keys =+ Post+ .public_posts+ .visible+ .joins(topic: :category)+ .where(+ topic_id: visible_topic_ids,+ post_number: report[:data].pluck(:post_number),+ deleted_at: nil,+ )+ .where("NOT categories.read_restricted")+ .where.not(post_type: Post.types[:whisper])+ .pluck(:topic_id, :post_number)++ report.merge(+ data:+ report[:data].select do |post|+ [post[:topic_id], post[:post_number]].in?(eligible_post_keys)+ end,+ )+ endendenddiff --git a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rbindex 204d91baf80..5344d751283 100644--- a/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb+++ b/plugins/discourse-rewind/app/services/discourse_rewind/fetch_reports.rb@@ -20,6 +20,7 @@ module DiscourseRewind# @return [Service::Base::Context]INITIAL_REPORT_COUNT = 3+ VISIBILITY_FILTERED_REPORTS = [Action::BestTopics, Action::BestPosts].freeze# The order here controls the order of reports in the UI,# so be careful when moving these around.diff --git a/plugins/discourse-rewind/spec/requests/rewinds_controller_spec.rb b/plugins/discourse-rewind/spec/requests/rewinds_controller_spec.rbindex 9f735442432..e67ac023666 100644--- a/plugins/discourse-rewind/spec/requests/rewinds_controller_spec.rb+++ b/plugins/discourse-rewind/spec/requests/rewinds_controller_spec.rb@@ -101,6 +101,46 @@ RSpec.describe DiscourseRewind::RewindsController docontext "when in valid month" dobefore { freeze_time DateTime.parse("2022-12-24") }+ it "does not return cached best post excerpts after their category becomes read-restricted" do+ report_user = Fabricate(:user)+ viewer = Fabricate(:user)+ category = Fabricate(:category)+ topic = Fabricate(:topic, category:, user: report_user)+ post =+ Fabricate(+ :post,+ topic:,+ user: report_user,+ post_number: 2,+ created_at: Time.zone.parse("2022-06-01"),+ raw: "classified rewind cache content",+ like_count: 1,+ )+ report_user.user_option.update!(discourse_rewind_share_publicly: true)++ sign_in(report_user)+ get "/rewinds/#{DiscourseRewind::FetchReports::REPORTS.index(DiscourseRewind::Action::BestPosts)}.json"++ expect(response.status).to eq(200)+ expect(response.body).to include(post.raw)++ category.update!(read_restricted: true)++ sign_in(viewer)+ get "/t/#{topic.id}.json"++ expect(response.status).to eq(404)+ expect(response.body).not_to include(post.raw)++ get "/rewinds/#{DiscourseRewind::FetchReports::REPORTS.index(DiscourseRewind::Action::BestPosts)}.json",+ params: {+ for_user_username: report_user.username,+ }++ expect(response.status).to eq(200)+ expect(response.body).not_to include(post.raw)+ end+context "when reports are cached" dobefore { get "/rewinds.json" }@@ -122,6 +162,164 @@ RSpec.describe DiscourseRewind::RewindsController do)endend++ context "when cached topic-backed reports become ineligible" do+ fab!(:rewind_owner, :user)+ fab!(:viewer, :user)+ fab!(:restricted_category) { Fabricate(:category, read_restricted: true) }+ fab!(:unlisted_topic) do+ Fabricate(+ :topic,+ user: rewind_owner,+ created_at: DateTime.parse("2022-01-01"),+ title: "Cached unlisted topic title",+ excerpt: "Cached unlisted topic excerpt",+ )+ end+ fab!(:restricted_topic) do+ Fabricate(+ :topic,+ user: rewind_owner,+ created_at: DateTime.parse("2022-01-02"),+ title: "Cached restricted topic title",+ )+ end+ fab!(:eligible_topic) do+ Fabricate(+ :topic,+ user: rewind_owner,+ created_at: DateTime.parse("2022-01-03"),+ title: "Cached eligible topic title",+ )+ end+ fab!(:hidden_reply) do+ Fabricate(+ :post,+ topic: eligible_topic,+ user: rewind_owner,+ post_number: 2,+ created_at: DateTime.parse("2022-01-04"),+ raw: "Cached hidden post excerpt",+ like_count: 100,+ )+ end+ fab!(:deleted_reply) do+ Fabricate(+ :post,+ topic: eligible_topic,+ user: rewind_owner,+ post_number: 3,+ created_at: DateTime.parse("2022-01-05"),+ raw: "Cached deleted post excerpt",+ like_count: 99,+ )+ end+ fab!(:eligible_reply) do+ Fabricate(+ :post,+ topic: eligible_topic,+ user: rewind_owner,+ post_number: 4,+ created_at: DateTime.parse("2022-01-06"),+ raw: "Cached eligible post excerpt",+ like_count: 98,+ )+ end++ before do+ rewind_owner.user_option.update!(discourse_rewind_share_publicly: true)+ TopTopic.refresh!+ TopTopic.find_by!(topic_id: unlisted_topic.id).update!(yearly_score: 100)+ TopTopic.find_by!(topic_id: restricted_topic.id).update!(yearly_score: 99)+ TopTopic.find_by!(topic_id: eligible_topic.id).update!(yearly_score: 98)+ sign_in(rewind_owner)+ get "/rewinds/7.json"+ get "/rewinds/8.json"+ end++ it "omits an unlisted topic from a cached shared Best Topics report" do+ cached_report =+ DiscourseRewind::FetchReportsHelper.load_single_report_from_cache(+ rewind_owner.id,+ 2022,+ "BestTopics",+ )+ cached_topic = cached_report[:data].find { |topic| topic[:topic_id] == unlisted_topic.id }+ expect(cached_topic).to be_present++ unlisted_topic.update!(visible: false)+ sign_in(viewer)++ get "/rewinds/7.json", params: { for_user_username: rewind_owner.username }++ expect(response.status).to eq(200)+ expect(+ response.parsed_body.dig("report", "data").map { |topic| topic["topic_id"] },+ ).to contain_exactly(restricted_topic.id, eligible_topic.id)+ expect(response.body).not_to include(cached_topic[:title])+ expect(response.body).not_to include(cached_topic[:excerpt])+ expect(+ DiscourseRewind::FetchReportsHelper.load_single_report_from_cache(+ rewind_owner.id,+ 2022,+ "BestTopics",+ ),+ ).to include(data: include(cached_topic))+ end++ it "omits a deleted topic while retaining eligible cached Best Topics entries" do+ restricted_topic.trash!(Discourse.system_user)+ sign_in(viewer)++ get "/rewinds/7.json", params: { for_user_username: rewind_owner.username }++ expect(response.status).to eq(200)+ expect(+ response.parsed_body.dig("report", "data").map { |topic| topic["topic_id"] },+ ).to contain_exactly(unlisted_topic.id, eligible_topic.id)+ end++ it "omits a topic after the viewer loses category access while retaining eligible cached Best Topics entries" do+ restricted_topic.change_category_to_id(restricted_category.id)+ expect(viewer.guardian.can_see?(restricted_topic)).to eq(false)+ sign_in(viewer)++ get "/rewinds/7.json", params: { for_user_username: rewind_owner.username }++ expect(response.status).to eq(200)+ expect(+ response.parsed_body.dig("report", "data").map { |topic| topic["topic_id"] },+ ).to contain_exactly(unlisted_topic.id, eligible_topic.id)+ end++ it "omits hidden and deleted posts from a cached shared Best Posts report" do+ cached_report =+ DiscourseRewind::FetchReportsHelper.load_single_report_from_cache(+ rewind_owner.id,+ 2022,+ "BestPosts",+ )+ cached_hidden_post =+ cached_report[:data].find { |post| post[:post_number] == hidden_reply.post_number }+ cached_deleted_post =+ cached_report[:data].find { |post| post[:post_number] == deleted_reply.post_number }+ expect(cached_hidden_post).to be_present+ expect(cached_deleted_post).to be_present++ hidden_reply.update!(hidden: true)+ deleted_reply.trash!(Discourse.system_user)+ sign_in(viewer)++ get "/rewinds/8.json", params: { for_user_username: rewind_owner.username }++ expect(response.status).to eq(200)+ expect(+ response.parsed_body.dig("report", "data").map { |post| post["post_number"] },+ ).to contain_exactly(eligible_reply.post_number)+ expect(response.body).not_to include(cached_hidden_post[:excerpt])+ expect(response.body).not_to include(cached_deleted_post[:excerpt])+ end+ endendenddiff --git a/plugins/discourse-rewind/spec/services/fetch_report_spec.rb b/plugins/discourse-rewind/spec/services/fetch_report_spec.rbindex 7415000e42a..89d7e4ac319 100644--- a/plugins/discourse-rewind/spec/services/fetch_report_spec.rb+++ b/plugins/discourse-rewind/spec/services/fetch_report_spec.rb@@ -129,6 +129,43 @@ RSpec.describe(DiscourseRewind::FetchReport) doexpect(DiscourseRewind::Action::Reactions).to_not have_received(:call)endend++ context "when a cached best topics report contains content from a now-restricted topic" do+ let(:params) { { index: 7 } }++ before do+ DiscourseRewind::FetchReportsHelper.cache_single_report(+ current_user.id,+ 2021,+ "BestTopics",+ {+ data: [{ topic_id: 1, title: "restricted topic", excerpt: "restricted content" }],+ identifier: "best-topics",+ },+ )+ end++ it "filters the cached report without regenerating it" do+ allow(DiscourseRewind::Action::BestTopics).to receive(:call)++ expect(result).to be_success+ expect(result.report[:data]).to eq([])+ expect(DiscourseRewind::Action::BestTopics).to_not have_received(:call)+ expect(+ DiscourseRewind::FetchReportsHelper.load_single_report_from_cache(+ current_user.id,+ 2021,+ "BestTopics",+ )[+ :data+ ],+ ).to contain_exactly(+ topic_id: 1,+ title: "restricted topic",+ excerpt: "restricted content",+ )+ end+ endendcontext "when a report is not cached" do