Skip to content

Commit 1cf3fde

Browse files
authored
Merge pull request rails#44537 from brtrick/fix-skip-forgery-protection
Allow skip_forgery_protection if no protection set
2 parents 0dd8c08 + 880a1be commit 1cf3fde

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

actionmailbox/app/controllers/action_mailbox/base_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
module ActionMailbox
44
# The base class for all Action Mailbox ingress controllers.
55
class BaseController < ActionController::Base
6-
skip_forgery_protection if default_protect_from_forgery
6+
skip_forgery_protection
77

88
before_action :ensure_configured
99

actionpack/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
* Fix `skip_forgery_protection` to run without raising an error if forgery
2+
protection has not been enabled / `verify_authenticity_token` is not a
3+
defined callback.
4+
5+
This fix prevents the Rails 7.0 Welcome Page (`/`) from raising an
6+
`ArgumentError` if `default_protect_from_forgery` is false.
7+
8+
*Brad Trick*
9+
110
* Make `redirect_to` return an empty response body.
211

312
Application controllers that wish to add a response body after calling

actionpack/lib/action_controller/metal/request_forgery_protection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def protect_from_forgery(options = {})
155155
#
156156
# See +skip_before_action+ for allowed options.
157157
def skip_forgery_protection(options = {})
158-
skip_before_action :verify_authenticity_token, options
158+
skip_before_action :verify_authenticity_token, options.reverse_merge(raise: false)
159159
end
160160

161161
private

actionpack/test/controller/request_forgery_protection_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ class SkipProtectionController < ActionController::Base
190190
attr_accessor :skip_requested
191191
end
192192

193+
class SkipProtectionWhenUnprotectedController < ActionController::Base
194+
include RequestForgeryProtectionActions
195+
skip_forgery_protection
196+
end
197+
193198
# common test methods
194199
module RequestForgeryProtectionTests
195200
def setup
@@ -1121,3 +1126,14 @@ def assert_not_blocked(&block)
11211126
assert_response :success
11221127
end
11231128
end
1129+
1130+
class SkipProtectionWhenUnprotectedControllerTest < ActionController::TestCase
1131+
def test_should_allow_skip_request_when_protection_is_not_set
1132+
assert_not_blocked { post :index }
1133+
end
1134+
1135+
def assert_not_blocked(&block)
1136+
assert_nothing_raised(&block)
1137+
assert_response :success
1138+
end
1139+
end

railties/test/application/routing_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,5 +747,17 @@ def index
747747
get "/"
748748
assert_equal 200, last_response.status
749749
end
750+
751+
test "request to rails/welcome is successful when default_protect_from_forgery is false" do
752+
add_to_config <<-RUBY
753+
config.action_dispatch.show_exceptions = false
754+
config.action_controller.default_protect_from_forgery = false
755+
RUBY
756+
757+
app "development"
758+
759+
get "/"
760+
assert_equal 200, last_response.status
761+
end
750762
end
751763
end

0 commit comments

Comments
 (0)