2008-01-05 08:32:06 -05:00
|
|
|
require 'abstract_unit'
|
2007-09-28 12:48:59 -04:00
|
|
|
require 'digest/sha1'
|
2010-09-11 05:04:19 -04:00
|
|
|
require 'active_support/core_ext/string/strip'
|
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
|
|
|
|
|
2007-09-28 12:50:48 -04:00
|
|
|
def rescue_action(e) raise e end
|
|
|
|
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
|
|
|
|
|
2011-01-04 19:36:07 -05: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 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-01-04 19:36:07 -05:00
|
|
|
ActiveSupport::SecureRandom.stubs(:base64).returns(@token)
|
|
|
|
ActionController::Base.request_forgery_protection_token = :authenticity_token
|
2010-09-22 15:04:42 -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
|
|
|
|
assert_select 'form>div>input[name=?][value=?]', '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
|
2010-09-22 15:04:42 -04:00
|
|
|
assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token
|
|
|
|
end
|
|
|
|
|
|
|
|
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-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
|
|
|
|
assert_not_blocked { post :index, :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_put_with_token
|
|
|
|
assert_not_blocked { put :index, :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
|
|
|
|
assert_not_blocked { delete :index, :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-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-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-04-07 20:18:33 -04:00
|
|
|
test 'should emit a csrf-param meta tag and a csrf-token meta tag' do
|
2010-02-04 21:03:06 -05:00
|
|
|
ActiveSupport::SecureRandom.stubs(:base64).returns(@token + '<=?')
|
2010-02-04 17:15:16 -05:00
|
|
|
get :meta
|
2011-04-07 20:18:33 -04:00
|
|
|
assert_select 'meta[name=?][content=?]', 'csrf-param', 'authenticity_token'
|
|
|
|
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
|
|
|
|
|
2011-01-04 19:36:07 -05:00
|
|
|
class RequestForgeryProtectionControllerUsingOldBehaviourTest < ActionController::TestCase
|
|
|
|
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
|
|
|
|
|
|
|
ActiveSupport::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
|
|
|
|
[:post, :put, :delete].each do |method|
|
|
|
|
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
|
|
|
|
2011-01-04 19:36:07 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|