request_forgery_protection_test.rb 6.5 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
  def external_form_for
32
    render :inline => "<%= form_for(:some_resource, :authenticity_token => 'external_token') {} %>"
33 34 35
  end

  def form_for_without_protection
36
    render :inline => "<%= form_for(:some_resource, :authenticity_token => false ) {} %>"
37 38
  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 49 50 51 52 53 54 55 56 57
class RequestForgeryProtectionControllerUsingOldBehaviour < ActionController::Base
  include RequestForgeryProtectionActions
  protect_from_forgery :only => %w(index meta)

  def handle_unverified_request
    raise(ActionController::InvalidAuthenticityToken)
  end
end


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

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

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

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


77 78
# common test methods

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

160 161 162 163
  def assert_blocked
    session[:something_like_user_id] = 1
    yield
    assert_nil session[:something_like_user_id], "session values are still present"
164 165
    assert_response :success
  end
166

167 168
  def assert_not_blocked
    assert_nothing_raised { yield }
169 170 171 172
    assert_response :success
  end
end

173
# OK let's get our test on
174

175
class RequestForgeryProtectionControllerTest < ActionController::TestCase
176
  include RequestForgeryProtectionTests
J
Jeremy Kemper 已提交
177

178 179 180 181 182 183 184 185
  setup do
    ActionController::Base.request_forgery_protection_token = :custom_authenticity_token
  end

  teardown do
    ActionController::Base.request_forgery_protection_token = nil
  end

186
  test 'should emit a csrf-param meta tag and a csrf-token meta tag' do
187
    ActiveSupport::SecureRandom.stubs(:base64).returns(@token + '<=?')
J
Jeremy Kemper 已提交
188
    get :meta
189
    assert_select 'meta[name=?][content=?]', 'csrf-param', 'custom_authenticity_token'
190
    assert_select 'meta[name=?][content=?]', 'csrf-token', 'cf50faa3fe97702ca1ae&lt;=?'
J
Jeremy Kemper 已提交
191
  end
192 193
end

194 195 196 197 198 199 200 201 202
class RequestForgeryProtectionControllerUsingOldBehaviourTest < ActionController::TestCase
  include RequestForgeryProtectionTests
  def assert_blocked
    assert_raises(ActionController::InvalidAuthenticityToken) do
      yield
    end
  end
end

203
class FreeCookieControllerTest < ActionController::TestCase
204 205 206 207
  def setup
    @controller = FreeCookieController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
J
Jeremy Kemper 已提交
208
    @token      = "cf50faa3fe97702ca1ae"
209 210

    ActiveSupport::SecureRandom.stubs(:base64).returns(@token)
211
  end
212

213 214 215 216
  def test_should_not_render_form_with_token_tag
    get :index
    assert_select 'form>div>input[name=?][value=?]', 'authenticity_token', @token, false
  end
217

218 219 220 221
  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
222

223 224 225 226 227
  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 已提交
228 229 230

  test 'should not emit a csrf-token meta tag' do
    get :meta
231
    assert_blank @response.body
J
Jeremy Kemper 已提交
232
  end
233
end
234

235 236 237 238




239 240
class CustomAuthenticityParamControllerTest < ActionController::TestCase
  def setup
241 242 243 244 245
    ActionController::Base.request_forgery_protection_token = :custom_token_name
    super
  end

  def teardown
J
Jeremy Kemper 已提交
246
    ActionController::Base.request_forgery_protection_token = :authenticity_token
247
    super
248 249 250
  end

  def test_should_allow_custom_token
251
    post :index, :custom_token_name => 'foobar'
252 253 254
    assert_response :ok
  end
end