2008-05-18 06:22:34 +02:00
|
|
|
require 'abstract_unit'
|
2007-12-21 08:48:59 +01:00
|
|
|
require 'digest/sha1'
|
|
|
|
|
|
|
|
ActionController::Routing::Routes.draw do |map|
|
|
|
|
map.connect ':controller/:action/:id'
|
|
|
|
end
|
|
|
|
|
|
|
|
# common controller actions
|
|
|
|
module RequestForgeryProtectionActions
|
|
|
|
def index
|
|
|
|
render :inline => "<%= form_tag('/') {} %>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_button
|
|
|
|
render :inline => "<%= button_to('New', '/') {} %>"
|
|
|
|
end
|
|
|
|
|
2008-05-18 06:22:34 +02:00
|
|
|
def remote_form
|
|
|
|
render :inline => "<% form_remote_tag(:url => '/') {} %>"
|
|
|
|
end
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def unsafe
|
|
|
|
render :text => 'pwn'
|
|
|
|
end
|
2009-12-01 02:38:34 +01:00
|
|
|
|
2011-02-19 06:36:23 +01:00
|
|
|
def meta
|
|
|
|
render :inline => "<%= csrf_meta_tag %>"
|
|
|
|
end
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def rescue_action(e) raise e end
|
|
|
|
end
|
|
|
|
|
|
|
|
# sample controllers
|
|
|
|
class RequestForgeryProtectionController < ActionController::Base
|
|
|
|
include RequestForgeryProtectionActions
|
|
|
|
protect_from_forgery :only => :index
|
|
|
|
end
|
|
|
|
|
2011-02-19 06:36:23 +01:00
|
|
|
class RequestForgeryProtectionControllerUsingOldBehaviour < ActionController::Base
|
|
|
|
include RequestForgeryProtectionActions
|
|
|
|
protect_from_forgery :only => %w(index meta)
|
|
|
|
|
|
|
|
def handle_unverified_request
|
|
|
|
raise(ActionController::InvalidAuthenticityToken)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
class FreeCookieController < RequestForgeryProtectionController
|
2007-12-21 08:48:59 +01:00
|
|
|
self.allow_forgery_protection = false
|
|
|
|
|
|
|
|
def index
|
|
|
|
render :inline => "<%= form_tag('/') {} %>"
|
|
|
|
end
|
|
|
|
|
|
|
|
def show_button
|
|
|
|
render :inline => "<%= button_to('New', '/') {} %>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-12-01 02:38:34 +01:00
|
|
|
class CustomAuthenticityParamController < RequestForgeryProtectionController
|
|
|
|
def form_authenticity_param
|
|
|
|
'foobar'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
# common test methods
|
|
|
|
|
|
|
|
module RequestForgeryProtectionTests
|
2011-02-19 06:36:23 +01:00
|
|
|
def setup
|
|
|
|
@token = "cf50faa3fe97702ca1ae"
|
2008-11-24 22:53:39 +01:00
|
|
|
|
2011-02-19 06:36:23 +01:00
|
|
|
ActiveSupport::SecureRandom.stubs(:base64).returns(@token)
|
|
|
|
ActionController::Base.request_forgery_protection_token = :authenticity_token
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2011-02-19 06:36:23 +01:00
|
|
|
def test_should_render_form_with_token_tag
|
|
|
|
assert_not_blocked do
|
|
|
|
get :index
|
2008-06-02 08:35:38 +02:00
|
|
|
end
|
2011-02-19 06:36:23 +01:00
|
|
|
assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token
|
2008-06-02 08:35:38 +02:00
|
|
|
end
|
|
|
|
|
2011-02-19 06:36:23 +01:00
|
|
|
def test_should_render_button_to_with_token_tag
|
|
|
|
assert_not_blocked do
|
|
|
|
get :show_button
|
2008-06-02 08:35:38 +02:00
|
|
|
end
|
2011-02-19 06:36:23 +01:00
|
|
|
assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token
|
2008-06-02 08:35:38 +02:00
|
|
|
end
|
|
|
|
|
2011-02-19 06:36:23 +01:00
|
|
|
def test_should_allow_get
|
|
|
|
assert_not_blocked { get :index }
|
2008-06-02 08:35:38 +02:00
|
|
|
end
|
|
|
|
|
2011-02-19 06:36:23 +01:00
|
|
|
def test_should_allow_post_without_token_on_unsafe_action
|
|
|
|
assert_not_blocked { post :unsafe }
|
2008-06-02 08:35:38 +02:00
|
|
|
end
|
|
|
|
|
2011-02-19 06:36:23 +01:00
|
|
|
def test_should_not_allow_post_without_token
|
|
|
|
assert_blocked { post :index }
|
2008-06-02 08:35:38 +02:00
|
|
|
end
|
|
|
|
|
2011-02-19 06:36:23 +01:00
|
|
|
def test_should_not_allow_post_without_token_irrespective_of_format
|
|
|
|
assert_blocked { post :index, :format=>'xml' }
|
2008-06-02 08:35:38 +02:00
|
|
|
end
|
|
|
|
|
2011-02-19 06:36:23 +01:00
|
|
|
def test_should_not_allow_put_without_token
|
|
|
|
assert_blocked { put :index }
|
2008-06-02 08:35:38 +02:00
|
|
|
end
|
|
|
|
|
2011-02-19 06:36:23 +01:00
|
|
|
def test_should_not_allow_delete_without_token
|
|
|
|
assert_blocked { delete :index }
|
2008-06-02 08:35:38 +02:00
|
|
|
end
|
|
|
|
|
2011-02-19 06:36:23 +01:00
|
|
|
def test_should_not_allow_xhr_post_without_token
|
|
|
|
assert_blocked { xhr :post, :index }
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
2011-02-19 06:36:23 +01:00
|
|
|
|
2007-12-21 08:48:59 +01:00
|
|
|
def test_should_allow_post_with_token
|
2011-02-19 06:36:23 +01:00
|
|
|
assert_not_blocked { post :index, :authenticity_token => @token }
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_allow_put_with_token
|
2011-02-19 06:36:23 +01:00
|
|
|
assert_not_blocked { put :index, :authenticity_token => @token }
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_allow_delete_with_token
|
2011-02-19 06:36:23 +01:00
|
|
|
assert_not_blocked { delete :index, :authenticity_token => @token }
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
2011-02-19 06:36:23 +01:00
|
|
|
def test_should_allow_post_with_token_in_header
|
|
|
|
@request.env['HTTP_X_CSRF_TOKEN'] = @token
|
|
|
|
assert_not_blocked { post :index }
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_allow_delete_with_token_in_header
|
|
|
|
@request.env['HTTP_X_CSRF_TOKEN'] = @token
|
|
|
|
assert_not_blocked { delete :index }
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
2011-02-19 06:36:23 +01:00
|
|
|
def test_should_allow_put_with_token_in_header
|
|
|
|
@request.env['HTTP_X_CSRF_TOKEN'] = @token
|
|
|
|
assert_not_blocked { put :index }
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_blocked
|
|
|
|
session[:something_like_user_id] = 1
|
|
|
|
yield
|
|
|
|
assert_nil session[:something_like_user_id], "session values are still present"
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
|
2011-02-19 06:36:23 +01:00
|
|
|
def assert_not_blocked
|
|
|
|
assert_nothing_raised { yield }
|
2007-12-21 08:48:59 +01:00
|
|
|
assert_response :success
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# OK let's get our test on
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
class RequestForgeryProtectionControllerTest < ActionController::TestCase
|
2007-12-21 08:48:59 +01:00
|
|
|
include RequestForgeryProtectionTests
|
|
|
|
|
2011-02-19 06:36:23 +01:00
|
|
|
test 'should emit a csrf-token meta tag' do
|
|
|
|
ActiveSupport::SecureRandom.stubs(:base64).returns(@token + '<=?')
|
|
|
|
get :meta
|
|
|
|
assert_equal %(<meta name="csrf-param" content="authenticity_token"/>\n<meta name="csrf-token" content="cf50faa3fe97702ca1ae<=?"/>), @response.body
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class RequestForgeryProtectionControllerUsingOldBehaviourTest < ActionController::TestCase
|
|
|
|
include RequestForgeryProtectionTests
|
|
|
|
def assert_blocked
|
|
|
|
assert_raises(ActionController::InvalidAuthenticityToken) do
|
|
|
|
yield
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-04 21:26:08 +01:00
|
|
|
class FreeCookieControllerTest < ActionController::TestCase
|
2007-12-21 08:48:59 +01:00
|
|
|
def setup
|
|
|
|
@controller = FreeCookieController.new
|
|
|
|
@request = ActionController::TestRequest.new
|
|
|
|
@response = ActionController::TestResponse.new
|
2009-02-04 21:26:08 +01:00
|
|
|
@token = "cf50faa3fe97702ca1ae"
|
|
|
|
|
|
|
|
ActiveSupport::SecureRandom.stubs(:base64).returns(@token)
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_not_render_form_with_token_tag
|
|
|
|
get :index
|
|
|
|
assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token, false
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_should_allow_all_methods_without_token
|
|
|
|
[:post, :put, :delete].each do |method|
|
|
|
|
assert_nothing_raised { send(method, :index)}
|
|
|
|
end
|
|
|
|
end
|
2011-02-19 06:36:23 +01:00
|
|
|
|
|
|
|
test 'should not emit a csrf-token meta tag' do
|
|
|
|
get :meta
|
|
|
|
assert_blank @response.body
|
|
|
|
end
|
2007-12-21 08:48:59 +01:00
|
|
|
end
|
2009-12-01 02:38:34 +01:00
|
|
|
|
2011-02-19 06:36:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-01 02:38:34 +01:00
|
|
|
class CustomAuthenticityParamControllerTest < ActionController::TestCase
|
|
|
|
def setup
|
2011-02-19 06:36:23 +01:00
|
|
|
ActionController::Base.request_forgery_protection_token = :custom_token_name
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
|
|
|
def teardown
|
2009-12-01 02:38:34 +01:00
|
|
|
ActionController::Base.request_forgery_protection_token = :authenticity_token
|
2011-02-19 06:36:23 +01:00
|
|
|
super
|
2009-12-01 02:38:34 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_should_allow_custom_token
|
2011-02-19 06:36:23 +01:00
|
|
|
post :index, :custom_token_name => 'foobar'
|
2009-12-01 02:38:34 +01:00
|
|
|
assert_response :ok
|
|
|
|
end
|
|
|
|
end
|