A platform for community discussion. Free, open, simple.
FIX: Include the Vary:Accept header on all Accept-based responses (#14647)
David Taylor9ac6f1d3bbf3
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex bdb33fee0ce..74eab9c71f3 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -9,6 +9,7 @@ class ApplicationController < ActionController::Baseinclude GlobalPathinclude Hijackinclude ReadOnlyHeader+ include VaryHeaderattr_reader :theme_id@@ -46,6 +47,7 @@ class ApplicationController < ActionController::Baseafter_action :perform_refresh_sessionafter_action :dont_cache_pageafter_action :conditionally_allow_site_embedding+ after_action :ensure_vary_headerHONEYPOT_KEY ||= 'HONEYPOT_KEY'CHALLENGE_KEY ||= 'CHALLENGE_KEY'diff --git a/lib/vary_header.rb b/lib/vary_header.rbnew file mode 100644index 00000000000..c80eb7a9b8d--- /dev/null+++ b/lib/vary_header.rb@@ -0,0 +1,7 @@+# frozen_string_literal: true++module VaryHeader+ def ensure_vary_header+ response.headers['Vary'] ||= 'Accept' if !params[:format]+ end+enddiff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rbindex bff3fdcaa9a..610cebccbd2 100644--- a/spec/requests/application_controller_spec.rb+++ b/spec/requests/application_controller_spec.rb@@ -831,4 +831,24 @@ RSpec.describe ApplicationController doendendend++ describe 'vary header' do+ it 'includes Vary:Accept on all requests where format is not explicit' do+ # Rails default behaviour - include Vary:Accept when Accept is supplied+ get "/latest", headers: { "Accept" => "application/json" }+ expect(response.status).to eq(200)+ expect(response.headers["Vary"]).to eq("Accept")++ # Discourse additional behaviour (see lib/vary_header.rb)+ # Include Vary:Accept even when Accept is not supplied+ get "/latest"+ expect(response.status).to eq(200)+ expect(response.headers["Vary"]).to eq("Accept")++ # Not needed, because the path 'format' parameter overrides the Accept header+ get "/latest.json"+ expect(response.status).to eq(200)+ expect(response.headers["Vary"]).to eq(nil)+ end+ endend