Skip to content

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.rb
index a3dd050c1db..38dee81e823 100644
--- a/app/controllers/application_controller.rb
+++ b/app/controllers/application_controller.rb
@@ -33,6 +33,7 @@ class ApplicationController < ActionController::Base
end
end
+ around_action :ensure_dont_cache_page
before_action :check_readonly_mode
before_action :handle_theme
before_action :set_current_user_for_logs
@@ -51,7 +52,6 @@ class ApplicationController < ActionController::Base
before_action :set_crawler_header
after_action :add_readonly_header
after_action :perform_refresh_session
- after_action :dont_cache_page
after_action :conditionally_allow_site_embedding
after_action :ensure_vary_header
after_action :add_noindex_header,
@@ -161,6 +161,11 @@ class ApplicationController < ActionController::Base
end
rescue_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::Base
def set_crawler_header
response.headers["X-Discourse-Crawler-View"] = "true" if use_crawler_layout?
end
+
+ def ensure_dont_cache_page
+ yield
+ ensure
+ dont_cache_page
+ end
end
diff --git a/lib/middleware/discourse_public_exceptions.rb b/lib/middleware/discourse_public_exceptions.rb
index 8f117f8b684..2a13cc10efa 100644
--- a/lib/middleware/discourse_public_exceptions.rb
+++ b/lib/middleware/discourse_public_exceptions.rb
@@ -65,6 +65,8 @@ module Middleware
end
if ApplicationController.rescue_with_handler(exception, object: fake_controller)
+ # Calling `ActionDispatch::Response#to_a` here to ensure that cache control headers are set.
+ response.to_a
body = response.body
body = [body] if String === body
rack_response = [response.status, response.headers, body]
diff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rb
index 96cae02335d..b877f7dfbbc 100644
--- a/spec/requests/application_controller_spec.rb
+++ b/spec/requests/application_controller_spec.rb
@@ -3,6 +3,29 @@
RSpec.describe ApplicationController do
fab!(: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" do
it "displays the not found page with the user's active theme" do
theme = Fabricate(:theme, user_selectable: true)
diff --git a/spec/requests/metadata_controller_spec.rb b/spec/requests/metadata_controller_spec.rb
index 5b58f007986..ce544c62aed 100644
--- a/spec/requests/metadata_controller_spec.rb
+++ b/spec/requests/metadata_controller_spec.rb
@@ -251,7 +251,7 @@ RSpec.describe MetadataController do
get "/.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")
end
end
end