A platform for community discussion. Free, open, simple.
FIX: cross origin opener policy should apply to public error responses (#31559)
Kelv0d90f6e3c3cb
diff --git a/config/application.rb b/config/application.rbindex 8fd9a7127ad..dfc49e66f5d 100644--- a/config/application.rb+++ b/config/application.rb@@ -80,6 +80,7 @@ module Discoursesuperendend+# Settings in config/environments/* take precedence over those specified here.# Application configuration should go into files in config/initializers# -- all .rb files in that directory are automatically loaded.@@ -169,6 +170,9 @@ module Discourseconfig.middleware.insert_after Rack::MethodOverride, Middleware::EnforceHostnameend+ require "middleware/default_headers"+ config.middleware.insert_before ActionDispatch::ShowExceptions, Middleware::DefaultHeaders+require "content_security_policy/middleware"config.middleware.swap ActionDispatch::ContentSecurityPolicy::Middleware,ContentSecurityPolicy::Middlewarediff --git a/lib/middleware/default_headers.rb b/lib/middleware/default_headers.rbnew file mode 100644index 00000000000..b0c446a3c01--- /dev/null+++ b/lib/middleware/default_headers.rb@@ -0,0 +1,25 @@+# frozen_string_literal: true++module Middleware+ class DefaultHeaders+ def initialize(app, settings = {})+ @app = app+ end++ def call(env)+ status, headers, body = @app.call(env)+ headers[+ "Cross-Origin-Opener-Policy"+ ] = SiteSetting.cross_origin_opener_policy_header if html_response?(headers) &&+ headers["Cross-Origin-Opener-Policy"].nil?++ [status, headers, body]+ end++ private++ def html_response?(headers)+ headers["Content-Type"] && headers["Content-Type"] =~ /html/+ end+ end+enddiff --git a/lib/middleware/discourse_public_exceptions.rb b/lib/middleware/discourse_public_exceptions.rbindex c4ae0d673fb..dd82b0afd58 100644--- a/lib/middleware/discourse_public_exceptions.rb+++ b/lib/middleware/discourse_public_exceptions.rb@@ -4,6 +4,7 @@# we need to handle certain exceptions heremodule Middlewareclass DiscoursePublicExceptions < ::ActionDispatch::PublicExceptions+ require "middleware/default_headers"# These middlewares will be re-run when the exception response is generatedEXCEPTION_RESPONSE_MIDDLEWARES = [ContentSecurityPolicy::Middleware,diff --git a/spec/integrity/middleware_order_spec.rb b/spec/integrity/middleware_order_spec.rbindex 1ae0e167f9b..ece3a98761f 100644--- a/spec/integrity/middleware_order_spec.rb+++ b/spec/integrity/middleware_order_spec.rb@@ -16,6 +16,7 @@ RSpec.describe "Middleware order" doMiddleware::EnforceHostname,ActionDispatch::RequestId,SilenceLogger,+ Middleware::DefaultHeaders,ActionDispatch::ShowExceptions,ActionDispatch::DebugExceptions,ActionDispatch::Callbacks,@@ -45,4 +46,10 @@ RSpec.describe "Middleware order" doit "ensures that ActionDispatch::RemoteIp comes before Middleware::RequestTracker" doexpect(remote_ip_index).to be < request_tracker_indexend++ it "ensures that Middleware::DefaultHeaders comes before ActionDispatch::ShowExceptions" do+ default_headers_index = actual_middlewares.index(Middleware::DefaultHeaders)+ show_exceptions_index = actual_middlewares.index(ActionDispatch::ShowExceptions)+ expect(default_headers_index).to be < show_exceptions_index+ endenddiff --git a/spec/requests/default_headers_spec.rb b/spec/requests/default_headers_spec.rbnew file mode 100644index 00000000000..a9c32375f83--- /dev/null+++ b/spec/requests/default_headers_spec.rb@@ -0,0 +1,50 @@+# frozen_string_literal: true+RSpec.describe Middleware::DefaultHeaders do+ context "when a public exception(like RoutingError) is raised" do+ context "when requesting an HTML page" do+ let(:html_path) { "/nonexistent" }++ it "sets the Cross-Origin-Opener-Policy header" do+ SiteSetting.bootstrap_error_pages = true+ get html_path # triggers a RoutingError, handled by the exceptions_app+ expect(response.headers).to have_key("Cross-Origin-Opener-Policy")+ expect(response.headers["Cross-Origin-Opener-Policy"]).to eq("same-origin-allow-popups")+ end+ end++ context "when requesting a JSON response for an invalid URL" do+ let(: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"+ get json_path+ expect(response.headers["Cross-Origin-Opener-Policy"]).to be_nil+ end+ end+ end++ context "when a rescued exception is raised" do+ before do+ @old_logger = Rails.logger+ @logs = StringIO.new+ Rails.logger = Logger.new(@logs)+ end++ after { Rails.logger = @old_logger }++ it "should not raise a 500 (nor should it log a warning) for bad params" do+ bad_str = (+"d\xDE").force_encoding("utf-8")+ expect(bad_str.valid_encoding?).to eq(false)++ get "/latest", params: { test: bad_str }++ log = @logs.string+ expect(log).not_to include("exception app middleware")++ expect(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")+ end+ end+end