@@ -45,6 +45,16 @@ class RequestForgeryProtectionController < ActionController::Base
4545 protect_from_forgery :only => %w( index meta )
4646end
4747
48+ class RequestForgeryProtectionControllerUsingOldBehaviour < ActionController ::Base
49+ include RequestForgeryProtectionActions
50+ protect_from_forgery :only => %w( index meta )
51+
52+ def handle_unverified_request
53+ raise ( ActionController ::InvalidAuthenticityToken )
54+ end
55+ end
56+
57+
4858class FreeCookieController < RequestForgeryProtectionController
4959 self . allow_forgery_protection = false
5060
@@ -67,172 +77,92 @@ def form_authenticity_param
6777# common test methods
6878
6979module RequestForgeryProtectionTests
70- def teardown
71- ActionController ::Base . request_forgery_protection_token = nil
72- end
80+ def setup
81+ @token = "cf50faa3fe97702ca1ae"
7382
74- def test_should_render_form_with_token_tag
75- get :index
76- assert_select 'form>div>input[name=?][value=?]' , 'authenticity_token' , @token
83+ ActiveSupport ::SecureRandom . stubs ( :base64 ) . returns ( @token )
84+ ActionController ::Base . request_forgery_protection_token = :authenticity_token
7785 end
7886
79- def test_should_render_external_form_for_with_external_token
80- get :external_form_for
81- assert_select 'form>div>input[name=?][value=?]' , 'authenticity_token' , 'external_token'
82- end
8387
84- def test_should_render_form_for_without_token_tag
85- get :form_for_without_protection
86- assert_select 'form>div>input[name=?][value=?]' , 'authenticity_token' , @token , false
88+ def test_should_render_form_with_token_tag
89+ assert_not_blocked do
90+ get :index
91+ end
92+ assert_select 'form>div>input[name=?][value=?]' , 'authenticity_token' , @token
8793 end
8894
8995 def test_should_render_button_to_with_token_tag
90- get :show_button
96+ assert_not_blocked do
97+ get :show_button
98+ end
9199 assert_select 'form>div>input[name=?][value=?]' , 'authenticity_token' , @token
92100 end
93101
94- def test_should_render_external_form_with_external_token
95- get :external_form
96- assert_select 'form>div>input[name=?][value=?]' , 'authenticity_token' , 'external_token'
97- end
98-
99- def test_should_render_external_form_without_token
100- get :external_form_without_protection
101- assert_select 'form>div>input[name=?][value=?]' , 'authenticity_token' , @token , false
102- end
103-
104102 def test_should_allow_get
105- get :index
106- assert_response :success
103+ assert_not_blocked { get :index }
107104 end
108105
109106 def test_should_allow_post_without_token_on_unsafe_action
110- post :unsafe
111- assert_response :success
112- end
113-
114- def test_should_not_allow_html_post_without_token
115- @request . env [ 'CONTENT_TYPE' ] = Mime ::URL_ENCODED_FORM . to_s
116- assert_raise ( ActionController ::InvalidAuthenticityToken ) { post :index , :format => :html }
117- end
118-
119- def test_should_not_allow_html_put_without_token
120- @request . env [ 'CONTENT_TYPE' ] = Mime ::URL_ENCODED_FORM . to_s
121- assert_raise ( ActionController ::InvalidAuthenticityToken ) { put :index , :format => :html }
122- end
123-
124- def test_should_not_allow_html_delete_without_token
125- @request . env [ 'CONTENT_TYPE' ] = Mime ::URL_ENCODED_FORM . to_s
126- assert_raise ( ActionController ::InvalidAuthenticityToken ) { delete :index , :format => :html }
127- end
128-
129- def test_should_allow_api_formatted_post_without_token
130- assert_nothing_raised do
131- post :index , :format => 'xml'
132- end
133- end
134-
135- def test_should_not_allow_api_formatted_put_without_token
136- assert_nothing_raised do
137- put :index , :format => 'xml'
138- end
139- end
140-
141- def test_should_allow_api_formatted_delete_without_token
142- assert_nothing_raised do
143- delete :index , :format => 'xml'
144- end
145- end
146-
147- def test_should_not_allow_api_formatted_post_sent_as_url_encoded_form_without_token
148- assert_raise ( ActionController ::InvalidAuthenticityToken ) do
149- @request . env [ 'CONTENT_TYPE' ] = Mime ::URL_ENCODED_FORM . to_s
150- post :index , :format => 'xml'
151- end
152- end
153-
154- def test_should_not_allow_api_formatted_put_sent_as_url_encoded_form_without_token
155- assert_raise ( ActionController ::InvalidAuthenticityToken ) do
156- @request . env [ 'CONTENT_TYPE' ] = Mime ::URL_ENCODED_FORM . to_s
157- put :index , :format => 'xml'
158- end
159- end
160-
161- def test_should_not_allow_api_formatted_delete_sent_as_url_encoded_form_without_token
162- assert_raise ( ActionController ::InvalidAuthenticityToken ) do
163- @request . env [ 'CONTENT_TYPE' ] = Mime ::URL_ENCODED_FORM . to_s
164- delete :index , :format => 'xml'
165- end
107+ assert_not_blocked { post :unsafe }
166108 end
167109
168- def test_should_not_allow_api_formatted_post_sent_as_multipart_form_without_token
169- assert_raise ( ActionController ::InvalidAuthenticityToken ) do
170- @request . env [ 'CONTENT_TYPE' ] = Mime ::MULTIPART_FORM . to_s
171- post :index , :format => 'xml'
172- end
110+ def test_should_not_allow_post_without_token
111+ assert_blocked { post :index }
173112 end
174113
175- def test_should_not_allow_api_formatted_put_sent_as_multipart_form_without_token
176- assert_raise ( ActionController ::InvalidAuthenticityToken ) do
177- @request . env [ 'CONTENT_TYPE' ] = Mime ::MULTIPART_FORM . to_s
178- put :index , :format => 'xml'
179- end
114+ def test_should_not_allow_post_without_token_irrespective_of_format
115+ assert_blocked { post :index , :format => 'xml' }
180116 end
181117
182- 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- delete :index , :format => 'xml'
186- end
118+ def test_should_not_allow_put_without_token
119+ assert_blocked { put :index }
187120 end
188121
189- def test_should_allow_xhr_post_without_token
190- assert_nothing_raised { xhr :post , :index }
122+ def test_should_not_allow_delete_without_token
123+ assert_blocked { delete :index }
191124 end
192125
193- def test_should_allow_xhr_put_without_token
194- assert_nothing_raised { xhr :put , :index }
126+ def test_should_not_allow_xhr_post_without_token
127+ assert_blocked { xhr :post , :index }
195128 end
196129
197- def test_should_allow_xhr_delete_without_token
198- assert_nothing_raised { xhr :delete , :index }
130+ def test_should_allow_post_with_token
131+ assert_not_blocked { post :index , :authenticity_token => @token }
199132 end
200133
201- def test_should_allow_xhr_post_with_encoded_form_content_type_without_token
202- @request . env [ 'CONTENT_TYPE' ] = Mime ::URL_ENCODED_FORM . to_s
203- assert_nothing_raised { xhr :post , :index }
134+ def test_should_allow_put_with_token
135+ assert_not_blocked { put :index , :authenticity_token => @token }
204136 end
205137
206- def test_should_allow_post_with_token
207- post :index , :authenticity_token => @token
208- assert_response :success
138+ def test_should_allow_delete_with_token
139+ assert_not_blocked { delete :index , :authenticity_token => @token }
209140 end
210141
211- def test_should_allow_put_with_token
212- put :index , :authenticity_token => @token
213- assert_response :success
142+ def test_should_allow_post_with_token_in_header
143+ @request . env [ 'HTTP_X_CSRF_TOKEN' ] = @token
144+ assert_not_blocked { post :index }
214145 end
215146
216- def test_should_allow_delete_with_token
217- delete :index , :authenticity_token => @token
218- assert_response :success
147+ def test_should_allow_delete_with_token_in_header
148+ @request . env [ 'HTTP_X_CSRF_TOKEN' ] = @token
149+ assert_not_blocked { delete :index }
219150 end
220151
221- def test_should_allow_post_with_xml
222- @request . env [ 'CONTENT_TYPE' ] = Mime ::XML . to_s
223- post :index , :format => 'xml'
224- assert_response :success
152+ def test_should_allow_put_with_token_in_header
153+ @request . env [ 'HTTP_X_CSRF_TOKEN' ] = @token
154+ assert_not_blocked { put :index }
225155 end
226156
227- def test_should_allow_put_with_xml
228- @request . env [ 'CONTENT_TYPE' ] = Mime ::XML . to_s
229- put :index , :format => 'xml'
157+ def assert_blocked
158+ session [ :something_like_user_id ] = 1
159+ yield
160+ assert_nil session [ :something_like_user_id ] , "session values are still present"
230161 assert_response :success
231162 end
232163
233- def test_should_allow_delete_with_xml
234- @request . env [ 'CONTENT_TYPE' ] = Mime ::XML . to_s
235- delete :index , :format => 'xml'
164+ def assert_not_blocked
165+ assert_nothing_raised { yield }
236166 assert_response :success
237167 end
238168end
@@ -241,16 +171,6 @@ def test_should_allow_delete_with_xml
241171
242172class RequestForgeryProtectionControllerTest < ActionController ::TestCase
243173 include RequestForgeryProtectionTests
244- def setup
245- @controller = RequestForgeryProtectionController . new
246- @request = ActionController ::TestRequest . new
247- @request . format = :html
248- @response = ActionController ::TestResponse . new
249- @token = "cf50faa3fe97702ca1ae"
250-
251- ActiveSupport ::SecureRandom . stubs ( :base64 ) . returns ( @token )
252- ActionController ::Base . request_forgery_protection_token = :authenticity_token
253- end
254174
255175 test 'should emit a csrf-token meta tag' do
256176 ActiveSupport ::SecureRandom . stubs ( :base64 ) . returns ( @token + '<=?' )
@@ -262,6 +182,15 @@ def setup
262182 end
263183end
264184
185+ class RequestForgeryProtectionControllerUsingOldBehaviourTest < ActionController ::TestCase
186+ include RequestForgeryProtectionTests
187+ def assert_blocked
188+ assert_raises ( ActionController ::InvalidAuthenticityToken ) do
189+ yield
190+ end
191+ end
192+ end
193+
265194class FreeCookieControllerTest < ActionController ::TestCase
266195 def setup
267196 @controller = FreeCookieController . new
@@ -294,13 +223,23 @@ def test_should_allow_all_methods_without_token
294223 end
295224end
296225
226+
227+
228+
229+
297230class CustomAuthenticityParamControllerTest < ActionController ::TestCase
298231 def setup
232+ ActionController ::Base . request_forgery_protection_token = :custom_token_name
233+ super
234+ end
235+
236+ def teardown
299237 ActionController ::Base . request_forgery_protection_token = :authenticity_token
238+ super
300239 end
301240
302241 def test_should_allow_custom_token
303- post :index , :authenticity_token => 'foobar'
242+ post :index , :custom_token_name => 'foobar'
304243 assert_response :ok
305244 end
306245end
0 commit comments