Skip to content

Commit 4e3504f

Browse files
committed
Stop failing GSRF token generation when session is disabled
In theory this should have warned early that the CSRF check will fail, which would have been less puzzling for the developer. However there are several cases where we render forms but the session is inacessible. That's the case of turbo (hotwired/turbo-rails#243) as well as some others. So unless we figure a proper way to detect these cases, we're better to not cause this error. Writing to a disabled session directly will still raise, this only silence it for the specific case of CSRF.
1 parent 94a029c commit 4e3504f

File tree

2 files changed

+1
-34
lines changed

2 files changed

+1
-34
lines changed

actionpack/lib/action_controller/metal/request_forgery_protection.rb

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,6 @@ class InvalidCrossOriginRequest < ActionControllerError # :nodoc:
5757
module RequestForgeryProtection
5858
extend ActiveSupport::Concern
5959

60-
class DisabledSessionError < StandardError
61-
MESSAGE = <<~EOS.squish
62-
Request forgery protection requires a working session store but your application has sessions disabled.
63-
You need to either disable request forgery protection, or configure a working session store.
64-
EOS
65-
66-
def initialize(message = MESSAGE)
67-
super
68-
end
69-
end
70-
7160
include AbstractController::Helpers
7261
include AbstractController::Callbacks
7362

@@ -101,11 +90,6 @@ def initialize(message = MESSAGE)
10190
config_accessor :default_protect_from_forgery
10291
self.default_protect_from_forgery = false
10392

104-
# Controls whether trying to use forgery protection without a working session store
105-
# issues a warning or raises an error.
106-
config_accessor :silence_disabled_session_errors
107-
self.silence_disabled_session_errors = true
108-
10993
# Controls whether URL-safe CSRF tokens are generated.
11094
config_accessor :urlsafe_csrf_tokens, instance_writer: false
11195
self.urlsafe_csrf_tokens = false
@@ -469,20 +453,7 @@ def form_authenticity_param # :doc:
469453

470454
# Checks if the controller allows forgery protection.
471455
def protect_against_forgery? # :doc:
472-
allow_forgery_protection && ensure_session_is_enabled!
473-
end
474-
475-
def ensure_session_is_enabled!
476-
if !session.respond_to?(:enabled?) || session.enabled?
477-
true
478-
else
479-
if silence_disabled_session_errors
480-
ActiveSupport::Deprecation.warn(DisabledSessionError::MESSAGE)
481-
false
482-
else
483-
raise DisabledSessionError
484-
end
485-
end
456+
allow_forgery_protection && (!session.respond_to?(:enabled?) || session.enabled?)
486457
end
487458

488459
NULL_ORIGIN_MESSAGE = <<~MSG

railties/lib/rails/application/configuration.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,6 @@ def load_defaults(target_version)
203203
action_dispatch.cookies_serializer = :json
204204
end
205205

206-
if respond_to?(:action_controller)
207-
action_controller.silence_disabled_session_errors = false
208-
end
209-
210206
if respond_to?(:action_view)
211207
action_view.button_to_generates_button_tag = true
212208
action_view.apply_stylesheet_media_default = false

0 commit comments

Comments
 (0)