request_forgery_protection_test.rb 6.9 KB
Newer Older
1
require 'abstract_unit'
M
Michael Koziarski 已提交
2
require 'digest/sha1'
3
require 'active_support/core_ext/string/strip'
4
require "active_support/log_subscriber/test_helper"
5

6 7 8 9 10
# common controller actions
module RequestForgeryProtectionActions
  def index
    render :inline => "<%= form_tag('/') {} %>"
  end
11

12 13 14
  def show_button
    render :inline => "<%= button_to('New', '/') {} %>"
  end
15

16 17 18 19 20 21 22 23
  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

24 25 26
  def unsafe
    render :text => 'pwn'
  end
27

J
Jeremy Kemper 已提交
28
  def meta
29
    render :inline => "<%= csrf_meta_tags %>"
J
Jeremy Kemper 已提交
30 31
  end

32
  def external_form_for
33
    render :inline => "<%= form_for(:some_resource, :authenticity_token => 'external_token') {} %>"
34 35 36
  end

  def form_for_without_protection
37
    render :inline => "<%= form_for(:some_resource, :authenticity_token => false ) {} %>"
38 39
  end

40 41 42 43 44 45
  def rescue_action(e) raise e end
end

# sample controllers
class RequestForgeryProtectionController < ActionController::Base
  include RequestForgeryProtectionActions
J
Jeremy Kemper 已提交
46
  protect_from_forgery :only => %w(index meta)
47 48
end

49 50 51 52 53 54 55 56 57 58
class RequestForgeryProtectionControllerUsingOldBehaviour < ActionController::Base
  include RequestForgeryProtectionActions
  protect_from_forgery :only => %w(index meta)

  def handle_unverified_request
    raise(ActionController::InvalidAuthenticityToken)
  end
end


59
class FreeCookieController < RequestForgeryProtectionController
60
  self.allow_forgery_protection = false
61

62 63 64
  def index
    render :inline => "<%= form_tag('/') {} %>"
  end
65

66 67 68 69 70
  def show_button
    render :inline => "<%= button_to('New', '/') {} %>"
  end
end

71 72 73 74 75 76 77
class CustomAuthenticityParamController < RequestForgeryProtectionController
  def form_authenticity_param
    'foobar'
  end
end


78 79
# common test methods

80
module RequestForgeryProtectionTests
81 82
  def setup
    @token      = "cf50faa3fe97702ca1ae"
83

84
    SecureRandom.stubs(:base64).returns(@token)
85
    ActionController::Base.request_forgery_protection_token = :custom_authenticity_token
E
Emilio Tagua 已提交
86 87
  end

88 89 90
  def teardown
    ActionController::Base.request_forgery_protection_token = nil
  end
91

92 93 94 95
  def test_should_render_form_with_token_tag
    assert_not_blocked do
      get :index
    end
96
    assert_select 'form>div>input[name=?][value=?]', 'custom_authenticity_token', @token
97 98
  end

E
Emilio Tagua 已提交
99
  def test_should_render_button_to_with_token_tag
100 101 102
    assert_not_blocked do
      get :show_button
    end
103
    assert_select 'form>div>input[name=?][value=?]', 'custom_authenticity_token', @token
E
Emilio Tagua 已提交
104 105 106
  end

  def test_should_allow_get
107
    assert_not_blocked { get :index }
E
Emilio Tagua 已提交
108 109 110
  end

  def test_should_allow_post_without_token_on_unsafe_action
111
    assert_not_blocked { post :unsafe }
112 113
  end

114 115
  def test_should_not_allow_post_without_token
    assert_blocked { post :index }
116 117
  end

118 119
  def test_should_not_allow_post_without_token_irrespective_of_format
    assert_blocked { post :index, :format=>'xml' }
120 121
  end

122 123
  def test_should_not_allow_put_without_token
    assert_blocked { put :index }
124
  end
125

126 127
  def test_should_not_allow_delete_without_token
    assert_blocked { delete :index }
128
  end
129

130 131
  def test_should_not_allow_xhr_post_without_token
    assert_blocked { xhr :post, :index }
132
  end
133

134
  def test_should_allow_post_with_token
135
    assert_not_blocked { post :index, :custom_authenticity_token => @token }
136
  end
137

138
  def test_should_allow_put_with_token
139
    assert_not_blocked { put :index, :custom_authenticity_token => @token }
140
  end
141

142
  def test_should_allow_delete_with_token
143
    assert_not_blocked { delete :index, :custom_authenticity_token => @token }
144
  end
145

146 147 148
  def test_should_allow_post_with_token_in_header
    @request.env['HTTP_X_CSRF_TOKEN'] = @token
    assert_not_blocked { post :index }
149
  end
150

151 152 153
  def test_should_allow_delete_with_token_in_header
    @request.env['HTTP_X_CSRF_TOKEN'] = @token
    assert_not_blocked { delete :index }
154
  end
155

156 157 158
  def test_should_allow_put_with_token_in_header
    @request.env['HTTP_X_CSRF_TOKEN'] = @token
    assert_not_blocked { put :index }
159
  end
160

161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  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)
    rescue
      ActionController::Base.logger = old_logger
    end
  end

176 177 178 179
  def assert_blocked
    session[:something_like_user_id] = 1
    yield
    assert_nil session[:something_like_user_id], "session values are still present"
180 181
    assert_response :success
  end
182

183 184
  def assert_not_blocked
    assert_nothing_raised { yield }
185 186 187 188
    assert_response :success
  end
end

189
# OK let's get our test on
190

191
class RequestForgeryProtectionControllerTest < ActionController::TestCase
192
  include RequestForgeryProtectionTests
J
Jeremy Kemper 已提交
193

194 195 196 197 198 199 200 201
  setup do
    ActionController::Base.request_forgery_protection_token = :custom_authenticity_token
  end

  teardown do
    ActionController::Base.request_forgery_protection_token = nil
  end

202
  test 'should emit a csrf-param meta tag and a csrf-token meta tag' do
203
    SecureRandom.stubs(:base64).returns(@token + '<=?')
J
Jeremy Kemper 已提交
204
    get :meta
205
    assert_select 'meta[name=?][content=?]', 'csrf-param', 'custom_authenticity_token'
206
    assert_select 'meta[name=?][content=?]', 'csrf-token', 'cf50faa3fe97702ca1ae&lt;=?'
J
Jeremy Kemper 已提交
207
  end
208 209
end

210 211 212 213 214 215 216 217 218
class RequestForgeryProtectionControllerUsingOldBehaviourTest < ActionController::TestCase
  include RequestForgeryProtectionTests
  def assert_blocked
    assert_raises(ActionController::InvalidAuthenticityToken) do
      yield
    end
  end
end

219
class FreeCookieControllerTest < ActionController::TestCase
220 221 222 223
  def setup
    @controller = FreeCookieController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
J
Jeremy Kemper 已提交
224
    @token      = "cf50faa3fe97702ca1ae"
225

226
    SecureRandom.stubs(:base64).returns(@token)
227
  end
228

229 230 231 232
  def test_should_not_render_form_with_token_tag
    get :index
    assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token, false
  end
233

234 235 236 237
  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
238

239 240 241 242 243
  def test_should_allow_all_methods_without_token
    [:post, :put, :delete].each do |method|
      assert_nothing_raised { send(method, :index)}
    end
  end
J
Jeremy Kemper 已提交
244 245 246

  test 'should not emit a csrf-token meta tag' do
    get :meta
247
    assert_blank @response.body
J
Jeremy Kemper 已提交
248
  end
249
end
250

251 252 253 254




255 256
class CustomAuthenticityParamControllerTest < ActionController::TestCase
  def setup
257 258 259 260 261
    ActionController::Base.request_forgery_protection_token = :custom_token_name
    super
  end

  def teardown
J
Jeremy Kemper 已提交
262
    ActionController::Base.request_forgery_protection_token = :authenticity_token
263
    super
264 265 266
  end

  def test_should_allow_custom_token
267
    post :index, :custom_token_name => 'foobar'
268 269 270
    assert_response :ok
  end
end