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

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

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

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

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

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

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

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

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

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

48
class FreeCookieController < RequestForgeryProtectionController
49
  self.allow_forgery_protection = false
50

51 52 53
  def index
    render :inline => "<%= form_tag('/') {} %>"
  end
54

55 56 57 58 59
  def show_button
    render :inline => "<%= button_to('New', '/') {} %>"
  end
end

60 61 62 63 64 65 66
class CustomAuthenticityParamController < RequestForgeryProtectionController
  def form_authenticity_param
    'foobar'
  end
end


67 68
# common test methods

69
module RequestForgeryProtectionTests
70 71 72
  def teardown
    ActionController::Base.request_forgery_protection_token = nil
  end
73

74
  def test_should_render_form_with_token_tag
E
Emilio Tagua 已提交
75 76 77 78
    get :index
    assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token
  end

79 80 81 82 83 84 85 86 87 88
  def test_should_render_external_form_for_with_external_token
    get :external_form_for
    assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', 'external_token'
  end

  def test_should_render_form_for_without_token_tag
    get :form_for_without_protection
    assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token, false
  end

E
Emilio Tagua 已提交
89 90 91 92 93
  def test_should_render_button_to_with_token_tag
    get :show_button
    assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token
  end

94 95 96 97 98 99 100 101 102 103
  def test_should_render_external_form_with_external_token
    get :external_form
    assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', 'external_token'
  end

  def test_should_render_external_form_without_token
    get :external_form_without_protection
    assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token, false
  end

E
Emilio Tagua 已提交
104 105 106 107 108 109 110 111 112
  def test_should_allow_get
    get :index
    assert_response :success
  end

  def test_should_allow_post_without_token_on_unsafe_action
    post :unsafe
    assert_response :success
  end
113 114 115

  def test_should_not_allow_html_post_without_token
    @request.env['CONTENT_TYPE'] = Mime::URL_ENCODED_FORM.to_s
116
    assert_raise(ActionController::InvalidAuthenticityToken) { post :index, :format => :html }
117
  end
118

119 120
  def test_should_not_allow_html_put_without_token
    @request.env['CONTENT_TYPE'] = Mime::URL_ENCODED_FORM.to_s
121
    assert_raise(ActionController::InvalidAuthenticityToken) { put :index, :format => :html }
122
  end
123

124 125
  def test_should_not_allow_html_delete_without_token
    @request.env['CONTENT_TYPE'] = Mime::URL_ENCODED_FORM.to_s
126
    assert_raise(ActionController::InvalidAuthenticityToken) { delete :index, :format => :html }
127
  end
128

129 130
  def test_should_allow_api_formatted_post_without_token
    assert_nothing_raised do
131 132 133 134
      post :index, :format => 'xml'
    end
  end

135
  def test_should_not_allow_api_formatted_put_without_token
136
    assert_nothing_raised do
137 138 139 140
      put :index, :format => 'xml'
    end
  end

141 142
  def test_should_allow_api_formatted_delete_without_token
    assert_nothing_raised do
143 144 145 146 147
      delete :index, :format => 'xml'
    end
  end

  def test_should_not_allow_api_formatted_post_sent_as_url_encoded_form_without_token
148
    assert_raise(ActionController::InvalidAuthenticityToken) do
149 150 151 152 153 154
      @request.env['CONTENT_TYPE'] = Mime::URL_ENCODED_FORM.to_s
      post :index, :format => 'xml'
    end
  end

  def test_should_not_allow_api_formatted_put_sent_as_url_encoded_form_without_token
155
    assert_raise(ActionController::InvalidAuthenticityToken) do
156 157 158 159 160 161
      @request.env['CONTENT_TYPE'] = Mime::URL_ENCODED_FORM.to_s
      put :index, :format => 'xml'
    end
  end

  def test_should_not_allow_api_formatted_delete_sent_as_url_encoded_form_without_token
162
    assert_raise(ActionController::InvalidAuthenticityToken) do
163 164 165 166 167 168
      @request.env['CONTENT_TYPE'] = Mime::URL_ENCODED_FORM.to_s
      delete :index, :format => 'xml'
    end
  end

  def test_should_not_allow_api_formatted_post_sent_as_multipart_form_without_token
169
    assert_raise(ActionController::InvalidAuthenticityToken) do
170 171 172 173 174 175
      @request.env['CONTENT_TYPE'] = Mime::MULTIPART_FORM.to_s
      post :index, :format => 'xml'
    end
  end

  def test_should_not_allow_api_formatted_put_sent_as_multipart_form_without_token
176
    assert_raise(ActionController::InvalidAuthenticityToken) do
177 178 179 180 181 182
      @request.env['CONTENT_TYPE'] = Mime::MULTIPART_FORM.to_s
      put :index, :format => 'xml'
    end
  end

  def test_should_not_allow_api_formatted_delete_sent_as_multipart_form_without_token
183
    assert_raise(ActionController::InvalidAuthenticityToken) do
184
      @request.env['CONTENT_TYPE'] = Mime::MULTIPART_FORM.to_s
185 186 187
      delete :index, :format => 'xml'
    end
  end
188

189 190 191
  def test_should_allow_xhr_post_without_token
    assert_nothing_raised { xhr :post, :index }
  end
192

193 194
  def test_should_allow_xhr_put_without_token
    assert_nothing_raised { xhr :put, :index }
195
  end
196

197 198
  def test_should_allow_xhr_delete_without_token
    assert_nothing_raised { xhr :delete, :index }
199
  end
200

201 202 203 204
  def test_should_allow_xhr_post_with_encoded_form_content_type_without_token
    @request.env['CONTENT_TYPE'] = Mime::URL_ENCODED_FORM.to_s
    assert_nothing_raised { xhr :post, :index }
  end
205

206
  def test_should_allow_post_with_token
207
    post :index, :authenticity_token => @token
208 209
    assert_response :success
  end
210

211
  def test_should_allow_put_with_token
212
    put :index, :authenticity_token => @token
213 214
    assert_response :success
  end
215

216
  def test_should_allow_delete_with_token
217
    delete :index, :authenticity_token => @token
218 219
    assert_response :success
  end
220

221
  def test_should_allow_post_with_xml
222
    @request.env['CONTENT_TYPE'] = Mime::XML.to_s
223 224 225
    post :index, :format => 'xml'
    assert_response :success
  end
226

227
  def test_should_allow_put_with_xml
228
    @request.env['CONTENT_TYPE'] = Mime::XML.to_s
229 230 231
    put :index, :format => 'xml'
    assert_response :success
  end
232

233
  def test_should_allow_delete_with_xml
234
    @request.env['CONTENT_TYPE'] = Mime::XML.to_s
235 236 237 238 239
    delete :index, :format => 'xml'
    assert_response :success
  end
end

240
# OK let's get our test on
241

242
class RequestForgeryProtectionControllerTest < ActionController::TestCase
243 244 245 246
  include RequestForgeryProtectionTests
  def setup
    @controller = RequestForgeryProtectionController.new
    @request    = ActionController::TestRequest.new
247
    @request.format = :html
248
    @response   = ActionController::TestResponse.new
J
Jeremy Kemper 已提交
249
    @token      = "cf50faa3fe97702ca1ae"
250

251
    ActiveSupport::SecureRandom.stubs(:base64).returns(@token)
252
    ActionController::Base.request_forgery_protection_token = :authenticity_token
253
  end
J
Jeremy Kemper 已提交
254 255

  test 'should emit a csrf-token meta tag' do
256
    ActiveSupport::SecureRandom.stubs(:base64).returns(@token + '<=?')
J
Jeremy Kemper 已提交
257
    get :meta
258
    assert_equal <<-METAS.strip_heredoc.chomp, @response.body
259 260 261
      <meta name="csrf-param" content="authenticity_token"/>
      <meta name="csrf-token" content="cf50faa3fe97702ca1ae&lt;=?"/>
    METAS
J
Jeremy Kemper 已提交
262
  end
263 264
end

265
class FreeCookieControllerTest < ActionController::TestCase
266 267 268 269
  def setup
    @controller = FreeCookieController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
J
Jeremy Kemper 已提交
270
    @token      = "cf50faa3fe97702ca1ae"
271 272

    ActiveSupport::SecureRandom.stubs(:base64).returns(@token)
273
  end
274

275 276 277 278
  def test_should_not_render_form_with_token_tag
    get :index
    assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token, false
  end
279

280 281 282 283
  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
284

285 286 287 288 289
  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 已提交
290 291 292

  test 'should not emit a csrf-token meta tag' do
    get :meta
293
    assert_blank @response.body
J
Jeremy Kemper 已提交
294
  end
295
end
296 297 298

class CustomAuthenticityParamControllerTest < ActionController::TestCase
  def setup
J
Jeremy Kemper 已提交
299
    ActionController::Base.request_forgery_protection_token = :authenticity_token
300 301 302 303 304 305 306
  end

  def test_should_allow_custom_token
    post :index, :authenticity_token => 'foobar'
    assert_response :ok
  end
end