require 'abstract_unit' require "active_support/log_subscriber/test_helper" # common controller actions module RequestForgeryProtectionActions def index render :inline => "<%= form_tag('/') {} %>" end def show_button render :inline => "<%= button_to('New', '/') %>" end def unsafe render plain: 'pwn' end def meta render :inline => "<%= csrf_meta_tags %>" end def form_for_remote render :inline => "<%= form_for(:some_resource, :remote => true ) {} %>" end def form_for_remote_with_token render :inline => "<%= form_for(:some_resource, :remote => true, :authenticity_token => true ) {} %>" end 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 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 end # sample controllers class RequestForgeryProtectionControllerUsingResetSession < ActionController::Base include RequestForgeryProtectionActions protect_from_forgery :only => %w(index meta same_origin_js negotiate_same_origin), :with => :reset_session end class RequestForgeryProtectionControllerUsingException < ActionController::Base include RequestForgeryProtectionActions protect_from_forgery :only => %w(index meta same_origin_js negotiate_same_origin), :with => :exception end class RequestForgeryProtectionControllerUsingNullSession < ActionController::Base protect_from_forgery :with => :null_session def signed cookies.signed[:foo] = 'bar' head :ok end def encrypted cookies.encrypted[:foo] = 'bar' head :ok end def try_to_reset_session reset_session head :ok end end 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 class FreeCookieController < RequestForgeryProtectionControllerUsingResetSession self.allow_forgery_protection = false def index render :inline => "<%= form_tag('/') {} %>" end def show_button render :inline => "<%= button_to('New', '/') %>" end end class CustomAuthenticityParamController < RequestForgeryProtectionControllerUsingResetSession def form_authenticity_param 'foobar' end end class PerFormTokensController < ActionController::Base protect_from_forgery :with => :exception self.per_form_csrf_tokens = true def index render inline: "<%= form_tag (params[:form_path] || '/per_form_tokens/post_one'), method: (params[:form_method] || :post) %>" end def post_one render plain: '' end def post_two render plain: '' end end # common test methods module RequestForgeryProtectionTests def setup @token = Base64.strict_encode64('railstestrailstestrailstestrails') @old_request_forgery_protection_token = ActionController::Base.request_forgery_protection_token ActionController::Base.request_forgery_protection_token = :custom_authenticity_token end def teardown ActionController::Base.request_forgery_protection_token = @old_request_forgery_protection_token end def test_should_render_form_with_token_tag @controller.stub :form_authenticity_token, @token do assert_not_blocked do get :index end assert_select 'form>input[name=?][value=?]', 'custom_authenticity_token', @token end end def test_should_render_button_to_with_token_tag @controller.stub :form_authenticity_token, @token do assert_not_blocked do get :show_button end assert_select 'form>input[name=?][value=?]', 'custom_authenticity_token', @token end end def test_should_render_form_without_token_tag_if_remote assert_not_blocked do get :form_for_remote end assert_no_match(/authenticity_token/, response.body) end 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 begin ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms = true assert_not_blocked do get :form_for_remote end assert_match(/authenticity_token/, response.body) ensure ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms = original end end 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 assert_not_blocked do get :form_for_remote_with_external_token end assert_select 'form>input[name=?][value=?]', 'custom_authenticity_token', 'external_token' ensure ActionView::Helpers::FormTagHelper.embed_authenticity_token_in_remote_forms = original end end 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 assert_select 'form>input[name=?][value=?]', 'custom_authenticity_token', 'external_token' end def test_should_render_form_with_token_tag_if_remote_and_authenticity_token_requested @controller.stub :form_authenticity_token, @token do assert_not_blocked do get :form_for_remote_with_token end assert_select 'form>input[name=?][value=?]', 'custom_authenticity_token', @token end end def test_should_render_form_with_token_tag_with_authenticity_token_requested @controller.stub :form_authenticity_token, @token do assert_not_blocked do get :form_for_with_token end assert_select 'form>input[name=?][value=?]', 'custom_authenticity_token', @token end end def test_should_allow_get assert_not_blocked { get :index } end def test_should_allow_head assert_not_blocked { head :index } end def test_should_allow_post_without_token_on_unsafe_action assert_not_blocked { post :unsafe } end def test_should_not_allow_post_without_token assert_blocked { post :index } end def test_should_not_allow_post_without_token_irrespective_of_format assert_blocked { post :index, format: 'xml' } end def test_should_not_allow_patch_without_token assert_blocked { patch :index } end def test_should_not_allow_put_without_token assert_blocked { put :index } end def test_should_not_allow_delete_without_token assert_blocked { delete :index } end def test_should_not_allow_xhr_post_without_token assert_blocked { post :index, xhr: true } end def test_should_allow_post_with_token session[:_csrf_token] = @token @controller.stub :form_authenticity_token, @token do assert_not_blocked { post :index, params: { custom_authenticity_token: @token } } end end def test_should_allow_patch_with_token session[:_csrf_token] = @token @controller.stub :form_authenticity_token, @token do assert_not_blocked { patch :index, params: { custom_authenticity_token: @token } } end end def test_should_allow_put_with_token session[:_csrf_token] = @token @controller.stub :form_authenticity_token, @token do assert_not_blocked { put :index, params: { custom_authenticity_token: @token } } end end def test_should_allow_delete_with_token session[:_csrf_token] = @token @controller.stub :form_authenticity_token, @token do assert_not_blocked { delete :index, params: { custom_authenticity_token: @token } } end end def test_should_allow_post_with_token_in_header session[:_csrf_token] = @token @request.env['HTTP_X_CSRF_TOKEN'] = @token assert_not_blocked { post :index } end def test_should_allow_delete_with_token_in_header session[:_csrf_token] = @token @request.env['HTTP_X_CSRF_TOKEN'] = @token assert_not_blocked { delete :index } end def test_should_allow_patch_with_token_in_header session[:_csrf_token] = @token @request.env['HTTP_X_CSRF_TOKEN'] = @token assert_not_blocked { patch :index } end def test_should_allow_put_with_token_in_header session[:_csrf_token] = @token @request.env['HTTP_X_CSRF_TOKEN'] = @token assert_not_blocked { put :index } end def test_should_allow_post_with_origin_checking_and_correct_origin forgery_protection_origin_check do session[:_csrf_token] = @token @controller.stub :form_authenticity_token, @token do assert_not_blocked do @request.set_header 'HTTP_ORIGIN', 'http://test.host' post :index, params: { custom_authenticity_token: @token } end end end end def test_should_allow_post_with_origin_checking_and_no_origin forgery_protection_origin_check do session[:_csrf_token] = @token @controller.stub :form_authenticity_token, @token do assert_not_blocked do post :index, params: { custom_authenticity_token: @token } end end end end def test_should_block_post_with_origin_checking_and_wrong_origin forgery_protection_origin_check do session[:_csrf_token] = @token @controller.stub :form_authenticity_token, @token do assert_blocked do @request.set_header 'HTTP_ORIGIN', 'http://bad.host' post :index, params: { custom_authenticity_token: @token } end end end end 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) ensure ActionController::Base.logger = old_logger end end 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 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 { get :same_origin_js, xhr: true } assert_cross_origin_not_blocked { get :same_origin_js, xhr: true, format: 'js'} assert_cross_origin_not_blocked do @request.accept = 'text/javascript' get :negotiate_same_origin, xhr: true end end # Allow non-GET requests since GET is all a remote