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
|
|
|
|
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
|
|
|
|
|
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
|
|
|
|
|
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'
|
2015-05-28 08:13:32 -04:00
|
|
|
head :ok
|
2013-02-08 13:22:16 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def encrypted
|
|
|
|
cookies.encrypted[:foo] = 'bar'
|
2015-05-28 08:13:32 -04:00
|
|
|
head :ok
|
2013-02-08 13:22:16 -05:00
|
|
|
end
|
2013-09-18 22:01:59 -04:00
|
|
|
|
|
|
|
def try_to_reset_session
|
|
|
|
reset_session
|
2015-05-28 08:13:32 -04:00
|
|
|
head :ok
|
2013-09-18 22:01:59 -04:00
|
|
|
end
|
2013-02-08 13:22:16 -05:00
|
|
|
end
|
2011-01-04 19:36:07 -05:00
|
|
|
|
2015-01-04 19:38:54 -05:00
|
|
|
class PrependProtectForgeryBaseController < ActionController::Base
|
|
|
|
before_action :custom_action
|
|
|
|
attr_accessor :called_callbacks
|
|
|
|
|
|
|
|
def index
|
|
|
|
render inline: 'OK'
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def add_called_callback(name)
|
|
|
|
@called_callbacks ||= []
|
|
|
|
@called_callbacks << name
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def custom_action
|
|
|
|
add_called_callback("custom_action")
|
|
|
|
end
|
|
|
|
|
|
|
|
def verify_authenticity_token
|
|
|
|
add_called_callback("verify_authenticity_token")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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"
|
2014-08-19 17:29:26 -04:00
|
|
|
@controller.stubs(:form_authenticity_token).returns(@token)
|
|
|
|
@controller.stubs(:valid_authenticity_token?).with{ |_, t| t == @token }.returns(true)
|
|
|
|
@controller.stubs(:valid_authenticity_token?).with{ |_, t| t != @token }.returns(false)
|
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
|
2015-02-01 08:07:42 -05:00
|
|
|
assert_blocked { post :index, xhr: true }
|
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
|
2015-01-04 04:35:06 -05:00
|
|
|
assert_not_blocked { post :index, params: { 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
|
2015-01-04 04:35:06 -05:00
|
|
|
assert_not_blocked { patch :index, params: { custom_authenticity_token: @token } }
|
2011-05-06 17:03:55 -04:00
|
|
|
end
|
|
|
|
|
2011-01-04 19:36:07 -05:00
|
|
|
def test_should_allow_put_with_token
|
2015-01-04 04:35:06 -05:00
|
|
|
assert_not_blocked { put :index, params: { 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
|
2015-01-04 04:35:06 -05:00
|
|
|
assert_not_blocked { delete :index, params: { 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
|
|
|
|
|
2015-02-01 08:07:42 -05:00
|
|
|
assert_cross_origin_not_blocked { get :same_origin_js, xhr: true }
|
|
|
|
assert_cross_origin_not_blocked { get :same_origin_js, xhr: true, format: 'js'}
|
2013-12-12 22:41:14 -05:00
|
|
|
assert_cross_origin_not_blocked do
|
|
|
|
@request.accept = 'text/javascript'
|
2015-02-01 08:07:42 -05:00
|
|
|
get :negotiate_same_origin, xhr: true
|
2013-12-12 22:41:14 -05:00
|
|
|
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
|
2015-01-04 04:35:06 -05:00
|
|
|
assert_cross_origin_not_blocked { post :same_origin_js, params: { custom_authenticity_token: @token } }
|
|
|
|
assert_cross_origin_not_blocked { post :same_origin_js, params: { format: 'js', custom_authenticity_token: @token } }
|
2013-12-17 18:02:04 -05:00
|
|
|
assert_cross_origin_not_blocked do
|
|
|
|
@request.accept = 'text/javascript'
|
2015-01-04 04:35:06 -05:00
|
|
|
post :negotiate_same_origin, params: { custom_authenticity_token: @token}
|
2013-12-17 18:02:04 -05:00
|
|
|
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
|
|
|
|
|
2015-02-01 08:07:42 -05:00
|
|
|
assert_cross_origin_not_blocked { get :cross_origin_js, xhr: true }
|
|
|
|
assert_cross_origin_not_blocked { get :cross_origin_js, xhr: true, format: 'js' }
|
2013-12-12 22:41:14 -05:00
|
|
|
assert_cross_origin_not_blocked do
|
|
|
|
@request.accept = 'text/javascript'
|
2015-02-01 08:07:42 -05:00
|
|
|
get :negotiate_cross_origin, xhr: true
|
2013-12-12 22:41:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-02-12 15:24:45 -05:00
|
|
|
def test_should_not_raise_error_if_token_is_not_a_string
|
|
|
|
@controller.unstub(:valid_authenticity_token?)
|
|
|
|
assert_blocked do
|
|
|
|
patch :index, params: { custom_authenticity_token: { foo: 'bar' } }
|
|
|
|
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
|
2014-08-19 17:29:26 -04:00
|
|
|
@controller.stubs(:form_authenticity_token).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'
|
2013-09-20 14:51:08 -04:00
|
|
|
assert_select 'meta[name=?]', 'csrf-token'
|
|
|
|
assert_match(/cf50faa3fe97702ca1ae<=\?/, @response.body)
|
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
|
|
|
|
|
2015-01-04 19:38:54 -05:00
|
|
|
class PrependProtectForgeryBaseControllerTest < ActionController::TestCase
|
|
|
|
PrependTrueController = Class.new(PrependProtectForgeryBaseController) do
|
|
|
|
protect_from_forgery prepend: true
|
|
|
|
end
|
|
|
|
|
|
|
|
PrependFalseController = Class.new(PrependProtectForgeryBaseController) do
|
|
|
|
protect_from_forgery prepend: false
|
|
|
|
end
|
|
|
|
|
|
|
|
PrependDefaultController = Class.new(PrependProtectForgeryBaseController) do
|
|
|
|
protect_from_forgery
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_verify_authenticity_token_is_prepended
|
|
|
|
@controller = PrependTrueController.new
|
|
|
|
get :index
|
|
|
|
expected_callback_order = ["verify_authenticity_token", "custom_action"]
|
|
|
|
assert_equal(expected_callback_order, @controller.called_callbacks)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_verify_authenticity_token_is_not_prepended
|
|
|
|
@controller = PrependFalseController.new
|
|
|
|
get :index
|
|
|
|
expected_callback_order = ["custom_action", "verify_authenticity_token"]
|
|
|
|
assert_equal(expected_callback_order, @controller.called_callbacks)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_verify_authenticity_token_is_prepended_by_default
|
|
|
|
@controller = PrependDefaultController.new
|
|
|
|
get :index
|
|
|
|
expected_callback_order = ["verify_authenticity_token", "custom_action"]
|
|
|
|
assert_equal(expected_callback_order, @controller.called_callbacks)
|
|
|
|
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
|
2014-08-19 17:29:26 -04:00
|
|
|
@token = Base64.strict_encode64(SecureRandom.random_bytes(32))
|
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
|
2014-08-19 17:29:26 -04:00
|
|
|
@controller.stubs(:valid_authenticity_token?).returns(:true)
|
2014-05-02 10:54:35 -04:00
|
|
|
|
|
|
|
begin
|
2015-01-04 04:35:06 -05:00
|
|
|
post :index, params: { custom_token_name: 'foobar' }
|
2014-05-02 10:54:35 -04:00
|
|
|
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
|
2015-01-04 04:35:06 -05:00
|
|
|
post :index, params: { custom_token_name: 'bazqux' }
|
2014-05-02 10:54:35 -04:00
|
|
|
assert_equal 1, @logger.logged(:warn).size
|
|
|
|
ensure
|
|
|
|
ActionController::Base.logger = @old_logger
|
|
|
|
end
|
2009-11-18 02:36:48 -05:00
|
|
|
end
|
|
|
|
end
|