提交 acdba1c6 编写于 作者: J Jack McCracken

Add a better error message when a "null" Origin header occurs

上级 9ec67362
......@@ -414,11 +414,21 @@ def protect_against_forgery? # :doc:
allow_forgery_protection
end
NULL_ORIGIN_MESSAGE = <<-MSG.strip_heredoc
The browser returned a 'null' origin for a request with origin-based forgery protection turned on. This usually
means you have the 'no-referrer' Referrer-Policy header enabled, or that you the request came from a site that
refused to give its origin. This makes it impossible for Rails to verify the source of the requests. Likely the
best solution is to change your referrer policy to something less strict like same-origin or strict-same-origin.
If you cannot change the referrer policy, you can disable origin checking with the
Rails.application.config.action_controller.forgery_protection_origin_check setting.
MSG
# Checks if the request originated from the same origin by looking at the
# Origin header.
def valid_request_origin? # :doc:
if forgery_protection_origin_check
# We accept blank origin headers because some user agents don't send it.
raise InvalidAuthenticityToken, NULL_ORIGIN_MESSAGE if request.origin == "null"
request.origin.nil? || request.origin == request.base_url
else
true
......
......@@ -446,6 +446,19 @@ def test_should_allow_post_with_origin_checking_and_no_origin
end
end
def test_should_raise_for_post_with_null_origin
forgery_protection_origin_check do
session[:_csrf_token] = @token
@controller.stub :form_authenticity_token, @token do
exception = assert_raises(ActionController::InvalidAuthenticityToken) do
@request.set_header "HTTP_ORIGIN", "null"
post :index, params: { custom_authenticity_token: @token }
end
assert_match "The browser returned a 'null' origin for a request", exception.message
end
end
end
def test_should_block_post_with_origin_checking_and_wrong_origin
old_logger = ActionController::Base.logger
logger = ActiveSupport::LogSubscriber::TestHelper::MockLogger.new
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册