A platform for community discussion. Free, open, simple.
SECURITY: Error responses missing `Cache-Control` header
Alan Guo Xiang Tan3ea1b663c82c
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex a3dd050c1db..38dee81e823 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -33,6 +33,7 @@ class ApplicationController < ActionController::Baseendend+ around_action :ensure_dont_cache_pagebefore_action :check_readonly_modebefore_action :handle_themebefore_action :set_current_user_for_logs@@ -51,7 +52,6 @@ class ApplicationController < ActionController::Basebefore_action :set_crawler_headerafter_action :add_readonly_headerafter_action :perform_refresh_session- after_action :dont_cache_pageafter_action :conditionally_allow_site_embeddingafter_action :ensure_vary_headerafter_action :add_noindex_header,@@ -161,6 +161,11 @@ class ApplicationController < ActionController::Baseendrescue_from ActionController::RoutingError, PluginDisabled do+ # This error is raised outside of the normal request response cycle and is called via the+ # `DiscoursePublicExceptions` middleware which creates a new instance of the ApplicationController.+ # As a result, controller actions hooks are not called and we need to explicitly call `dont_cache_page` here.+ dont_cache_page+rescue_discourse_actions(:not_found, 404)end@@ -1062,4 +1067,10 @@ class ApplicationController < ActionController::Basedef set_crawler_headerresponse.headers["X-Discourse-Crawler-View"] = "true" if use_crawler_layout?end++ def ensure_dont_cache_page+ yield+ ensure+ dont_cache_page+ endenddiff --git a/lib/middleware/discourse_public_exceptions.rb b/lib/middleware/discourse_public_exceptions.rbindex 8f117f8b684..2a13cc10efa 100644--- a/lib/middleware/discourse_public_exceptions.rb+++ b/lib/middleware/discourse_public_exceptions.rb@@ -65,6 +65,8 @@ module Middlewareendif ApplicationController.rescue_with_handler(exception, object: fake_controller)+ # Calling `ActionDispatch::Response#to_a` here to ensure that cache control headers are set.+ response.to_abody = response.bodybody = [body] if String === bodyrack_response = [response.status, response.headers, body]diff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rbindex 96cae02335d..b877f7dfbbc 100644--- a/spec/requests/application_controller_spec.rb+++ b/spec/requests/application_controller_spec.rb@@ -3,6 +3,29 @@RSpec.describe ApplicationController dofab!(:user)+ context "for cache control headers" do+ it "sets the `no-cache, no-store` cache control response header when no error is raised" do+ get "/latest"++ expect(response.status).to eq(200)+ expect(response.headers["Cache-Control"]).to eq("no-cache, no-store")+ end++ it "sets the `no-cache, no-store` cache control response header when ActionController::RoutingError is raised" do+ get "/invalid-urlllllllllll"++ expect(response.status).to eq(404)+ expect(response.headers["Cache-Control"]).to eq("no-cache, no-store")+ end++ it "sets the `no-cache, no-store` cache control response header when Discourse::InvalidAccess is raised" do+ get "/latest.json", headers: { HTTP_API_KEY: "invalid-api-key" }++ expect(response.status).to eq(403)+ expect(response.headers["Cache-Control"]).to eq("no-cache, no-store")+ end+ end+context "when visiting an invalid URL" doit "displays the not found page with the user's active theme" dotheme = Fabricate(:theme, user_selectable: true)diff --git a/spec/requests/metadata_controller_spec.rb b/spec/requests/metadata_controller_spec.rbindex 5b58f007986..ce544c62aed 100644--- a/spec/requests/metadata_controller_spec.rb+++ b/spec/requests/metadata_controller_spec.rb@@ -251,7 +251,7 @@ RSpec.describe MetadataController doget "/.well-known/discourse-id-challenge"expect(response.status).to eq(404)- expect(response.cache_control).to eq({})+ expect(response["Cache-Control"]).to eq("no-cache, no-store")endendend