A platform for community discussion. Free, open, simple.
FIX: Prevent the site itself from being quoted as an external referrer (#40898)
Natalie Tay277e0bb81aec
diff --git a/plugins/discourse-ai/lib/admin_dashboard/admin_dashboard_facts.rb b/plugins/discourse-ai/lib/admin_dashboard/admin_dashboard_facts.rbindex d34d220bfc5..af746b97adc 100644--- a/plugins/discourse-ai/lib/admin_dashboard/admin_dashboard_facts.rb+++ b/plugins/discourse-ai/lib/admin_dashboard/admin_dashboard_facts.rb@@ -221,7 +221,7 @@ module DiscourseAisource = dominant_referrer_for(peak.date)headline =if source- "Traffic spiked on #{peak.date} (#{multiple}x the typical day), driven by #{source}"+ "Traffic spiked on #{peak.date} (#{multiple}x the typical day), with #{source} as the top external referrer"else"Traffic spiked on #{peak.date} (#{multiple}x the typical day)"end@@ -231,7 +231,7 @@ module DiscourseAidef dominant_referrer_for(date)rows = DB.query(<<~SQL, date: date)- SELECT normalized_referrer AS referrer, count+ SELECT normalized_referrer, countFROM browser_pageview_referrer_daily_rollupsWHERE date = :date AND normalized_referrer IS NOT NULLORDER BY count DESC@@ -240,9 +240,19 @@ module DiscourseAireturn if rows.blank?total = rows.sum(&:count)- top = rows.first+ top = rows.find { |row| external_referrer?(row.normalized_referrer) }+ return if top.nil?return if total.zero? || (top.count.to_f / total) < 0.4- top.referrer+ top.normalized_referrer+ end++ def external_referrer?(referrer)+ site_host = BrowserPageviewReferrerInspector.normalize_host(Discourse.current_hostname)+ referrer = referrer.to_s+ return false if referrer.blank? || site_host.blank?++ referrer != site_host && !referrer.start_with?("#{site_host}/") &&+ !referrer.start_with?("#{site_host}?")enddef geographydiff --git a/plugins/discourse-ai/spec/lib/admin_dashboard/admin_dashboard_facts_spec.rb b/plugins/discourse-ai/spec/lib/admin_dashboard/admin_dashboard_facts_spec.rbindex 8d998fcc75a..3402cb0479d 100644--- a/plugins/discourse-ai/spec/lib/admin_dashboard/admin_dashboard_facts_spec.rb+++ b/plugins/discourse-ai/spec/lib/admin_dashboard/admin_dashboard_facts_spec.rb@@ -75,4 +75,52 @@ RSpec.describe DiscourseAi::AdminDashboard::AdminDashboardFacts doexpect(spike[:headline]).to include("news.ycombinator.com")end++ it "does not report the site's own hostname as the traffic spike source" do+ allow(Discourse).to receive(:current_hostname).and_return("meta.discourse.org")+ base = 29.days.ago.to_date+ spike_day = 20.days.ago.to_date+ base.upto(Date.current) do |date|+ ApplicationRequest.create!(+ date: date,+ req_type: ApplicationRequest.req_types[:page_view_logged_in_browser],+ count: date == spike_day ? 5000 : 100,+ )+ end+ BrowserPageviewReferrerDailyRollup.create!(+ date: spike_day,+ normalized_referrer: "meta.discourse.org",+ count: 4000,+ logged_in_count: 0,+ )++ spike = compute.fetch(:signals).find { |s| s[:key] == :traffic_spike }++ expect(spike[:headline]).not_to include("meta.discourse.org")+ expect(spike[:headline]).not_to include("external referrer")+ end++ it "does not report a same-site subfolder referrer as the traffic spike source" do+ allow(Discourse).to receive(:current_hostname).and_return("meta.discourse.org")+ base = 29.days.ago.to_date+ spike_day = 20.days.ago.to_date+ base.upto(Date.current) do |date|+ ApplicationRequest.create!(+ date: date,+ req_type: ApplicationRequest.req_types[:page_view_logged_in_browser],+ count: date == spike_day ? 5000 : 100,+ )+ end+ BrowserPageviewReferrerDailyRollup.create!(+ date: spike_day,+ normalized_referrer: "meta.discourse.org/forum/latest",+ count: 4000,+ logged_in_count: 0,+ )++ spike = compute.fetch(:signals).find { |s| s[:key] == :traffic_spike }++ expect(spike[:headline]).not_to include("meta.discourse.org")+ expect(spike[:headline]).not_to include("external referrer")+ endend