From ca7c820c6962788d510c99a68e725de83cbe9cad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20D=C3=ADaz?= Date: Tue, 18 May 2021 21:26:24 -0500 Subject: [PATCH] Remove `ActionDispatch::Flash::NullFlash` After #42167, all apps (except api only ones) have access to the flash module. If the session store is disabled, then an empty flash object is used. This patch also prevents the flash from being committed to the session if this is not enabled. --- .../metal/request_forgery_protection.rb | 4 ++++ actionpack/lib/action_controller/test_case.rb | 4 ++++ .../lib/action_dispatch/middleware/flash.rb | 21 ++----------------- actionpack/test/controller/flash_test.rb | 2 +- 4 files changed, 11 insertions(+), 20 deletions(-) diff --git a/actionpack/lib/action_controller/metal/request_forgery_protection.rb b/actionpack/lib/action_controller/metal/request_forgery_protection.rb index 1e505a9576..d6ececb3f6 100644 --- a/actionpack/lib/action_controller/metal/request_forgery_protection.rb +++ b/actionpack/lib/action_controller/metal/request_forgery_protection.rb @@ -199,6 +199,10 @@ module ActionController #:nodoc: def exists? true end + + def enabled? + false + end end class NullCookieJar < ActionDispatch::Cookies::CookieJar #:nodoc: diff --git a/actionpack/lib/action_controller/test_case.rb b/actionpack/lib/action_controller/test_case.rb index 87d2791ec8..7c9221e2ce 100644 --- a/actionpack/lib/action_controller/test_case.rb +++ b/actionpack/lib/action_controller/test_case.rb @@ -211,6 +211,10 @@ module ActionController @data.fetch(key.to_s, *args, &block) end + def enabled? + true + end + private def load! @id diff --git a/actionpack/lib/action_dispatch/middleware/flash.rb b/actionpack/lib/action_dispatch/middleware/flash.rb index a3be2daa05..d0efda70aa 100644 --- a/actionpack/lib/action_dispatch/middleware/flash.rb +++ b/actionpack/lib/action_dispatch/middleware/flash.rb @@ -45,7 +45,6 @@ module ActionDispatch # read a notice you put there or flash["notice"] = "hello" # to put a new one. def flash - return Flash::NullFlash unless session.respond_to?(:loaded?) flash = flash_hash return flash if flash self.flash = Flash::FlashHash.from_session_value(session["flash"]) @@ -60,16 +59,14 @@ module ActionDispatch end def commit_flash # :nodoc: - session = self.session || {} - flash_hash = self.flash_hash + return unless session.enabled? if flash_hash && (flash_hash.present? || session.key?("flash")) session["flash"] = flash_hash.to_session_value self.flash = flash_hash.dup end - if (!session.respond_to?(:loaded?) || session.loaded?) && # reset_session uses {}, which doesn't implement #loaded? - session.key?("flash") && session["flash"].nil? + if session.loaded? && session.key?("flash") && session["flash"].nil? session.delete("flash") end end @@ -80,20 +77,6 @@ module ActionDispatch end end - module NullFlash #:nodoc: - class << self - def []=(k, v); end - - def [](k); end - - def alert=(message); end - - def notice=(message); end - - def empty?; end - end - end - class FlashNow #:nodoc: attr_accessor :flash diff --git a/actionpack/test/controller/flash_test.rb b/actionpack/test/controller/flash_test.rb index 3f81094d96..d73371eb95 100644 --- a/actionpack/test/controller/flash_test.rb +++ b/actionpack/test/controller/flash_test.rb @@ -244,7 +244,7 @@ class FlashIntegrationTest < ActionDispatch::IntegrationTest SessionKey = "_myapp_session" Generator = ActiveSupport::CachingKeyGenerator.new( ActiveSupport::KeyGenerator.new("b3c631c314c0bbca50c1b2843150fe33", iterations: 1000) - ) + ) Rotations = ActiveSupport::Messages::RotationConfiguration.new SIGNED_COOKIE_SALT = "signed cookie"