2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2007-09-28 12:48:59 -04:00
|
|
|
require 'digest/sha1'
|
2011-09-10 12:51:55 -04:00
|
|
|
require "active_support/log_subscriber/test_helper"
|
2007-09-22 22:32:55 -04:00
|
|
|
|
2007-09-28 12:50:48 -04:00
|
|
|
# common controller actions
|
|
|
|
module RequestForgeryProtectionActions
|
|
|
|
def index
|
|
|
|
render :inline => "<%= form_tag('/') {} %>"
|
|
|
|
end
|
2010-01-30 16:42:30 -05:00
|
|
|
|
2007-09-28 12:50:48 -04:00
|
|
|
def show_button
|
|
|
|
render :inline => "<%= button_to('New', '/') {} %>"
|
|
|
|
end
|
2008-01-08 16:17:08 -05:00
|
|
|
|
2010-12-27 17:31:14 -05:00
|
|
|
def external_form
|
|
|
|
render :inline => "<%= form_tag('http://farfar.away/form', :authenticity_token => 'external_token') {} %>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def external_form_without_protection
|
|
|
|
render :inline => "<%= form_tag('http://farfar.away/form', :authenticity_token => false) {} %>"
|
|
|
|
end
|
|
|
|
|
2007-09-28 12:50:48 -04:00
|
|
|
def unsafe
|
|
|
|
render :text => 'pwn'
|
|
|
|
end
|
2009-11-18 02:36:48 -05:00
|
|
|
|
2010-02-04 17:15:16 -05:00
|
|
|
def meta
|
2010-09-11 05:04:19 -04:00
|
|
|
render :inline => "<%= csrf_meta_tags %>"
|
2010-02-04 17:15:16 -05:00
|
|
|
end
|
|
|
|
|
2011-02-05 10:37:53 -05:00
|
|
|
def external_form_for
|
2011-02-06 11:19:02 -05:00
|
|
|
render :inline => "<%= form_for(:some_resource, :authenticity_token => 'external_token') {} %>"
|
2011-02-05 10:37:53 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def form_for_without_protection
|
2011-02-06 11:19:02 -05:00
|
|
|
render :inline => "<%= form_for(:some_resource, :authenticity_token => false ) {} %>"
|
2011-02-05 10:37:53 -05:00
|
|
|
end
|
2012-03-14 19:03:39 -04:00
|
|
|
|
|
|
|
def form_for_remote
|
|
|
|
render :inline => "<%= form_for(:some_resource, :remote => true ) {} %>"
|
|
|
|
end
|
|
|
|
|
2012-03-14 19:28:36 -04:00
|
|
|
def form_for_remote_with_token
|
|
|
|
render :inline => "<%= form_for(:some_resource, :remote => true, :authenticity_token => true ) {} %>"
|
|
|
|
end
|
|
|
|
|
2012-03-27 22:03:50 -04:00
|
|
|
def form_for_with_token
|
|
|
|
render :inline => "<%= form_for(:some_resource, :authenticity_token => true ) {} %>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def form_for_remote_with_external_token
|
|
|
|
render :inline => "<%= form_for(:some_resource, :remote => true, :authenticity_token => 'external_token') {} %>"
|
|
|
|
end
|
|
|
|
|
2012-03-14 19:03:39 -04:00
|
|
|
def rescue_action(e) raise e end
|
2007-09-28 12:50:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# sample controllers
|
|
|
|
class RequestForgeryProtectionController < ActionController::Base
|
|
|
|
include RequestForgeryProtectionActions
|
2010-02-04 17:15:16 -05:00
|
|
|
protect_from_forgery :only => %w(index meta)
|
2007-09-28 12:50:48 -04:00
|
|
|
end
|
|
|
|
|
2012-03-09 11:33:06 -05:00
|
|
|
class RequestForgeryProtectionControllerUsingException < ActionController::Base
|
2011-01-04 19:36:07 -05:00
|
|
|
include RequestForgeryProtectionActions
|
|
|
|
protect_from_forgery :only => %w(index meta)
|
|
|
|
|
|
|
|
def handle_unverified_request
|
|
|
|
raise(ActionController::InvalidAuthenticityToken)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2008-11-20 17:06:19 -05:00
|
|
|
class FreeCookieController < RequestForgeryProtectionController
|
2007-09-28 12:50:48 -04:00
|
|
|
self.allow_forgery_protection = false
|
2010-01-30 16:42:30 -05:00
|
|
|
|
2007-09-28 12:50:48 -04:00
|
|
|
def index
|
|
|
|
render :inline => "<%= form_tag('/') {} %>"
|
|
|
|
end
|
2010-01-30 16:42:30 -05:00
|
|
|
|
2007-09-28 12:50:48 -04:00
|
|
|
def show_button
|
|
|
|
render :inline => "<%= button_to('New', '/') {} %>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-18 02:36:48 -05:00
|
|
|
class CustomAuthenticityParamController < RequestForgeryProtectionController
|
|
|
|
def form_authenticity_param
|
|
|
|
'foobar'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-09-28 12:50:48 -04:00
|
|
|
# common test methods
|
2007-09-25 12:50:35 -04:00
|
|
|
module RequestForgeryProtectionTests
|
2011-01-04 19:36:07 -05:00
|
|
|
def setup
|
|
|
|
@token = "cf50faa3fe97702ca1ae"
|
2009-08-16 22:14:26 -04:00
|
|
|
|
2011-05-23 07:02:06 -04:00
|
|
|
SecureRandom.stubs(:base64).returns(@token)
|
2011-05-09 19:17:38 -04:00
|
|
|
ActionController::Base.request_forgery_protection_token = :custom_authenticity_token
|
2010-09-22 15:04:42 -04:00
|
|
|
end
|
|
|
|
|
2011-05-09 19:17:38 -04:00
|
|
|
def teardown
|
|
|
|
ActionController::Base.request_forgery_protection_token = nil
|
|
|
|
end
|
2011-02-05 10:37:53 -05:00
|
|
|
|
2011-01-04 19:36:07 -05:00
|
|
|
def test_should_render_form_with_token_tag
|
|
|
|
assert_not_blocked do
|
|
|
|
get :index
|
|
|
|
end
|
2011-05-09 19:17:38 -04:00
|
|
|
assert_select 'form>div>input[name=?][value=?]', 'custom_authenticity_token', @token
|
2011-02-05 10:37:53 -05:00
|
|
|
end
|
|
|
|
|
2010-09-22 15:04:42 -04:00
|
|
|
def test_should_render_button_to_with_token_tag
|
2011-01-04 19:36:07 -05:00
|
|
|
assert_not_blocked do
|
|
|
|
get :show_button
|
|
|
|
end
|
2011-05-09 19:17:38 -04:00
|
|
|
assert_select 'form>div>input[name=?][value=?]', 'custom_authenticity_token', @token
|
2010-09-22 15:04:42 -04:00
|
|
|
end
|
|
|
|
|
2012-03-28 11:54:06 -04:00
|
|
|
def test_should_render_form_without_token_tag_if_remote
|
2012-03-14 19:03:39 -04:00
|
|
|
assert_not_blocked do
|
|
|
|
get :form_for_remote
|
|
|
|
end
|
2012-03-28 11:54:06 -04:00
|
|
|
assert_no_match(/authenticity_token/, response.body)
|
2012-03-27 22:03:50 -04:00
|
|
|
end
|
|
|
|
|
2012-03-28 11:54:06 -04:00
|
|
|
def test_should_render_form_with_token_tag_if_remote_and_embedding_token_is_on
|
|
|
|
original = ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms
|
2012-03-27 22:03:50 -04:00
|
|
|
begin
|
2012-03-28 11:54:06 -04:00
|
|
|
ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms = true
|
2012-03-27 22:03:50 -04:00
|
|
|
assert_not_blocked do
|
|
|
|
get :form_for_remote
|
|
|
|
end
|
2012-03-28 11:54:06 -04:00
|
|
|
assert_match(/authenticity_token/, response.body)
|
2012-03-27 22:03:50 -04:00
|
|
|
ensure
|
2012-03-28 11:54:06 -04:00
|
|
|
ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms = original
|
2012-03-27 22:03:50 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-28 11:54:06 -04:00
|
|
|
def test_should_render_form_with_token_tag_if_remote_and_external_authenticity_token_requested_and_embedding_is_on
|
|
|
|
original = ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms
|
|
|
|
begin
|
|
|
|
ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms = true
|
2012-03-27 22:03:50 -04:00
|
|
|
assert_not_blocked do
|
2012-03-28 11:54:06 -04:00
|
|
|
get :form_for_remote_with_external_token
|
2012-03-27 22:03:50 -04:00
|
|
|
end
|
2012-03-28 11:54:06 -04:00
|
|
|
assert_select 'form>div>input[name=?][value=?]', 'custom_authenticity_token', 'external_token'
|
2012-03-27 22:03:50 -04:00
|
|
|
ensure
|
2012-03-28 11:54:06 -04:00
|
|
|
ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms = original
|
2012-03-27 22:03:50 -04:00
|
|
|
end
|
2012-03-14 19:03:39 -04:00
|
|
|
end
|
|
|
|
|
2012-03-28 11:58:15 -04:00
|
|
|
def test_should_render_form_with_token_tag_if_remote_and_external_authenticity_token_requested
|
|
|
|
assert_not_blocked do
|
|
|
|
get :form_for_remote_with_external_token
|
|
|
|
end
|
|
|
|
assert_select 'form>div>input[name=?][value=?]', 'custom_authenticity_token', 'external_token'
|
|
|
|
end
|
|
|
|
|
2012-03-14 19:28:36 -04:00
|
|
|
def test_should_render_form_with_token_tag_if_remote_and_authenticity_token_requested
|
|
|
|
assert_not_blocked do
|
|
|
|
get :form_for_remote_with_token
|
|
|
|
end
|
|
|
|
assert_select 'form>div>input[name=?][value=?]', 'custom_authenticity_token', @token
|
|
|
|
end
|
|
|
|
|
2012-03-27 22:03:50 -04:00
|
|
|
def test_should_render_form_with_token_tag_with_authenticity_token_requested
|
|
|
|
assert_not_blocked do
|
|
|
|
get :form_for_with_token
|
|
|
|
end
|
|
|
|
assert_select 'form>div>input[name=?][value=?]', 'custom_authenticity_token', @token
|
|
|
|
end
|
|
|
|
|
2010-09-22 15:04:42 -04:00
|
|
|
def test_should_allow_get
|
2011-01-04 19:36:07 -05:00
|
|
|
assert_not_blocked { get :index }
|
2010-09-22 15:04:42 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_allow_post_without_token_on_unsafe_action
|
2011-01-04 19:36:07 -05:00
|
|
|
assert_not_blocked { post :unsafe }
|
2008-05-06 05:58:32 -04:00
|
|
|
end
|
|
|
|
|
2011-01-04 19:36:07 -05:00
|
|
|
def test_should_not_allow_post_without_token
|
|
|
|
assert_blocked { post :index }
|
2008-05-06 05:58:32 -04:00
|
|
|
end
|
|
|
|
|
2011-01-04 19:36:07 -05:00
|
|
|
def test_should_not_allow_post_without_token_irrespective_of_format
|
|
|
|
assert_blocked { post :index, :format=>'xml' }
|
2008-05-06 05:58:32 -04:00
|
|
|
end
|
|
|
|
|
2011-05-06 17:03:55 -04:00
|
|
|
def test_should_not_allow_patch_without_token
|
|
|
|
assert_blocked { patch :index }
|
|
|
|
end
|
|
|
|
|
2011-01-04 19:36:07 -05:00
|
|
|
def test_should_not_allow_put_without_token
|
|
|
|
assert_blocked { put :index }
|
2008-05-06 03:42:24 -04:00
|
|
|
end
|
2010-01-30 16:42:30 -05:00
|
|
|
|
2011-01-04 19:36:07 -05:00
|
|
|
def test_should_not_allow_delete_without_token
|
|
|
|
assert_blocked { delete :index }
|
2008-11-01 00:10:44 -04:00
|
|
|
end
|
2010-01-30 16:42:30 -05:00
|
|
|
|
2011-01-04 19:36:07 -05:00
|
|
|
def test_should_not_allow_xhr_post_without_token
|
|
|
|
assert_blocked { xhr :post, :index }
|
2007-09-22 22:32:55 -04:00
|
|
|
end
|
2010-01-30 16:42:30 -05:00
|
|
|
|
2011-01-04 19:36:07 -05:00
|
|
|
def test_should_allow_post_with_token
|
2011-05-09 19:17:38 -04:00
|
|
|
assert_not_blocked { post :index, :custom_authenticity_token => @token }
|
2007-09-22 22:32:55 -04:00
|
|
|
end
|
2010-01-30 16:42:30 -05:00
|
|
|
|
2011-05-06 17:03:55 -04:00
|
|
|
def test_should_allow_patch_with_token
|
|
|
|
assert_not_blocked { patch :index, :custom_authenticity_token => @token }
|
|
|
|
end
|
|
|
|
|
2011-01-04 19:36:07 -05:00
|
|
|
def test_should_allow_put_with_token
|
2011-05-09 19:17:38 -04:00
|
|
|
assert_not_blocked { put :index, :custom_authenticity_token => @token }
|
2009-03-04 16:05:15 -05:00
|
|
|
end
|
2010-01-30 16:42:30 -05:00
|
|
|
|
2011-01-04 19:36:07 -05:00
|
|
|
def test_should_allow_delete_with_token
|
2011-05-09 19:17:38 -04:00
|
|
|
assert_not_blocked { delete :index, :custom_authenticity_token => @token }
|
2007-09-22 22:32:55 -04:00
|
|
|
end
|
2010-01-30 16:42:30 -05:00
|
|
|
|
2011-01-04 19:36:07 -05:00
|
|
|
def test_should_allow_post_with_token_in_header
|
|
|
|
@request.env['HTTP_X_CSRF_TOKEN'] = @token
|
|
|
|
assert_not_blocked { post :index }
|
2007-09-22 22:32:55 -04:00
|
|
|
end
|
2010-01-30 16:42:30 -05:00
|
|
|
|
2011-01-04 19:36:07 -05:00
|
|
|
def test_should_allow_delete_with_token_in_header
|
|
|
|
@request.env['HTTP_X_CSRF_TOKEN'] = @token
|
|
|
|
assert_not_blocked { delete :index }
|
2007-09-22 22:32:55 -04:00
|
|
|
end
|
2010-01-30 16:42:30 -05:00
|
|
|
|
2011-05-06 17:03:55 -04:00
|
|
|
def test_should_allow_patch_with_token_in_header
|
|
|
|
@request.env['HTTP_X_CSRF_TOKEN'] = @token
|
|
|
|
assert_not_blocked { patch :index }
|
|
|
|
end
|
|
|
|
|
2011-01-04 19:36:07 -05:00
|
|
|
def test_should_allow_put_with_token_in_header
|
|
|
|
@request.env['HTTP_X_CSRF_TOKEN'] = @token
|
|
|
|
assert_not_blocked { put :index }
|
2007-09-22 22:32:55 -04:00
|
|
|
end
|
2010-01-30 16:42:30 -05:00
|
|
|
|
2011-09-10 12:51:55 -04:00
|
|
|
def test_should_warn_on_missing_csrf_token
|
|
|
|
old_logger = ActionController::Base.logger
|
|
|
|
logger = ActiveSupport::LogSubscriber::TestHelper::MockLogger.new
|
|
|
|
ActionController::Base.logger = logger
|
|
|
|
|
|
|
|
begin
|
|
|
|
assert_blocked { post :index }
|
|
|
|
|
|
|
|
assert_equal 1, logger.logged(:warn).size
|
|
|
|
assert_match(/CSRF token authenticity/, logger.logged(:warn).last)
|
2011-09-10 14:01:22 -04:00
|
|
|
ensure
|
2011-09-10 12:51:55 -04:00
|
|
|
ActionController::Base.logger = old_logger
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-04 19:36:07 -05:00
|
|
|
def assert_blocked
|
|
|
|
session[:something_like_user_id] = 1
|
|
|
|
yield
|
|
|
|
assert_nil session[:something_like_user_id], "session values are still present"
|
2007-09-22 22:32:55 -04:00
|
|
|
assert_response :success
|
|
|
|
end
|
2010-01-30 16:42:30 -05:00
|
|
|
|
2011-01-04 19:36:07 -05:00
|
|
|
def assert_not_blocked
|
|
|
|
assert_nothing_raised { yield }
|
2007-09-22 22:32:55 -04:00
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-09-28 12:50:48 -04:00
|
|
|
# OK let's get our test on
|
2007-09-25 12:50:35 -04:00
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class RequestForgeryProtectionControllerTest < ActionController::TestCase
|
2007-09-25 12:50:35 -04:00
|
|
|
include RequestForgeryProtectionTests
|
2010-02-04 17:15:16 -05:00
|
|
|
|
2011-05-09 19:17:38 -04:00
|
|
|
setup do
|
|
|
|
ActionController::Base.request_forgery_protection_token = :custom_authenticity_token
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
ActionController::Base.request_forgery_protection_token = nil
|
|
|
|
end
|
|
|
|
|
2011-04-07 20:18:33 -04:00
|
|
|
test 'should emit a csrf-param meta tag and a csrf-token meta tag' do
|
2011-05-23 07:02:06 -04:00
|
|
|
SecureRandom.stubs(:base64).returns(@token + '<=?')
|
2010-02-04 17:15:16 -05:00
|
|
|
get :meta
|
2011-05-09 19:17:38 -04:00
|
|
|
assert_select 'meta[name=?][content=?]', 'csrf-param', 'custom_authenticity_token'
|
2011-04-07 20:18:33 -04:00
|
|
|
assert_select 'meta[name=?][content=?]', 'csrf-token', 'cf50faa3fe97702ca1ae<=?'
|
2010-02-04 17:15:16 -05:00
|
|
|
end
|
2007-09-22 22:32:55 -04:00
|
|
|
end
|
|
|
|
|
2012-03-09 11:33:06 -05:00
|
|
|
class RequestForgeryProtectionControllerUsingExceptionTest < ActionController::TestCase
|
2011-01-04 19:36:07 -05:00
|
|
|
include RequestForgeryProtectionTests
|
|
|
|
def assert_blocked
|
|
|
|
assert_raises(ActionController::InvalidAuthenticityToken) do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-07 15:42:34 -05:00
|
|
|
class FreeCookieControllerTest < ActionController::TestCase
|
2007-09-28 11:55:45 -04:00
|
|
|
def setup
|
|
|
|
@controller = FreeCookieController.new
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
2010-02-04 20:49:23 -05:00
|
|
|
@token = "cf50faa3fe97702ca1ae"
|
2008-11-20 17:06:19 -05:00
|
|
|
|
2011-05-23 07:02:06 -04:00
|
|
|
SecureRandom.stubs(:base64).returns(@token)
|
2007-09-28 11:55:45 -04:00
|
|
|
end
|
2010-01-30 16:42:30 -05:00
|
|
|
|
2007-09-28 11:55:45 -04:00
|
|
|
def test_should_not_render_form_with_token_tag
|
|
|
|
get :index
|
|
|
|
assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token, false
|
|
|
|
end
|
2010-01-30 16:42:30 -05:00
|
|
|
|
2007-09-28 11:55:45 -04:00
|
|
|
def test_should_not_render_button_to_with_token_tag
|
|
|
|
get :show_button
|
|
|
|
assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token, false
|
|
|
|
end
|
2010-01-30 16:42:30 -05:00
|
|
|
|
2007-09-28 11:55:45 -04:00
|
|
|
def test_should_allow_all_methods_without_token
|
2011-05-06 17:03:55 -04:00
|
|
|
[:post, :patch, :put, :delete].each do |method|
|
2007-09-28 11:55:45 -04:00
|
|
|
assert_nothing_raised { send(method, :index)}
|
|
|
|
end
|
|
|
|
end
|
2010-02-04 17:15:16 -05:00
|
|
|
|
|
|
|
test 'should not emit a csrf-token meta tag' do
|
|
|
|
get :meta
|
2010-08-16 21:29:57 -04:00
|
|
|
assert_blank @response.body
|
2010-02-04 17:15:16 -05:00
|
|
|
end
|
2007-10-02 01:32:14 -04:00
|
|
|
end
|
2009-11-18 02:36:48 -05:00
|
|
|
|
|
|
|
class CustomAuthenticityParamControllerTest < ActionController::TestCase
|
|
|
|
def setup
|
2011-01-04 19:36:07 -05:00
|
|
|
ActionController::Base.request_forgery_protection_token = :custom_token_name
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2009-11-18 20:54:27 -05:00
|
|
|
ActionController::Base.request_forgery_protection_token = :authenticity_token
|
2011-01-04 19:36:07 -05:00
|
|
|
super
|
2009-11-18 02:36:48 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_allow_custom_token
|
2011-01-04 19:36:07 -05:00
|
|
|
post :index, :custom_token_name => 'foobar'
|
2009-11-18 02:36:48 -05:00
|
|
|
assert_response :ok
|
|
|
|
end
|
|
|
|
end
|