A platform for community discussion. Free, open, simple.
FIX: Prevent cached asset responses leaking usernames
Jake Goldsborough6735ce7e2b7e
diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rbindex 7e65f7dee92..a8e450ed8f2 100644--- a/app/controllers/application_controller.rb+++ b/app/controllers/application_controller.rb@@ -380,10 +380,7 @@ class ApplicationController < ActionController::Baseenddef set_current_user_for_logs- if current_user- Logster.add_to_env(request.env, "username", current_user.username)- response.headers["X-Discourse-Username"] = current_user.username- end+ Logster.add_to_env(request.env, "username", current_user.username) if current_userresponse.headers["X-Discourse-Route"] = "#{controller_path}/#{action_name}"end@@ -1126,6 +1123,24 @@ class ApplicationController < ActionController::Baseyieldensuredont_cache_page+ set_current_user_header_for_logs+ end++ def set_current_user_header_for_logs+ return if !current_user++ cache_control = response.cache_control+ extras = cache_control[:extras]+ # `dont_cache_page` sets both of these through `extras` rather than as+ # their own keys, and Rails only populates `cache_control[:private]` when+ # the directive was set that way, so both spellings have to be checked.+ no_store = cache_control[:no_store] || extras&.include?("no-store")+ private_response =+ cache_control[:private] || extras&.include?("private") ||+ (cache_control[:max_age] && !cache_control[:public])+ return if !private_response && !no_store++ response.headers["X-Discourse-Username"] = current_user.usernameenddef persist_locale_param_to_cookiediff --git a/spec/requests/application_controller_spec.rb b/spec/requests/application_controller_spec.rbindex 95fda889483..fc44c046869 100644--- a/spec/requests/application_controller_spec.rb+++ b/spec/requests/application_controller_spec.rb@@ -2135,6 +2135,39 @@ RSpec.describe ApplicationController dodescribe "#set_current_user_for_logs" dofab!(:admin)+ it "sets the X-Discourse-Username header for responses that cannot be stored" do+ sign_in(admin)++ get "/u/#{admin.username}.json"++ expect(response.status).to eq(200)+ expect(response.headers["Cache-Control"]).to include("no-store")+ expect(response.headers["X-Discourse-Username"]).to eq(admin.username)+ end++ it "sets the X-Discourse-Username header in bfcache compatibility mode" do+ # bfcache mode swaps `no-store` for `private`, which still keeps the+ # response out of any shared cache, so attribution is safe to keep.+ SiteSetting.cache_control_bfcache_compatibility = true+ sign_in(admin)++ get "/u/#{admin.username}.json"++ expect(response.status).to eq(200)+ expect(response.headers["Cache-Control"]).to eq("no-cache, private")+ expect(response.headers["X-Discourse-Username"]).to eq(admin.username)+ end++ it "sets the X-Discourse-Username header for private cached responses" do+ sign_in(admin)++ get "/manifest.webmanifest"++ expect(response.status).to eq(200)+ expect(response.headers["Cache-Control"]).to eq("max-age=60, private")+ expect(response.headers["X-Discourse-Username"]).to eq(admin.username)+ end+it "sets the X-Discourse-Route header to the controller name and action including namespace" dosign_in(admin)diff --git a/spec/requests/highlightjs_controller_spec.rb b/spec/requests/highlightjs_controller_spec.rbindex ed5f8c866eb..c1ce04d7611 100644--- a/spec/requests/highlightjs_controller_spec.rb+++ b/spec/requests/highlightjs_controller_spec.rb@@ -1,6 +1,8 @@# frozen_string_literal: trueRSpec.describe HighlightJsController do+ fab!(:user)+it "works via the site URL" doget HighlightJs.pathexpect(response.status).to eq(200)@@ -8,6 +10,15 @@ RSpec.describe HighlightJsController doexpect(response.headers["Access-Control-Allow-Origin"]).to eq("*")end+ it "does not include a username in the cacheable response" do+ sign_in(user)++ get HighlightJs.path++ expect(response.status).to eq(200)+ expect(response.headers["X-Discourse-Username"]).to be_nil+ end+it "works via a CDN" docdn = "https://original-app-cdn.example.com"set_cdn_url cdndiff --git a/spec/requests/static_controller_spec.rb b/spec/requests/static_controller_spec.rbindex d88e1d8a872..3ac945c64d4 100644--- a/spec/requests/static_controller_spec.rb+++ b/spec/requests/static_controller_spec.rb@@ -4,6 +4,8 @@ RSpec.describe StaticController dofab!(:upload)describe "#favicon" do+ fab!(:user)+let(:filename) { "smallest.png" }let(:file) { file_from_fixtures(filename) }@@ -20,6 +22,15 @@ RSpec.describe StaticController doexpect(response.body.bytesize).to eq(SiteIconManager.favicon.filesize)end+ it "does not include a username in the cacheable response" do+ sign_in(user)++ get "/favicon/proxied"++ expect(response.status).to eq(200)+ expect(response.headers["X-Discourse-Username"]).to be_nil+ end+it "returns the configured favicon" doSiteSetting.favicon = upload@@ -617,12 +628,23 @@ RSpec.describe StaticController doenddescribe "#service_worker_asset" do+ fab!(:user)+it "renders the requested static page" doget "/service-worker.js"expect(response.status).to eq(200)expect(response.content_type).to start_with("text/javascript")expect(response.body).to include("addEventListener")end++ it "does not include a username in the cacheable response" do+ sign_in(user)++ get "/service-worker.js"++ expect(response.status).to eq(200)+ expect(response.headers["X-Discourse-Username"]).to be_nil+ endenddescribe "#llms_txt" dodiff --git a/spec/requests/stylesheets_controller_spec.rb b/spec/requests/stylesheets_controller_spec.rbindex e39d2f57577..f43b65b2c31 100644--- a/spec/requests/stylesheets_controller_spec.rb+++ b/spec/requests/stylesheets_controller_spec.rb@@ -1,6 +1,23 @@# frozen_string_literal: trueRSpec.describe StylesheetsController do+ fab!(:user)++ describe "#show_source_map" do+ it "does not include a username in the cacheable response" do+ sign_in(user)++ manager = Stylesheet::Manager.new(theme_id: nil)+ builder = Stylesheet::Manager::Builder.new(target: "common", manager: manager, theme: nil)+ builder.compile++ get "/stylesheets/#{builder.stylesheet_filename}.map"++ expect(response.status).to eq(200)+ expect(response.headers["X-Discourse-Username"]).to be_nil+ end+ end+it "can survive cache miss" doStylesheetCache.destroy_allmanager = Stylesheet::Manager.new(theme_id: nil)diff --git a/spec/requests/svg_sprite_controller_spec.rb b/spec/requests/svg_sprite_controller_spec.rbindex 3af3668a8a2..d0f624d91d6 100644--- a/spec/requests/svg_sprite_controller_spec.rb+++ b/spec/requests/svg_sprite_controller_spec.rb@@ -17,6 +17,15 @@ RSpec.describe SvgSpriteController doexpect(response.status).to eq(200)end+ it "does not include a username in the cacheable response" do+ sign_in(user)++ get "/svg-sprite/#{Discourse.current_hostname}/svg--#{SvgSprite.version}.js"++ expect(response.status).to eq(200)+ expect(response.headers["X-Discourse-Username"]).to be_nil+ end+it "redirects to the current version" dorandom_hash = Digest::SHA1.hexdigest("somerandomstring")get "/svg-sprite/#{Discourse.current_hostname}/svg--#{random_hash}.js"