Skip to content

Commit 60a31fb

Browse files
ryenskibyroot
andcommitted
Fix NoMethodError when a non-string CSRF token is passed through header
The method was already checking for non-string objects, but only after calling `#empty?` as the only test was though a form-encoded request. But using JSON requests it's possible to pass a CSRF token that doesn't even respond to `#empty?`. Co-Authored-By: Jean Boussier <[email protected]>
1 parent 4d42d34 commit 60a31fb

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

actionpack/lib/action_controller/metal/request_forgery_protection.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ def masked_authenticity_token(form_options: {})
507507
# Checks the client's masked token to see if it matches the session token.
508508
# Essentially the inverse of `masked_authenticity_token`.
509509
def valid_authenticity_token?(session, encoded_masked_token) # :doc:
510-
if encoded_masked_token.nil? || encoded_masked_token.empty? || !encoded_masked_token.is_a?(String)
510+
if !encoded_masked_token.is_a?(String) || encoded_masked_token.empty?
511511
return false
512512
end
513513

actionpack/test/controller/request_forgery_protection_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ def test_csrf_token_is_not_saved_if_it_is_nil
675675

676676
def test_should_not_raise_error_if_token_is_not_a_string
677677
assert_blocked do
678-
patch :index, params: { custom_authenticity_token: { foo: "bar" } }
678+
patch :index, params: { custom_authenticity_token: 1 }, as: :json
679679
end
680680
end
681681

0 commit comments

Comments
 (0)