A platform for community discussion. Free, open, simple.
DEV: ensure Rails application default headers are present in responses (#31619)
Kelv305039b1c342
diff --git a/lib/middleware/default_headers.rb b/lib/middleware/default_headers.rbindex b0c446a3c01..9fa7d41a02e 100644--- a/lib/middleware/default_headers.rb+++ b/lib/middleware/default_headers.rb@@ -2,15 +2,29 @@module Middlewareclass DefaultHeaders+ HTML_ONLY_HEADERS = Set.new(%w[X-XSS-Protection])+ EXCLUDED_HEADERS = Set.new(%w[X-Frame-Options])+def initialize(app, settings = {})@app = appenddef call(env)status, headers, body = @app.call(env)+ is_html_response = html_response?(headers)++ default_headers =+ Rails.application.config.action_dispatch.default_headers.to_h.except(*EXCLUDED_HEADERS)++ default_headers.each do |header_name, value|+ next if !is_html_response && HTML_ONLY_HEADERS.include?(header_name)++ headers[header_name] ||= value+ end+headers["Cross-Origin-Opener-Policy"- ] = SiteSetting.cross_origin_opener_policy_header if html_response?(headers) &&+ ] = SiteSetting.cross_origin_opener_policy_header if is_html_response &&headers["Cross-Origin-Opener-Policy"].nil?[status, headers, body]diff --git a/spec/requests/default_headers_spec.rb b/spec/requests/default_headers_spec.rbindex a9c32375f83..42da932a1a3 100644--- a/spec/requests/default_headers_spec.rb+++ b/spec/requests/default_headers_spec.rb@@ -1,5 +1,23 @@# frozen_string_literal: trueRSpec.describe Middleware::DefaultHeaders do+ let(:mock_default_headers) do+ {+ "X-XSS-Protection" => "0",+ "X-Content-Type-Options" => "nosniff",+ "X-Permitted-Cross-Domain-Policies" => "none",+ "Referrer-Policy" => "strict-origin-when-cross-origin",+ }+ end++ let(:html_only_headers) { described_class::HTML_ONLY_HEADERS }+ let(:universal_headers) { Set.new(mock_default_headers.keys) - html_only_headers }++ before do+ allow(Rails.application.config.action_dispatch).to receive(:default_headers).and_return(+ mock_default_headers,+ )+ end+context "when a public exception(like RoutingError) is raised" docontext "when requesting an HTML page" dolet(:html_path) { "/nonexistent" }@@ -10,15 +28,24 @@ RSpec.describe Middleware::DefaultHeaders doexpect(response.headers).to have_key("Cross-Origin-Opener-Policy")expect(response.headers["Cross-Origin-Opener-Policy"]).to eq("same-origin-allow-popups")end++ it "sets all default Rails headers for HTML responses" do+ get html_path++ mock_default_headers.each { |name, value| expect(response.headers[name]).to eq(value) }+ endendcontext "when requesting a JSON response for an invalid URL" dolet(:json_path) { "/nonexistent.json" }- it "does not include the Cross-Origin-Opener-Policy header" do- SiteSetting.bootstrap_error_pages = true- SiteSetting.cross_origin_opener_policy_header = "same-origin"+ it "adds only universal default headers to non-HTML responses" doget json_path++ universal_headers.each do |name|+ expect(response.headers[name]).to eq(mock_default_headers[name])+ end+ html_only_headers.each { |name| expect(response.headers[name]).to be_nil }expect(response.headers["Cross-Origin-Opener-Policy"]).to be_nilendend@@ -33,7 +60,7 @@ RSpec.describe Middleware::DefaultHeaders doafter { Rails.logger = @old_logger }- it "should not raise a 500 (nor should it log a warning) for bad params" do+ it "adds default headers to the response" dobad_str = (+"d\xDE").force_encoding("utf-8")expect(bad_str.valid_encoding?).to eq(false)@@ -45,6 +72,7 @@ RSpec.describe Middleware::DefaultHeaders doexpect(response.status).to eq(400)expect(response.headers).to have_key("Cross-Origin-Opener-Policy")expect(response.headers["Cross-Origin-Opener-Policy"]).to eq("same-origin-allow-popups")+ mock_default_headers.each { |name, value| expect(response.headers[name]).to eq(value) }endendend