From 8a88ce194319b951237104c0235e70d655db8ccd Mon Sep 17 00:00:00 2001 From: Bruno Prieto Date: Wed, 2 Aug 2023 06:37:41 +0200 Subject: [PATCH 1/2] Disable session in ActiveStorage blobs and representations proxy controller This allows CDNs such as CloudFlare to cache files delivered by proxy by default Fix #44136 --- activestorage/CHANGELOG.md | 8 ++++++++ .../active_storage/blobs/proxy_controller.rb | 1 + .../representations/proxy_controller.rb | 1 + .../concerns/active_storage/disable_session.rb | 13 +++++++++++++ 4 files changed, 23 insertions(+) create mode 100644 activestorage/app/controllers/concerns/active_storage/disable_session.rb diff --git a/activestorage/CHANGELOG.md b/activestorage/CHANGELOG.md index f147cad6f5434..64d1d7577d927 100644 --- a/activestorage/CHANGELOG.md +++ b/activestorage/CHANGELOG.md @@ -1,3 +1,11 @@ +* Disables the session in `ActiveStorage::Blobs::ProxyController` + and `ActiveStorage::Representations::ProxyController` + in order to allow caching by default in some CDNs as CloudFlare + + Fixes #44136 + + *Bruno Prieto* + * Add `tags` to `ActiveStorage::Analyzer::AudioAnalyzer` output *Keaton Roux* diff --git a/activestorage/app/controllers/active_storage/blobs/proxy_controller.rb b/activestorage/app/controllers/active_storage/blobs/proxy_controller.rb index 6ec2772717c70..438623858474e 100644 --- a/activestorage/app/controllers/active_storage/blobs/proxy_controller.rb +++ b/activestorage/app/controllers/active_storage/blobs/proxy_controller.rb @@ -9,6 +9,7 @@ class ActiveStorage::Blobs::ProxyController < ActiveStorage::BaseController include ActiveStorage::SetBlob include ActiveStorage::Streaming + include ActiveStorage::DisableSession def show if request.headers["Range"].present? diff --git a/activestorage/app/controllers/active_storage/representations/proxy_controller.rb b/activestorage/app/controllers/active_storage/representations/proxy_controller.rb index 0f6c0f79978ab..7024f6534a501 100644 --- a/activestorage/app/controllers/active_storage/representations/proxy_controller.rb +++ b/activestorage/app/controllers/active_storage/representations/proxy_controller.rb @@ -8,6 +8,7 @@ # {Authenticated Controllers}[https://guides.rubyonrails.org/active_storage_overview.html#authenticated-controllers]. class ActiveStorage::Representations::ProxyController < ActiveStorage::Representations::BaseController include ActiveStorage::Streaming + include ActiveStorage::DisableSession def show http_cache_forever public: true do diff --git a/activestorage/app/controllers/concerns/active_storage/disable_session.rb b/activestorage/app/controllers/concerns/active_storage/disable_session.rb new file mode 100644 index 0000000000000..977c2bc80c4ea --- /dev/null +++ b/activestorage/app/controllers/concerns/active_storage/disable_session.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +# This concern disables the session in order to allow caching by default in some CDNs as CloudFlare. + +module ActiveStorage::DisableSession + extend ActiveSupport::Concern + + included do + before_action do + request.session_options[:skip] = true + end + end +end From 379e3b4d79895e7a513afb769029bf880954ffca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20Mendon=C3=A7a=20Fran=C3=A7a?= Date: Thu, 3 Aug 2023 15:41:58 -0400 Subject: [PATCH 2/2] Remove empty line --- .../app/controllers/concerns/active_storage/disable_session.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/activestorage/app/controllers/concerns/active_storage/disable_session.rb b/activestorage/app/controllers/concerns/active_storage/disable_session.rb index 977c2bc80c4ea..200ad7c9d23ac 100644 --- a/activestorage/app/controllers/concerns/active_storage/disable_session.rb +++ b/activestorage/app/controllers/concerns/active_storage/disable_session.rb @@ -1,7 +1,6 @@ # frozen_string_literal: true # This concern disables the session in order to allow caching by default in some CDNs as CloudFlare. - module ActiveStorage::DisableSession extend ActiveSupport::Concern