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
|
2012-05-30 16:02:26 -04:00
|
|
|
render :inline => "<%= button_to('New', '/') %>"
|
2007-09-28 12:50:48 -04:00
|
|
|
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
|
|
|
|
|
2013-12-12 22:41:14 -05:00
|
|
|
def same_origin_js
|
|
|
|
render js: 'foo();'
|
|
|
|
end
|
|
|
|
|
|
|
|
def negotiate_same_origin
|
|
|
|
respond_to do |format|
|
|
|
|
format.js { same_origin_js }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def cross_origin_js
|
|
|
|
same_origin_js
|
|
|
|
end
|
|
|
|
|
|
|
|
def negotiate_cross_origin
|
|
|
|
negotiate_same_origin
|
|
|
|
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
|
2012-09-13 05:07:37 -04:00
|
|
|
class RequestForgeryProtectionControllerUsingResetSession < ActionController::Base
|
2007-09-28 12:50:48 -04:00
|
|
|
include RequestForgeryProtectionActions
|
2013-12-12 22:41:14 -05:00
|
|
|
protect_from_forgery :only => %w(index meta same_origin_js negotiate_same_origin), :with => :reset_session
|
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
|
2013-12-12 22:41:14 -05:00
|
|
|
protect_from_forgery :only => %w(index meta same_origin_js negotiate_same_origin), :with => :exception
|
2011-01-04 19:36:07 -05:00
|
|
|
end
|
|
|
|
|
2013-02-08 13:22:16 -05:00
|
|
|
class RequestForgeryProtectionControllerUsingNullSession < ActionController::Base
|
|
|
|
protect_from_forgery :with => :null_session
|
|
|
|
|
|
|
|
def signed
|
|
|
|
cookies.signed[:foo] = 'bar'
|
|
|
|
render :nothing => true
|
|
|
|
end
|
|
|
|
|
|
|
|
def encrypted
|
|
|
|
cookies.encrypted[:foo] = 'bar'
|
|
|
|
render :nothing => true
|
|
|
|
end
|
2013-09-18 22:01:59 -04:00
|
|
|
|
|
|
|
def try_to_reset_session
|
|
|
|
reset_session
|
|
|
|
render :nothing => true
|
|
|
|
end
|
2013-02-08 13:22:16 -05:00
|
|
|
end
|
2011-01-04 19:36:07 -05:00
|
|
|
|
2012-09-13 05:07:37 -04:00
|
|
|
class FreeCookieController < RequestForgeryProtectionControllerUsingResetSession
|
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
|
2012-05-30 16:02:26 -04:00
|
|
|
render :inline => "<%= button_to('New', '/') %>"
|
2007-09-28 12:50:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-13 05:07:37 -04:00
|
|
|
class CustomAuthenticityParamController < RequestForgeryProtectionControllerUsingResetSession
|
2009-11-18 02:36:48 -05:00
|
|
|
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)
|
2014-05-28 10:35:48 -04:00
|
|
|
@old_request_forgery_protection_token = ActionController::Base.request_forgery_protection_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
|
2014-05-28 10:35:48 -04:00
|
|
|
ActionController::Base.request_forgery_protection_token = @old_request_forgery_protection_token
|
2011-05-09 19:17:38 -04:00
|
|
|
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
|
2014-04-17 15:14:40 -04:00
|
|
|
assert_select 'form>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
|
2014-04-17 15:15:43 -04:00
|
|
|
assert_select 'form>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
|
2014-04-17 15:14:40 -04:00
|
|
|
assert_select 'form>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
|
2014-04-17 15:14:40 -04:00
|
|
|
assert_select 'form>input[name=?][value=?]', 'custom_authenticity_token', 'external_token'
|
2012-03-28 11:58:15 -04:00
|
|
|
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
|
2014-04-17 15:14:40 -04:00
|
|
|
assert_select 'form>input[name=?][value=?]', 'custom_authenticity_token', @token
|
2012-03-14 19:28:36 -04:00
|
|
|
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
|
2014-04-17 15:14:40 -04:00
|
|
|
assert_select 'form>input[name=?][value=?]', 'custom_authenticity_token', @token
|
2012-03-27 22:03:50 -04:00
|
|
|
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
|
|
|
|
|
2013-01-22 15:05:22 -05:00
|
|
|
def test_should_allow_head
|
|
|
|
assert_not_blocked { head :index }
|
|
|
|
end
|
|
|
|
|
2010-09-22 15:04:42 -04:00
|
|
|
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
|
2013-12-12 22:41:14 -05:00
|
|
|
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
|
2014-03-04 19:24:51 -05:00
|
|
|
|
|
|
|
def test_should_not_warn_if_csrf_logging_disabled
|
|
|
|
old_logger = ActionController::Base.logger
|
|
|
|
logger = ActiveSupport::LogSubscriber::TestHelper::MockLogger.new
|
|
|
|
ActionController::Base.logger = logger
|
|
|
|
ActionController::Base.log_warning_on_csrf_failure = false
|
|
|
|
|
|
|
|
begin
|
|
|
|
assert_blocked { post :index }
|
|
|
|
|
|
|
|
assert_equal 0, logger.logged(:warn).size
|
|
|
|
ensure
|
|
|
|
ActionController::Base.logger = old_logger
|
|
|
|
ActionController::Base.log_warning_on_csrf_failure = true
|
|
|
|
end
|
|
|
|
end
|
2011-09-10 12:51:55 -04:00
|
|
|
|
2013-12-12 22:41:14 -05:00
|
|
|
def test_should_only_allow_same_origin_js_get_with_xhr_header
|
|
|
|
assert_cross_origin_blocked { get :same_origin_js }
|
|
|
|
assert_cross_origin_blocked { get :same_origin_js, format: 'js' }
|
|
|
|
assert_cross_origin_blocked do
|
|
|
|
@request.accept = 'text/javascript'
|
|
|
|
get :negotiate_same_origin
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_cross_origin_not_blocked { xhr :get, :same_origin_js }
|
|
|
|
assert_cross_origin_not_blocked { xhr :get, :same_origin_js, format: 'js' }
|
|
|
|
assert_cross_origin_not_blocked do
|
|
|
|
@request.accept = 'text/javascript'
|
|
|
|
xhr :get, :negotiate_same_origin
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-17 18:02:04 -05:00
|
|
|
# Allow non-GET requests since GET is all a remote <script> tag can muster.
|
|
|
|
def test_should_allow_non_get_js_without_xhr_header
|
|
|
|
assert_cross_origin_not_blocked { post :same_origin_js, custom_authenticity_token: @token }
|
|
|
|
assert_cross_origin_not_blocked { post :same_origin_js, format: 'js', custom_authenticity_token: @token }
|
|
|
|
assert_cross_origin_not_blocked do
|
|
|
|
@request.accept = 'text/javascript'
|
|
|
|
post :negotiate_same_origin, custom_authenticity_token: @token
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-12-12 22:41:14 -05:00
|
|
|
def test_should_only_allow_cross_origin_js_get_without_xhr_header_if_protection_disabled
|
|
|
|
assert_cross_origin_not_blocked { get :cross_origin_js }
|
|
|
|
assert_cross_origin_not_blocked { get :cross_origin_js, format: 'js' }
|
|
|
|
assert_cross_origin_not_blocked do
|
|
|
|
@request.accept = 'text/javascript'
|
|
|
|
get :negotiate_cross_origin
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_cross_origin_not_blocked { xhr :get, :cross_origin_js }
|
|
|
|
assert_cross_origin_not_blocked { xhr :get, :cross_origin_js, format: 'js' }
|
|
|
|
assert_cross_origin_not_blocked do
|
|
|
|
@request.accept = 'text/javascript'
|
|
|
|
xhr :get, :negotiate_cross_origin
|
|
|
|
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
|
2013-12-12 22:41:14 -05:00
|
|
|
|
|
|
|
def assert_cross_origin_blocked
|
|
|
|
assert_raises(ActionController::InvalidCrossOriginRequest) do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_cross_origin_not_blocked
|
|
|
|
assert_not_blocked { yield }
|
|
|
|
end
|
2007-09-22 22:32:55 -04:00
|
|
|
end
|
|
|
|
|
2007-09-28 12:50:48 -04:00
|
|
|
# OK let's get our test on
|
2007-09-25 12:50:35 -04:00
|
|
|
|
2012-09-13 05:07:37 -04:00
|
|
|
class RequestForgeryProtectionControllerUsingResetSessionTest < 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
|
2014-05-28 10:35:48 -04:00
|
|
|
@old_request_forgery_protection_token = ActionController::Base.request_forgery_protection_token
|
2011-05-09 19:17:38 -04:00
|
|
|
ActionController::Base.request_forgery_protection_token = :custom_authenticity_token
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
2014-05-28 10:35:48 -04:00
|
|
|
ActionController::Base.request_forgery_protection_token = @old_request_forgery_protection_token
|
2011-05-09 19:17:38 -04:00
|
|
|
end
|
|
|
|
|
2011-04-07 20:18:33 -04:00
|
|
|
test 'should emit a csrf-param meta tag and a csrf-token meta tag' do
|
2013-07-23 10:35:07 -04:00
|
|
|
SecureRandom.stubs(:base64).returns(@token + 'U+003c=U+0022U+003fU+0022') # '<="?"'
|
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'
|
2013-07-23 10:35:07 -04:00
|
|
|
assert_select 'meta[name=?][content=?]', 'csrf-token', 'cf50faa3fe97702ca1aeU+003c=U+0022U+003fU+0022'
|
2010-02-04 17:15:16 -05:00
|
|
|
end
|
2007-09-22 22:32:55 -04:00
|
|
|
end
|
|
|
|
|
2013-12-12 22:41:14 -05:00
|
|
|
class RequestForgeryProtectionControllerUsingNullSessionTest < ActionController::TestCase
|
|
|
|
class NullSessionDummyKeyGenerator
|
|
|
|
def generate_key(secret)
|
|
|
|
'03312270731a2ed0d11ed091c2338a06'
|
|
|
|
end
|
2013-02-08 13:22:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def setup
|
|
|
|
@request.env[ActionDispatch::Cookies::GENERATOR_KEY] = NullSessionDummyKeyGenerator.new
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should allow to set signed cookies' do
|
|
|
|
post :signed
|
|
|
|
assert_response :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should allow to set encrypted cookies' do
|
|
|
|
post :encrypted
|
|
|
|
assert_response :ok
|
|
|
|
end
|
2013-09-18 22:01:59 -04:00
|
|
|
|
|
|
|
test 'should allow reset_session' do
|
|
|
|
post :try_to_reset_session
|
|
|
|
assert_response :ok
|
|
|
|
end
|
2013-02-08 13:22:16 -05: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
|
2013-01-05 11:59:36 -05:00
|
|
|
assert @response.body.blank?
|
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
|
|
|
super
|
2014-05-02 10:54:35 -04:00
|
|
|
@old_logger = ActionController::Base.logger
|
|
|
|
@logger = ActiveSupport::LogSubscriber::TestHelper::MockLogger.new
|
|
|
|
@token = "foobar"
|
2014-05-28 10:35:48 -04:00
|
|
|
@old_request_forgery_protection_token = ActionController::Base.request_forgery_protection_token
|
2014-05-02 10:54:35 -04:00
|
|
|
ActionController::Base.request_forgery_protection_token = @token
|
2011-01-04 19:36:07 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2014-05-28 10:35:48 -04:00
|
|
|
ActionController::Base.request_forgery_protection_token = @old_request_forgery_protection_token
|
2011-01-04 19:36:07 -05:00
|
|
|
super
|
2009-11-18 02:36:48 -05:00
|
|
|
end
|
|
|
|
|
2014-05-02 10:54:35 -04:00
|
|
|
def test_should_not_warn_if_form_authenticity_param_matches_form_authenticity_token
|
|
|
|
ActionController::Base.logger = @logger
|
|
|
|
SecureRandom.stubs(:base64).returns(@token)
|
|
|
|
|
|
|
|
begin
|
|
|
|
post :index, :custom_token_name => 'foobar'
|
|
|
|
assert_equal 0, @logger.logged(:warn).size
|
|
|
|
ensure
|
|
|
|
ActionController::Base.logger = @old_logger
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_warn_if_form_authenticity_param_does_not_match_form_authenticity_token
|
|
|
|
ActionController::Base.logger = @logger
|
|
|
|
|
|
|
|
begin
|
|
|
|
post :index, :custom_token_name => 'bazqux'
|
|
|
|
assert_equal 1, @logger.logged(:warn).size
|
|
|
|
ensure
|
|
|
|
ActionController::Base.logger = @old_logger
|
|
|
|
end
|
2009-11-18 02:36:48 -05:00
|
|
|
end
|
|
|
|
end
|