2008-01-05 13:32:06 +00:00
|
|
|
require 'abstract_unit'
|
2007-09-28 16:48:59 +00:00
|
|
|
require 'digest/sha1'
|
2010-09-11 09:04:19 +00:00
|
|
|
require 'active_support/core_ext/string/strip'
|
2007-09-23 02:32:55 +00:00
|
|
|
|
2007-09-28 16:50:48 +00:00
|
|
|
# common controller actions
|
|
|
|
module RequestForgeryProtectionActions
|
|
|
|
def index
|
|
|
|
render :inline => "<%= form_tag('/') {} %>"
|
|
|
|
end
|
2010-01-30 21:42:30 +00:00
|
|
|
|
2007-09-28 16:50:48 +00:00
|
|
|
def show_button
|
|
|
|
render :inline => "<%= button_to('New', '/') {} %>"
|
|
|
|
end
|
2008-01-08 21:17:08 +00:00
|
|
|
|
2010-12-27 22:31:14 +00: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 16:50:48 +00:00
|
|
|
def unsafe
|
|
|
|
render :text => 'pwn'
|
|
|
|
end
|
2009-11-18 07:36:48 +00:00
|
|
|
|
2010-02-04 22:15:16 +00:00
|
|
|
def meta
|
2010-09-11 09:04:19 +00:00
|
|
|
render :inline => "<%= csrf_meta_tags %>"
|
2010-02-04 22:15:16 +00:00
|
|
|
end
|
|
|
|
|
2011-02-05 15:37:53 +00:00
|
|
|
def external_form_for
|
2011-02-06 16:19:02 +00:00
|
|
|
render :inline => "<%= form_for(:some_resource, :authenticity_token => 'external_token') {} %>"
|
2011-02-05 15:37:53 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def form_for_without_protection
|
2011-02-06 16:19:02 +00:00
|
|
|
render :inline => "<%= form_for(:some_resource, :authenticity_token => false ) {} %>"
|
2011-02-05 15:37:53 +00:00
|
|
|
end
|
|
|
|
|
2007-09-28 16:50:48 +00:00
|
|
|
def rescue_action(e) raise e end
|
|
|
|
end
|
|
|
|
|
|
|
|
# sample controllers
|
|
|
|
class RequestForgeryProtectionController < ActionController::Base
|
|
|
|
include RequestForgeryProtectionActions
|
2010-02-04 22:15:16 +00:00
|
|
|
protect_from_forgery :only => %w(index meta)
|
2007-09-28 16:50:48 +00:00
|
|
|
end
|
|
|
|
|
2011-01-05 00:36:07 +00:00
|
|
|
class RequestForgeryProtectionControllerUsingOldBehaviour < ActionController::Base
|
|
|
|
include RequestForgeryProtectionActions
|
|
|
|
protect_from_forgery :only => %w(index meta)
|
|
|
|
|
|
|
|
def handle_unverified_request
|
|
|
|
raise(ActionController::InvalidAuthenticityToken)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2008-11-20 22:06:19 +00:00
|
|
|
class FreeCookieController < RequestForgeryProtectionController
|
2007-09-28 16:50:48 +00:00
|
|
|
self.allow_forgery_protection = false
|
2010-01-30 21:42:30 +00:00
|
|
|
|
2007-09-28 16:50:48 +00:00
|
|
|
def index
|
|
|
|
render :inline => "<%= form_tag('/') {} %>"
|
|
|
|
end
|
2010-01-30 21:42:30 +00:00
|
|
|
|
2007-09-28 16:50:48 +00:00
|
|
|
def show_button
|
|
|
|
render :inline => "<%= button_to('New', '/') {} %>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-11-18 07:36:48 +00:00
|
|
|
class CustomAuthenticityParamController < RequestForgeryProtectionController
|
|
|
|
def form_authenticity_param
|
|
|
|
'foobar'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2007-09-28 16:50:48 +00:00
|
|
|
# common test methods
|
|
|
|
|
2007-09-25 16:50:35 +00:00
|
|
|
module RequestForgeryProtectionTests
|
2011-01-05 00:36:07 +00:00
|
|
|
def setup
|
|
|
|
@token = "cf50faa3fe97702ca1ae"
|
2009-08-17 02:14:26 +00:00
|
|
|
|
2011-01-05 00:36:07 +00:00
|
|
|
ActiveSupport::SecureRandom.stubs(:base64).returns(@token)
|
|
|
|
ActionController::Base.request_forgery_protection_token = :authenticity_token
|
2010-09-22 19:04:42 +00:00
|
|
|
end
|
|
|
|
|
2011-02-05 15:37:53 +00:00
|
|
|
|
2011-01-05 00:36:07 +00:00
|
|
|
def test_should_render_form_with_token_tag
|
|
|
|
assert_not_blocked do
|
|
|
|
get :index
|
|
|
|
end
|
|
|
|
assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token
|
2011-02-05 15:37:53 +00:00
|
|
|
end
|
|
|
|
|
2010-09-22 19:04:42 +00:00
|
|
|
def test_should_render_button_to_with_token_tag
|
2011-01-05 00:36:07 +00:00
|
|
|
assert_not_blocked do
|
|
|
|
get :show_button
|
|
|
|
end
|
2010-09-22 19:04:42 +00:00
|
|
|
assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_allow_get
|
2011-01-05 00:36:07 +00:00
|
|
|
assert_not_blocked { get :index }
|
2010-09-22 19:04:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_allow_post_without_token_on_unsafe_action
|
2011-01-05 00:36:07 +00:00
|
|
|
assert_not_blocked { post :unsafe }
|
2008-05-06 09:58:32 +00:00
|
|
|
end
|
|
|
|
|
2011-01-05 00:36:07 +00:00
|
|
|
def test_should_not_allow_post_without_token
|
|
|
|
assert_blocked { post :index }
|
2008-05-06 09:58:32 +00:00
|
|
|
end
|
|
|
|
|
2011-01-05 00:36:07 +00:00
|
|
|
def test_should_not_allow_post_without_token_irrespective_of_format
|
|
|
|
assert_blocked { post :index, :format=>'xml' }
|
2008-05-06 09:58:32 +00:00
|
|
|
end
|
|
|
|
|
2011-01-05 00:36:07 +00:00
|
|
|
def test_should_not_allow_put_without_token
|
|
|
|
assert_blocked { put :index }
|
2008-05-06 07:42:24 +00:00
|
|
|
end
|
2010-01-30 21:42:30 +00:00
|
|
|
|
2011-01-05 00:36:07 +00:00
|
|
|
def test_should_not_allow_delete_without_token
|
|
|
|
assert_blocked { delete :index }
|
2008-11-01 04:10:44 +00:00
|
|
|
end
|
2010-01-30 21:42:30 +00:00
|
|
|
|
2011-01-05 00:36:07 +00:00
|
|
|
def test_should_not_allow_xhr_post_without_token
|
|
|
|
assert_blocked { xhr :post, :index }
|
2007-09-23 02:32:55 +00:00
|
|
|
end
|
2010-01-30 21:42:30 +00:00
|
|
|
|
2011-01-05 00:36:07 +00:00
|
|
|
def test_should_allow_post_with_token
|
|
|
|
assert_not_blocked { post :index, :authenticity_token => @token }
|
2007-09-23 02:32:55 +00:00
|
|
|
end
|
2010-01-30 21:42:30 +00:00
|
|
|
|
2011-01-05 00:36:07 +00:00
|
|
|
def test_should_allow_put_with_token
|
|
|
|
assert_not_blocked { put :index, :authenticity_token => @token }
|
2009-03-04 21:05:15 +00:00
|
|
|
end
|
2010-01-30 21:42:30 +00:00
|
|
|
|
2011-01-05 00:36:07 +00:00
|
|
|
def test_should_allow_delete_with_token
|
|
|
|
assert_not_blocked { delete :index, :authenticity_token => @token }
|
2007-09-23 02:32:55 +00:00
|
|
|
end
|
2010-01-30 21:42:30 +00:00
|
|
|
|
2011-01-05 00:36:07 +00:00
|
|
|
def test_should_allow_post_with_token_in_header
|
|
|
|
@request.env['HTTP_X_CSRF_TOKEN'] = @token
|
|
|
|
assert_not_blocked { post :index }
|
2007-09-23 02:32:55 +00:00
|
|
|
end
|
2010-01-30 21:42:30 +00:00
|
|
|
|
2011-01-05 00:36:07 +00:00
|
|
|
def test_should_allow_delete_with_token_in_header
|
|
|
|
@request.env['HTTP_X_CSRF_TOKEN'] = @token
|
|
|
|
assert_not_blocked { delete :index }
|
2007-09-23 02:32:55 +00:00
|
|
|
end
|
2010-01-30 21:42:30 +00:00
|
|
|
|
2011-01-05 00:36:07 +00:00
|
|
|
def test_should_allow_put_with_token_in_header
|
|
|
|
@request.env['HTTP_X_CSRF_TOKEN'] = @token
|
|
|
|
assert_not_blocked { put :index }
|
2007-09-23 02:32:55 +00:00
|
|
|
end
|
2010-01-30 21:42:30 +00:00
|
|
|
|
2011-01-05 00:36:07 +00: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-23 02:32:55 +00:00
|
|
|
assert_response :success
|
|
|
|
end
|
2010-01-30 21:42:30 +00:00
|
|
|
|
2011-01-05 00:36:07 +00:00
|
|
|
def assert_not_blocked
|
|
|
|
assert_nothing_raised { yield }
|
2007-09-23 02:32:55 +00:00
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-09-28 16:50:48 +00:00
|
|
|
# OK let's get our test on
|
2007-09-25 16:50:35 +00:00
|
|
|
|
2008-11-07 20:42:34 +00:00
|
|
|
class RequestForgeryProtectionControllerTest < ActionController::TestCase
|
2007-09-25 16:50:35 +00:00
|
|
|
include RequestForgeryProtectionTests
|
2010-02-04 22:15:16 +00:00
|
|
|
|
|
|
|
test 'should emit a csrf-token meta tag' do
|
2010-02-05 02:03:06 +00:00
|
|
|
ActiveSupport::SecureRandom.stubs(:base64).returns(@token + '<=?')
|
2010-02-04 22:15:16 +00:00
|
|
|
get :meta
|
2010-09-13 23:35:44 +00:00
|
|
|
assert_equal <<-METAS.strip_heredoc.chomp, @response.body
|
2010-09-11 09:04:19 +00:00
|
|
|
<meta name="csrf-param" content="authenticity_token"/>
|
|
|
|
<meta name="csrf-token" content="cf50faa3fe97702ca1ae<=?"/>
|
|
|
|
METAS
|
2010-02-04 22:15:16 +00:00
|
|
|
end
|
2007-09-23 02:32:55 +00:00
|
|
|
end
|
|
|
|
|
2011-01-05 00:36:07 +00:00
|
|
|
class RequestForgeryProtectionControllerUsingOldBehaviourTest < ActionController::TestCase
|
|
|
|
include RequestForgeryProtectionTests
|
|
|
|
def assert_blocked
|
|
|
|
assert_raises(ActionController::InvalidAuthenticityToken) do
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-07 20:42:34 +00:00
|
|
|
class FreeCookieControllerTest < ActionController::TestCase
|
2007-09-28 15:55:45 +00:00
|
|
|
def setup
|
|
|
|
@controller = FreeCookieController.new
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
2010-02-05 01:49:23 +00:00
|
|
|
@token = "cf50faa3fe97702ca1ae"
|
2008-11-20 22:06:19 +00:00
|
|
|
|
|
|
|
ActiveSupport::SecureRandom.stubs(:base64).returns(@token)
|
2007-09-28 15:55:45 +00:00
|
|
|
end
|
2010-01-30 21:42:30 +00:00
|
|
|
|
2007-09-28 15:55:45 +00: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 21:42:30 +00:00
|
|
|
|
2007-09-28 15:55:45 +00: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 21:42:30 +00:00
|
|
|
|
2007-09-28 15:55:45 +00:00
|
|
|
def test_should_allow_all_methods_without_token
|
|
|
|
[:post, :put, :delete].each do |method|
|
|
|
|
assert_nothing_raised { send(method, :index)}
|
|
|
|
end
|
|
|
|
end
|
2010-02-04 22:15:16 +00:00
|
|
|
|
|
|
|
test 'should not emit a csrf-token meta tag' do
|
|
|
|
get :meta
|
2010-08-17 01:29:57 +00:00
|
|
|
assert_blank @response.body
|
2010-02-04 22:15:16 +00:00
|
|
|
end
|
2007-10-02 05:32:14 +00:00
|
|
|
end
|
2009-11-18 07:36:48 +00:00
|
|
|
|
2011-01-05 00:36:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-11-18 07:36:48 +00:00
|
|
|
class CustomAuthenticityParamControllerTest < ActionController::TestCase
|
|
|
|
def setup
|
2011-01-05 00:36:07 +00:00
|
|
|
ActionController::Base.request_forgery_protection_token = :custom_token_name
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2009-11-19 01:54:27 +00:00
|
|
|
ActionController::Base.request_forgery_protection_token = :authenticity_token
|
2011-01-05 00:36:07 +00:00
|
|
|
super
|
2009-11-18 07:36:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_allow_custom_token
|
2011-01-05 00:36:07 +00:00
|
|
|
post :index, :custom_token_name => 'foobar'
|
2009-11-18 07:36:48 +00:00
|
|
|
assert_response :ok
|
|
|
|
end
|
|
|
|
end
|