Skip to content

Commit e133b38

Browse files
committed
Do cheap/happy check first in verified_request?
Since Rails 5.2, Action Controller enables `protect_from_forgery` by default. `verified_request?` is called in a `before_action` to perform a series of checks to ensure the request should proceed. This commit reorders the checks so that the cheaper ones happen first, which allows the method to return more quickly in cases that the request does not need to be protected (GET/HEAD requests). In the `r10k` benchmark, `verified_request?` shows up as ~2% in a profile before this change and does not show up afterwards.
1 parent 976e518 commit e133b38

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
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
@@ -468,7 +468,7 @@ def non_xhr_javascript_response? # :doc:
468468
# * Does the `X-CSRF-Token` header match the form_authenticity_token?
469469
#
470470
def verified_request? # :doc:
471-
!protect_against_forgery? || request.get? || request.head? ||
471+
request.get? || request.head? || !protect_against_forgery? ||
472472
(valid_request_origin? && any_authenticity_token_valid?)
473473
end
474474

0 commit comments

Comments
 (0)