A platform for community discussion. Free, open, simple.
FEATURE: Send a 'noindex' header in non-canonical responses (#15026)
Rafael dos Santos Silva5647819de40b
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex 0561be4c82e..8d1494136ea 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -41,13 +41,14 @@ class ApplicationController < ActionController::Basebefore_action :redirect_to_login_if_requiredbefore_action :block_if_requires_loginbefore_action :preload_json- before_action :add_noindex_header, if: -> { is_feed_request? || !SiteSetting.allow_index_in_robots_txt }before_action :check_xhrafter_action :add_readonly_headerafter_action :perform_refresh_sessionafter_action :dont_cache_pageafter_action :conditionally_allow_site_embeddingafter_action :ensure_vary_header+ after_action :add_noindex_header, if: -> { is_feed_request? || !SiteSetting.allow_index_in_robots_txt }+ after_action :add_noindex_header_to_non_canonical, if: -> { request.get? && !(request.format && request.format.json?) && !request.xhr? }HONEYPOT_KEY ||= 'HONEYPOT_KEY'CHALLENGE_KEY ||= 'CHALLENGE_KEY'@@ -912,6 +913,13 @@ class ApplicationController < ActionController::Baseendend+ def add_noindex_header_to_non_canonical+ canonical = (@canonical_url || @default_canonical)+ if canonical.present? && canonical != request.url && !SiteSetting.allow_indexing_non_canonical_urls+ response.headers['X-Robots-Tag'] ||= 'noindex'+ end+ end+protecteddef honeypot_valuediff --git a/config/site_settings.yml b/config/site_settings.ymlindex 508981517d4..d82c83144a6 100644--- a/config/site_settings.yml+++ b/config/site_settings.yml@@ -1602,6 +1602,9 @@ security:regex: "^(Lax|Strict|Disabled|None)$"enable_escaped_fragments: trueallow_index_in_robots_txt: true+ allow_indexing_non_canonical_urls:+ default: true+ hidden: truemoderators_manage_categories_and_groups: falsemoderators_change_post_ownership:client: truediff --git a/lib/canonical_url.rb b/lib/canonical_url.rbindex d318bd56de4..df66c37a6c7 100644--- a/lib/canonical_url.rb+++ b/lib/canonical_url.rb@@ -2,6 +2,8 @@module CanonicalURLmodule ControllerExtensions+ ALLOWED_CANONICAL_PARAMS = %w(page)+def canonical_url(url_for_options = {})case url_for_optionswhen Hash@@ -10,22 +12,27 @@ module CanonicalURL@canonical_url = url_for_optionsendend++ def default_canonical+ @default_canonical ||= begin+ canonical = +"#{Discourse.base_url_no_prefix}#{request.path}"+ allowed_params = params.select { |key| ALLOWED_CANONICAL_PARAMS.include?(key) }+ if allowed_params.present?+ canonical << "?#{allowed_params.keys.zip(allowed_params.values).map { |key, value| "#{key}=#{value}" }.join("&")}"+ end+ canonical+ end+ end++ def self.included(base)+ base.helper_method :default_canonical+ endendmodule Helpers- ALLOWED_CANONICAL_PARAMS = %w(page)def canonical_link_tag(url = nil)tag('link', rel: 'canonical', href: url || @canonical_url || default_canonical)end-- def default_canonical- canonical = +"#{Discourse.base_url_no_prefix}#{request.path}"- allowed_params = params.select { |key| ALLOWED_CANONICAL_PARAMS.include?(key) }- if allowed_params.present?- canonical << "?#{allowed_params.keys.zip(allowed_params.values).map { |key, value| "#{key}=#{value}" }.join("&")}"- end- canonical- endendenddiff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rbindex 603d2f3fa03..34d9ecb07fc 100644--- a/spec/requests/application_controller_spec.rb+++ b/spec/requests/application_controller_spec.rb@@ -668,6 +668,33 @@ RSpec.describe ApplicationController doexpect(response.body).to have_tag("link", with: { rel: "canonical", href: "http://test.localhost/t/#{topic.slug}/#{topic.id}" })end+ it "adds a noindex header if non-canonical indexing is disabled" do+ SiteSetting.allow_indexing_non_canonical_urls = false+ get '/'+ expect(response.headers['X-Robots-Tag']).to be_nil++ get '/latest'+ expect(response.headers['X-Robots-Tag']).to be_nil++ get '/categories'+ expect(response.headers['X-Robots-Tag']).to be_nil++ topic = create_post.topic+ get "/t/#{topic.slug}/#{topic.id}"+ expect(response.headers['X-Robots-Tag']).to be_nil+ post = create_post(topic_id: topic.id)+ get "/t/#{topic.slug}/#{topic.id}/2"+ expect(response.headers['X-Robots-Tag']).to eq('noindex')++ 20.times do+ create_post(topic_id: topic.id)+ end+ get "/t/#{topic.slug}/#{topic.id}/21"+ expect(response.headers['X-Robots-Tag']).to eq('noindex')+ get "/t/#{topic.slug}/#{topic.id}?page=2"+ expect(response.headers['X-Robots-Tag']).to be_nil+ end+context "default locale" dobefore doSiteSetting.default_locale = :frdiff --git a/spec/requests/groups_controller_spec.rb b/spec/requests/groups_controller_spec.rbindex d918db850af..ff7b00a4a70 100644--- a/spec/requests/groups_controller_spec.rb+++ b/spec/requests/groups_controller_spec.rb@@ -176,12 +176,6 @@ describe GroupsController do)end- it 'should return correct X-Robots-Tag header when allow_index_in_robots_txt is set to false' do- SiteSetting.allow_index_in_robots_txt = false- get "/groups"- expect(response.headers['X-Robots-Tag']).to eq('noindex, nofollow')- end-context 'viewing groups of another user' dodescribe 'when an invalid username is given' doit 'should return the right response' do